Commit | Line | Data |
---|---|---|
2691221a MD |
1 | /* |
2 | * lttng-ust-comm.c | |
3 | * | |
4 | * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca> | |
5 | * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
6 | * | |
7 | * This library is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; only | |
10 | * version 2.1 of the License. | |
11 | * | |
12 | * This library is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with this library; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <sys/types.h> | |
23 | #include <sys/socket.h> | |
2629549e | 24 | #include <sys/prctl.h> |
2691221a MD |
25 | #include <unistd.h> |
26 | #include <errno.h> | |
d9e99d10 | 27 | #include <pthread.h> |
11ff9c7d MD |
28 | #include <semaphore.h> |
29 | #include <time.h> | |
1ea11eab | 30 | #include <assert.h> |
e822f505 | 31 | #include <signal.h> |
95259bd0 | 32 | #include <urcu/uatomic.h> |
1ea11eab | 33 | |
edaa1431 MD |
34 | #include <lttng-ust-comm.h> |
35 | #include <ust/usterr-signal-safe.h> | |
36 | #include <ust/lttng-ust-abi.h> | |
37 | #include <ust/tracepoint.h> | |
d0a1ae63 | 38 | #include <ust/tracepoint-internal.h> |
e822f505 | 39 | #include <ust/ust.h> |
b751f722 | 40 | #include "ltt-tracer-core.h" |
edaa1431 MD |
41 | |
42 | /* | |
43 | * Has lttng ust comm constructor been called ? | |
44 | */ | |
45 | static int initialized; | |
46 | ||
1ea11eab | 47 | /* |
17dfb34b MD |
48 | * The ust_lock/ust_unlock lock is used as a communication thread mutex. |
49 | * Held when handling a command, also held by fork() to deal with | |
50 | * removal of threads, and by exit path. | |
1ea11eab | 51 | */ |
1ea11eab MD |
52 | |
53 | /* Should the ust comm thread quit ? */ | |
54 | static int lttng_ust_comm_should_quit; | |
55 | ||
11ff9c7d MD |
56 | /* |
57 | * Wait for either of these before continuing to the main | |
58 | * program: | |
59 | * - the register_done message from sessiond daemon | |
60 | * (will let the sessiond daemon enable sessions before main | |
61 | * starts.) | |
62 | * - sessiond daemon is not reachable. | |
63 | * - timeout (ensuring applications are resilient to session | |
64 | * daemon problems). | |
65 | */ | |
66 | static sem_t constructor_wait; | |
950aab0c MD |
67 | /* |
68 | * Doing this for both the global and local sessiond. | |
69 | */ | |
95259bd0 | 70 | static int sem_count = { 2 }; |
11ff9c7d | 71 | |
1ea11eab MD |
72 | /* |
73 | * Info about socket and associated listener thread. | |
74 | */ | |
75 | struct sock_info { | |
11ff9c7d | 76 | const char *name; |
1ea11eab MD |
77 | char sock_path[PATH_MAX]; |
78 | int socket; | |
79 | pthread_t ust_listener; /* listener thread */ | |
46050b1a | 80 | int root_handle; |
8d20bf54 MD |
81 | int constructor_sem_posted; |
82 | int allowed; | |
1ea11eab | 83 | }; |
2691221a MD |
84 | |
85 | /* Socket from app (connect) to session daemon (listen) for communication */ | |
1ea11eab | 86 | struct sock_info global_apps = { |
11ff9c7d | 87 | .name = "global", |
1ea11eab MD |
88 | .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK, |
89 | .socket = -1, | |
46050b1a | 90 | .root_handle = -1, |
8d20bf54 | 91 | .allowed = 1, |
1ea11eab | 92 | }; |
2691221a MD |
93 | |
94 | /* TODO: allow global_apps_sock_path override */ | |
95 | ||
1ea11eab | 96 | struct sock_info local_apps = { |
11ff9c7d | 97 | .name = "local", |
1ea11eab | 98 | .socket = -1, |
46050b1a | 99 | .root_handle = -1, |
8d20bf54 | 100 | .allowed = 0, /* Check setuid bit first */ |
1ea11eab | 101 | }; |
2691221a | 102 | |
edaa1431 MD |
103 | extern void ltt_ring_buffer_client_overwrite_init(void); |
104 | extern void ltt_ring_buffer_client_discard_init(void); | |
105 | extern void ltt_ring_buffer_metadata_client_init(void); | |
106 | extern void ltt_ring_buffer_client_overwrite_exit(void); | |
107 | extern void ltt_ring_buffer_client_discard_exit(void); | |
108 | extern void ltt_ring_buffer_metadata_client_exit(void); | |
109 | ||
2691221a | 110 | static |
8d20bf54 | 111 | int setup_local_apps(void) |
2691221a MD |
112 | { |
113 | const char *home_dir; | |
2691221a | 114 | |
8d20bf54 MD |
115 | /* |
116 | * Disallow per-user tracing for setuid binaries. | |
117 | */ | |
118 | if (getuid() != geteuid()) { | |
119 | local_apps.allowed = 0; | |
d0a1ae63 | 120 | return 0; |
8d20bf54 MD |
121 | } else { |
122 | local_apps.allowed = 1; | |
123 | } | |
2691221a MD |
124 | home_dir = (const char *) getenv("HOME"); |
125 | if (!home_dir) | |
126 | return -ENOENT; | |
1ea11eab | 127 | snprintf(local_apps.sock_path, PATH_MAX, |
2691221a | 128 | DEFAULT_HOME_APPS_UNIX_SOCK, home_dir); |
2691221a MD |
129 | return 0; |
130 | } | |
131 | ||
132 | static | |
133 | int register_app_to_sessiond(int socket) | |
134 | { | |
135 | ssize_t ret; | |
2629549e | 136 | int prctl_ret; |
2691221a | 137 | struct { |
e44418f3 MD |
138 | uint32_t major; |
139 | uint32_t minor; | |
2691221a | 140 | pid_t pid; |
5c33bde8 | 141 | pid_t ppid; |
2691221a | 142 | uid_t uid; |
83610856 | 143 | gid_t gid; |
2629549e | 144 | char name[16]; /* process name */ |
2691221a MD |
145 | } reg_msg; |
146 | ||
e44418f3 MD |
147 | reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR; |
148 | reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR; | |
2691221a | 149 | reg_msg.pid = getpid(); |
5c33bde8 | 150 | reg_msg.ppid = getppid(); |
2691221a | 151 | reg_msg.uid = getuid(); |
83610856 | 152 | reg_msg.gid = getgid(); |
2629549e MD |
153 | prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0); |
154 | if (prctl_ret) { | |
155 | ERR("Error executing prctl"); | |
156 | return -errno; | |
157 | } | |
2691221a MD |
158 | |
159 | ret = lttcomm_send_unix_sock(socket, ®_msg, sizeof(reg_msg)); | |
160 | if (ret >= 0 && ret != sizeof(reg_msg)) | |
161 | return -EIO; | |
162 | return ret; | |
163 | } | |
164 | ||
d9e99d10 | 165 | static |
d3a492d1 | 166 | int send_reply(int sock, struct lttcomm_ust_reply *lur) |
d9e99d10 | 167 | { |
9eb62b9c | 168 | ssize_t len; |
d3a492d1 | 169 | |
a4be8962 | 170 | len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 171 | switch (len) { |
a4be8962 | 172 | case sizeof(*lur): |
d3a492d1 MD |
173 | DBG("message successfully sent"); |
174 | return 0; | |
175 | case -1: | |
176 | if (errno == ECONNRESET) { | |
177 | printf("remote end closed connection\n"); | |
178 | return 0; | |
179 | } | |
180 | return -1; | |
181 | default: | |
182 | printf("incorrect message size: %zd\n", len); | |
183 | return -1; | |
184 | } | |
185 | } | |
186 | ||
187 | static | |
edaa1431 | 188 | int handle_register_done(struct sock_info *sock_info) |
11ff9c7d MD |
189 | { |
190 | int ret; | |
191 | ||
edaa1431 MD |
192 | if (sock_info->constructor_sem_posted) |
193 | return 0; | |
194 | sock_info->constructor_sem_posted = 1; | |
95259bd0 MD |
195 | ret = uatomic_add_return(&sem_count, -1); |
196 | if (ret == 0) { | |
197 | ret = sem_post(&constructor_wait); | |
198 | assert(!ret); | |
199 | } | |
11ff9c7d MD |
200 | return 0; |
201 | } | |
202 | ||
203 | static | |
204 | int handle_message(struct sock_info *sock_info, | |
205 | int sock, struct lttcomm_ust_msg *lum) | |
d3a492d1 | 206 | { |
1ea11eab | 207 | int ret = 0; |
46050b1a MD |
208 | const struct objd_ops *ops; |
209 | struct lttcomm_ust_reply lur; | |
1ea11eab | 210 | |
17dfb34b | 211 | ust_lock(); |
1ea11eab | 212 | |
46050b1a MD |
213 | memset(&lur, 0, sizeof(lur)); |
214 | ||
1ea11eab | 215 | if (lttng_ust_comm_should_quit) { |
46050b1a | 216 | ret = -EPERM; |
1ea11eab MD |
217 | goto end; |
218 | } | |
9eb62b9c | 219 | |
46050b1a MD |
220 | ops = objd_ops(lum->handle); |
221 | if (!ops) { | |
222 | ret = -ENOENT; | |
223 | goto end; | |
1ea11eab | 224 | } |
46050b1a MD |
225 | |
226 | switch (lum->cmd) { | |
11ff9c7d MD |
227 | case LTTNG_UST_REGISTER_DONE: |
228 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 229 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
230 | else |
231 | ret = -EINVAL; | |
232 | break; | |
46050b1a MD |
233 | case LTTNG_UST_RELEASE: |
234 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
235 | ret = -EPERM; | |
236 | else | |
237 | ret = objd_unref(lum->handle); | |
d9e99d10 MD |
238 | break; |
239 | default: | |
46050b1a MD |
240 | if (ops->cmd) |
241 | ret = ops->cmd(lum->handle, lum->cmd, | |
242 | (unsigned long) &lum->u); | |
243 | else | |
244 | ret = -ENOSYS; | |
245 | break; | |
d9e99d10 | 246 | } |
46050b1a | 247 | |
1ea11eab | 248 | end: |
46050b1a MD |
249 | lur.handle = lum->handle; |
250 | lur.cmd = lum->cmd; | |
251 | lur.ret_val = ret; | |
252 | if (ret >= 0) { | |
253 | lur.ret_code = LTTCOMM_OK; | |
254 | } else { | |
255 | lur.ret_code = LTTCOMM_SESSION_FAIL; | |
256 | } | |
257 | ret = send_reply(sock, &lur); | |
258 | ||
17dfb34b | 259 | ust_unlock(); |
1ea11eab | 260 | return ret; |
d9e99d10 MD |
261 | } |
262 | ||
46050b1a MD |
263 | static |
264 | void cleanup_sock_info(struct sock_info *sock_info) | |
265 | { | |
266 | int ret; | |
267 | ||
268 | if (sock_info->socket != -1) { | |
269 | ret = close(sock_info->socket); | |
270 | if (ret) { | |
271 | ERR("Error closing local apps socket"); | |
272 | } | |
273 | sock_info->socket = -1; | |
274 | } | |
275 | if (sock_info->root_handle != -1) { | |
276 | ret = objd_unref(sock_info->root_handle); | |
277 | if (ret) { | |
278 | ERR("Error unref root handle"); | |
279 | } | |
280 | sock_info->root_handle = -1; | |
281 | } | |
282 | } | |
283 | ||
1ea11eab MD |
284 | /* |
285 | * This thread does not allocate any resource, except within | |
286 | * handle_message, within mutex protection. This mutex protects against | |
287 | * fork and exit. | |
288 | * The other moment it allocates resources is at socket connexion, which | |
289 | * is also protected by the mutex. | |
290 | */ | |
d9e99d10 MD |
291 | static |
292 | void *ust_listener_thread(void *arg) | |
293 | { | |
1ea11eab MD |
294 | struct sock_info *sock_info = arg; |
295 | int sock, ret; | |
d9e99d10 | 296 | |
9eb62b9c MD |
297 | /* Restart trying to connect to the session daemon */ |
298 | restart: | |
17dfb34b | 299 | ust_lock(); |
1ea11eab MD |
300 | |
301 | if (lttng_ust_comm_should_quit) { | |
17dfb34b | 302 | ust_unlock(); |
1ea11eab MD |
303 | goto quit; |
304 | } | |
9eb62b9c | 305 | |
1ea11eab MD |
306 | if (sock_info->socket != -1) { |
307 | ret = close(sock_info->socket); | |
308 | if (ret) { | |
11ff9c7d | 309 | ERR("Error closing %s apps socket", sock_info->name); |
1ea11eab MD |
310 | } |
311 | sock_info->socket = -1; | |
312 | } | |
46050b1a | 313 | |
9eb62b9c MD |
314 | /* Check for sessiond availability with pipe TODO */ |
315 | ||
316 | /* Register */ | |
1ea11eab | 317 | ret = lttcomm_connect_unix_sock(sock_info->sock_path); |
9eb62b9c | 318 | if (ret < 0) { |
11ff9c7d MD |
319 | ERR("Error connecting to %s apps socket", sock_info->name); |
320 | /* | |
321 | * If we cannot find the sessiond daemon, don't delay | |
322 | * constructor execution. | |
323 | */ | |
edaa1431 | 324 | ret = handle_register_done(sock_info); |
11ff9c7d | 325 | assert(!ret); |
17dfb34b | 326 | ust_unlock(); |
4eef2998 | 327 | sleep(5); |
1ea11eab | 328 | goto restart; |
46050b1a MD |
329 | } |
330 | ||
331 | sock_info->socket = sock = ret; | |
332 | ||
333 | /* | |
334 | * Create only one root handle per listener thread for the whole | |
335 | * process lifetime. | |
336 | */ | |
337 | if (sock_info->root_handle == -1) { | |
338 | ret = lttng_abi_create_root_handle(); | |
339 | if (ret) { | |
340 | ERR("Error creating root handle"); | |
17dfb34b | 341 | ust_unlock(); |
46050b1a MD |
342 | goto quit; |
343 | } | |
344 | sock_info->root_handle = ret; | |
9eb62b9c | 345 | } |
1ea11eab | 346 | |
9eb62b9c MD |
347 | ret = register_app_to_sessiond(sock); |
348 | if (ret < 0) { | |
11ff9c7d MD |
349 | ERR("Error registering to %s apps socket", sock_info->name); |
350 | /* | |
351 | * If we cannot register to the sessiond daemon, don't | |
352 | * delay constructor execution. | |
353 | */ | |
edaa1431 | 354 | ret = handle_register_done(sock_info); |
11ff9c7d | 355 | assert(!ret); |
17dfb34b | 356 | ust_unlock(); |
9eb62b9c MD |
357 | sleep(5); |
358 | goto restart; | |
359 | } | |
17dfb34b | 360 | ust_unlock(); |
46050b1a | 361 | |
d9e99d10 MD |
362 | for (;;) { |
363 | ssize_t len; | |
e7723462 | 364 | struct lttcomm_ust_msg lum; |
d9e99d10 | 365 | |
e7723462 | 366 | len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
367 | switch (len) { |
368 | case 0: /* orderly shutdown */ | |
11ff9c7d | 369 | DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name); |
d9e99d10 | 370 | goto end; |
e7723462 | 371 | case sizeof(lum): |
d9e99d10 | 372 | DBG("message received\n"); |
11ff9c7d | 373 | ret = handle_message(sock_info, sock, &lum); |
2a80c9d8 | 374 | if (ret < 0) { |
11ff9c7d | 375 | ERR("Error handling message for %s socket", sock_info->name); |
d9e99d10 MD |
376 | } |
377 | continue; | |
378 | case -1: | |
379 | if (errno == ECONNRESET) { | |
11ff9c7d | 380 | ERR("%s remote end closed connection\n", sock_info->name); |
d9e99d10 MD |
381 | goto end; |
382 | } | |
383 | goto end; | |
384 | default: | |
11ff9c7d | 385 | ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len); |
d9e99d10 MD |
386 | continue; |
387 | } | |
388 | ||
389 | } | |
390 | end: | |
9eb62b9c | 391 | goto restart; /* try to reconnect */ |
1ea11eab | 392 | quit: |
d9e99d10 MD |
393 | return NULL; |
394 | } | |
395 | ||
cf12a773 MD |
396 | /* |
397 | * Return values: -1: don't wait. 0: wait forever. 1: timeout wait. | |
398 | */ | |
11ff9c7d MD |
399 | static |
400 | int get_timeout(struct timespec *constructor_timeout) | |
401 | { | |
cf12a773 MD |
402 | long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS; |
403 | char *str_delay; | |
11ff9c7d MD |
404 | int ret; |
405 | ||
cf12a773 MD |
406 | str_delay = getenv("UST_REGISTER_TIMEOUT"); |
407 | if (str_delay) { | |
408 | constructor_delay_ms = strtol(str_delay, NULL, 10); | |
409 | } | |
410 | ||
411 | switch (constructor_delay_ms) { | |
412 | case -1:/* fall-through */ | |
413 | case 0: | |
414 | return constructor_delay_ms; | |
415 | default: | |
416 | break; | |
417 | } | |
418 | ||
419 | /* | |
420 | * If we are unable to find the current time, don't wait. | |
421 | */ | |
422 | ret = clock_gettime(CLOCK_REALTIME, constructor_timeout); | |
423 | if (ret) { | |
424 | return -1; | |
425 | } | |
95259bd0 MD |
426 | constructor_timeout->tv_sec += constructor_delay_ms / 1000UL; |
427 | constructor_timeout->tv_nsec += | |
428 | (constructor_delay_ms % 1000UL) * 1000000UL; | |
11ff9c7d MD |
429 | if (constructor_timeout->tv_nsec >= 1000000000UL) { |
430 | constructor_timeout->tv_sec++; | |
431 | constructor_timeout->tv_nsec -= 1000000000UL; | |
432 | } | |
cf12a773 | 433 | return 1; |
11ff9c7d | 434 | } |
d9e99d10 | 435 | |
2691221a MD |
436 | /* |
437 | * sessiond monitoring thread: monitor presence of global and per-user | |
438 | * sessiond by polling the application common named pipe. | |
439 | */ | |
440 | /* TODO */ | |
441 | ||
edaa1431 | 442 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 443 | { |
11ff9c7d | 444 | struct timespec constructor_timeout; |
cf12a773 | 445 | int timeout_mode; |
2691221a MD |
446 | int ret; |
447 | ||
edaa1431 MD |
448 | if (uatomic_xchg(&initialized, 1) == 1) |
449 | return; | |
450 | ||
451 | /* | |
452 | * We want precise control over the order in which we construct | |
453 | * our sub-libraries vs starting to receive commands from | |
454 | * sessiond (otherwise leading to errors when trying to create | |
455 | * sessiond before the init functions are completed). | |
456 | */ | |
2691221a | 457 | init_usterr(); |
edaa1431 MD |
458 | init_tracepoint(); |
459 | ltt_ring_buffer_metadata_client_init(); | |
460 | ltt_ring_buffer_client_overwrite_init(); | |
461 | ltt_ring_buffer_client_discard_init(); | |
2691221a | 462 | |
cf12a773 | 463 | timeout_mode = get_timeout(&constructor_timeout); |
11ff9c7d | 464 | |
95259bd0 | 465 | ret = sem_init(&constructor_wait, 0, 0); |
11ff9c7d MD |
466 | assert(!ret); |
467 | ||
8d20bf54 | 468 | ret = setup_local_apps(); |
2691221a | 469 | if (ret) { |
8d20bf54 | 470 | ERR("Error setting up to local apps"); |
2691221a | 471 | } |
1ea11eab MD |
472 | ret = pthread_create(&local_apps.ust_listener, NULL, |
473 | ust_listener_thread, &local_apps); | |
11ff9c7d | 474 | |
8d20bf54 MD |
475 | if (local_apps.allowed) { |
476 | ret = pthread_create(&global_apps.ust_listener, NULL, | |
477 | ust_listener_thread, &global_apps); | |
478 | } else { | |
479 | handle_register_done(&local_apps); | |
480 | } | |
481 | ||
cf12a773 MD |
482 | switch (timeout_mode) { |
483 | case 1: /* timeout wait */ | |
95259bd0 MD |
484 | do { |
485 | ret = sem_timedwait(&constructor_wait, | |
486 | &constructor_timeout); | |
487 | } while (ret < 0 && errno == EINTR); | |
cf12a773 MD |
488 | if (ret < 0 && errno == ETIMEDOUT) { |
489 | ERR("Timed out waiting for ltt-sessiond"); | |
490 | } else { | |
491 | assert(!ret); | |
492 | } | |
493 | break; | |
7b766b16 | 494 | case -1:/* wait forever */ |
95259bd0 MD |
495 | do { |
496 | ret = sem_wait(&constructor_wait); | |
497 | } while (ret < 0 && errno == EINTR); | |
11ff9c7d | 498 | assert(!ret); |
cf12a773 | 499 | break; |
7b766b16 | 500 | case 0: /* no timeout */ |
cf12a773 | 501 | break; |
11ff9c7d | 502 | } |
2691221a MD |
503 | } |
504 | ||
17dfb34b MD |
505 | static |
506 | void lttng_ust_cleanup(int exiting) | |
507 | { | |
508 | cleanup_sock_info(&global_apps); | |
509 | if (local_apps.allowed) { | |
510 | cleanup_sock_info(&local_apps); | |
511 | } | |
512 | lttng_ust_abi_exit(); | |
513 | ltt_events_exit(); | |
514 | ltt_ring_buffer_client_discard_exit(); | |
515 | ltt_ring_buffer_client_overwrite_exit(); | |
516 | ltt_ring_buffer_metadata_client_exit(); | |
517 | exit_tracepoint(); | |
518 | if (!exiting) { | |
519 | /* Reinitialize values for fork */ | |
520 | sem_count = 2; | |
521 | lttng_ust_comm_should_quit = 0; | |
522 | initialized = 0; | |
523 | } | |
524 | } | |
525 | ||
edaa1431 | 526 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
527 | { |
528 | int ret; | |
529 | ||
9eb62b9c MD |
530 | /* |
531 | * Using pthread_cancel here because: | |
532 | * A) we don't want to hang application teardown. | |
533 | * B) the thread is not allocating any resource. | |
534 | */ | |
1ea11eab MD |
535 | |
536 | /* | |
537 | * Require the communication thread to quit. Synchronize with | |
538 | * mutexes to ensure it is not in a mutex critical section when | |
539 | * pthread_cancel is later called. | |
540 | */ | |
17dfb34b | 541 | ust_lock(); |
1ea11eab | 542 | lttng_ust_comm_should_quit = 1; |
17dfb34b | 543 | ust_unlock(); |
1ea11eab | 544 | |
1ea11eab | 545 | ret = pthread_cancel(global_apps.ust_listener); |
9eb62b9c MD |
546 | if (ret) { |
547 | ERR("Error cancelling global ust listener thread"); | |
2691221a | 548 | } |
8d20bf54 MD |
549 | if (local_apps.allowed) { |
550 | ret = pthread_cancel(local_apps.ust_listener); | |
551 | if (ret) { | |
552 | ERR("Error cancelling local ust listener thread"); | |
553 | } | |
8d20bf54 | 554 | } |
17dfb34b | 555 | lttng_ust_cleanup(1); |
2691221a | 556 | } |
e822f505 MD |
557 | |
558 | /* | |
559 | * We exclude the worker threads across fork and clone (except | |
560 | * CLONE_VM), because these system calls only keep the forking thread | |
561 | * running in the child. Therefore, we don't want to call fork or clone | |
562 | * in the middle of an tracepoint or ust tracing state modification. | |
563 | * Holding this mutex protects these structures across fork and clone. | |
564 | */ | |
565 | void ust_before_fork(ust_fork_info_t *fork_info) | |
566 | { | |
567 | /* | |
568 | * Disable signals. This is to avoid that the child intervenes | |
569 | * before it is properly setup for tracing. It is safer to | |
570 | * disable all signals, because then we know we are not breaking | |
571 | * anything by restoring the original mask. | |
572 | */ | |
573 | sigset_t all_sigs; | |
574 | int ret; | |
575 | ||
576 | /* Disable signals */ | |
577 | sigfillset(&all_sigs); | |
578 | ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs); | |
579 | if (ret == -1) { | |
580 | PERROR("sigprocmask"); | |
581 | } | |
17dfb34b | 582 | ust_lock(); |
e822f505 MD |
583 | rcu_bp_before_fork(); |
584 | } | |
585 | ||
586 | static void ust_after_fork_common(ust_fork_info_t *fork_info) | |
587 | { | |
588 | int ret; | |
589 | ||
17dfb34b MD |
590 | DBG("process %d", getpid()); |
591 | ust_unlock(); | |
e822f505 MD |
592 | /* Restore signals */ |
593 | ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL); | |
594 | if (ret == -1) { | |
595 | PERROR("sigprocmask"); | |
596 | } | |
597 | } | |
598 | ||
599 | void ust_after_fork_parent(ust_fork_info_t *fork_info) | |
600 | { | |
17dfb34b | 601 | DBG("process %d", getpid()); |
e822f505 MD |
602 | rcu_bp_after_fork_parent(); |
603 | /* Release mutexes and reenable signals */ | |
604 | ust_after_fork_common(fork_info); | |
605 | } | |
606 | ||
17dfb34b MD |
607 | /* |
608 | * After fork, in the child, we need to cleanup all the leftover state, | |
609 | * except the worker thread which already magically disappeared thanks | |
610 | * to the weird Linux fork semantics. After tyding up, we call | |
611 | * lttng_ust_init() again to start over as a new PID. | |
612 | * | |
613 | * This is meant for forks() that have tracing in the child between the | |
614 | * fork and following exec call (if there is any). | |
615 | */ | |
e822f505 MD |
616 | void ust_after_fork_child(ust_fork_info_t *fork_info) |
617 | { | |
17dfb34b | 618 | DBG("process %d", getpid()); |
e822f505 MD |
619 | /* Release urcu mutexes */ |
620 | rcu_bp_after_fork_child(); | |
17dfb34b MD |
621 | lttng_ust_cleanup(0); |
622 | lttng_ust_init(); | |
e822f505 MD |
623 | /* Release mutexes and reenable signals */ |
624 | ust_after_fork_common(fork_info); | |
625 | } |