4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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.
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
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <sys/types.h>
33 #include <semaphore.h>
37 #include <urcu/uatomic.h>
38 #include <urcu/futex.h>
39 #include <urcu/compiler.h>
41 #include <lttng/ust-events.h>
42 #include <lttng/ust-abi.h>
43 #include <lttng/ust.h>
45 #include <usterr-signal-safe.h>
46 #include "tracepoint-internal.h"
47 #include "ltt-tracer-core.h"
49 #include "../libringbuffer/tlsfixup.h"
52 * Has lttng ust comm constructor been called ?
54 static int initialized
;
57 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
58 * Held when handling a command, also held by fork() to deal with
59 * removal of threads, and by exit path.
62 /* Should the ust comm thread quit ? */
63 static int lttng_ust_comm_should_quit
;
66 * Wait for either of these before continuing to the main
68 * - the register_done message from sessiond daemon
69 * (will let the sessiond daemon enable sessions before main
71 * - sessiond daemon is not reachable.
72 * - timeout (ensuring applications are resilient to session
75 static sem_t constructor_wait
;
77 * Doing this for both the global and local sessiond.
79 static int sem_count
= { 2 };
82 * Counting nesting within lttng-ust. Used to ensure that calling fork()
83 * from liblttng-ust does not execute the pre/post fork handlers.
85 static int __thread lttng_ust_nest_count
;
88 * Info about socket and associated listener thread.
92 pthread_t ust_listener
; /* listener thread */
94 int constructor_sem_posted
;
98 char sock_path
[PATH_MAX
];
101 char wait_shm_path
[PATH_MAX
];
105 /* Socket from app (connect) to session daemon (listen) for communication */
106 struct sock_info global_apps
= {
113 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
116 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
119 /* TODO: allow global_apps_sock_path override */
121 struct sock_info local_apps
= {
125 .allowed
= 0, /* Check setuid bit first */
130 static int wait_poll_fallback
;
132 extern void ltt_ring_buffer_client_overwrite_init(void);
133 extern void ltt_ring_buffer_client_discard_init(void);
134 extern void ltt_ring_buffer_metadata_client_init(void);
135 extern void ltt_ring_buffer_client_overwrite_exit(void);
136 extern void ltt_ring_buffer_client_discard_exit(void);
137 extern void ltt_ring_buffer_metadata_client_exit(void);
140 * Force a read (imply TLS fixup for dlopen) of TLS variables.
143 void lttng_fixup_nest_count_tls(void)
145 asm volatile ("" : : "m" (lttng_ust_nest_count
));
149 int setup_local_apps(void)
151 const char *home_dir
;
156 * Disallow per-user tracing for setuid binaries.
158 if (uid
!= geteuid()) {
159 local_apps
.allowed
= 0;
162 local_apps
.allowed
= 1;
164 home_dir
= (const char *) getenv("HOME");
167 snprintf(local_apps
.sock_path
, PATH_MAX
,
168 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
169 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
170 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
175 int register_app_to_sessiond(int socket
)
185 uint32_t bits_per_long
;
186 char name
[16]; /* process name */
189 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
190 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
191 reg_msg
.pid
= getpid();
192 reg_msg
.ppid
= getppid();
193 reg_msg
.uid
= getuid();
194 reg_msg
.gid
= getgid();
195 reg_msg
.bits_per_long
= CAA_BITS_PER_LONG
;
196 lttng_ust_getprocname(reg_msg
.name
);
198 ret
= ustcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
199 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
205 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
209 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
212 DBG("message successfully sent");
215 if (errno
== ECONNRESET
) {
216 printf("remote end closed connection\n");
221 printf("incorrect message size: %zd\n", len
);
227 int handle_register_done(struct sock_info
*sock_info
)
231 if (sock_info
->constructor_sem_posted
)
233 sock_info
->constructor_sem_posted
= 1;
234 if (uatomic_read(&sem_count
) <= 0) {
237 ret
= uatomic_add_return(&sem_count
, -1);
239 ret
= sem_post(&constructor_wait
);
246 int handle_message(struct sock_info
*sock_info
,
247 int sock
, struct ustcomm_ust_msg
*lum
)
250 const struct lttng_ust_objd_ops
*ops
;
251 struct ustcomm_ust_reply lur
;
258 memset(&lur
, 0, sizeof(lur
));
260 if (lttng_ust_comm_should_quit
) {
265 ops
= objd_ops(lum
->handle
);
272 case LTTNG_UST_REGISTER_DONE
:
273 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
274 ret
= handle_register_done(sock_info
);
278 case LTTNG_UST_RELEASE
:
279 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
282 ret
= lttng_ust_objd_unref(lum
->handle
);
284 case LTTNG_UST_FILTER
:
286 /* Receive filter data */
289 uint16_t reloc_offset
;
290 char data
[FILTER_BYTECODE_MAX_LEN
];
293 if (lum
->u
.filter
.data_size
> 65536) {
294 ERR("Filter data size is too large: %u bytes\n",
295 lum
->u
.filter
.data_size
);
299 len
= ustcomm_recv_unix_sock(sock
, filter_data
.data
,
300 lum
->u
.filter
.data_size
);
302 case 0: /* orderly shutdown */
306 DBG("Receive failed from lttng-sessiond with errno %d", errno
);
307 if (errno
== ECONNRESET
) {
308 ERR("%s remote end closed connection\n", sock_info
->name
);
315 if (len
== lum
->u
.filter
.data_size
) {
316 DBG("filter data received\n");
319 ERR("incorrect filter data message size: %zd\n", len
);
324 filter_data
.len
= lum
->u
.filter
.data_size
;
325 filter_data
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
327 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
328 (unsigned long) &filter_data
,
336 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
337 (unsigned long) &lum
->u
,
345 lur
.handle
= lum
->handle
;
349 lur
.ret_code
= USTCOMM_OK
;
351 //lur.ret_code = USTCOMM_SESSION_FAIL;
356 case LTTNG_UST_STREAM
:
358 * Special-case reply to send stream info.
361 lur
.u
.stream
.memory_map_size
= *args
.stream
.memory_map_size
;
362 shm_fd
= *args
.stream
.shm_fd
;
363 wait_fd
= *args
.stream
.wait_fd
;
365 case LTTNG_UST_METADATA
:
366 case LTTNG_UST_CHANNEL
:
367 lur
.u
.channel
.memory_map_size
= *args
.channel
.memory_map_size
;
368 shm_fd
= *args
.channel
.shm_fd
;
369 wait_fd
= *args
.channel
.wait_fd
;
371 case LTTNG_UST_TRACER_VERSION
:
372 lur
.u
.version
= lum
->u
.version
;
374 case LTTNG_UST_TRACEPOINT_LIST_GET
:
375 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
379 ret
= send_reply(sock
, &lur
);
381 perror("error sending reply");
385 if ((lum
->cmd
== LTTNG_UST_STREAM
386 || lum
->cmd
== LTTNG_UST_CHANNEL
387 || lum
->cmd
== LTTNG_UST_METADATA
)
388 && lur
.ret_code
== USTCOMM_OK
) {
391 /* we also need to send the file descriptors. */
392 ret
= ustcomm_send_fds_unix_sock(sock
,
396 perror("send shm_fd");
400 * The sessiond expects 2 file descriptors, even upon
403 ret
= ustcomm_send_fds_unix_sock(sock
,
407 perror("send wait_fd");
416 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
419 if (lur
.ret_code
== USTCOMM_OK
) {
421 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
422 len
= ustcomm_send_unix_sock(sock
,
423 &args
.field_list
.entry
,
424 sizeof(args
.field_list
.entry
));
425 if (len
!= sizeof(args
.field_list
.entry
)) {
432 * We still have the memory map reference, and the fds have been
433 * sent to the sessiond. We can therefore close those fds. Note
434 * that we keep the write side of the wait_fd open, but close
437 if (lur
.ret_code
== USTCOMM_OK
) {
439 case LTTNG_UST_STREAM
:
443 PERROR("Error closing stream shm_fd");
445 *args
.stream
.shm_fd
= -1;
448 ret
= close(wait_fd
);
450 PERROR("Error closing stream wait_fd");
452 *args
.stream
.wait_fd
= -1;
455 case LTTNG_UST_METADATA
:
456 case LTTNG_UST_CHANNEL
:
460 PERROR("Error closing channel shm_fd");
462 *args
.channel
.shm_fd
= -1;
465 ret
= close(wait_fd
);
467 PERROR("Error closing channel wait_fd");
469 *args
.channel
.wait_fd
= -1;
481 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
485 if (sock_info
->socket
!= -1) {
486 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
488 ERR("Error closing apps socket");
490 sock_info
->socket
= -1;
492 if (sock_info
->root_handle
!= -1) {
493 ret
= lttng_ust_objd_unref(sock_info
->root_handle
);
495 ERR("Error unref root handle");
497 sock_info
->root_handle
= -1;
499 sock_info
->constructor_sem_posted
= 0;
501 * wait_shm_mmap is used by listener threads outside of the
502 * ust lock, so we cannot tear it down ourselves, because we
503 * cannot join on these threads. Leave this task to the OS
506 if (!exiting
&& sock_info
->wait_shm_mmap
) {
507 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
509 ERR("Error unmapping wait shm");
511 sock_info
->wait_shm_mmap
= NULL
;
516 * Using fork to set umask in the child process (not multi-thread safe).
517 * We deal with the shm_open vs ftruncate race (happening when the
518 * sessiond owns the shm and does not let everybody modify it, to ensure
519 * safety against shm_unlink) by simply letting the mmap fail and
520 * retrying after a few seconds.
521 * For global shm, everybody has rw access to it until the sessiond
525 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
527 int wait_shm_fd
, ret
;
531 * Try to open read-only.
533 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
534 if (wait_shm_fd
>= 0) {
536 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
538 * Real-only open did not work, and it's not because the
539 * entry was not present. It's a failure that prohibits
542 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
546 * If the open failed because the file did not exist, try
547 * creating it ourself.
549 lttng_ust_nest_count
++;
551 lttng_ust_nest_count
--;
556 * Parent: wait for child to return, in which case the
557 * shared memory map will have been created.
560 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
565 * Try to open read-only again after creation.
567 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
568 if (wait_shm_fd
< 0) {
570 * Real-only open did not work. It's a failure
571 * that prohibits using shm.
573 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
577 } else if (pid
== 0) {
581 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
582 if (sock_info
->global
)
583 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
585 * We're alone in a child process, so we can modify the
586 * process-wide umask.
590 * Try creating shm (or get rw access).
591 * We don't do an exclusive open, because we allow other
592 * processes to create+ftruncate it concurrently.
594 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
595 O_RDWR
| O_CREAT
, create_mode
);
596 if (wait_shm_fd
>= 0) {
597 ret
= ftruncate(wait_shm_fd
, mmap_size
);
605 * For local shm, we need to have rw access to accept
606 * opening it: this means the local sessiond will be
607 * able to wake us up. For global shm, we open it even
608 * if rw access is not granted, because the root.root
609 * sessiond will be able to override all rights and wake
612 if (!sock_info
->global
&& errno
!= EACCES
) {
613 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
617 * The shm exists, but we cannot open it RW. Report
625 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
629 * Ensure that our user is the owner of the shm file for
630 * local shm. If we do not own the file, it means our
631 * sessiond will not have access to wake us up (there is
632 * probably a rogue process trying to fake our
633 * sessiond). Fallback to polling method in this case.
635 ret
= fstat(wait_shm_fd
, &statbuf
);
640 if (statbuf
.st_uid
!= getuid())
646 ret
= close(wait_shm_fd
);
648 PERROR("Error closing fd");
654 char *get_map_shm(struct sock_info
*sock_info
)
656 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
657 int wait_shm_fd
, ret
;
660 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
661 if (wait_shm_fd
< 0) {
664 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
665 MAP_SHARED
, wait_shm_fd
, 0);
666 /* close shm fd immediately after taking the mmap reference */
667 ret
= close(wait_shm_fd
);
669 PERROR("Error closing fd");
671 if (wait_shm_mmap
== MAP_FAILED
) {
672 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
675 return wait_shm_mmap
;
682 void wait_for_sessiond(struct sock_info
*sock_info
)
687 if (lttng_ust_comm_should_quit
) {
690 if (wait_poll_fallback
) {
693 if (!sock_info
->wait_shm_mmap
) {
694 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
695 if (!sock_info
->wait_shm_mmap
)
700 DBG("Waiting for %s apps sessiond", sock_info
->name
);
701 /* Wait for futex wakeup */
702 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
703 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
704 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
706 if (errno
== EFAULT
) {
707 wait_poll_fallback
= 1;
709 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
710 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
711 "Please upgrade your kernel "
712 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
713 "mainline). LTTng-UST will use polling mode fallback.");
731 * This thread does not allocate any resource, except within
732 * handle_message, within mutex protection. This mutex protects against
734 * The other moment it allocates resources is at socket connection, which
735 * is also protected by the mutex.
738 void *ust_listener_thread(void *arg
)
740 struct sock_info
*sock_info
= arg
;
741 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
743 /* Restart trying to connect to the session daemon */
745 if (prev_connect_failed
) {
746 /* Wait for sessiond availability with pipe */
747 wait_for_sessiond(sock_info
);
751 * Sleep for 5 seconds before retrying after a
752 * sequence of failure / wait / failure. This
753 * deals with a killed or broken session daemon.
758 prev_connect_failed
= 0;
762 if (lttng_ust_comm_should_quit
) {
767 if (sock_info
->socket
!= -1) {
768 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
770 ERR("Error closing %s apps socket", sock_info
->name
);
772 sock_info
->socket
= -1;
776 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
778 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
779 prev_connect_failed
= 1;
781 * If we cannot find the sessiond daemon, don't delay
782 * constructor execution.
784 ret
= handle_register_done(sock_info
);
790 sock_info
->socket
= sock
= ret
;
793 * Create only one root handle per listener thread for the whole
796 if (sock_info
->root_handle
== -1) {
797 ret
= lttng_abi_create_root_handle();
799 ERR("Error creating root handle");
803 sock_info
->root_handle
= ret
;
806 ret
= register_app_to_sessiond(sock
);
808 ERR("Error registering to %s apps socket", sock_info
->name
);
809 prev_connect_failed
= 1;
811 * If we cannot register to the sessiond daemon, don't
812 * delay constructor execution.
814 ret
= handle_register_done(sock_info
);
823 struct ustcomm_ust_msg lum
;
825 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
827 case 0: /* orderly shutdown */
828 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
831 * Either sessiond has shutdown or refused us by closing the socket.
832 * In either case, we don't want to delay construction execution,
833 * and we need to wait before retry.
835 prev_connect_failed
= 1;
837 * If we cannot register to the sessiond daemon, don't
838 * delay constructor execution.
840 ret
= handle_register_done(sock_info
);
845 DBG("message received\n");
846 ret
= handle_message(sock_info
, sock
, &lum
);
848 ERR("Error handling message for %s socket", sock_info
->name
);
852 DBG("Receive failed from lttng-sessiond with errno %d", errno
);
853 if (errno
== ECONNRESET
) {
854 ERR("%s remote end closed connection\n", sock_info
->name
);
859 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
865 goto restart
; /* try to reconnect */
871 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
874 int get_timeout(struct timespec
*constructor_timeout
)
876 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
880 str_delay
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
882 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
885 switch (constructor_delay_ms
) {
886 case -1:/* fall-through */
888 return constructor_delay_ms
;
894 * If we are unable to find the current time, don't wait.
896 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
900 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
901 constructor_timeout
->tv_nsec
+=
902 (constructor_delay_ms
% 1000UL) * 1000000UL;
903 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
904 constructor_timeout
->tv_sec
++;
905 constructor_timeout
->tv_nsec
-= 1000000000UL;
911 * sessiond monitoring thread: monitor presence of global and per-user
912 * sessiond by polling the application common named pipe.
916 void __attribute__((constructor
)) lttng_ust_init(void)
918 struct timespec constructor_timeout
;
919 sigset_t sig_all_blocked
, orig_parent_mask
;
923 if (uatomic_xchg(&initialized
, 1) == 1)
927 * Fixup interdependency between TLS fixup mutex (which happens
928 * to be the dynamic linker mutex) and ust_lock, taken within
931 lttng_fixup_event_tls();
932 lttng_fixup_ringbuffer_tls();
933 lttng_fixup_vtid_tls();
934 lttng_fixup_nest_count_tls();
937 * We want precise control over the order in which we construct
938 * our sub-libraries vs starting to receive commands from
939 * sessiond (otherwise leading to errors when trying to create
940 * sessiond before the init functions are completed).
944 ltt_ring_buffer_metadata_client_init();
945 ltt_ring_buffer_client_overwrite_init();
946 ltt_ring_buffer_client_discard_init();
948 timeout_mode
= get_timeout(&constructor_timeout
);
950 ret
= sem_init(&constructor_wait
, 0, 0);
953 ret
= setup_local_apps();
955 ERR("Error setting up to local apps");
958 /* A new thread created by pthread_create inherits the signal mask
959 * from the parent. To avoid any signal being received by the
960 * listener thread, we block all signals temporarily in the parent,
961 * while we create the listener thread.
963 sigfillset(&sig_all_blocked
);
964 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
966 ERR("pthread_sigmask: %s", strerror(ret
));
969 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
970 ust_listener_thread
, &global_apps
);
972 ERR("pthread_create global: %s", strerror(ret
));
974 if (local_apps
.allowed
) {
975 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
976 ust_listener_thread
, &local_apps
);
978 ERR("pthread_create local: %s", strerror(ret
));
981 handle_register_done(&local_apps
);
984 /* Restore original signal mask in parent */
985 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
987 ERR("pthread_sigmask: %s", strerror(ret
));
990 switch (timeout_mode
) {
991 case 1: /* timeout wait */
993 ret
= sem_timedwait(&constructor_wait
,
994 &constructor_timeout
);
995 } while (ret
< 0 && errno
== EINTR
);
996 if (ret
< 0 && errno
== ETIMEDOUT
) {
997 ERR("Timed out waiting for ltt-sessiond");
1002 case -1:/* wait forever */
1004 ret
= sem_wait(&constructor_wait
);
1005 } while (ret
< 0 && errno
== EINTR
);
1008 case 0: /* no timeout */
1014 void lttng_ust_cleanup(int exiting
)
1016 cleanup_sock_info(&global_apps
, exiting
);
1017 if (local_apps
.allowed
) {
1018 cleanup_sock_info(&local_apps
, exiting
);
1021 * The teardown in this function all affect data structures
1022 * accessed under the UST lock by the listener thread. This
1023 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1024 * that none of these threads are accessing this data at this
1027 lttng_ust_abi_exit();
1028 lttng_ust_events_exit();
1029 ltt_ring_buffer_client_discard_exit();
1030 ltt_ring_buffer_client_overwrite_exit();
1031 ltt_ring_buffer_metadata_client_exit();
1034 /* Reinitialize values for fork */
1036 lttng_ust_comm_should_quit
= 0;
1041 void __attribute__((destructor
)) lttng_ust_exit(void)
1046 * Using pthread_cancel here because:
1047 * A) we don't want to hang application teardown.
1048 * B) the thread is not allocating any resource.
1052 * Require the communication thread to quit. Synchronize with
1053 * mutexes to ensure it is not in a mutex critical section when
1054 * pthread_cancel is later called.
1057 lttng_ust_comm_should_quit
= 1;
1060 /* cancel threads */
1061 ret
= pthread_cancel(global_apps
.ust_listener
);
1063 ERR("Error cancelling global ust listener thread: %s",
1066 if (local_apps
.allowed
) {
1067 ret
= pthread_cancel(local_apps
.ust_listener
);
1069 ERR("Error cancelling local ust listener thread: %s",
1074 * Do NOT join threads: use of sys_futex makes it impossible to
1075 * join the threads without using async-cancel, but async-cancel
1076 * is delivered by a signal, which could hit the target thread
1077 * anywhere in its code path, including while the ust_lock() is
1078 * held, causing a deadlock for the other thread. Let the OS
1079 * cleanup the threads if there are stalled in a syscall.
1081 lttng_ust_cleanup(1);
1085 * We exclude the worker threads across fork and clone (except
1086 * CLONE_VM), because these system calls only keep the forking thread
1087 * running in the child. Therefore, we don't want to call fork or clone
1088 * in the middle of an tracepoint or ust tracing state modification.
1089 * Holding this mutex protects these structures across fork and clone.
1091 void ust_before_fork(sigset_t
*save_sigset
)
1094 * Disable signals. This is to avoid that the child intervenes
1095 * before it is properly setup for tracing. It is safer to
1096 * disable all signals, because then we know we are not breaking
1097 * anything by restoring the original mask.
1102 if (lttng_ust_nest_count
)
1104 /* Disable signals */
1105 sigfillset(&all_sigs
);
1106 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1108 PERROR("sigprocmask");
1111 rcu_bp_before_fork();
1114 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1118 DBG("process %d", getpid());
1120 /* Restore signals */
1121 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1123 PERROR("sigprocmask");
1127 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1129 if (lttng_ust_nest_count
)
1131 DBG("process %d", getpid());
1132 rcu_bp_after_fork_parent();
1133 /* Release mutexes and reenable signals */
1134 ust_after_fork_common(restore_sigset
);
1138 * After fork, in the child, we need to cleanup all the leftover state,
1139 * except the worker thread which already magically disappeared thanks
1140 * to the weird Linux fork semantics. After tyding up, we call
1141 * lttng_ust_init() again to start over as a new PID.
1143 * This is meant for forks() that have tracing in the child between the
1144 * fork and following exec call (if there is any).
1146 void ust_after_fork_child(sigset_t
*restore_sigset
)
1148 if (lttng_ust_nest_count
)
1150 DBG("process %d", getpid());
1151 /* Release urcu mutexes */
1152 rcu_bp_after_fork_child();
1153 lttng_ust_cleanup(0);
1154 lttng_context_vtid_reset();
1155 /* Release mutexes and reenable signals */
1156 ust_after_fork_common(restore_sigset
);