restore signals in error case (shm allocation)
[lttng-ust.git] / libust / lttng-ust-comm.c
CommitLineData
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>
7fc90dca
MD
25#include <sys/mman.h>
26#include <sys/stat.h>
27#include <fcntl.h>
2691221a
MD
28#include <unistd.h>
29#include <errno.h>
d9e99d10 30#include <pthread.h>
11ff9c7d
MD
31#include <semaphore.h>
32#include <time.h>
1ea11eab 33#include <assert.h>
e822f505 34#include <signal.h>
95259bd0 35#include <urcu/uatomic.h>
1ea11eab 36
edaa1431
MD
37#include <lttng-ust-comm.h>
38#include <ust/usterr-signal-safe.h>
39#include <ust/lttng-ust-abi.h>
40#include <ust/tracepoint.h>
d0a1ae63 41#include <ust/tracepoint-internal.h>
e822f505 42#include <ust/ust.h>
b751f722 43#include "ltt-tracer-core.h"
edaa1431
MD
44
45/*
46 * Has lttng ust comm constructor been called ?
47 */
48static int initialized;
49
1ea11eab 50/*
17dfb34b
MD
51 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
52 * Held when handling a command, also held by fork() to deal with
53 * removal of threads, and by exit path.
1ea11eab 54 */
1ea11eab
MD
55
56/* Should the ust comm thread quit ? */
57static int lttng_ust_comm_should_quit;
58
11ff9c7d
MD
59/*
60 * Wait for either of these before continuing to the main
61 * program:
62 * - the register_done message from sessiond daemon
63 * (will let the sessiond daemon enable sessions before main
64 * starts.)
65 * - sessiond daemon is not reachable.
66 * - timeout (ensuring applications are resilient to session
67 * daemon problems).
68 */
69static sem_t constructor_wait;
950aab0c
MD
70/*
71 * Doing this for both the global and local sessiond.
72 */
95259bd0 73static int sem_count = { 2 };
11ff9c7d 74
1ea11eab
MD
75/*
76 * Info about socket and associated listener thread.
77 */
78struct sock_info {
11ff9c7d 79 const char *name;
1ea11eab 80 pthread_t ust_listener; /* listener thread */
46050b1a 81 int root_handle;
8d20bf54
MD
82 int constructor_sem_posted;
83 int allowed;
44e073f5 84 int global;
7fc90dca
MD
85
86 char sock_path[PATH_MAX];
87 int socket;
88
89 char wait_shm_path[PATH_MAX];
90 char *wait_shm_mmap;
1ea11eab 91};
2691221a
MD
92
93/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 94struct sock_info global_apps = {
11ff9c7d 95 .name = "global",
44e073f5 96 .global = 1,
7fc90dca 97
46050b1a 98 .root_handle = -1,
8d20bf54 99 .allowed = 1,
7fc90dca
MD
100
101 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
102 .socket = -1,
103
104 .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
1ea11eab 105};
2691221a
MD
106
107/* TODO: allow global_apps_sock_path override */
108
1ea11eab 109struct sock_info local_apps = {
11ff9c7d 110 .name = "local",
44e073f5 111 .global = 0,
46050b1a 112 .root_handle = -1,
8d20bf54 113 .allowed = 0, /* Check setuid bit first */
7fc90dca
MD
114
115 .socket = -1,
1ea11eab 116};
2691221a 117
edaa1431
MD
118extern void ltt_ring_buffer_client_overwrite_init(void);
119extern void ltt_ring_buffer_client_discard_init(void);
120extern void ltt_ring_buffer_metadata_client_init(void);
121extern void ltt_ring_buffer_client_overwrite_exit(void);
122extern void ltt_ring_buffer_client_discard_exit(void);
123extern void ltt_ring_buffer_metadata_client_exit(void);
124
2691221a 125static
8d20bf54 126int setup_local_apps(void)
2691221a
MD
127{
128 const char *home_dir;
7fc90dca 129 uid_t uid;
2691221a 130
7fc90dca 131 uid = getuid();
8d20bf54
MD
132 /*
133 * Disallow per-user tracing for setuid binaries.
134 */
7fc90dca 135 if (uid != geteuid()) {
8d20bf54 136 local_apps.allowed = 0;
d0a1ae63 137 return 0;
8d20bf54
MD
138 } else {
139 local_apps.allowed = 1;
140 }
2691221a
MD
141 home_dir = (const char *) getenv("HOME");
142 if (!home_dir)
143 return -ENOENT;
1ea11eab 144 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 145 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
7fc90dca
MD
146 snprintf(local_apps.wait_shm_path, PATH_MAX,
147 DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid);
2691221a
MD
148 return 0;
149}
150
151static
152int register_app_to_sessiond(int socket)
153{
154 ssize_t ret;
2629549e 155 int prctl_ret;
2691221a 156 struct {
e44418f3
MD
157 uint32_t major;
158 uint32_t minor;
2691221a 159 pid_t pid;
5c33bde8 160 pid_t ppid;
2691221a 161 uid_t uid;
83610856 162 gid_t gid;
2629549e 163 char name[16]; /* process name */
2691221a
MD
164 } reg_msg;
165
e44418f3
MD
166 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
167 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a 168 reg_msg.pid = getpid();
5c33bde8 169 reg_msg.ppid = getppid();
2691221a 170 reg_msg.uid = getuid();
83610856 171 reg_msg.gid = getgid();
2629549e
MD
172 prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0);
173 if (prctl_ret) {
174 ERR("Error executing prctl");
175 return -errno;
176 }
2691221a
MD
177
178 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
179 if (ret >= 0 && ret != sizeof(reg_msg))
180 return -EIO;
181 return ret;
182}
183
d9e99d10 184static
d3a492d1 185int send_reply(int sock, struct lttcomm_ust_reply *lur)
d9e99d10 186{
9eb62b9c 187 ssize_t len;
d3a492d1 188
a4be8962 189 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 190 switch (len) {
a4be8962 191 case sizeof(*lur):
d3a492d1
MD
192 DBG("message successfully sent");
193 return 0;
194 case -1:
195 if (errno == ECONNRESET) {
196 printf("remote end closed connection\n");
197 return 0;
198 }
199 return -1;
200 default:
201 printf("incorrect message size: %zd\n", len);
202 return -1;
203 }
204}
205
206static
edaa1431 207int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
208{
209 int ret;
210
edaa1431
MD
211 if (sock_info->constructor_sem_posted)
212 return 0;
213 sock_info->constructor_sem_posted = 1;
95259bd0
MD
214 ret = uatomic_add_return(&sem_count, -1);
215 if (ret == 0) {
216 ret = sem_post(&constructor_wait);
217 assert(!ret);
218 }
11ff9c7d
MD
219 return 0;
220}
221
222static
223int handle_message(struct sock_info *sock_info,
224 int sock, struct lttcomm_ust_msg *lum)
d3a492d1 225{
1ea11eab 226 int ret = 0;
46050b1a
MD
227 const struct objd_ops *ops;
228 struct lttcomm_ust_reply lur;
1ea11eab 229
17dfb34b 230 ust_lock();
1ea11eab 231
46050b1a
MD
232 memset(&lur, 0, sizeof(lur));
233
1ea11eab 234 if (lttng_ust_comm_should_quit) {
46050b1a 235 ret = -EPERM;
1ea11eab
MD
236 goto end;
237 }
9eb62b9c 238
46050b1a
MD
239 ops = objd_ops(lum->handle);
240 if (!ops) {
241 ret = -ENOENT;
242 goto end;
1ea11eab 243 }
46050b1a
MD
244
245 switch (lum->cmd) {
11ff9c7d
MD
246 case LTTNG_UST_REGISTER_DONE:
247 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 248 ret = handle_register_done(sock_info);
11ff9c7d
MD
249 else
250 ret = -EINVAL;
251 break;
46050b1a
MD
252 case LTTNG_UST_RELEASE:
253 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
254 ret = -EPERM;
255 else
256 ret = objd_unref(lum->handle);
d9e99d10
MD
257 break;
258 default:
46050b1a
MD
259 if (ops->cmd)
260 ret = ops->cmd(lum->handle, lum->cmd,
261 (unsigned long) &lum->u);
262 else
263 ret = -ENOSYS;
264 break;
d9e99d10 265 }
46050b1a 266
1ea11eab 267end:
46050b1a
MD
268 lur.handle = lum->handle;
269 lur.cmd = lum->cmd;
270 lur.ret_val = ret;
271 if (ret >= 0) {
272 lur.ret_code = LTTCOMM_OK;
273 } else {
274 lur.ret_code = LTTCOMM_SESSION_FAIL;
275 }
276 ret = send_reply(sock, &lur);
277
17dfb34b 278 ust_unlock();
1ea11eab 279 return ret;
d9e99d10
MD
280}
281
46050b1a
MD
282static
283void cleanup_sock_info(struct sock_info *sock_info)
284{
285 int ret;
286
287 if (sock_info->socket != -1) {
288 ret = close(sock_info->socket);
289 if (ret) {
7fc90dca 290 ERR("Error closing apps socket");
46050b1a
MD
291 }
292 sock_info->socket = -1;
293 }
294 if (sock_info->root_handle != -1) {
295 ret = objd_unref(sock_info->root_handle);
296 if (ret) {
297 ERR("Error unref root handle");
298 }
299 sock_info->root_handle = -1;
300 }
318dfea9 301 sock_info->constructor_sem_posted = 0;
7fc90dca
MD
302 if (sock_info->wait_shm_mmap) {
303 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
304 if (ret) {
305 ERR("Error unmapping wait shm");
306 }
307 sock_info->wait_shm_mmap = NULL;
308 }
309}
310
311static
312char *get_map_shm(struct sock_info *sock_info)
313{
314 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
315 int wait_shm_fd, ret;
316 char *wait_shm_mmap;
44e073f5
MD
317 int mode;
318
319 mode = S_IRUSR | S_IRGRP;
320 if (sock_info->global)
321 mode |= S_IROTH;
7fc90dca
MD
322
323 /*
324 * Get existing (read-only) shm, or open new shm.
325 * First try to open read-only.
326 */
327 wait_shm_fd = shm_open(sock_info->wait_shm_path,
44e073f5 328 O_RDONLY, mode);
7fc90dca
MD
329 if (wait_shm_fd >= 0)
330 goto got_shm;
331 /*
332 * Real-only open did not work. If it is because it did
333 * not exist, try creating it. Else it's a failure that
334 * prohibits using shm.
335 */
336 if (errno != ENOENT) {
337 ERR("Error opening shm %s", sock_info->wait_shm_path);
338 goto error;
339 }
340 wait_shm_fd = shm_open(sock_info->wait_shm_path,
44e073f5 341 O_RDWR | O_CREAT | O_EXCL, mode | S_IWUSR);
7fc90dca
MD
342 if (wait_shm_fd >= 0)
343 goto created_shm;
344 if (errno != EEXIST) {
345 ERR("Error opening shm %s", sock_info->wait_shm_path);
346 goto error;
347 }
348 /*
349 * If another process beat us to create the shm, we are
350 * pretty certain the shm is available for us in
351 * read-only mode.
352 */
353 wait_shm_fd = shm_open(sock_info->wait_shm_path,
44e073f5 354 O_RDWR | O_CREAT | O_EXCL, mode);
7fc90dca
MD
355 if (wait_shm_fd >= 0)
356 goto got_shm;
357 else
358 goto error;
359
360created_shm:
361 ret = ftruncate(wait_shm_fd, mmap_size);
362 if (ret) {
363 PERROR("ftruncate");
364 ret = close(wait_shm_fd);
365 if (ret) {
366 ERR("Error closing fd");
367 }
368 wait_shm_fd = -1;
369 goto error;
370 }
44e073f5
MD
371 /* Drop write access ASAP */
372 ret = chmod(sock_info->wait_shm_path, mode);
373 if (ret) {
374 PERROR("chmod");
375 }
7fc90dca
MD
376got_shm:
377 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
378 MAP_SHARED, wait_shm_fd, 0);
379 if (wait_shm_mmap == MAP_FAILED) {
380 PERROR("mmap");
381 goto error;
382 }
383 /* close shm fd immediately after taking the mmap reference */
384 ret = close(wait_shm_fd);
385 if (ret) {
386 ERR("Error closing fd");
387 }
388 return wait_shm_mmap;
389
390error:
391 return NULL;
392}
393
394static
395void wait_for_sessiond(struct sock_info *sock_info)
396{
397 ust_lock();
398 if (lttng_ust_comm_should_quit) {
399 goto quit;
400 }
401 if (!sock_info->wait_shm_mmap) {
402 sock_info->wait_shm_mmap = get_map_shm(sock_info);
403 if (!sock_info->wait_shm_mmap)
404 goto error;
405 }
406 ust_unlock();
407
408 DBG("Waiting for %s apps sessiond", sock_info->name);
409 /* Wait for futex wakeup TODO */
410 sleep(5);
411
412 return;
413
414quit:
415 ust_unlock();
416 return;
417
418error:
419 ust_unlock();
420 /* Error handling: fallback on a 5 seconds sleep. */
421 sleep(5);
422 return;
46050b1a
MD
423}
424
1ea11eab
MD
425/*
426 * This thread does not allocate any resource, except within
427 * handle_message, within mutex protection. This mutex protects against
428 * fork and exit.
429 * The other moment it allocates resources is at socket connexion, which
430 * is also protected by the mutex.
431 */
d9e99d10
MD
432static
433void *ust_listener_thread(void *arg)
434{
1ea11eab
MD
435 struct sock_info *sock_info = arg;
436 int sock, ret;
d9e99d10 437
9eb62b9c
MD
438 /* Restart trying to connect to the session daemon */
439restart:
17dfb34b 440 ust_lock();
1ea11eab
MD
441
442 if (lttng_ust_comm_should_quit) {
17dfb34b 443 ust_unlock();
1ea11eab
MD
444 goto quit;
445 }
9eb62b9c 446
1ea11eab
MD
447 if (sock_info->socket != -1) {
448 ret = close(sock_info->socket);
449 if (ret) {
11ff9c7d 450 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
451 }
452 sock_info->socket = -1;
453 }
46050b1a 454
9eb62b9c 455 /* Register */
1ea11eab 456 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 457 if (ret < 0) {
11ff9c7d
MD
458 ERR("Error connecting to %s apps socket", sock_info->name);
459 /*
460 * If we cannot find the sessiond daemon, don't delay
461 * constructor execution.
462 */
edaa1431 463 ret = handle_register_done(sock_info);
11ff9c7d 464 assert(!ret);
17dfb34b 465 ust_unlock();
7fc90dca
MD
466
467 /* Wait for sessiond availability with pipe */
468 wait_for_sessiond(sock_info);
1ea11eab 469 goto restart;
46050b1a
MD
470 }
471
472 sock_info->socket = sock = ret;
473
474 /*
475 * Create only one root handle per listener thread for the whole
476 * process lifetime.
477 */
478 if (sock_info->root_handle == -1) {
479 ret = lttng_abi_create_root_handle();
480 if (ret) {
481 ERR("Error creating root handle");
17dfb34b 482 ust_unlock();
46050b1a
MD
483 goto quit;
484 }
485 sock_info->root_handle = ret;
9eb62b9c 486 }
1ea11eab 487
9eb62b9c
MD
488 ret = register_app_to_sessiond(sock);
489 if (ret < 0) {
11ff9c7d
MD
490 ERR("Error registering to %s apps socket", sock_info->name);
491 /*
492 * If we cannot register to the sessiond daemon, don't
493 * delay constructor execution.
494 */
edaa1431 495 ret = handle_register_done(sock_info);
11ff9c7d 496 assert(!ret);
17dfb34b 497 ust_unlock();
7fc90dca 498 wait_for_sessiond(sock_info);
9eb62b9c
MD
499 goto restart;
500 }
17dfb34b 501 ust_unlock();
46050b1a 502
d9e99d10
MD
503 for (;;) {
504 ssize_t len;
e7723462 505 struct lttcomm_ust_msg lum;
d9e99d10 506
e7723462 507 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
508 switch (len) {
509 case 0: /* orderly shutdown */
11ff9c7d 510 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
d9e99d10 511 goto end;
e7723462 512 case sizeof(lum):
d9e99d10 513 DBG("message received\n");
11ff9c7d 514 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 515 if (ret < 0) {
11ff9c7d 516 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
517 }
518 continue;
519 case -1:
520 if (errno == ECONNRESET) {
11ff9c7d 521 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
522 goto end;
523 }
524 goto end;
525 default:
11ff9c7d 526 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
527 continue;
528 }
529
530 }
531end:
9eb62b9c 532 goto restart; /* try to reconnect */
1ea11eab 533quit:
d9e99d10
MD
534 return NULL;
535}
536
cf12a773
MD
537/*
538 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
539 */
11ff9c7d
MD
540static
541int get_timeout(struct timespec *constructor_timeout)
542{
cf12a773
MD
543 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
544 char *str_delay;
11ff9c7d
MD
545 int ret;
546
cf12a773
MD
547 str_delay = getenv("UST_REGISTER_TIMEOUT");
548 if (str_delay) {
549 constructor_delay_ms = strtol(str_delay, NULL, 10);
550 }
551
552 switch (constructor_delay_ms) {
553 case -1:/* fall-through */
554 case 0:
555 return constructor_delay_ms;
556 default:
557 break;
558 }
559
560 /*
561 * If we are unable to find the current time, don't wait.
562 */
563 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
564 if (ret) {
565 return -1;
566 }
95259bd0
MD
567 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
568 constructor_timeout->tv_nsec +=
569 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
570 if (constructor_timeout->tv_nsec >= 1000000000UL) {
571 constructor_timeout->tv_sec++;
572 constructor_timeout->tv_nsec -= 1000000000UL;
573 }
cf12a773 574 return 1;
11ff9c7d 575}
d9e99d10 576
2691221a
MD
577/*
578 * sessiond monitoring thread: monitor presence of global and per-user
579 * sessiond by polling the application common named pipe.
580 */
581/* TODO */
582
edaa1431 583void __attribute__((constructor)) lttng_ust_init(void)
2691221a 584{
11ff9c7d 585 struct timespec constructor_timeout;
cf12a773 586 int timeout_mode;
2691221a
MD
587 int ret;
588
edaa1431
MD
589 if (uatomic_xchg(&initialized, 1) == 1)
590 return;
591
592 /*
593 * We want precise control over the order in which we construct
594 * our sub-libraries vs starting to receive commands from
595 * sessiond (otherwise leading to errors when trying to create
596 * sessiond before the init functions are completed).
597 */
2691221a 598 init_usterr();
edaa1431
MD
599 init_tracepoint();
600 ltt_ring_buffer_metadata_client_init();
601 ltt_ring_buffer_client_overwrite_init();
602 ltt_ring_buffer_client_discard_init();
2691221a 603
cf12a773 604 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 605
95259bd0 606 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
607 assert(!ret);
608
8d20bf54 609 ret = setup_local_apps();
2691221a 610 if (ret) {
8d20bf54 611 ERR("Error setting up to local apps");
2691221a 612 }
1ea11eab
MD
613 ret = pthread_create(&local_apps.ust_listener, NULL,
614 ust_listener_thread, &local_apps);
11ff9c7d 615
8d20bf54
MD
616 if (local_apps.allowed) {
617 ret = pthread_create(&global_apps.ust_listener, NULL,
618 ust_listener_thread, &global_apps);
619 } else {
620 handle_register_done(&local_apps);
621 }
622
cf12a773
MD
623 switch (timeout_mode) {
624 case 1: /* timeout wait */
95259bd0
MD
625 do {
626 ret = sem_timedwait(&constructor_wait,
627 &constructor_timeout);
628 } while (ret < 0 && errno == EINTR);
cf12a773
MD
629 if (ret < 0 && errno == ETIMEDOUT) {
630 ERR("Timed out waiting for ltt-sessiond");
631 } else {
632 assert(!ret);
633 }
634 break;
7b766b16 635 case -1:/* wait forever */
95259bd0
MD
636 do {
637 ret = sem_wait(&constructor_wait);
638 } while (ret < 0 && errno == EINTR);
11ff9c7d 639 assert(!ret);
cf12a773 640 break;
7b766b16 641 case 0: /* no timeout */
cf12a773 642 break;
11ff9c7d 643 }
2691221a
MD
644}
645
17dfb34b
MD
646static
647void lttng_ust_cleanup(int exiting)
648{
649 cleanup_sock_info(&global_apps);
650 if (local_apps.allowed) {
651 cleanup_sock_info(&local_apps);
652 }
653 lttng_ust_abi_exit();
654 ltt_events_exit();
655 ltt_ring_buffer_client_discard_exit();
656 ltt_ring_buffer_client_overwrite_exit();
657 ltt_ring_buffer_metadata_client_exit();
658 exit_tracepoint();
659 if (!exiting) {
660 /* Reinitialize values for fork */
661 sem_count = 2;
662 lttng_ust_comm_should_quit = 0;
663 initialized = 0;
664 }
665}
666
edaa1431 667void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
668{
669 int ret;
670
9eb62b9c
MD
671 /*
672 * Using pthread_cancel here because:
673 * A) we don't want to hang application teardown.
674 * B) the thread is not allocating any resource.
675 */
1ea11eab
MD
676
677 /*
678 * Require the communication thread to quit. Synchronize with
679 * mutexes to ensure it is not in a mutex critical section when
680 * pthread_cancel is later called.
681 */
17dfb34b 682 ust_lock();
1ea11eab 683 lttng_ust_comm_should_quit = 1;
17dfb34b 684 ust_unlock();
1ea11eab 685
1ea11eab 686 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
687 if (ret) {
688 ERR("Error cancelling global ust listener thread");
2691221a 689 }
8d20bf54
MD
690 if (local_apps.allowed) {
691 ret = pthread_cancel(local_apps.ust_listener);
692 if (ret) {
693 ERR("Error cancelling local ust listener thread");
694 }
8d20bf54 695 }
17dfb34b 696 lttng_ust_cleanup(1);
2691221a 697}
e822f505
MD
698
699/*
700 * We exclude the worker threads across fork and clone (except
701 * CLONE_VM), because these system calls only keep the forking thread
702 * running in the child. Therefore, we don't want to call fork or clone
703 * in the middle of an tracepoint or ust tracing state modification.
704 * Holding this mutex protects these structures across fork and clone.
705 */
706void ust_before_fork(ust_fork_info_t *fork_info)
707{
708 /*
709 * Disable signals. This is to avoid that the child intervenes
710 * before it is properly setup for tracing. It is safer to
711 * disable all signals, because then we know we are not breaking
712 * anything by restoring the original mask.
713 */
714 sigset_t all_sigs;
715 int ret;
716
717 /* Disable signals */
718 sigfillset(&all_sigs);
719 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
720 if (ret == -1) {
721 PERROR("sigprocmask");
722 }
17dfb34b 723 ust_lock();
e822f505
MD
724 rcu_bp_before_fork();
725}
726
727static void ust_after_fork_common(ust_fork_info_t *fork_info)
728{
729 int ret;
730
17dfb34b
MD
731 DBG("process %d", getpid());
732 ust_unlock();
e822f505
MD
733 /* Restore signals */
734 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
735 if (ret == -1) {
736 PERROR("sigprocmask");
737 }
738}
739
740void ust_after_fork_parent(ust_fork_info_t *fork_info)
741{
17dfb34b 742 DBG("process %d", getpid());
e822f505
MD
743 rcu_bp_after_fork_parent();
744 /* Release mutexes and reenable signals */
745 ust_after_fork_common(fork_info);
746}
747
17dfb34b
MD
748/*
749 * After fork, in the child, we need to cleanup all the leftover state,
750 * except the worker thread which already magically disappeared thanks
751 * to the weird Linux fork semantics. After tyding up, we call
752 * lttng_ust_init() again to start over as a new PID.
753 *
754 * This is meant for forks() that have tracing in the child between the
755 * fork and following exec call (if there is any).
756 */
e822f505
MD
757void ust_after_fork_child(ust_fork_info_t *fork_info)
758{
17dfb34b 759 DBG("process %d", getpid());
e822f505
MD
760 /* Release urcu mutexes */
761 rcu_bp_after_fork_child();
17dfb34b 762 lttng_ust_cleanup(0);
e822f505
MD
763 /* Release mutexes and reenable signals */
764 ust_after_fork_common(fork_info);
318dfea9 765 lttng_ust_init();
e822f505 766}
This page took 0.060475 seconds and 4 git commands to generate.