Update lib ring buffer for external consumer
[ust.git] / libust / 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-comm.h>
42 #include <ust/usterr-signal-safe.h>
43 #include <ust/lttng-ust-abi.h>
44 #include <ust/tracepoint.h>
45 #include <ust/tracepoint-internal.h>
46 #include <ust/ust.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 = lttcomm_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 lttcomm_ust_reply *lur)
192 {
193 ssize_t len;
194
195 len = lttcomm_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 lttcomm_ust_msg *lum)
234 {
235 int ret = 0;
236 const struct objd_ops *ops;
237 struct lttcomm_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 = 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 = LTTCOMM_OK;
283 } else {
284 //lur.ret_code = LTTCOMM_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 }
304 ret = send_reply(sock, &lur);
305 if (ret < 0) {
306 perror("error sending reply");
307 goto error;
308 }
309
310 if ((lum->cmd == LTTNG_UST_STREAM
311 || lum->cmd == LTTNG_UST_CHANNEL
312 || lum->cmd == LTTNG_UST_METADATA)
313 && lur.ret_code == LTTCOMM_OK) {
314 /* we also need to send the file descriptors. */
315 ret = lttcomm_send_fds_unix_sock(sock,
316 &shm_fd, &shm_fd,
317 1, sizeof(int));
318 if (ret < 0) {
319 perror("send shm_fd");
320 goto error;
321 }
322 ret = lttcomm_send_fds_unix_sock(sock,
323 &wait_fd, &wait_fd,
324 1, sizeof(int));
325 if (ret < 0) {
326 perror("send wait_fd");
327 goto error;
328 }
329 }
330 error:
331 ust_unlock();
332 return ret;
333 }
334
335 static
336 void cleanup_sock_info(struct sock_info *sock_info)
337 {
338 int ret;
339
340 if (sock_info->socket != -1) {
341 ret = close(sock_info->socket);
342 if (ret) {
343 ERR("Error closing apps socket");
344 }
345 sock_info->socket = -1;
346 }
347 if (sock_info->root_handle != -1) {
348 ret = objd_unref(sock_info->root_handle);
349 if (ret) {
350 ERR("Error unref root handle");
351 }
352 sock_info->root_handle = -1;
353 }
354 sock_info->constructor_sem_posted = 0;
355 if (sock_info->wait_shm_mmap) {
356 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
357 if (ret) {
358 ERR("Error unmapping wait shm");
359 }
360 sock_info->wait_shm_mmap = NULL;
361 }
362 }
363
364 /*
365 * Using fork to set umask in the child process (not multi-thread safe).
366 * We deal with the shm_open vs ftruncate race (happening when the
367 * sessiond owns the shm and does not let everybody modify it, to ensure
368 * safety against shm_unlink) by simply letting the mmap fail and
369 * retrying after a few seconds.
370 * For global shm, everybody has rw access to it until the sessiond
371 * starts.
372 */
373 static
374 int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
375 {
376 int wait_shm_fd, ret;
377 pid_t pid;
378
379 /*
380 * Try to open read-only.
381 */
382 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
383 if (wait_shm_fd >= 0) {
384 goto end;
385 } else if (wait_shm_fd < 0 && errno != ENOENT) {
386 /*
387 * Real-only open did not work, and it's not because the
388 * entry was not present. It's a failure that prohibits
389 * using shm.
390 */
391 ERR("Error opening shm %s", sock_info->wait_shm_path);
392 goto end;
393 }
394 /*
395 * If the open failed because the file did not exist, try
396 * creating it ourself.
397 */
398 pid = fork();
399 if (pid > 0) {
400 int status;
401
402 /*
403 * Parent: wait for child to return, in which case the
404 * shared memory map will have been created.
405 */
406 pid = wait(&status);
407 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
408 wait_shm_fd = -1;
409 goto end;
410 }
411 /*
412 * Try to open read-only again after creation.
413 */
414 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
415 if (wait_shm_fd < 0) {
416 /*
417 * Real-only open did not work. It's a failure
418 * that prohibits using shm.
419 */
420 ERR("Error opening shm %s", sock_info->wait_shm_path);
421 goto end;
422 }
423 goto end;
424 } else if (pid == 0) {
425 int create_mode;
426
427 /* Child */
428 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
429 if (sock_info->global)
430 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
431 /*
432 * We're alone in a child process, so we can modify the
433 * process-wide umask.
434 */
435 umask(~create_mode);
436 /*
437 * Try creating shm (or get rw access).
438 * We don't do an exclusive open, because we allow other
439 * processes to create+ftruncate it concurrently.
440 */
441 wait_shm_fd = shm_open(sock_info->wait_shm_path,
442 O_RDWR | O_CREAT, create_mode);
443 if (wait_shm_fd >= 0) {
444 ret = ftruncate(wait_shm_fd, mmap_size);
445 if (ret) {
446 PERROR("ftruncate");
447 exit(EXIT_FAILURE);
448 }
449 exit(EXIT_SUCCESS);
450 }
451 /*
452 * For local shm, we need to have rw access to accept
453 * opening it: this means the local sessiond will be
454 * able to wake us up. For global shm, we open it even
455 * if rw access is not granted, because the root.root
456 * sessiond will be able to override all rights and wake
457 * us up.
458 */
459 if (!sock_info->global && errno != EACCES) {
460 ERR("Error opening shm %s", sock_info->wait_shm_path);
461 exit(EXIT_FAILURE);
462 }
463 /*
464 * The shm exists, but we cannot open it RW. Report
465 * success.
466 */
467 exit(EXIT_SUCCESS);
468 } else {
469 return -1;
470 }
471 end:
472 if (wait_shm_fd >= 0 && !sock_info->global) {
473 struct stat statbuf;
474
475 /*
476 * Ensure that our user is the owner of the shm file for
477 * local shm. If we do not own the file, it means our
478 * sessiond will not have access to wake us up (there is
479 * probably a rogue process trying to fake our
480 * sessiond). Fallback to polling method in this case.
481 */
482 ret = fstat(wait_shm_fd, &statbuf);
483 if (ret) {
484 PERROR("fstat");
485 goto error_close;
486 }
487 if (statbuf.st_uid != getuid())
488 goto error_close;
489 }
490 return wait_shm_fd;
491
492 error_close:
493 ret = close(wait_shm_fd);
494 if (ret) {
495 PERROR("Error closing fd");
496 }
497 return -1;
498 }
499
500 static
501 char *get_map_shm(struct sock_info *sock_info)
502 {
503 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
504 int wait_shm_fd, ret;
505 char *wait_shm_mmap;
506
507 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
508 if (wait_shm_fd < 0) {
509 goto error;
510 }
511 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
512 MAP_SHARED, wait_shm_fd, 0);
513 /* close shm fd immediately after taking the mmap reference */
514 ret = close(wait_shm_fd);
515 if (ret) {
516 PERROR("Error closing fd");
517 }
518 if (wait_shm_mmap == MAP_FAILED) {
519 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
520 goto error;
521 }
522 return wait_shm_mmap;
523
524 error:
525 return NULL;
526 }
527
528 static
529 void wait_for_sessiond(struct sock_info *sock_info)
530 {
531 int ret;
532
533 ust_lock();
534 if (lttng_ust_comm_should_quit) {
535 goto quit;
536 }
537 if (wait_poll_fallback) {
538 goto error;
539 }
540 if (!sock_info->wait_shm_mmap) {
541 sock_info->wait_shm_mmap = get_map_shm(sock_info);
542 if (!sock_info->wait_shm_mmap)
543 goto error;
544 }
545 ust_unlock();
546
547 DBG("Waiting for %s apps sessiond", sock_info->name);
548 /* Wait for futex wakeup */
549 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
550 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
551 FUTEX_WAIT, 0, NULL, NULL, 0);
552 if (ret < 0) {
553 if (errno == EFAULT) {
554 wait_poll_fallback = 1;
555 ERR(
556 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
557 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
558 "Please upgrade your kernel "
559 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
560 "mainline). LTTng-UST will use polling mode fallback.");
561 }
562 PERROR("futex");
563 }
564 }
565 return;
566
567 quit:
568 ust_unlock();
569 return;
570
571 error:
572 ust_unlock();
573 return;
574 }
575
576 /*
577 * This thread does not allocate any resource, except within
578 * handle_message, within mutex protection. This mutex protects against
579 * fork and exit.
580 * The other moment it allocates resources is at socket connexion, which
581 * is also protected by the mutex.
582 */
583 static
584 void *ust_listener_thread(void *arg)
585 {
586 struct sock_info *sock_info = arg;
587 int sock, ret, prev_connect_failed = 0, has_waited = 0;
588
589 /* Restart trying to connect to the session daemon */
590 restart:
591 if (prev_connect_failed) {
592 /* Wait for sessiond availability with pipe */
593 wait_for_sessiond(sock_info);
594 if (has_waited) {
595 has_waited = 0;
596 /*
597 * Sleep for 5 seconds before retrying after a
598 * sequence of failure / wait / failure. This
599 * deals with a killed or broken session daemon.
600 */
601 sleep(5);
602 }
603 has_waited = 1;
604 prev_connect_failed = 0;
605 }
606 ust_lock();
607
608 if (lttng_ust_comm_should_quit) {
609 ust_unlock();
610 goto quit;
611 }
612
613 if (sock_info->socket != -1) {
614 ret = close(sock_info->socket);
615 if (ret) {
616 ERR("Error closing %s apps socket", sock_info->name);
617 }
618 sock_info->socket = -1;
619 }
620
621 /* Register */
622 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
623 if (ret < 0) {
624 ERR("Error connecting to %s apps socket", sock_info->name);
625 prev_connect_failed = 1;
626 /*
627 * If we cannot find the sessiond daemon, don't delay
628 * constructor execution.
629 */
630 ret = handle_register_done(sock_info);
631 assert(!ret);
632 ust_unlock();
633 goto restart;
634 }
635
636 sock_info->socket = sock = ret;
637
638 /*
639 * Create only one root handle per listener thread for the whole
640 * process lifetime.
641 */
642 if (sock_info->root_handle == -1) {
643 ret = lttng_abi_create_root_handle();
644 if (ret) {
645 ERR("Error creating root handle");
646 ust_unlock();
647 goto quit;
648 }
649 sock_info->root_handle = ret;
650 }
651
652 ret = register_app_to_sessiond(sock);
653 if (ret < 0) {
654 ERR("Error registering to %s apps socket", sock_info->name);
655 prev_connect_failed = 1;
656 /*
657 * If we cannot register to the sessiond daemon, don't
658 * delay constructor execution.
659 */
660 ret = handle_register_done(sock_info);
661 assert(!ret);
662 ust_unlock();
663 goto restart;
664 }
665 ust_unlock();
666
667 for (;;) {
668 ssize_t len;
669 struct lttcomm_ust_msg lum;
670
671 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
672 switch (len) {
673 case 0: /* orderly shutdown */
674 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
675 goto end;
676 case sizeof(lum):
677 DBG("message received\n");
678 ret = handle_message(sock_info, sock, &lum);
679 if (ret < 0) {
680 ERR("Error handling message for %s socket", sock_info->name);
681 }
682 continue;
683 case -1:
684 if (errno == ECONNRESET) {
685 ERR("%s remote end closed connection\n", sock_info->name);
686 goto end;
687 }
688 goto end;
689 default:
690 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
691 continue;
692 }
693
694 }
695 end:
696 goto restart; /* try to reconnect */
697 quit:
698 return NULL;
699 }
700
701 /*
702 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
703 */
704 static
705 int get_timeout(struct timespec *constructor_timeout)
706 {
707 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
708 char *str_delay;
709 int ret;
710
711 str_delay = getenv("UST_REGISTER_TIMEOUT");
712 if (str_delay) {
713 constructor_delay_ms = strtol(str_delay, NULL, 10);
714 }
715
716 switch (constructor_delay_ms) {
717 case -1:/* fall-through */
718 case 0:
719 return constructor_delay_ms;
720 default:
721 break;
722 }
723
724 /*
725 * If we are unable to find the current time, don't wait.
726 */
727 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
728 if (ret) {
729 return -1;
730 }
731 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
732 constructor_timeout->tv_nsec +=
733 (constructor_delay_ms % 1000UL) * 1000000UL;
734 if (constructor_timeout->tv_nsec >= 1000000000UL) {
735 constructor_timeout->tv_sec++;
736 constructor_timeout->tv_nsec -= 1000000000UL;
737 }
738 return 1;
739 }
740
741 /*
742 * sessiond monitoring thread: monitor presence of global and per-user
743 * sessiond by polling the application common named pipe.
744 */
745 /* TODO */
746
747 void __attribute__((constructor)) lttng_ust_init(void)
748 {
749 struct timespec constructor_timeout;
750 int timeout_mode;
751 int ret;
752
753 if (uatomic_xchg(&initialized, 1) == 1)
754 return;
755
756 /*
757 * We want precise control over the order in which we construct
758 * our sub-libraries vs starting to receive commands from
759 * sessiond (otherwise leading to errors when trying to create
760 * sessiond before the init functions are completed).
761 */
762 init_usterr();
763 init_tracepoint();
764 ltt_ring_buffer_metadata_client_init();
765 ltt_ring_buffer_client_overwrite_init();
766 ltt_ring_buffer_client_discard_init();
767
768 timeout_mode = get_timeout(&constructor_timeout);
769
770 ret = sem_init(&constructor_wait, 0, 0);
771 assert(!ret);
772
773 ret = setup_local_apps();
774 if (ret) {
775 ERR("Error setting up to local apps");
776 }
777 ret = pthread_create(&local_apps.ust_listener, NULL,
778 ust_listener_thread, &local_apps);
779
780 if (local_apps.allowed) {
781 ret = pthread_create(&global_apps.ust_listener, NULL,
782 ust_listener_thread, &global_apps);
783 } else {
784 handle_register_done(&local_apps);
785 }
786
787 switch (timeout_mode) {
788 case 1: /* timeout wait */
789 do {
790 ret = sem_timedwait(&constructor_wait,
791 &constructor_timeout);
792 } while (ret < 0 && errno == EINTR);
793 if (ret < 0 && errno == ETIMEDOUT) {
794 ERR("Timed out waiting for ltt-sessiond");
795 } else {
796 assert(!ret);
797 }
798 break;
799 case -1:/* wait forever */
800 do {
801 ret = sem_wait(&constructor_wait);
802 } while (ret < 0 && errno == EINTR);
803 assert(!ret);
804 break;
805 case 0: /* no timeout */
806 break;
807 }
808 }
809
810 static
811 void lttng_ust_cleanup(int exiting)
812 {
813 cleanup_sock_info(&global_apps);
814 if (local_apps.allowed) {
815 cleanup_sock_info(&local_apps);
816 }
817 lttng_ust_abi_exit();
818 ltt_events_exit();
819 ltt_ring_buffer_client_discard_exit();
820 ltt_ring_buffer_client_overwrite_exit();
821 ltt_ring_buffer_metadata_client_exit();
822 exit_tracepoint();
823 if (!exiting) {
824 /* Reinitialize values for fork */
825 sem_count = 2;
826 lttng_ust_comm_should_quit = 0;
827 initialized = 0;
828 }
829 }
830
831 void __attribute__((destructor)) lttng_ust_exit(void)
832 {
833 int ret;
834
835 /*
836 * Using pthread_cancel here because:
837 * A) we don't want to hang application teardown.
838 * B) the thread is not allocating any resource.
839 */
840
841 /*
842 * Require the communication thread to quit. Synchronize with
843 * mutexes to ensure it is not in a mutex critical section when
844 * pthread_cancel is later called.
845 */
846 ust_lock();
847 lttng_ust_comm_should_quit = 1;
848 ust_unlock();
849
850 ret = pthread_cancel(global_apps.ust_listener);
851 if (ret) {
852 ERR("Error cancelling global ust listener thread");
853 }
854 if (local_apps.allowed) {
855 ret = pthread_cancel(local_apps.ust_listener);
856 if (ret) {
857 ERR("Error cancelling local ust listener thread");
858 }
859 }
860 lttng_ust_cleanup(1);
861 }
862
863 /*
864 * We exclude the worker threads across fork and clone (except
865 * CLONE_VM), because these system calls only keep the forking thread
866 * running in the child. Therefore, we don't want to call fork or clone
867 * in the middle of an tracepoint or ust tracing state modification.
868 * Holding this mutex protects these structures across fork and clone.
869 */
870 void ust_before_fork(ust_fork_info_t *fork_info)
871 {
872 /*
873 * Disable signals. This is to avoid that the child intervenes
874 * before it is properly setup for tracing. It is safer to
875 * disable all signals, because then we know we are not breaking
876 * anything by restoring the original mask.
877 */
878 sigset_t all_sigs;
879 int ret;
880
881 /* Disable signals */
882 sigfillset(&all_sigs);
883 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
884 if (ret == -1) {
885 PERROR("sigprocmask");
886 }
887 ust_lock();
888 rcu_bp_before_fork();
889 }
890
891 static void ust_after_fork_common(ust_fork_info_t *fork_info)
892 {
893 int ret;
894
895 DBG("process %d", getpid());
896 ust_unlock();
897 /* Restore signals */
898 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
899 if (ret == -1) {
900 PERROR("sigprocmask");
901 }
902 }
903
904 void ust_after_fork_parent(ust_fork_info_t *fork_info)
905 {
906 DBG("process %d", getpid());
907 rcu_bp_after_fork_parent();
908 /* Release mutexes and reenable signals */
909 ust_after_fork_common(fork_info);
910 }
911
912 /*
913 * After fork, in the child, we need to cleanup all the leftover state,
914 * except the worker thread which already magically disappeared thanks
915 * to the weird Linux fork semantics. After tyding up, we call
916 * lttng_ust_init() again to start over as a new PID.
917 *
918 * This is meant for forks() that have tracing in the child between the
919 * fork and following exec call (if there is any).
920 */
921 void ust_after_fork_child(ust_fork_info_t *fork_info)
922 {
923 DBG("process %d", getpid());
924 /* Release urcu mutexes */
925 rcu_bp_after_fork_child();
926 lttng_ust_cleanup(0);
927 /* Release mutexes and reenable signals */
928 ust_after_fork_common(fork_info);
929 lttng_ust_init();
930 }
This page took 0.0492 seconds and 4 git commands to generate.