Do not install usterr-signal-safe
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
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 #define _LGPL_SOURCE
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/prctl.h>
26 #include <sys/mman.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <pthread.h>
34 #include <semaphore.h>
35 #include <time.h>
36 #include <assert.h>
37 #include <signal.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/futex.h>
40
41 #include <lttng/ust-events.h>
42 #include <lttng/ust-abi.h>
43 #include <lttng/ust.h>
44 #include <ust-comm.h>
45 #include <usterr-signal-safe.h>
46 #include "tracepoint-internal.h"
47 #include "ltt-tracer-core.h"
48
49 /*
50 * Has lttng ust comm constructor been called ?
51 */
52 static int initialized;
53
54 /*
55 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
56 * Held when handling a command, also held by fork() to deal with
57 * removal of threads, and by exit path.
58 */
59
60 /* Should the ust comm thread quit ? */
61 static int lttng_ust_comm_should_quit;
62
63 /*
64 * Wait for either of these before continuing to the main
65 * program:
66 * - the register_done message from sessiond daemon
67 * (will let the sessiond daemon enable sessions before main
68 * starts.)
69 * - sessiond daemon is not reachable.
70 * - timeout (ensuring applications are resilient to session
71 * daemon problems).
72 */
73 static sem_t constructor_wait;
74 /*
75 * Doing this for both the global and local sessiond.
76 */
77 static int sem_count = { 2 };
78
79 /*
80 * Info about socket and associated listener thread.
81 */
82 struct sock_info {
83 const char *name;
84 pthread_t ust_listener; /* listener thread */
85 int root_handle;
86 int constructor_sem_posted;
87 int allowed;
88 int global;
89
90 char sock_path[PATH_MAX];
91 int socket;
92
93 char wait_shm_path[PATH_MAX];
94 char *wait_shm_mmap;
95 };
96
97 /* Socket from app (connect) to session daemon (listen) for communication */
98 struct sock_info global_apps = {
99 .name = "global",
100 .global = 1,
101
102 .root_handle = -1,
103 .allowed = 1,
104
105 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
106 .socket = -1,
107
108 .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
109 };
110
111 /* TODO: allow global_apps_sock_path override */
112
113 struct sock_info local_apps = {
114 .name = "local",
115 .global = 0,
116 .root_handle = -1,
117 .allowed = 0, /* Check setuid bit first */
118
119 .socket = -1,
120 };
121
122 static int wait_poll_fallback;
123
124 extern void ltt_ring_buffer_client_overwrite_init(void);
125 extern void ltt_ring_buffer_client_discard_init(void);
126 extern void ltt_ring_buffer_metadata_client_init(void);
127 extern void ltt_ring_buffer_client_overwrite_exit(void);
128 extern void ltt_ring_buffer_client_discard_exit(void);
129 extern void ltt_ring_buffer_metadata_client_exit(void);
130
131 static
132 int setup_local_apps(void)
133 {
134 const char *home_dir;
135 uid_t uid;
136
137 uid = getuid();
138 /*
139 * Disallow per-user tracing for setuid binaries.
140 */
141 if (uid != geteuid()) {
142 local_apps.allowed = 0;
143 return 0;
144 } else {
145 local_apps.allowed = 1;
146 }
147 home_dir = (const char *) getenv("HOME");
148 if (!home_dir)
149 return -ENOENT;
150 snprintf(local_apps.sock_path, PATH_MAX,
151 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
152 snprintf(local_apps.wait_shm_path, PATH_MAX,
153 DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid);
154 return 0;
155 }
156
157 static
158 int register_app_to_sessiond(int socket)
159 {
160 ssize_t ret;
161 int prctl_ret;
162 struct {
163 uint32_t major;
164 uint32_t minor;
165 pid_t pid;
166 pid_t ppid;
167 uid_t uid;
168 gid_t gid;
169 char name[16]; /* process name */
170 } reg_msg;
171
172 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
173 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
174 reg_msg.pid = getpid();
175 reg_msg.ppid = getppid();
176 reg_msg.uid = getuid();
177 reg_msg.gid = getgid();
178 prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0);
179 if (prctl_ret) {
180 ERR("Error executing prctl");
181 return -errno;
182 }
183
184 ret = ustcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
185 if (ret >= 0 && ret != sizeof(reg_msg))
186 return -EIO;
187 return ret;
188 }
189
190 static
191 int send_reply(int sock, struct ustcomm_ust_reply *lur)
192 {
193 ssize_t len;
194
195 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
196 switch (len) {
197 case sizeof(*lur):
198 DBG("message successfully sent");
199 return 0;
200 case -1:
201 if (errno == ECONNRESET) {
202 printf("remote end closed connection\n");
203 return 0;
204 }
205 return -1;
206 default:
207 printf("incorrect message size: %zd\n", len);
208 return -1;
209 }
210 }
211
212 static
213 int handle_register_done(struct sock_info *sock_info)
214 {
215 int ret;
216
217 if (sock_info->constructor_sem_posted)
218 return 0;
219 sock_info->constructor_sem_posted = 1;
220 if (uatomic_read(&sem_count) <= 0) {
221 return 0;
222 }
223 ret = uatomic_add_return(&sem_count, -1);
224 if (ret == 0) {
225 ret = sem_post(&constructor_wait);
226 assert(!ret);
227 }
228 return 0;
229 }
230
231 static
232 int handle_message(struct sock_info *sock_info,
233 int sock, struct ustcomm_ust_msg *lum)
234 {
235 int ret = 0;
236 const struct lttng_ust_objd_ops *ops;
237 struct ustcomm_ust_reply lur;
238 int shm_fd, wait_fd;
239
240 ust_lock();
241
242 memset(&lur, 0, sizeof(lur));
243
244 if (lttng_ust_comm_should_quit) {
245 ret = -EPERM;
246 goto end;
247 }
248
249 ops = objd_ops(lum->handle);
250 if (!ops) {
251 ret = -ENOENT;
252 goto end;
253 }
254
255 switch (lum->cmd) {
256 case LTTNG_UST_REGISTER_DONE:
257 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
258 ret = handle_register_done(sock_info);
259 else
260 ret = -EINVAL;
261 break;
262 case LTTNG_UST_RELEASE:
263 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
264 ret = -EPERM;
265 else
266 ret = lttng_ust_objd_unref(lum->handle);
267 break;
268 default:
269 if (ops->cmd)
270 ret = ops->cmd(lum->handle, lum->cmd,
271 (unsigned long) &lum->u);
272 else
273 ret = -ENOSYS;
274 break;
275 }
276
277 end:
278 lur.handle = lum->handle;
279 lur.cmd = lum->cmd;
280 lur.ret_val = ret;
281 if (ret >= 0) {
282 lur.ret_code = USTCOMM_OK;
283 } else {
284 //lur.ret_code = USTCOMM_SESSION_FAIL;
285 lur.ret_code = ret;
286 }
287 switch (lum->cmd) {
288 case LTTNG_UST_STREAM:
289 /*
290 * Special-case reply to send stream info.
291 * Use lum.u output.
292 */
293 lur.u.stream.memory_map_size = lum->u.stream.memory_map_size;
294 shm_fd = lum->u.stream.shm_fd;
295 wait_fd = lum->u.stream.wait_fd;
296 break;
297 case LTTNG_UST_METADATA:
298 case LTTNG_UST_CHANNEL:
299 lur.u.channel.memory_map_size = lum->u.channel.memory_map_size;
300 shm_fd = lum->u.channel.shm_fd;
301 wait_fd = lum->u.channel.wait_fd;
302 break;
303 case LTTNG_UST_TRACER_VERSION:
304 lur.u.version = lum->u.version;
305 break;
306 case LTTNG_UST_TRACEPOINT_LIST_GET:
307 memcpy(lur.u.tracepoint_list_entry,
308 lum->u.tracepoint_list_entry,
309 LTTNG_UST_SYM_NAME_LEN);
310 break;
311 }
312 ret = send_reply(sock, &lur);
313 if (ret < 0) {
314 perror("error sending reply");
315 goto error;
316 }
317
318 if ((lum->cmd == LTTNG_UST_STREAM
319 || lum->cmd == LTTNG_UST_CHANNEL
320 || lum->cmd == LTTNG_UST_METADATA)
321 && lur.ret_code == USTCOMM_OK) {
322 /* we also need to send the file descriptors. */
323 ret = ustcomm_send_fds_unix_sock(sock,
324 &shm_fd, &shm_fd,
325 1, sizeof(int));
326 if (ret < 0) {
327 perror("send shm_fd");
328 goto error;
329 }
330 ret = ustcomm_send_fds_unix_sock(sock,
331 &wait_fd, &wait_fd,
332 1, sizeof(int));
333 if (ret < 0) {
334 perror("send wait_fd");
335 goto error;
336 }
337 }
338 error:
339 ust_unlock();
340 return ret;
341 }
342
343 static
344 void cleanup_sock_info(struct sock_info *sock_info)
345 {
346 int ret;
347
348 if (sock_info->socket != -1) {
349 ret = close(sock_info->socket);
350 if (ret) {
351 ERR("Error closing apps socket");
352 }
353 sock_info->socket = -1;
354 }
355 if (sock_info->root_handle != -1) {
356 ret = lttng_ust_objd_unref(sock_info->root_handle);
357 if (ret) {
358 ERR("Error unref root handle");
359 }
360 sock_info->root_handle = -1;
361 }
362 sock_info->constructor_sem_posted = 0;
363 if (sock_info->wait_shm_mmap) {
364 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
365 if (ret) {
366 ERR("Error unmapping wait shm");
367 }
368 sock_info->wait_shm_mmap = NULL;
369 }
370 }
371
372 /*
373 * Using fork to set umask in the child process (not multi-thread safe).
374 * We deal with the shm_open vs ftruncate race (happening when the
375 * sessiond owns the shm and does not let everybody modify it, to ensure
376 * safety against shm_unlink) by simply letting the mmap fail and
377 * retrying after a few seconds.
378 * For global shm, everybody has rw access to it until the sessiond
379 * starts.
380 */
381 static
382 int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
383 {
384 int wait_shm_fd, ret;
385 pid_t pid;
386
387 /*
388 * Try to open read-only.
389 */
390 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
391 if (wait_shm_fd >= 0) {
392 goto end;
393 } else if (wait_shm_fd < 0 && errno != ENOENT) {
394 /*
395 * Real-only open did not work, and it's not because the
396 * entry was not present. It's a failure that prohibits
397 * using shm.
398 */
399 ERR("Error opening shm %s", sock_info->wait_shm_path);
400 goto end;
401 }
402 /*
403 * If the open failed because the file did not exist, try
404 * creating it ourself.
405 */
406 pid = fork();
407 if (pid > 0) {
408 int status;
409
410 /*
411 * Parent: wait for child to return, in which case the
412 * shared memory map will have been created.
413 */
414 pid = wait(&status);
415 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
416 wait_shm_fd = -1;
417 goto end;
418 }
419 /*
420 * Try to open read-only again after creation.
421 */
422 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
423 if (wait_shm_fd < 0) {
424 /*
425 * Real-only open did not work. It's a failure
426 * that prohibits using shm.
427 */
428 ERR("Error opening shm %s", sock_info->wait_shm_path);
429 goto end;
430 }
431 goto end;
432 } else if (pid == 0) {
433 int create_mode;
434
435 /* Child */
436 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
437 if (sock_info->global)
438 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
439 /*
440 * We're alone in a child process, so we can modify the
441 * process-wide umask.
442 */
443 umask(~create_mode);
444 /*
445 * Try creating shm (or get rw access).
446 * We don't do an exclusive open, because we allow other
447 * processes to create+ftruncate it concurrently.
448 */
449 wait_shm_fd = shm_open(sock_info->wait_shm_path,
450 O_RDWR | O_CREAT, create_mode);
451 if (wait_shm_fd >= 0) {
452 ret = ftruncate(wait_shm_fd, mmap_size);
453 if (ret) {
454 PERROR("ftruncate");
455 exit(EXIT_FAILURE);
456 }
457 exit(EXIT_SUCCESS);
458 }
459 /*
460 * For local shm, we need to have rw access to accept
461 * opening it: this means the local sessiond will be
462 * able to wake us up. For global shm, we open it even
463 * if rw access is not granted, because the root.root
464 * sessiond will be able to override all rights and wake
465 * us up.
466 */
467 if (!sock_info->global && errno != EACCES) {
468 ERR("Error opening shm %s", sock_info->wait_shm_path);
469 exit(EXIT_FAILURE);
470 }
471 /*
472 * The shm exists, but we cannot open it RW. Report
473 * success.
474 */
475 exit(EXIT_SUCCESS);
476 } else {
477 return -1;
478 }
479 end:
480 if (wait_shm_fd >= 0 && !sock_info->global) {
481 struct stat statbuf;
482
483 /*
484 * Ensure that our user is the owner of the shm file for
485 * local shm. If we do not own the file, it means our
486 * sessiond will not have access to wake us up (there is
487 * probably a rogue process trying to fake our
488 * sessiond). Fallback to polling method in this case.
489 */
490 ret = fstat(wait_shm_fd, &statbuf);
491 if (ret) {
492 PERROR("fstat");
493 goto error_close;
494 }
495 if (statbuf.st_uid != getuid())
496 goto error_close;
497 }
498 return wait_shm_fd;
499
500 error_close:
501 ret = close(wait_shm_fd);
502 if (ret) {
503 PERROR("Error closing fd");
504 }
505 return -1;
506 }
507
508 static
509 char *get_map_shm(struct sock_info *sock_info)
510 {
511 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
512 int wait_shm_fd, ret;
513 char *wait_shm_mmap;
514
515 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
516 if (wait_shm_fd < 0) {
517 goto error;
518 }
519 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
520 MAP_SHARED, wait_shm_fd, 0);
521 /* close shm fd immediately after taking the mmap reference */
522 ret = close(wait_shm_fd);
523 if (ret) {
524 PERROR("Error closing fd");
525 }
526 if (wait_shm_mmap == MAP_FAILED) {
527 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
528 goto error;
529 }
530 return wait_shm_mmap;
531
532 error:
533 return NULL;
534 }
535
536 static
537 void wait_for_sessiond(struct sock_info *sock_info)
538 {
539 int ret;
540
541 ust_lock();
542 if (lttng_ust_comm_should_quit) {
543 goto quit;
544 }
545 if (wait_poll_fallback) {
546 goto error;
547 }
548 if (!sock_info->wait_shm_mmap) {
549 sock_info->wait_shm_mmap = get_map_shm(sock_info);
550 if (!sock_info->wait_shm_mmap)
551 goto error;
552 }
553 ust_unlock();
554
555 DBG("Waiting for %s apps sessiond", sock_info->name);
556 /* Wait for futex wakeup */
557 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
558 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
559 FUTEX_WAIT, 0, NULL, NULL, 0);
560 if (ret < 0) {
561 if (errno == EFAULT) {
562 wait_poll_fallback = 1;
563 WARN(
564 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
565 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
566 "Please upgrade your kernel "
567 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
568 "mainline). LTTng-UST will use polling mode fallback.");
569 }
570 PERROR("futex");
571 }
572 }
573 return;
574
575 quit:
576 ust_unlock();
577 return;
578
579 error:
580 ust_unlock();
581 return;
582 }
583
584 /*
585 * This thread does not allocate any resource, except within
586 * handle_message, within mutex protection. This mutex protects against
587 * fork and exit.
588 * The other moment it allocates resources is at socket connexion, which
589 * is also protected by the mutex.
590 */
591 static
592 void *ust_listener_thread(void *arg)
593 {
594 struct sock_info *sock_info = arg;
595 int sock, ret, prev_connect_failed = 0, has_waited = 0;
596
597 /* Restart trying to connect to the session daemon */
598 restart:
599 if (prev_connect_failed) {
600 /* Wait for sessiond availability with pipe */
601 wait_for_sessiond(sock_info);
602 if (has_waited) {
603 has_waited = 0;
604 /*
605 * Sleep for 5 seconds before retrying after a
606 * sequence of failure / wait / failure. This
607 * deals with a killed or broken session daemon.
608 */
609 sleep(5);
610 }
611 has_waited = 1;
612 prev_connect_failed = 0;
613 }
614 ust_lock();
615
616 if (lttng_ust_comm_should_quit) {
617 ust_unlock();
618 goto quit;
619 }
620
621 if (sock_info->socket != -1) {
622 ret = close(sock_info->socket);
623 if (ret) {
624 ERR("Error closing %s apps socket", sock_info->name);
625 }
626 sock_info->socket = -1;
627 }
628
629 /* Register */
630 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
631 if (ret < 0) {
632 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
633 prev_connect_failed = 1;
634 /*
635 * If we cannot find the sessiond daemon, don't delay
636 * constructor execution.
637 */
638 ret = handle_register_done(sock_info);
639 assert(!ret);
640 ust_unlock();
641 goto restart;
642 }
643
644 sock_info->socket = sock = ret;
645
646 /*
647 * Create only one root handle per listener thread for the whole
648 * process lifetime.
649 */
650 if (sock_info->root_handle == -1) {
651 ret = lttng_abi_create_root_handle();
652 if (ret < 0) {
653 ERR("Error creating root handle");
654 ust_unlock();
655 goto quit;
656 }
657 sock_info->root_handle = ret;
658 }
659
660 ret = register_app_to_sessiond(sock);
661 if (ret < 0) {
662 ERR("Error registering to %s apps socket", sock_info->name);
663 prev_connect_failed = 1;
664 /*
665 * If we cannot register to the sessiond daemon, don't
666 * delay constructor execution.
667 */
668 ret = handle_register_done(sock_info);
669 assert(!ret);
670 ust_unlock();
671 goto restart;
672 }
673 ust_unlock();
674
675 for (;;) {
676 ssize_t len;
677 struct ustcomm_ust_msg lum;
678
679 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
680 switch (len) {
681 case 0: /* orderly shutdown */
682 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
683 goto end;
684 case sizeof(lum):
685 DBG("message received\n");
686 ret = handle_message(sock_info, sock, &lum);
687 if (ret < 0) {
688 ERR("Error handling message for %s socket", sock_info->name);
689 }
690 continue;
691 case -1:
692 if (errno == ECONNRESET) {
693 ERR("%s remote end closed connection\n", sock_info->name);
694 goto end;
695 }
696 goto end;
697 default:
698 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
699 continue;
700 }
701
702 }
703 end:
704 goto restart; /* try to reconnect */
705 quit:
706 return NULL;
707 }
708
709 /*
710 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
711 */
712 static
713 int get_timeout(struct timespec *constructor_timeout)
714 {
715 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
716 char *str_delay;
717 int ret;
718
719 str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT");
720 if (str_delay) {
721 constructor_delay_ms = strtol(str_delay, NULL, 10);
722 }
723
724 switch (constructor_delay_ms) {
725 case -1:/* fall-through */
726 case 0:
727 return constructor_delay_ms;
728 default:
729 break;
730 }
731
732 /*
733 * If we are unable to find the current time, don't wait.
734 */
735 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
736 if (ret) {
737 return -1;
738 }
739 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
740 constructor_timeout->tv_nsec +=
741 (constructor_delay_ms % 1000UL) * 1000000UL;
742 if (constructor_timeout->tv_nsec >= 1000000000UL) {
743 constructor_timeout->tv_sec++;
744 constructor_timeout->tv_nsec -= 1000000000UL;
745 }
746 return 1;
747 }
748
749 /*
750 * sessiond monitoring thread: monitor presence of global and per-user
751 * sessiond by polling the application common named pipe.
752 */
753 /* TODO */
754
755 void __attribute__((constructor)) lttng_ust_init(void)
756 {
757 struct timespec constructor_timeout;
758 int timeout_mode;
759 int ret;
760
761 if (uatomic_xchg(&initialized, 1) == 1)
762 return;
763
764 /*
765 * We want precise control over the order in which we construct
766 * our sub-libraries vs starting to receive commands from
767 * sessiond (otherwise leading to errors when trying to create
768 * sessiond before the init functions are completed).
769 */
770 init_usterr();
771 init_tracepoint();
772 ltt_ring_buffer_metadata_client_init();
773 ltt_ring_buffer_client_overwrite_init();
774 ltt_ring_buffer_client_discard_init();
775
776 timeout_mode = get_timeout(&constructor_timeout);
777
778 ret = sem_init(&constructor_wait, 0, 0);
779 assert(!ret);
780
781 ret = setup_local_apps();
782 if (ret) {
783 ERR("Error setting up to local apps");
784 }
785 ret = pthread_create(&local_apps.ust_listener, NULL,
786 ust_listener_thread, &local_apps);
787
788 if (local_apps.allowed) {
789 ret = pthread_create(&global_apps.ust_listener, NULL,
790 ust_listener_thread, &global_apps);
791 } else {
792 handle_register_done(&local_apps);
793 }
794
795 switch (timeout_mode) {
796 case 1: /* timeout wait */
797 do {
798 ret = sem_timedwait(&constructor_wait,
799 &constructor_timeout);
800 } while (ret < 0 && errno == EINTR);
801 if (ret < 0 && errno == ETIMEDOUT) {
802 ERR("Timed out waiting for ltt-sessiond");
803 } else {
804 assert(!ret);
805 }
806 break;
807 case -1:/* wait forever */
808 do {
809 ret = sem_wait(&constructor_wait);
810 } while (ret < 0 && errno == EINTR);
811 assert(!ret);
812 break;
813 case 0: /* no timeout */
814 break;
815 }
816 }
817
818 static
819 void lttng_ust_cleanup(int exiting)
820 {
821 cleanup_sock_info(&global_apps);
822 if (local_apps.allowed) {
823 cleanup_sock_info(&local_apps);
824 }
825 lttng_ust_abi_exit();
826 lttng_ust_events_exit();
827 ltt_ring_buffer_client_discard_exit();
828 ltt_ring_buffer_client_overwrite_exit();
829 ltt_ring_buffer_metadata_client_exit();
830 exit_tracepoint();
831 if (!exiting) {
832 /* Reinitialize values for fork */
833 sem_count = 2;
834 lttng_ust_comm_should_quit = 0;
835 initialized = 0;
836 }
837 }
838
839 void __attribute__((destructor)) lttng_ust_exit(void)
840 {
841 int ret;
842
843 /*
844 * Using pthread_cancel here because:
845 * A) we don't want to hang application teardown.
846 * B) the thread is not allocating any resource.
847 */
848
849 /*
850 * Require the communication thread to quit. Synchronize with
851 * mutexes to ensure it is not in a mutex critical section when
852 * pthread_cancel is later called.
853 */
854 ust_lock();
855 lttng_ust_comm_should_quit = 1;
856 ust_unlock();
857
858 ret = pthread_cancel(global_apps.ust_listener);
859 if (ret) {
860 ERR("Error cancelling global ust listener thread");
861 }
862 if (local_apps.allowed) {
863 ret = pthread_cancel(local_apps.ust_listener);
864 if (ret) {
865 ERR("Error cancelling local ust listener thread");
866 }
867 }
868 lttng_ust_cleanup(1);
869 }
870
871 /*
872 * We exclude the worker threads across fork and clone (except
873 * CLONE_VM), because these system calls only keep the forking thread
874 * running in the child. Therefore, we don't want to call fork or clone
875 * in the middle of an tracepoint or ust tracing state modification.
876 * Holding this mutex protects these structures across fork and clone.
877 */
878 void ust_before_fork(sigset_t *save_sigset)
879 {
880 /*
881 * Disable signals. This is to avoid that the child intervenes
882 * before it is properly setup for tracing. It is safer to
883 * disable all signals, because then we know we are not breaking
884 * anything by restoring the original mask.
885 */
886 sigset_t all_sigs;
887 int ret;
888
889 /* Disable signals */
890 sigfillset(&all_sigs);
891 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
892 if (ret == -1) {
893 PERROR("sigprocmask");
894 }
895 ust_lock();
896 rcu_bp_before_fork();
897 }
898
899 static void ust_after_fork_common(sigset_t *restore_sigset)
900 {
901 int ret;
902
903 DBG("process %d", getpid());
904 ust_unlock();
905 /* Restore signals */
906 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
907 if (ret == -1) {
908 PERROR("sigprocmask");
909 }
910 }
911
912 void ust_after_fork_parent(sigset_t *restore_sigset)
913 {
914 DBG("process %d", getpid());
915 rcu_bp_after_fork_parent();
916 /* Release mutexes and reenable signals */
917 ust_after_fork_common(restore_sigset);
918 }
919
920 /*
921 * After fork, in the child, we need to cleanup all the leftover state,
922 * except the worker thread which already magically disappeared thanks
923 * to the weird Linux fork semantics. After tyding up, we call
924 * lttng_ust_init() again to start over as a new PID.
925 *
926 * This is meant for forks() that have tracing in the child between the
927 * fork and following exec call (if there is any).
928 */
929 void ust_after_fork_child(sigset_t *restore_sigset)
930 {
931 DBG("process %d", getpid());
932 /* Release urcu mutexes */
933 rcu_bp_after_fork_child();
934 lttng_ust_cleanup(0);
935 lttng_context_vtid_reset();
936 /* Release mutexes and reenable signals */
937 ust_after_fork_common(restore_sigset);
938 lttng_ust_init();
939 }
This page took 0.065142 seconds and 5 git commands to generate.