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>
47 #include "tracepoint-internal.h"
48 #include "ltt-tracer-core.h"
50 #include "../libringbuffer/tlsfixup.h"
53 * Has lttng ust comm constructor been called ?
55 static int initialized
;
58 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
59 * Held when handling a command, also held by fork() to deal with
60 * removal of threads, and by exit path.
63 /* Should the ust comm thread quit ? */
64 static int lttng_ust_comm_should_quit
;
67 * Wait for either of these before continuing to the main
69 * - the register_done message from sessiond daemon
70 * (will let the sessiond daemon enable sessions before main
72 * - sessiond daemon is not reachable.
73 * - timeout (ensuring applications are resilient to session
76 static sem_t constructor_wait
;
78 * Doing this for both the global and local sessiond.
80 static int sem_count
= { 2 };
83 * Counting nesting within lttng-ust. Used to ensure that calling fork()
84 * from liblttng-ust does not execute the pre/post fork handlers.
86 static int __thread lttng_ust_nest_count
;
89 * Info about socket and associated listener thread.
93 pthread_t ust_listener
; /* listener thread */
95 int constructor_sem_posted
;
99 char sock_path
[PATH_MAX
];
102 char wait_shm_path
[PATH_MAX
];
106 /* Socket from app (connect) to session daemon (listen) for communication */
107 struct sock_info global_apps
= {
114 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
117 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
120 /* TODO: allow global_apps_sock_path override */
122 struct sock_info local_apps
= {
126 .allowed
= 0, /* Check setuid bit first */
131 static int wait_poll_fallback
;
133 extern void ltt_ring_buffer_client_overwrite_init(void);
134 extern void ltt_ring_buffer_client_discard_init(void);
135 extern void ltt_ring_buffer_metadata_client_init(void);
136 extern void ltt_ring_buffer_client_overwrite_exit(void);
137 extern void ltt_ring_buffer_client_discard_exit(void);
138 extern void ltt_ring_buffer_metadata_client_exit(void);
141 * Force a read (imply TLS fixup for dlopen) of TLS variables.
144 void lttng_fixup_nest_count_tls(void)
146 asm volatile ("" : : "m" (lttng_ust_nest_count
));
150 int setup_local_apps(void)
152 const char *home_dir
;
157 * Disallow per-user tracing for setuid binaries.
159 if (uid
!= geteuid()) {
160 local_apps
.allowed
= 0;
163 local_apps
.allowed
= 1;
165 home_dir
= (const char *) getenv("HOME");
168 snprintf(local_apps
.sock_path
, PATH_MAX
,
169 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
170 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
171 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
176 int register_app_to_sessiond(int socket
)
186 uint32_t bits_per_long
;
187 char name
[16]; /* process name */
190 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
191 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
192 reg_msg
.pid
= getpid();
193 reg_msg
.ppid
= getppid();
194 reg_msg
.uid
= getuid();
195 reg_msg
.gid
= getgid();
196 reg_msg
.bits_per_long
= CAA_BITS_PER_LONG
;
197 lttng_ust_getprocname(reg_msg
.name
);
199 ret
= ustcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
200 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
206 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
210 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
213 DBG("message successfully sent");
216 if (errno
== ECONNRESET
) {
217 printf("remote end closed connection\n");
222 printf("incorrect message size: %zd\n", len
);
228 int handle_register_done(struct sock_info
*sock_info
)
232 if (sock_info
->constructor_sem_posted
)
234 sock_info
->constructor_sem_posted
= 1;
235 if (uatomic_read(&sem_count
) <= 0) {
238 ret
= uatomic_add_return(&sem_count
, -1);
240 ret
= sem_post(&constructor_wait
);
247 int handle_message(struct sock_info
*sock_info
,
248 int sock
, struct ustcomm_ust_msg
*lum
)
251 const struct lttng_ust_objd_ops
*ops
;
252 struct ustcomm_ust_reply lur
;
259 memset(&lur
, 0, sizeof(lur
));
261 if (lttng_ust_comm_should_quit
) {
266 ops
= objd_ops(lum
->handle
);
273 case LTTNG_UST_REGISTER_DONE
:
274 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
275 ret
= handle_register_done(sock_info
);
279 case LTTNG_UST_RELEASE
:
280 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
283 ret
= lttng_ust_objd_unref(lum
->handle
);
285 case LTTNG_UST_FILTER
:
287 /* Receive filter data */
288 struct lttng_ust_filter_bytecode
*bytecode
;
290 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
291 ERR("Filter data size is too large: %u bytes\n",
292 lum
->u
.filter
.data_size
);
297 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
- 1) {
298 ERR("Filter reloc offset %u is not within data\n",
299 lum
->u
.filter
.reloc_offset
);
304 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
309 len
= ustcomm_recv_unix_sock(sock
, bytecode
->data
,
310 lum
->u
.filter
.data_size
);
312 case 0: /* orderly shutdown */
317 DBG("Receive failed from lttng-sessiond with errno %d", errno
);
318 if (errno
== ECONNRESET
) {
319 ERR("%s remote end closed connection\n", sock_info
->name
);
327 if (len
== lum
->u
.filter
.data_size
) {
328 DBG("filter data received\n");
331 ERR("incorrect filter data message size: %zd\n", len
);
337 bytecode
->len
= lum
->u
.filter
.data_size
;
338 bytecode
->reloc_offset
= lum
->u
.filter
.reloc_offset
;
340 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
341 (unsigned long) bytecode
,
346 /* don't free bytecode if everything went fine. */
355 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
356 (unsigned long) &lum
->u
,
364 lur
.handle
= lum
->handle
;
368 lur
.ret_code
= USTCOMM_OK
;
370 //lur.ret_code = USTCOMM_SESSION_FAIL;
375 case LTTNG_UST_STREAM
:
377 * Special-case reply to send stream info.
380 lur
.u
.stream
.memory_map_size
= *args
.stream
.memory_map_size
;
381 shm_fd
= *args
.stream
.shm_fd
;
382 wait_fd
= *args
.stream
.wait_fd
;
384 case LTTNG_UST_METADATA
:
385 case LTTNG_UST_CHANNEL
:
386 lur
.u
.channel
.memory_map_size
= *args
.channel
.memory_map_size
;
387 shm_fd
= *args
.channel
.shm_fd
;
388 wait_fd
= *args
.channel
.wait_fd
;
390 case LTTNG_UST_TRACER_VERSION
:
391 lur
.u
.version
= lum
->u
.version
;
393 case LTTNG_UST_TRACEPOINT_LIST_GET
:
394 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
398 ret
= send_reply(sock
, &lur
);
400 perror("error sending reply");
404 if ((lum
->cmd
== LTTNG_UST_STREAM
405 || lum
->cmd
== LTTNG_UST_CHANNEL
406 || lum
->cmd
== LTTNG_UST_METADATA
)
407 && lur
.ret_code
== USTCOMM_OK
) {
410 /* we also need to send the file descriptors. */
411 ret
= ustcomm_send_fds_unix_sock(sock
,
415 perror("send shm_fd");
419 * The sessiond expects 2 file descriptors, even upon
422 ret
= ustcomm_send_fds_unix_sock(sock
,
426 perror("send wait_fd");
435 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
438 if (lur
.ret_code
== USTCOMM_OK
) {
440 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
441 len
= ustcomm_send_unix_sock(sock
,
442 &args
.field_list
.entry
,
443 sizeof(args
.field_list
.entry
));
444 if (len
!= sizeof(args
.field_list
.entry
)) {
451 * We still have the memory map reference, and the fds have been
452 * sent to the sessiond. We can therefore close those fds. Note
453 * that we keep the write side of the wait_fd open, but close
456 if (lur
.ret_code
== USTCOMM_OK
) {
458 case LTTNG_UST_STREAM
:
462 PERROR("Error closing stream shm_fd");
464 *args
.stream
.shm_fd
= -1;
467 ret
= close(wait_fd
);
469 PERROR("Error closing stream wait_fd");
471 *args
.stream
.wait_fd
= -1;
474 case LTTNG_UST_METADATA
:
475 case LTTNG_UST_CHANNEL
:
479 PERROR("Error closing channel shm_fd");
481 *args
.channel
.shm_fd
= -1;
484 ret
= close(wait_fd
);
486 PERROR("Error closing channel wait_fd");
488 *args
.channel
.wait_fd
= -1;
500 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
504 if (sock_info
->socket
!= -1) {
505 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
507 ERR("Error closing apps socket");
509 sock_info
->socket
= -1;
511 if (sock_info
->root_handle
!= -1) {
512 ret
= lttng_ust_objd_unref(sock_info
->root_handle
);
514 ERR("Error unref root handle");
516 sock_info
->root_handle
= -1;
518 sock_info
->constructor_sem_posted
= 0;
520 * wait_shm_mmap is used by listener threads outside of the
521 * ust lock, so we cannot tear it down ourselves, because we
522 * cannot join on these threads. Leave this task to the OS
525 if (!exiting
&& sock_info
->wait_shm_mmap
) {
526 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
528 ERR("Error unmapping wait shm");
530 sock_info
->wait_shm_mmap
= NULL
;
535 * Using fork to set umask in the child process (not multi-thread safe).
536 * We deal with the shm_open vs ftruncate race (happening when the
537 * sessiond owns the shm and does not let everybody modify it, to ensure
538 * safety against shm_unlink) by simply letting the mmap fail and
539 * retrying after a few seconds.
540 * For global shm, everybody has rw access to it until the sessiond
544 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
546 int wait_shm_fd
, ret
;
550 * Try to open read-only.
552 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
553 if (wait_shm_fd
>= 0) {
555 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
557 * Real-only open did not work, and it's not because the
558 * entry was not present. It's a failure that prohibits
561 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
565 * If the open failed because the file did not exist, try
566 * creating it ourself.
568 lttng_ust_nest_count
++;
570 lttng_ust_nest_count
--;
575 * Parent: wait for child to return, in which case the
576 * shared memory map will have been created.
579 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
584 * Try to open read-only again after creation.
586 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
587 if (wait_shm_fd
< 0) {
589 * Real-only open did not work. It's a failure
590 * that prohibits using shm.
592 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
596 } else if (pid
== 0) {
600 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
601 if (sock_info
->global
)
602 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
604 * We're alone in a child process, so we can modify the
605 * process-wide umask.
609 * Try creating shm (or get rw access).
610 * We don't do an exclusive open, because we allow other
611 * processes to create+ftruncate it concurrently.
613 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
614 O_RDWR
| O_CREAT
, create_mode
);
615 if (wait_shm_fd
>= 0) {
616 ret
= ftruncate(wait_shm_fd
, mmap_size
);
624 * For local shm, we need to have rw access to accept
625 * opening it: this means the local sessiond will be
626 * able to wake us up. For global shm, we open it even
627 * if rw access is not granted, because the root.root
628 * sessiond will be able to override all rights and wake
631 if (!sock_info
->global
&& errno
!= EACCES
) {
632 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
636 * The shm exists, but we cannot open it RW. Report
644 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
648 * Ensure that our user is the owner of the shm file for
649 * local shm. If we do not own the file, it means our
650 * sessiond will not have access to wake us up (there is
651 * probably a rogue process trying to fake our
652 * sessiond). Fallback to polling method in this case.
654 ret
= fstat(wait_shm_fd
, &statbuf
);
659 if (statbuf
.st_uid
!= getuid())
665 ret
= close(wait_shm_fd
);
667 PERROR("Error closing fd");
673 char *get_map_shm(struct sock_info
*sock_info
)
675 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
676 int wait_shm_fd
, ret
;
679 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
680 if (wait_shm_fd
< 0) {
683 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
684 MAP_SHARED
, wait_shm_fd
, 0);
685 /* close shm fd immediately after taking the mmap reference */
686 ret
= close(wait_shm_fd
);
688 PERROR("Error closing fd");
690 if (wait_shm_mmap
== MAP_FAILED
) {
691 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
694 return wait_shm_mmap
;
701 void wait_for_sessiond(struct sock_info
*sock_info
)
706 if (lttng_ust_comm_should_quit
) {
709 if (wait_poll_fallback
) {
712 if (!sock_info
->wait_shm_mmap
) {
713 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
714 if (!sock_info
->wait_shm_mmap
)
719 DBG("Waiting for %s apps sessiond", sock_info
->name
);
720 /* Wait for futex wakeup */
721 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
722 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
723 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
725 if (errno
== EFAULT
) {
726 wait_poll_fallback
= 1;
728 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
729 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
730 "Please upgrade your kernel "
731 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
732 "mainline). LTTng-UST will use polling mode fallback.");
750 * This thread does not allocate any resource, except within
751 * handle_message, within mutex protection. This mutex protects against
753 * The other moment it allocates resources is at socket connection, which
754 * is also protected by the mutex.
757 void *ust_listener_thread(void *arg
)
759 struct sock_info
*sock_info
= arg
;
760 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
762 /* Restart trying to connect to the session daemon */
764 if (prev_connect_failed
) {
765 /* Wait for sessiond availability with pipe */
766 wait_for_sessiond(sock_info
);
770 * Sleep for 5 seconds before retrying after a
771 * sequence of failure / wait / failure. This
772 * deals with a killed or broken session daemon.
777 prev_connect_failed
= 0;
781 if (lttng_ust_comm_should_quit
) {
786 if (sock_info
->socket
!= -1) {
787 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
789 ERR("Error closing %s apps socket", sock_info
->name
);
791 sock_info
->socket
= -1;
795 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
797 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
798 prev_connect_failed
= 1;
800 * If we cannot find the sessiond daemon, don't delay
801 * constructor execution.
803 ret
= handle_register_done(sock_info
);
809 sock_info
->socket
= sock
= ret
;
812 * Create only one root handle per listener thread for the whole
815 if (sock_info
->root_handle
== -1) {
816 ret
= lttng_abi_create_root_handle();
818 ERR("Error creating root handle");
822 sock_info
->root_handle
= ret
;
825 ret
= register_app_to_sessiond(sock
);
827 ERR("Error registering to %s apps socket", sock_info
->name
);
828 prev_connect_failed
= 1;
830 * If we cannot register to the sessiond daemon, don't
831 * delay constructor execution.
833 ret
= handle_register_done(sock_info
);
842 struct ustcomm_ust_msg lum
;
844 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
846 case 0: /* orderly shutdown */
847 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
850 * Either sessiond has shutdown or refused us by closing the socket.
851 * In either case, we don't want to delay construction execution,
852 * and we need to wait before retry.
854 prev_connect_failed
= 1;
856 * If we cannot register to the sessiond daemon, don't
857 * delay constructor execution.
859 ret
= handle_register_done(sock_info
);
864 DBG("message received\n");
865 ret
= handle_message(sock_info
, sock
, &lum
);
867 ERR("Error handling message for %s socket", sock_info
->name
);
871 DBG("Receive failed from lttng-sessiond with errno %d", errno
);
872 if (errno
== ECONNRESET
) {
873 ERR("%s remote end closed connection\n", sock_info
->name
);
878 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
884 goto restart
; /* try to reconnect */
890 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
893 int get_timeout(struct timespec
*constructor_timeout
)
895 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
899 str_delay
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
901 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
904 switch (constructor_delay_ms
) {
905 case -1:/* fall-through */
907 return constructor_delay_ms
;
913 * If we are unable to find the current time, don't wait.
915 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
919 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
920 constructor_timeout
->tv_nsec
+=
921 (constructor_delay_ms
% 1000UL) * 1000000UL;
922 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
923 constructor_timeout
->tv_sec
++;
924 constructor_timeout
->tv_nsec
-= 1000000000UL;
930 * sessiond monitoring thread: monitor presence of global and per-user
931 * sessiond by polling the application common named pipe.
935 void __attribute__((constructor
)) lttng_ust_init(void)
937 struct timespec constructor_timeout
;
938 sigset_t sig_all_blocked
, orig_parent_mask
;
939 pthread_attr_t thread_attr
;
943 if (uatomic_xchg(&initialized
, 1) == 1)
947 * Fixup interdependency between TLS fixup mutex (which happens
948 * to be the dynamic linker mutex) and ust_lock, taken within
951 lttng_fixup_event_tls();
952 lttng_fixup_ringbuffer_tls();
953 lttng_fixup_vtid_tls();
954 lttng_fixup_nest_count_tls();
957 * We want precise control over the order in which we construct
958 * our sub-libraries vs starting to receive commands from
959 * sessiond (otherwise leading to errors when trying to create
960 * sessiond before the init functions are completed).
964 ltt_ring_buffer_metadata_client_init();
965 ltt_ring_buffer_client_overwrite_init();
966 ltt_ring_buffer_client_discard_init();
968 timeout_mode
= get_timeout(&constructor_timeout
);
970 ret
= sem_init(&constructor_wait
, 0, 0);
973 ret
= setup_local_apps();
975 ERR("Error setting up to local apps");
978 /* A new thread created by pthread_create inherits the signal mask
979 * from the parent. To avoid any signal being received by the
980 * listener thread, we block all signals temporarily in the parent,
981 * while we create the listener thread.
983 sigfillset(&sig_all_blocked
);
984 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
986 ERR("pthread_sigmask: %s", strerror(ret
));
989 ret
= pthread_attr_init(&thread_attr
);
991 ERR("pthread_attr_init: %s", strerror(ret
));
993 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
995 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
998 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
999 ust_listener_thread
, &global_apps
);
1001 ERR("pthread_create global: %s", strerror(ret
));
1003 if (local_apps
.allowed
) {
1004 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1005 ust_listener_thread
, &local_apps
);
1007 ERR("pthread_create local: %s", strerror(ret
));
1010 handle_register_done(&local_apps
);
1012 ret
= pthread_attr_destroy(&thread_attr
);
1014 ERR("pthread_attr_destroy: %s", strerror(ret
));
1017 /* Restore original signal mask in parent */
1018 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1020 ERR("pthread_sigmask: %s", strerror(ret
));
1023 switch (timeout_mode
) {
1024 case 1: /* timeout wait */
1026 ret
= sem_timedwait(&constructor_wait
,
1027 &constructor_timeout
);
1028 } while (ret
< 0 && errno
== EINTR
);
1029 if (ret
< 0 && errno
== ETIMEDOUT
) {
1030 ERR("Timed out waiting for ltt-sessiond");
1035 case -1:/* wait forever */
1037 ret
= sem_wait(&constructor_wait
);
1038 } while (ret
< 0 && errno
== EINTR
);
1041 case 0: /* no timeout */
1047 void lttng_ust_cleanup(int exiting
)
1049 cleanup_sock_info(&global_apps
, exiting
);
1050 if (local_apps
.allowed
) {
1051 cleanup_sock_info(&local_apps
, exiting
);
1054 * The teardown in this function all affect data structures
1055 * accessed under the UST lock by the listener thread. This
1056 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1057 * that none of these threads are accessing this data at this
1060 lttng_ust_abi_exit();
1061 lttng_ust_events_exit();
1062 ltt_ring_buffer_client_discard_exit();
1063 ltt_ring_buffer_client_overwrite_exit();
1064 ltt_ring_buffer_metadata_client_exit();
1067 /* Reinitialize values for fork */
1069 lttng_ust_comm_should_quit
= 0;
1074 void __attribute__((destructor
)) lttng_ust_exit(void)
1079 * Using pthread_cancel here because:
1080 * A) we don't want to hang application teardown.
1081 * B) the thread is not allocating any resource.
1085 * Require the communication thread to quit. Synchronize with
1086 * mutexes to ensure it is not in a mutex critical section when
1087 * pthread_cancel is later called.
1090 lttng_ust_comm_should_quit
= 1;
1093 /* cancel threads */
1094 ret
= pthread_cancel(global_apps
.ust_listener
);
1096 ERR("Error cancelling global ust listener thread: %s",
1099 if (local_apps
.allowed
) {
1100 ret
= pthread_cancel(local_apps
.ust_listener
);
1102 ERR("Error cancelling local ust listener thread: %s",
1107 * Do NOT join threads: use of sys_futex makes it impossible to
1108 * join the threads without using async-cancel, but async-cancel
1109 * is delivered by a signal, which could hit the target thread
1110 * anywhere in its code path, including while the ust_lock() is
1111 * held, causing a deadlock for the other thread. Let the OS
1112 * cleanup the threads if there are stalled in a syscall.
1114 lttng_ust_cleanup(1);
1118 * We exclude the worker threads across fork and clone (except
1119 * CLONE_VM), because these system calls only keep the forking thread
1120 * running in the child. Therefore, we don't want to call fork or clone
1121 * in the middle of an tracepoint or ust tracing state modification.
1122 * Holding this mutex protects these structures across fork and clone.
1124 void ust_before_fork(sigset_t
*save_sigset
)
1127 * Disable signals. This is to avoid that the child intervenes
1128 * before it is properly setup for tracing. It is safer to
1129 * disable all signals, because then we know we are not breaking
1130 * anything by restoring the original mask.
1135 if (lttng_ust_nest_count
)
1137 /* Disable signals */
1138 sigfillset(&all_sigs
);
1139 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1141 PERROR("sigprocmask");
1144 rcu_bp_before_fork();
1147 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1151 DBG("process %d", getpid());
1153 /* Restore signals */
1154 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1156 PERROR("sigprocmask");
1160 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1162 if (lttng_ust_nest_count
)
1164 DBG("process %d", getpid());
1165 rcu_bp_after_fork_parent();
1166 /* Release mutexes and reenable signals */
1167 ust_after_fork_common(restore_sigset
);
1171 * After fork, in the child, we need to cleanup all the leftover state,
1172 * except the worker thread which already magically disappeared thanks
1173 * to the weird Linux fork semantics. After tyding up, we call
1174 * lttng_ust_init() again to start over as a new PID.
1176 * This is meant for forks() that have tracing in the child between the
1177 * fork and following exec call (if there is any).
1179 void ust_after_fork_child(sigset_t
*restore_sigset
)
1181 if (lttng_ust_nest_count
)
1183 DBG("process %d", getpid());
1184 /* Release urcu mutexes */
1185 rcu_bp_after_fork_child();
1186 lttng_ust_cleanup(0);
1187 lttng_context_vtid_reset();
1188 /* Release mutexes and reenable signals */
1189 ust_after_fork_common(restore_sigset
);