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>
44 #include <lttng/ust-error.h>
45 #include <lttng/ust-ctl.h>
47 #include <usterr-signal-safe.h>
49 #include "tracepoint-internal.h"
50 #include "lttng-tracer-core.h"
52 #include "../libringbuffer/tlsfixup.h"
55 * Has lttng ust comm constructor been called ?
57 static int initialized
;
60 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
61 * Held when handling a command, also held by fork() to deal with
62 * removal of threads, and by exit path.
65 /* Should the ust comm thread quit ? */
66 static int lttng_ust_comm_should_quit
;
69 * Wait for either of these before continuing to the main
71 * - the register_done message from sessiond daemon
72 * (will let the sessiond daemon enable sessions before main
74 * - sessiond daemon is not reachable.
75 * - timeout (ensuring applications are resilient to session
78 static sem_t constructor_wait
;
80 * Doing this for both the global and local sessiond.
82 static int sem_count
= { 2 };
85 * Counting nesting within lttng-ust. Used to ensure that calling fork()
86 * from liblttng-ust does not execute the pre/post fork handlers.
88 static int __thread lttng_ust_nest_count
;
91 * Info about socket and associated listener thread.
95 pthread_t ust_listener
; /* listener thread */
97 int constructor_sem_posted
;
102 char sock_path
[PATH_MAX
];
105 char wait_shm_path
[PATH_MAX
];
109 /* Socket from app (connect) to session daemon (listen) for communication */
110 struct sock_info global_apps
= {
118 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
121 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
124 /* TODO: allow global_apps_sock_path override */
126 struct sock_info local_apps
= {
130 .allowed
= 0, /* Check setuid bit first */
136 static int wait_poll_fallback
;
138 static const char *cmd_name_mapping
[] = {
139 [ LTTNG_UST_RELEASE
] = "Release",
140 [ LTTNG_UST_SESSION
] = "Create Session",
141 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
143 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
144 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
145 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
146 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
148 /* Session FD commands */
149 [ LTTNG_UST_CHANNEL
] = "Create Channel",
150 [ LTTNG_UST_SESSION_START
] = "Start Session",
151 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
153 /* Channel FD commands */
154 [ LTTNG_UST_STREAM
] = "Create Stream",
155 [ LTTNG_UST_EVENT
] = "Create Event",
157 /* Event and Channel FD commands */
158 [ LTTNG_UST_CONTEXT
] = "Create Context",
159 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
161 /* Event, Channel and Session commands */
162 [ LTTNG_UST_ENABLE
] = "Enable",
163 [ LTTNG_UST_DISABLE
] = "Disable",
165 /* Tracepoint list commands */
166 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
167 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
169 /* Event FD commands */
170 [ LTTNG_UST_FILTER
] = "Create Filter",
173 extern void lttng_ring_buffer_client_overwrite_init(void);
174 extern void lttng_ring_buffer_client_discard_init(void);
175 extern void lttng_ring_buffer_metadata_client_init(void);
176 extern void lttng_ring_buffer_client_overwrite_exit(void);
177 extern void lttng_ring_buffer_client_discard_exit(void);
178 extern void lttng_ring_buffer_metadata_client_exit(void);
181 * Force a read (imply TLS fixup for dlopen) of TLS variables.
184 void lttng_fixup_nest_count_tls(void)
186 asm volatile ("" : : "m" (lttng_ust_nest_count
));
190 void print_cmd(int cmd
, int handle
)
192 const char *cmd_name
= "Unknown";
194 if (cmd_name_mapping
[cmd
]) {
195 cmd_name
= cmd_name_mapping
[cmd
];
197 DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name
,
198 lttng_ust_obj_get_name(handle
), handle
);
202 int setup_local_apps(void)
204 const char *home_dir
;
209 * Disallow per-user tracing for setuid binaries.
211 if (uid
!= geteuid()) {
212 assert(local_apps
.allowed
== 0);
215 home_dir
= (const char *) getenv("HOME");
217 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
218 assert(local_apps
.allowed
== 0);
221 local_apps
.allowed
= 1;
222 snprintf(local_apps
.sock_path
, PATH_MAX
,
223 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
224 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
225 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
230 int register_app_to_sessiond(int socket
)
240 uint32_t bits_per_long
;
241 char name
[16]; /* process name */
244 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
245 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
246 reg_msg
.pid
= getpid();
247 reg_msg
.ppid
= getppid();
248 reg_msg
.uid
= getuid();
249 reg_msg
.gid
= getgid();
250 reg_msg
.bits_per_long
= CAA_BITS_PER_LONG
;
251 lttng_ust_getprocname(reg_msg
.name
);
253 ret
= ustcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
254 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
260 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
264 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
267 DBG("message successfully sent");
270 if (len
== -ECONNRESET
) {
271 DBG("remote end closed connection");
276 DBG("incorrect message size: %zd", len
);
282 int handle_register_done(struct sock_info
*sock_info
)
286 if (sock_info
->constructor_sem_posted
)
288 sock_info
->constructor_sem_posted
= 1;
289 if (uatomic_read(&sem_count
) <= 0) {
292 ret
= uatomic_add_return(&sem_count
, -1);
294 ret
= sem_post(&constructor_wait
);
301 int handle_message(struct sock_info
*sock_info
,
302 int sock
, struct ustcomm_ust_msg
*lum
)
305 const struct lttng_ust_objd_ops
*ops
;
306 struct ustcomm_ust_reply lur
;
312 memset(&lur
, 0, sizeof(lur
));
314 if (lttng_ust_comm_should_quit
) {
315 ret
= -LTTNG_UST_ERR_EXITING
;
319 ops
= objd_ops(lum
->handle
);
326 case LTTNG_UST_REGISTER_DONE
:
327 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
328 ret
= handle_register_done(sock_info
);
332 case LTTNG_UST_RELEASE
:
333 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
336 ret
= lttng_ust_objd_unref(lum
->handle
);
338 case LTTNG_UST_FILTER
:
340 /* Receive filter data */
341 struct lttng_ust_filter_bytecode_node
*bytecode
;
343 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
344 ERR("Filter data size is too large: %u bytes",
345 lum
->u
.filter
.data_size
);
350 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
351 ERR("Filter reloc offset %u is not within data",
352 lum
->u
.filter
.reloc_offset
);
357 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
362 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
363 lum
->u
.filter
.data_size
);
365 case 0: /* orderly shutdown */
370 if (len
== lum
->u
.filter
.data_size
) {
371 DBG("filter data received");
373 } else if (len
< 0) {
374 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
375 if (len
== -ECONNRESET
) {
376 ERR("%s remote end closed connection", sock_info
->name
);
384 DBG("incorrect filter data message size: %zd", len
);
390 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
391 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
392 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
394 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
395 (unsigned long) bytecode
,
400 /* don't free bytecode if everything went fine. */
407 case LTTNG_UST_CHANNEL
:
411 len
= ustcomm_recv_channel_from_sessiond(sock
,
412 &chan_data
, lum
->u
.channel
.len
);
414 case 0: /* orderly shutdown */
418 if (len
== lum
->u
.channel
.len
) {
419 DBG("channel data received");
421 } else if (len
< 0) {
422 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
423 if (len
== -ECONNRESET
) {
424 ERR("%s remote end closed connection", sock_info
->name
);
431 DBG("incorrect channel data message size: %zd", len
);
436 args
.channel
.chan_data
= chan_data
;
438 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
439 (unsigned long) &lum
->u
,
445 case LTTNG_UST_STREAM
:
447 /* Receive shm_fd, wakeup_fd */
448 ret
= ustcomm_recv_stream_from_sessiond(sock
,
451 &args
.stream
.wakeup_fd
);
456 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
457 (unsigned long) &lum
->u
,
465 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
466 (unsigned long) &lum
->u
,
474 lur
.handle
= lum
->handle
;
478 lur
.ret_code
= LTTNG_UST_OK
;
481 * Use -LTTNG_UST_ERR as wildcard for UST internal
482 * error that are not caused by the transport, except if
483 * we already have a more precise error message to
486 if (ret
> -LTTNG_UST_ERR
) {
487 /* Translate code to UST error. */
490 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
493 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
496 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
499 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
502 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
505 lur
.ret_code
= -LTTNG_UST_ERR
;
514 case LTTNG_UST_TRACER_VERSION
:
515 lur
.u
.version
= lum
->u
.version
;
517 case LTTNG_UST_TRACEPOINT_LIST_GET
:
518 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
522 DBG("Return value: %d", lur
.ret_val
);
523 ret
= send_reply(sock
, &lur
);
525 DBG("error sending reply");
530 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
533 if (lur
.ret_code
== LTTNG_UST_OK
) {
535 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
536 len
= ustcomm_send_unix_sock(sock
,
537 &args
.field_list
.entry
,
538 sizeof(args
.field_list
.entry
));
543 if (len
!= sizeof(args
.field_list
.entry
)) {
556 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
560 if (sock_info
->socket
!= -1) {
561 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
563 ERR("Error closing apps socket");
565 sock_info
->socket
= -1;
567 if (sock_info
->root_handle
!= -1) {
568 ret
= lttng_ust_objd_unref(sock_info
->root_handle
);
570 ERR("Error unref root handle");
572 sock_info
->root_handle
= -1;
574 sock_info
->constructor_sem_posted
= 0;
576 * wait_shm_mmap is used by listener threads outside of the
577 * ust lock, so we cannot tear it down ourselves, because we
578 * cannot join on these threads. Leave this task to the OS
581 if (!exiting
&& sock_info
->wait_shm_mmap
) {
582 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
584 ERR("Error unmapping wait shm");
586 sock_info
->wait_shm_mmap
= NULL
;
591 * Using fork to set umask in the child process (not multi-thread safe).
592 * We deal with the shm_open vs ftruncate race (happening when the
593 * sessiond owns the shm and does not let everybody modify it, to ensure
594 * safety against shm_unlink) by simply letting the mmap fail and
595 * retrying after a few seconds.
596 * For global shm, everybody has rw access to it until the sessiond
600 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
602 int wait_shm_fd
, ret
;
606 * Try to open read-only.
608 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
609 if (wait_shm_fd
>= 0) {
611 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
613 * Real-only open did not work, and it's not because the
614 * entry was not present. It's a failure that prohibits
617 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
621 * If the open failed because the file did not exist, try
622 * creating it ourself.
624 lttng_ust_nest_count
++;
626 lttng_ust_nest_count
--;
631 * Parent: wait for child to return, in which case the
632 * shared memory map will have been created.
635 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
640 * Try to open read-only again after creation.
642 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
643 if (wait_shm_fd
< 0) {
645 * Real-only open did not work. It's a failure
646 * that prohibits using shm.
648 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
652 } else if (pid
== 0) {
656 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
657 if (sock_info
->global
)
658 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
660 * We're alone in a child process, so we can modify the
661 * process-wide umask.
665 * Try creating shm (or get rw access).
666 * We don't do an exclusive open, because we allow other
667 * processes to create+ftruncate it concurrently.
669 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
670 O_RDWR
| O_CREAT
, create_mode
);
671 if (wait_shm_fd
>= 0) {
672 ret
= ftruncate(wait_shm_fd
, mmap_size
);
680 * For local shm, we need to have rw access to accept
681 * opening it: this means the local sessiond will be
682 * able to wake us up. For global shm, we open it even
683 * if rw access is not granted, because the root.root
684 * sessiond will be able to override all rights and wake
687 if (!sock_info
->global
&& errno
!= EACCES
) {
688 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
692 * The shm exists, but we cannot open it RW. Report
700 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
704 * Ensure that our user is the owner of the shm file for
705 * local shm. If we do not own the file, it means our
706 * sessiond will not have access to wake us up (there is
707 * probably a rogue process trying to fake our
708 * sessiond). Fallback to polling method in this case.
710 ret
= fstat(wait_shm_fd
, &statbuf
);
715 if (statbuf
.st_uid
!= getuid())
721 ret
= close(wait_shm_fd
);
723 PERROR("Error closing fd");
729 char *get_map_shm(struct sock_info
*sock_info
)
731 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
732 int wait_shm_fd
, ret
;
735 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
736 if (wait_shm_fd
< 0) {
739 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
740 MAP_SHARED
, wait_shm_fd
, 0);
741 /* close shm fd immediately after taking the mmap reference */
742 ret
= close(wait_shm_fd
);
744 PERROR("Error closing fd");
746 if (wait_shm_mmap
== MAP_FAILED
) {
747 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
750 return wait_shm_mmap
;
757 void wait_for_sessiond(struct sock_info
*sock_info
)
762 if (lttng_ust_comm_should_quit
) {
765 if (wait_poll_fallback
) {
768 if (!sock_info
->wait_shm_mmap
) {
769 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
770 if (!sock_info
->wait_shm_mmap
)
775 DBG("Waiting for %s apps sessiond", sock_info
->name
);
776 /* Wait for futex wakeup */
777 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
778 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
779 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
781 if (errno
== EFAULT
) {
782 wait_poll_fallback
= 1;
784 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
785 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
786 "Please upgrade your kernel "
787 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
788 "mainline). LTTng-UST will use polling mode fallback.");
806 * This thread does not allocate any resource, except within
807 * handle_message, within mutex protection. This mutex protects against
809 * The other moment it allocates resources is at socket connection, which
810 * is also protected by the mutex.
813 void *ust_listener_thread(void *arg
)
815 struct sock_info
*sock_info
= arg
;
816 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
818 /* Restart trying to connect to the session daemon */
820 if (prev_connect_failed
) {
821 /* Wait for sessiond availability with pipe */
822 wait_for_sessiond(sock_info
);
826 * Sleep for 5 seconds before retrying after a
827 * sequence of failure / wait / failure. This
828 * deals with a killed or broken session daemon.
833 prev_connect_failed
= 0;
837 if (lttng_ust_comm_should_quit
) {
841 if (sock_info
->socket
!= -1) {
842 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
844 ERR("Error closing %s apps socket", sock_info
->name
);
846 sock_info
->socket
= -1;
850 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
852 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
853 prev_connect_failed
= 1;
855 * If we cannot find the sessiond daemon, don't delay
856 * constructor execution.
858 ret
= handle_register_done(sock_info
);
864 sock_info
->socket
= sock
= ret
;
867 * Create only one root handle per listener thread for the whole
868 * process lifetime, so we ensure we get ID which is statically
869 * assigned to the root handle.
871 if (sock_info
->root_handle
== -1) {
872 ret
= lttng_abi_create_root_handle();
874 ERR("Error creating root handle");
877 sock_info
->root_handle
= ret
;
880 ret
= register_app_to_sessiond(sock
);
882 ERR("Error registering to %s apps socket", sock_info
->name
);
883 prev_connect_failed
= 1;
885 * If we cannot register to the sessiond daemon, don't
886 * delay constructor execution.
888 ret
= handle_register_done(sock_info
);
897 struct ustcomm_ust_msg lum
;
899 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
901 case 0: /* orderly shutdown */
902 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
904 if (lttng_ust_comm_should_quit
) {
908 * Either sessiond has shutdown or refused us by closing the socket.
909 * In either case, we don't want to delay construction execution,
910 * and we need to wait before retry.
912 prev_connect_failed
= 1;
914 * If we cannot register to the sessiond daemon, don't
915 * delay constructor execution.
917 ret
= handle_register_done(sock_info
);
922 print_cmd(lum
.cmd
, lum
.handle
);
923 ret
= handle_message(sock_info
, sock
, &lum
);
925 ERR("Error handling message for %s socket", sock_info
->name
);
930 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
932 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
934 if (len
== -ECONNRESET
) {
935 DBG("%s remote end closed connection", sock_info
->name
);
944 if (lttng_ust_comm_should_quit
) {
947 /* Cleanup socket handles before trying to reconnect */
948 lttng_ust_objd_table_owner_cleanup(sock_info
);
950 goto restart
; /* try to reconnect */
953 sock_info
->thread_active
= 0;
959 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
962 int get_timeout(struct timespec
*constructor_timeout
)
964 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
968 str_delay
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
970 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
973 switch (constructor_delay_ms
) {
974 case -1:/* fall-through */
976 return constructor_delay_ms
;
982 * If we are unable to find the current time, don't wait.
984 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
988 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
989 constructor_timeout
->tv_nsec
+=
990 (constructor_delay_ms
% 1000UL) * 1000000UL;
991 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
992 constructor_timeout
->tv_sec
++;
993 constructor_timeout
->tv_nsec
-= 1000000000UL;
999 * sessiond monitoring thread: monitor presence of global and per-user
1000 * sessiond by polling the application common named pipe.
1002 void __attribute__((constructor
)) lttng_ust_init(void)
1004 struct timespec constructor_timeout
;
1005 sigset_t sig_all_blocked
, orig_parent_mask
;
1006 pthread_attr_t thread_attr
;
1010 if (uatomic_xchg(&initialized
, 1) == 1)
1014 * Fixup interdependency between TLS fixup mutex (which happens
1015 * to be the dynamic linker mutex) and ust_lock, taken within
1018 lttng_fixup_ringbuffer_tls();
1019 lttng_fixup_vtid_tls();
1020 lttng_fixup_nest_count_tls();
1021 lttng_fixup_procname_tls();
1024 * We want precise control over the order in which we construct
1025 * our sub-libraries vs starting to receive commands from
1026 * sessiond (otherwise leading to errors when trying to create
1027 * sessiond before the init functions are completed).
1031 lttng_ring_buffer_metadata_client_init();
1032 lttng_ring_buffer_client_overwrite_init();
1033 lttng_ring_buffer_client_discard_init();
1035 timeout_mode
= get_timeout(&constructor_timeout
);
1037 ret
= sem_init(&constructor_wait
, 0, 0);
1040 ret
= setup_local_apps();
1042 DBG("local apps setup returned %d", ret
);
1045 /* A new thread created by pthread_create inherits the signal mask
1046 * from the parent. To avoid any signal being received by the
1047 * listener thread, we block all signals temporarily in the parent,
1048 * while we create the listener thread.
1050 sigfillset(&sig_all_blocked
);
1051 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1053 ERR("pthread_sigmask: %s", strerror(ret
));
1056 ret
= pthread_attr_init(&thread_attr
);
1058 ERR("pthread_attr_init: %s", strerror(ret
));
1060 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1062 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1066 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1067 ust_listener_thread
, &global_apps
);
1069 ERR("pthread_create global: %s", strerror(ret
));
1071 global_apps
.thread_active
= 1;
1074 if (local_apps
.allowed
) {
1076 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1077 ust_listener_thread
, &local_apps
);
1079 ERR("pthread_create local: %s", strerror(ret
));
1081 local_apps
.thread_active
= 1;
1084 handle_register_done(&local_apps
);
1086 ret
= pthread_attr_destroy(&thread_attr
);
1088 ERR("pthread_attr_destroy: %s", strerror(ret
));
1091 /* Restore original signal mask in parent */
1092 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1094 ERR("pthread_sigmask: %s", strerror(ret
));
1097 switch (timeout_mode
) {
1098 case 1: /* timeout wait */
1100 ret
= sem_timedwait(&constructor_wait
,
1101 &constructor_timeout
);
1102 } while (ret
< 0 && errno
== EINTR
);
1103 if (ret
< 0 && errno
== ETIMEDOUT
) {
1104 ERR("Timed out waiting for lttng-sessiond");
1109 case -1:/* wait forever */
1111 ret
= sem_wait(&constructor_wait
);
1112 } while (ret
< 0 && errno
== EINTR
);
1115 case 0: /* no timeout */
1121 void lttng_ust_cleanup(int exiting
)
1123 cleanup_sock_info(&global_apps
, exiting
);
1124 if (local_apps
.allowed
) {
1125 cleanup_sock_info(&local_apps
, exiting
);
1128 * The teardown in this function all affect data structures
1129 * accessed under the UST lock by the listener thread. This
1130 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1131 * that none of these threads are accessing this data at this
1134 lttng_ust_abi_exit();
1135 lttng_ust_events_exit();
1136 lttng_ring_buffer_client_discard_exit();
1137 lttng_ring_buffer_client_overwrite_exit();
1138 lttng_ring_buffer_metadata_client_exit();
1141 /* Reinitialize values for fork */
1143 lttng_ust_comm_should_quit
= 0;
1148 void __attribute__((destructor
)) lttng_ust_exit(void)
1153 * Using pthread_cancel here because:
1154 * A) we don't want to hang application teardown.
1155 * B) the thread is not allocating any resource.
1159 * Require the communication thread to quit. Synchronize with
1160 * mutexes to ensure it is not in a mutex critical section when
1161 * pthread_cancel is later called.
1164 lttng_ust_comm_should_quit
= 1;
1166 /* cancel threads */
1167 if (global_apps
.thread_active
) {
1168 ret
= pthread_cancel(global_apps
.ust_listener
);
1170 ERR("Error cancelling global ust listener thread: %s",
1173 global_apps
.thread_active
= 0;
1176 if (local_apps
.thread_active
) {
1177 ret
= pthread_cancel(local_apps
.ust_listener
);
1179 ERR("Error cancelling local ust listener thread: %s",
1182 local_apps
.thread_active
= 0;
1188 * Do NOT join threads: use of sys_futex makes it impossible to
1189 * join the threads without using async-cancel, but async-cancel
1190 * is delivered by a signal, which could hit the target thread
1191 * anywhere in its code path, including while the ust_lock() is
1192 * held, causing a deadlock for the other thread. Let the OS
1193 * cleanup the threads if there are stalled in a syscall.
1195 lttng_ust_cleanup(1);
1199 * We exclude the worker threads across fork and clone (except
1200 * CLONE_VM), because these system calls only keep the forking thread
1201 * running in the child. Therefore, we don't want to call fork or clone
1202 * in the middle of an tracepoint or ust tracing state modification.
1203 * Holding this mutex protects these structures across fork and clone.
1205 void ust_before_fork(sigset_t
*save_sigset
)
1208 * Disable signals. This is to avoid that the child intervenes
1209 * before it is properly setup for tracing. It is safer to
1210 * disable all signals, because then we know we are not breaking
1211 * anything by restoring the original mask.
1216 if (lttng_ust_nest_count
)
1218 /* Disable signals */
1219 sigfillset(&all_sigs
);
1220 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1222 PERROR("sigprocmask");
1225 rcu_bp_before_fork();
1228 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1232 DBG("process %d", getpid());
1234 /* Restore signals */
1235 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1237 PERROR("sigprocmask");
1241 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1243 if (lttng_ust_nest_count
)
1245 DBG("process %d", getpid());
1246 rcu_bp_after_fork_parent();
1247 /* Release mutexes and reenable signals */
1248 ust_after_fork_common(restore_sigset
);
1252 * After fork, in the child, we need to cleanup all the leftover state,
1253 * except the worker thread which already magically disappeared thanks
1254 * to the weird Linux fork semantics. After tyding up, we call
1255 * lttng_ust_init() again to start over as a new PID.
1257 * This is meant for forks() that have tracing in the child between the
1258 * fork and following exec call (if there is any).
1260 void ust_after_fork_child(sigset_t
*restore_sigset
)
1262 if (lttng_ust_nest_count
)
1264 DBG("process %d", getpid());
1265 /* Release urcu mutexes */
1266 rcu_bp_after_fork_child();
1267 lttng_ust_cleanup(0);
1268 lttng_context_vtid_reset();
1269 /* Release mutexes and reenable signals */
1270 ust_after_fork_common(restore_sigset
);