Commit | Line | Data |
---|---|---|
7649924e JG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
4 | * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License, version 2 only, | |
8 | * as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; if not, write to the Free Software Foundation, Inc., | |
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 | */ | |
19 | ||
20 | #include "manage-apps.h" | |
21 | #include "testpoint.h" | |
22 | #include "health-sessiond.h" | |
23 | #include "utils.h" | |
24 | #include "thread.h" | |
25 | ||
26 | struct thread_notifiers { | |
27 | struct lttng_pipe *quit_pipe; | |
28 | int apps_cmd_pipe_read_fd; | |
29 | }; | |
30 | ||
31 | static void cleanup_application_management_thread(void *data) | |
32 | { | |
33 | struct thread_notifiers *notifiers = data; | |
34 | ||
35 | lttng_pipe_destroy(notifiers->quit_pipe); | |
36 | free(notifiers); | |
37 | } | |
38 | ||
39 | /* | |
40 | * This thread receives application command sockets (FDs) on the | |
41 | * apps_cmd_pipe and waits (polls) on them until they are closed | |
42 | * or an error occurs. | |
43 | * | |
44 | * At that point, it flushes the data (tracing and metadata) associated | |
45 | * with this application and tears down ust app sessions and other | |
46 | * associated data structures through ust_app_unregister(). | |
47 | * | |
48 | * Note that this thread never sends commands to the applications | |
49 | * through the command sockets; it merely listens for hang-ups | |
50 | * and errors on those sockets and cleans-up as they occur. | |
51 | */ | |
52 | static void *thread_application_management(void *data) | |
53 | { | |
54 | int i, ret, pollfd, err = -1; | |
55 | ssize_t size_ret; | |
56 | uint32_t revents, nb_fd; | |
57 | struct lttng_poll_event events; | |
58 | struct thread_notifiers *notifiers = data; | |
59 | const int quit_pipe_read_fd = lttng_pipe_get_readfd( | |
60 | notifiers->quit_pipe); | |
61 | ||
62 | DBG("[thread] Manage application started"); | |
63 | ||
64 | rcu_register_thread(); | |
65 | rcu_thread_online(); | |
66 | ||
67 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE); | |
68 | ||
69 | if (testpoint(sessiond_thread_manage_apps)) { | |
70 | goto error_testpoint; | |
71 | } | |
72 | ||
73 | health_code_update(); | |
74 | ||
75 | ret = lttng_poll_create(&events, 2, LTTNG_CLOEXEC); | |
76 | if (ret < 0) { | |
77 | goto error_poll_create; | |
78 | } | |
79 | ||
80 | ret = lttng_poll_add(&events, notifiers->apps_cmd_pipe_read_fd, | |
81 | LPOLLIN | LPOLLRDHUP); | |
82 | if (ret < 0) { | |
83 | goto error; | |
84 | } | |
85 | ||
86 | ret = lttng_poll_add(&events, quit_pipe_read_fd, LPOLLIN | LPOLLERR); | |
87 | if (ret < 0) { | |
88 | goto error; | |
89 | } | |
90 | ||
91 | if (testpoint(sessiond_thread_manage_apps_before_loop)) { | |
92 | goto error; | |
93 | } | |
94 | ||
95 | health_code_update(); | |
96 | ||
97 | while (1) { | |
98 | DBG("Apps thread polling"); | |
99 | ||
100 | /* Inifinite blocking call, waiting for transmission */ | |
101 | restart: | |
102 | health_poll_entry(); | |
103 | ret = lttng_poll_wait(&events, -1); | |
104 | DBG("Apps thread return from poll on %d fds", | |
105 | LTTNG_POLL_GETNB(&events)); | |
106 | health_poll_exit(); | |
107 | if (ret < 0) { | |
108 | /* | |
109 | * Restart interrupted system call. | |
110 | */ | |
111 | if (errno == EINTR) { | |
112 | goto restart; | |
113 | } | |
114 | goto error; | |
115 | } | |
116 | ||
117 | nb_fd = ret; | |
118 | ||
119 | for (i = 0; i < nb_fd; i++) { | |
120 | /* Fetch once the poll data */ | |
121 | revents = LTTNG_POLL_GETEV(&events, i); | |
122 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
123 | ||
124 | health_code_update(); | |
125 | ||
126 | if (!revents) { | |
127 | /* No activity for this FD (poll implementation). */ | |
128 | continue; | |
129 | } | |
130 | ||
131 | if (pollfd == quit_pipe_read_fd) { | |
132 | err = 0; | |
133 | goto exit; | |
134 | } else if (pollfd == notifiers->apps_cmd_pipe_read_fd) { | |
135 | /* Inspect the apps cmd pipe */ | |
136 | if (revents & LPOLLIN) { | |
137 | int sock; | |
138 | ||
139 | /* Empty pipe */ | |
140 | size_ret = lttng_read( | |
141 | notifiers->apps_cmd_pipe_read_fd, | |
142 | &sock, sizeof(sock)); | |
143 | if (size_ret < sizeof(sock)) { | |
144 | PERROR("read apps cmd pipe"); | |
145 | goto error; | |
146 | } | |
147 | ||
148 | health_code_update(); | |
149 | ||
150 | /* | |
151 | * Since this is a command socket (write then read), | |
152 | * we only monitor the error events of the socket. | |
153 | */ | |
154 | ret = lttng_poll_add(&events, sock, | |
155 | LPOLLERR | LPOLLHUP | LPOLLRDHUP); | |
156 | if (ret < 0) { | |
157 | goto error; | |
158 | } | |
159 | ||
160 | DBG("Apps with sock %d added to poll set", sock); | |
161 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
162 | ERR("Apps command pipe error"); | |
163 | goto error; | |
164 | } else { | |
165 | ERR("Unknown poll events %u for sock %d", revents, pollfd); | |
166 | goto error; | |
167 | } | |
168 | } else { | |
169 | /* | |
170 | * At this point, we know that a registered application made | |
171 | * the event at poll_wait. | |
172 | */ | |
173 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { | |
174 | /* Removing from the poll set */ | |
175 | ret = lttng_poll_del(&events, pollfd); | |
176 | if (ret < 0) { | |
177 | goto error; | |
178 | } | |
179 | ||
180 | /* Socket closed on remote end. */ | |
181 | ust_app_unregister(pollfd); | |
182 | } else { | |
183 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
184 | goto error; | |
185 | } | |
186 | } | |
187 | ||
188 | health_code_update(); | |
189 | } | |
190 | } | |
191 | ||
192 | exit: | |
193 | error: | |
194 | lttng_poll_clean(&events); | |
195 | error_poll_create: | |
196 | error_testpoint: | |
197 | ||
198 | /* | |
199 | * We don't clean the UST app hash table here since already registered | |
200 | * applications can still be controlled so let them be until the session | |
201 | * daemon dies or the applications stop. | |
202 | */ | |
203 | ||
204 | if (err) { | |
205 | health_error(); | |
206 | ERR("Health error occurred in %s", __func__); | |
207 | } | |
208 | health_unregister(health_sessiond); | |
209 | DBG("Application communication apps thread cleanup complete"); | |
210 | rcu_thread_offline(); | |
211 | rcu_unregister_thread(); | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | static bool shutdown_application_management_thread(void *data) | |
216 | { | |
217 | struct thread_notifiers *notifiers = data; | |
218 | const int write_fd = lttng_pipe_get_writefd(notifiers->quit_pipe); | |
219 | ||
220 | return notify_thread_pipe(write_fd) == 1; | |
221 | } | |
222 | ||
223 | bool launch_application_management_thread(int apps_cmd_pipe_read_fd) | |
224 | { | |
225 | struct lttng_pipe *quit_pipe; | |
226 | struct thread_notifiers *notifiers = NULL; | |
227 | struct lttng_thread *thread; | |
228 | ||
229 | quit_pipe = lttng_pipe_open(FD_CLOEXEC); | |
230 | if (!quit_pipe) { | |
231 | goto error; | |
232 | } | |
233 | ||
234 | notifiers = zmalloc(sizeof(*notifiers)); | |
235 | if (!notifiers) { | |
236 | goto error; | |
237 | } | |
238 | notifiers->quit_pipe = quit_pipe; | |
239 | notifiers->apps_cmd_pipe_read_fd = apps_cmd_pipe_read_fd; | |
240 | ||
241 | thread = lttng_thread_create("UST application management", | |
242 | thread_application_management, | |
243 | shutdown_application_management_thread, | |
244 | cleanup_application_management_thread, | |
245 | notifiers); | |
246 | if (!thread) { | |
247 | goto error; | |
248 | } | |
249 | ||
250 | lttng_thread_put(thread); | |
251 | return true; | |
252 | error: | |
253 | cleanup_application_management_thread(notifiers); | |
254 | return false; | |
255 | } |