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>
46 #include <urcu/tls-compat.h>
48 #include <usterr-signal-safe.h>
50 #include "tracepoint-internal.h"
51 #include "lttng-tracer-core.h"
53 #include "../libringbuffer/tlsfixup.h"
56 * Has lttng ust comm constructor been called ?
58 static int initialized
;
61 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
62 * Held when handling a command, also held by fork() to deal with
63 * removal of threads, and by exit path.
66 /* Should the ust comm thread quit ? */
67 static int lttng_ust_comm_should_quit
;
70 * Wait for either of these before continuing to the main
72 * - the register_done message from sessiond daemon
73 * (will let the sessiond daemon enable sessions before main
75 * - sessiond daemon is not reachable.
76 * - timeout (ensuring applications are resilient to session
79 static sem_t constructor_wait
;
81 * Doing this for both the global and local sessiond.
83 static int sem_count
= { 2 };
86 * Counting nesting within lttng-ust. Used to ensure that calling fork()
87 * from liblttng-ust does not execute the pre/post fork handlers.
89 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
92 * Info about socket and associated listener thread.
96 pthread_t ust_listener
; /* listener thread */
98 int constructor_sem_posted
;
103 char sock_path
[PATH_MAX
];
107 char wait_shm_path
[PATH_MAX
];
111 /* Socket from app (connect) to session daemon (listen) for communication */
112 struct sock_info global_apps
= {
120 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
124 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
127 /* TODO: allow global_apps_sock_path override */
129 struct sock_info local_apps
= {
133 .allowed
= 0, /* Check setuid bit first */
140 static int wait_poll_fallback
;
142 static const char *cmd_name_mapping
[] = {
143 [ LTTNG_UST_RELEASE
] = "Release",
144 [ LTTNG_UST_SESSION
] = "Create Session",
145 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
147 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
148 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
149 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
150 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
152 /* Session FD commands */
153 [ LTTNG_UST_CHANNEL
] = "Create Channel",
154 [ LTTNG_UST_SESSION_START
] = "Start Session",
155 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
157 /* Channel FD commands */
158 [ LTTNG_UST_STREAM
] = "Create Stream",
159 [ LTTNG_UST_EVENT
] = "Create Event",
161 /* Event and Channel FD commands */
162 [ LTTNG_UST_CONTEXT
] = "Create Context",
163 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
165 /* Event, Channel and Session commands */
166 [ LTTNG_UST_ENABLE
] = "Enable",
167 [ LTTNG_UST_DISABLE
] = "Disable",
169 /* Tracepoint list commands */
170 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
171 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
173 /* Event FD commands */
174 [ LTTNG_UST_FILTER
] = "Create Filter",
177 static const char *str_timeout
;
178 static int got_timeout_env
;
180 extern void lttng_ring_buffer_client_overwrite_init(void);
181 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
182 extern void lttng_ring_buffer_client_discard_init(void);
183 extern void lttng_ring_buffer_client_discard_rt_init(void);
184 extern void lttng_ring_buffer_metadata_client_init(void);
185 extern void lttng_ring_buffer_client_overwrite_exit(void);
186 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
187 extern void lttng_ring_buffer_client_discard_exit(void);
188 extern void lttng_ring_buffer_client_discard_rt_exit(void);
189 extern void lttng_ring_buffer_metadata_client_exit(void);
192 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
196 const char *get_lttng_home_dir(void)
200 val
= (const char *) getenv("LTTNG_HOME");
204 return (const char *) getenv("HOME");
208 * Force a read (imply TLS fixup for dlopen) of TLS variables.
211 void lttng_fixup_nest_count_tls(void)
213 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
216 int lttng_get_notify_socket(void *owner
)
218 struct sock_info
*info
= owner
;
220 return info
->notify_socket
;
224 void print_cmd(int cmd
, int handle
)
226 const char *cmd_name
= "Unknown";
228 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
229 && cmd_name_mapping
[cmd
]) {
230 cmd_name
= cmd_name_mapping
[cmd
];
232 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
234 lttng_ust_obj_get_name(handle
), handle
);
238 int setup_local_apps(void)
240 const char *home_dir
;
245 * Disallow per-user tracing for setuid binaries.
247 if (uid
!= geteuid()) {
248 assert(local_apps
.allowed
== 0);
251 home_dir
= get_lttng_home_dir();
253 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
254 assert(local_apps
.allowed
== 0);
257 local_apps
.allowed
= 1;
258 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
260 LTTNG_DEFAULT_HOME_RUNDIR
,
261 LTTNG_UST_SOCK_FILENAME
);
262 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
263 LTTNG_UST_WAIT_FILENAME
,
269 * Get notify_sock timeout, in ms.
270 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
273 long get_timeout(void)
275 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
277 if (!got_timeout_env
) {
278 str_timeout
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
282 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
283 return constructor_delay_ms
;
287 long get_notify_sock_timeout(void)
289 return get_timeout();
293 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
296 int get_constructor_timeout(struct timespec
*constructor_timeout
)
298 long constructor_delay_ms
;
301 constructor_delay_ms
= get_timeout();
303 switch (constructor_delay_ms
) {
304 case -1:/* fall-through */
306 return constructor_delay_ms
;
312 * If we are unable to find the current time, don't wait.
314 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
318 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
319 constructor_timeout
->tv_nsec
+=
320 (constructor_delay_ms
% 1000UL) * 1000000UL;
321 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
322 constructor_timeout
->tv_sec
++;
323 constructor_timeout
->tv_nsec
-= 1000000000UL;
329 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
331 return ustcomm_send_reg_msg(socket
,
334 lttng_alignof(uint8_t) * CHAR_BIT
,
335 lttng_alignof(uint16_t) * CHAR_BIT
,
336 lttng_alignof(uint32_t) * CHAR_BIT
,
337 lttng_alignof(uint64_t) * CHAR_BIT
,
338 lttng_alignof(unsigned long) * CHAR_BIT
);
342 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
346 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
349 DBG("message successfully sent");
352 if (len
== -ECONNRESET
) {
353 DBG("remote end closed connection");
358 DBG("incorrect message size: %zd", len
);
364 int handle_register_done(struct sock_info
*sock_info
)
368 if (sock_info
->constructor_sem_posted
)
370 sock_info
->constructor_sem_posted
= 1;
371 if (uatomic_read(&sem_count
) <= 0) {
374 ret
= uatomic_add_return(&sem_count
, -1);
376 ret
= sem_post(&constructor_wait
);
383 int handle_message(struct sock_info
*sock_info
,
384 int sock
, struct ustcomm_ust_msg
*lum
)
387 const struct lttng_ust_objd_ops
*ops
;
388 struct ustcomm_ust_reply lur
;
394 memset(&lur
, 0, sizeof(lur
));
396 if (lttng_ust_comm_should_quit
) {
397 ret
= -LTTNG_UST_ERR_EXITING
;
401 ops
= objd_ops(lum
->handle
);
408 case LTTNG_UST_REGISTER_DONE
:
409 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
410 ret
= handle_register_done(sock_info
);
414 case LTTNG_UST_RELEASE
:
415 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
418 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
420 case LTTNG_UST_FILTER
:
422 /* Receive filter data */
423 struct lttng_ust_filter_bytecode_node
*bytecode
;
425 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
426 ERR("Filter data size is too large: %u bytes",
427 lum
->u
.filter
.data_size
);
432 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
433 ERR("Filter reloc offset %u is not within data",
434 lum
->u
.filter
.reloc_offset
);
439 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
444 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
445 lum
->u
.filter
.data_size
);
447 case 0: /* orderly shutdown */
452 if (len
== lum
->u
.filter
.data_size
) {
453 DBG("filter data received");
455 } else if (len
< 0) {
456 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
457 if (len
== -ECONNRESET
) {
458 ERR("%s remote end closed connection", sock_info
->name
);
467 DBG("incorrect filter data message size: %zd", len
);
473 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
474 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
475 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
477 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
478 (unsigned long) bytecode
,
483 /* don't free bytecode if everything went fine. */
490 case LTTNG_UST_CHANNEL
:
495 len
= ustcomm_recv_channel_from_sessiond(sock
,
496 &chan_data
, lum
->u
.channel
.len
,
499 case 0: /* orderly shutdown */
503 if (len
== lum
->u
.channel
.len
) {
504 DBG("channel data received");
506 } else if (len
< 0) {
507 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
508 if (len
== -ECONNRESET
) {
509 ERR("%s remote end closed connection", sock_info
->name
);
516 DBG("incorrect channel data message size: %zd", len
);
521 args
.channel
.chan_data
= chan_data
;
522 args
.channel
.wakeup_fd
= wakeup_fd
;
524 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
525 (unsigned long) &lum
->u
,
531 case LTTNG_UST_STREAM
:
533 /* Receive shm_fd, wakeup_fd */
534 ret
= ustcomm_recv_stream_from_sessiond(sock
,
537 &args
.stream
.wakeup_fd
);
542 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
543 (unsigned long) &lum
->u
,
551 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
552 (unsigned long) &lum
->u
,
560 lur
.handle
= lum
->handle
;
564 lur
.ret_code
= LTTNG_UST_OK
;
567 * Use -LTTNG_UST_ERR as wildcard for UST internal
568 * error that are not caused by the transport, except if
569 * we already have a more precise error message to
572 if (ret
> -LTTNG_UST_ERR
) {
573 /* Translate code to UST error. */
576 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
579 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
582 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
585 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
588 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
591 lur
.ret_code
= -LTTNG_UST_ERR
;
600 case LTTNG_UST_TRACER_VERSION
:
601 lur
.u
.version
= lum
->u
.version
;
603 case LTTNG_UST_TRACEPOINT_LIST_GET
:
604 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
608 DBG("Return value: %d", lur
.ret_val
);
609 ret
= send_reply(sock
, &lur
);
611 DBG("error sending reply");
616 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
619 if (lur
.ret_code
== LTTNG_UST_OK
) {
621 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
622 len
= ustcomm_send_unix_sock(sock
,
623 &args
.field_list
.entry
,
624 sizeof(args
.field_list
.entry
));
629 if (len
!= sizeof(args
.field_list
.entry
)) {
642 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
646 if (sock_info
->root_handle
!= -1) {
647 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
649 ERR("Error unref root handle");
651 sock_info
->root_handle
= -1;
653 sock_info
->constructor_sem_posted
= 0;
656 * wait_shm_mmap, socket and notify socket are used by listener
657 * threads outside of the ust lock, so we cannot tear them down
658 * ourselves, because we cannot join on these threads. Leave
659 * responsibility of cleaning up these resources to the OS
665 if (sock_info
->socket
!= -1) {
666 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
668 ERR("Error closing ust cmd socket");
670 sock_info
->socket
= -1;
672 if (sock_info
->notify_socket
!= -1) {
673 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
675 ERR("Error closing ust notify socket");
677 sock_info
->notify_socket
= -1;
679 if (sock_info
->wait_shm_mmap
) {
680 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
682 ERR("Error unmapping wait shm");
684 sock_info
->wait_shm_mmap
= NULL
;
689 * Using fork to set umask in the child process (not multi-thread safe).
690 * We deal with the shm_open vs ftruncate race (happening when the
691 * sessiond owns the shm and does not let everybody modify it, to ensure
692 * safety against shm_unlink) by simply letting the mmap fail and
693 * retrying after a few seconds.
694 * For global shm, everybody has rw access to it until the sessiond
698 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
700 int wait_shm_fd
, ret
;
704 * Try to open read-only.
706 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
707 if (wait_shm_fd
>= 0) {
709 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
711 * Real-only open did not work, and it's not because the
712 * entry was not present. It's a failure that prohibits
715 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
719 * If the open failed because the file did not exist, try
720 * creating it ourself.
722 URCU_TLS(lttng_ust_nest_count
)++;
724 URCU_TLS(lttng_ust_nest_count
)--;
729 * Parent: wait for child to return, in which case the
730 * shared memory map will have been created.
733 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
738 * Try to open read-only again after creation.
740 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
741 if (wait_shm_fd
< 0) {
743 * Real-only open did not work. It's a failure
744 * that prohibits using shm.
746 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
750 } else if (pid
== 0) {
754 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
755 if (sock_info
->global
)
756 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
758 * We're alone in a child process, so we can modify the
759 * process-wide umask.
763 * Try creating shm (or get rw access).
764 * We don't do an exclusive open, because we allow other
765 * processes to create+ftruncate it concurrently.
767 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
768 O_RDWR
| O_CREAT
, create_mode
);
769 if (wait_shm_fd
>= 0) {
770 ret
= ftruncate(wait_shm_fd
, mmap_size
);
778 * For local shm, we need to have rw access to accept
779 * opening it: this means the local sessiond will be
780 * able to wake us up. For global shm, we open it even
781 * if rw access is not granted, because the root.root
782 * sessiond will be able to override all rights and wake
785 if (!sock_info
->global
&& errno
!= EACCES
) {
786 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
790 * The shm exists, but we cannot open it RW. Report
798 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
802 * Ensure that our user is the owner of the shm file for
803 * local shm. If we do not own the file, it means our
804 * sessiond will not have access to wake us up (there is
805 * probably a rogue process trying to fake our
806 * sessiond). Fallback to polling method in this case.
808 ret
= fstat(wait_shm_fd
, &statbuf
);
813 if (statbuf
.st_uid
!= getuid())
819 ret
= close(wait_shm_fd
);
821 PERROR("Error closing fd");
827 char *get_map_shm(struct sock_info
*sock_info
)
829 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
830 int wait_shm_fd
, ret
;
833 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
834 if (wait_shm_fd
< 0) {
837 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
838 MAP_SHARED
, wait_shm_fd
, 0);
839 /* close shm fd immediately after taking the mmap reference */
840 ret
= close(wait_shm_fd
);
842 PERROR("Error closing fd");
844 if (wait_shm_mmap
== MAP_FAILED
) {
845 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
848 return wait_shm_mmap
;
855 void wait_for_sessiond(struct sock_info
*sock_info
)
860 if (lttng_ust_comm_should_quit
) {
863 if (wait_poll_fallback
) {
866 if (!sock_info
->wait_shm_mmap
) {
867 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
868 if (!sock_info
->wait_shm_mmap
)
873 DBG("Waiting for %s apps sessiond", sock_info
->name
);
874 /* Wait for futex wakeup */
875 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
876 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
877 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
879 if (errno
== EFAULT
) {
880 wait_poll_fallback
= 1;
882 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
883 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
884 "Please upgrade your kernel "
885 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
886 "mainline). LTTng-UST will use polling mode fallback.");
904 * This thread does not allocate any resource, except within
905 * handle_message, within mutex protection. This mutex protects against
907 * The other moment it allocates resources is at socket connection, which
908 * is also protected by the mutex.
911 void *ust_listener_thread(void *arg
)
913 struct sock_info
*sock_info
= arg
;
914 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
919 /* Restart trying to connect to the session daemon */
921 if (prev_connect_failed
) {
922 /* Wait for sessiond availability with pipe */
923 wait_for_sessiond(sock_info
);
927 * Sleep for 5 seconds before retrying after a
928 * sequence of failure / wait / failure. This
929 * deals with a killed or broken session daemon.
934 prev_connect_failed
= 0;
937 if (sock_info
->socket
!= -1) {
938 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
940 ERR("Error closing %s ust cmd socket",
943 sock_info
->socket
= -1;
945 if (sock_info
->notify_socket
!= -1) {
946 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
948 ERR("Error closing %s ust notify socket",
951 sock_info
->notify_socket
= -1;
955 for (i
= 0; i
< 2; i
++) {
956 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
958 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
959 prev_connect_failed
= 1;
963 if (lttng_ust_comm_should_quit
) {
968 * If we cannot find the sessiond daemon, don't delay
969 * constructor execution.
971 ret
= handle_register_done(sock_info
);
979 sock_info
->socket
= open_sock
[0];
980 sock_info
->notify_socket
= open_sock
[1];
982 timeout
= get_notify_sock_timeout();
985 * Give at least 10ms to sessiond to reply to
990 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
993 WARN("Error setting socket receive timeout");
995 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
998 WARN("Error setting socket send timeout");
1000 } else if (timeout
< -1) {
1001 WARN("Unsuppoorted timeout value %ld", timeout
);
1006 if (lttng_ust_comm_should_quit
) {
1011 * Create only one root handle per listener thread for the whole
1012 * process lifetime, so we ensure we get ID which is statically
1013 * assigned to the root handle.
1015 if (sock_info
->root_handle
== -1) {
1016 ret
= lttng_abi_create_root_handle();
1018 ERR("Error creating root handle");
1021 sock_info
->root_handle
= ret
;
1024 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1026 ERR("Error registering to %s ust cmd socket",
1028 prev_connect_failed
= 1;
1030 * If we cannot register to the sessiond daemon, don't
1031 * delay constructor execution.
1033 ret
= handle_register_done(sock_info
);
1038 ret
= register_to_sessiond(sock_info
->notify_socket
,
1039 USTCTL_SOCKET_NOTIFY
);
1041 ERR("Error registering to %s ust notify socket",
1043 prev_connect_failed
= 1;
1045 * If we cannot register to the sessiond daemon, don't
1046 * delay constructor execution.
1048 ret
= handle_register_done(sock_info
);
1053 sock
= sock_info
->socket
;
1059 struct ustcomm_ust_msg lum
;
1061 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1063 case 0: /* orderly shutdown */
1064 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1066 if (lttng_ust_comm_should_quit
) {
1070 * Either sessiond has shutdown or refused us by closing the socket.
1071 * In either case, we don't want to delay construction execution,
1072 * and we need to wait before retry.
1074 prev_connect_failed
= 1;
1076 * If we cannot register to the sessiond daemon, don't
1077 * delay constructor execution.
1079 ret
= handle_register_done(sock_info
);
1084 print_cmd(lum
.cmd
, lum
.handle
);
1085 ret
= handle_message(sock_info
, sock
, &lum
);
1087 ERR("Error handling message for %s socket", sock_info
->name
);
1092 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1094 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1096 if (len
== -ECONNRESET
) {
1097 DBG("%s remote end closed connection", sock_info
->name
);
1106 if (lttng_ust_comm_should_quit
) {
1109 /* Cleanup socket handles before trying to reconnect */
1110 lttng_ust_objd_table_owner_cleanup(sock_info
);
1112 goto restart
; /* try to reconnect */
1115 sock_info
->thread_active
= 0;
1121 * sessiond monitoring thread: monitor presence of global and per-user
1122 * sessiond by polling the application common named pipe.
1124 void __attribute__((constructor
)) lttng_ust_init(void)
1126 struct timespec constructor_timeout
;
1127 sigset_t sig_all_blocked
, orig_parent_mask
;
1128 pthread_attr_t thread_attr
;
1132 if (uatomic_xchg(&initialized
, 1) == 1)
1136 * Fixup interdependency between TLS fixup mutex (which happens
1137 * to be the dynamic linker mutex) and ust_lock, taken within
1140 lttng_fixup_ringbuffer_tls();
1141 lttng_fixup_vtid_tls();
1142 lttng_fixup_nest_count_tls();
1143 lttng_fixup_procname_tls();
1146 * We want precise control over the order in which we construct
1147 * our sub-libraries vs starting to receive commands from
1148 * sessiond (otherwise leading to errors when trying to create
1149 * sessiond before the init functions are completed).
1153 lttng_ring_buffer_metadata_client_init();
1154 lttng_ring_buffer_client_overwrite_init();
1155 lttng_ring_buffer_client_overwrite_rt_init();
1156 lttng_ring_buffer_client_discard_init();
1157 lttng_ring_buffer_client_discard_rt_init();
1158 lttng_context_init();
1160 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1162 ret
= sem_init(&constructor_wait
, 0, 0);
1165 ret
= setup_local_apps();
1167 DBG("local apps setup returned %d", ret
);
1170 /* A new thread created by pthread_create inherits the signal mask
1171 * from the parent. To avoid any signal being received by the
1172 * listener thread, we block all signals temporarily in the parent,
1173 * while we create the listener thread.
1175 sigfillset(&sig_all_blocked
);
1176 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1178 ERR("pthread_sigmask: %s", strerror(ret
));
1181 ret
= pthread_attr_init(&thread_attr
);
1183 ERR("pthread_attr_init: %s", strerror(ret
));
1185 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1187 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1191 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1192 ust_listener_thread
, &global_apps
);
1194 ERR("pthread_create global: %s", strerror(ret
));
1196 global_apps
.thread_active
= 1;
1199 if (local_apps
.allowed
) {
1201 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1202 ust_listener_thread
, &local_apps
);
1204 ERR("pthread_create local: %s", strerror(ret
));
1206 local_apps
.thread_active
= 1;
1209 handle_register_done(&local_apps
);
1211 ret
= pthread_attr_destroy(&thread_attr
);
1213 ERR("pthread_attr_destroy: %s", strerror(ret
));
1216 /* Restore original signal mask in parent */
1217 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1219 ERR("pthread_sigmask: %s", strerror(ret
));
1222 switch (timeout_mode
) {
1223 case 1: /* timeout wait */
1225 ret
= sem_timedwait(&constructor_wait
,
1226 &constructor_timeout
);
1227 } while (ret
< 0 && errno
== EINTR
);
1228 if (ret
< 0 && errno
== ETIMEDOUT
) {
1229 ERR("Timed out waiting for lttng-sessiond");
1234 case -1:/* wait forever */
1236 ret
= sem_wait(&constructor_wait
);
1237 } while (ret
< 0 && errno
== EINTR
);
1240 case 0: /* no timeout */
1246 void lttng_ust_cleanup(int exiting
)
1248 cleanup_sock_info(&global_apps
, exiting
);
1249 if (local_apps
.allowed
) {
1250 cleanup_sock_info(&local_apps
, exiting
);
1253 * The teardown in this function all affect data structures
1254 * accessed under the UST lock by the listener thread. This
1255 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1256 * that none of these threads are accessing this data at this
1259 lttng_ust_abi_exit();
1260 lttng_ust_events_exit();
1261 lttng_context_exit();
1262 lttng_ring_buffer_client_discard_rt_exit();
1263 lttng_ring_buffer_client_discard_exit();
1264 lttng_ring_buffer_client_overwrite_rt_exit();
1265 lttng_ring_buffer_client_overwrite_exit();
1266 lttng_ring_buffer_metadata_client_exit();
1269 /* Reinitialize values for fork */
1271 lttng_ust_comm_should_quit
= 0;
1276 void __attribute__((destructor
)) lttng_ust_exit(void)
1281 * Using pthread_cancel here because:
1282 * A) we don't want to hang application teardown.
1283 * B) the thread is not allocating any resource.
1287 * Require the communication thread to quit. Synchronize with
1288 * mutexes to ensure it is not in a mutex critical section when
1289 * pthread_cancel is later called.
1292 lttng_ust_comm_should_quit
= 1;
1294 /* cancel threads */
1295 if (global_apps
.thread_active
) {
1296 ret
= pthread_cancel(global_apps
.ust_listener
);
1298 ERR("Error cancelling global ust listener thread: %s",
1301 global_apps
.thread_active
= 0;
1304 if (local_apps
.thread_active
) {
1305 ret
= pthread_cancel(local_apps
.ust_listener
);
1307 ERR("Error cancelling local ust listener thread: %s",
1310 local_apps
.thread_active
= 0;
1316 * Do NOT join threads: use of sys_futex makes it impossible to
1317 * join the threads without using async-cancel, but async-cancel
1318 * is delivered by a signal, which could hit the target thread
1319 * anywhere in its code path, including while the ust_lock() is
1320 * held, causing a deadlock for the other thread. Let the OS
1321 * cleanup the threads if there are stalled in a syscall.
1323 lttng_ust_cleanup(1);
1327 * We exclude the worker threads across fork and clone (except
1328 * CLONE_VM), because these system calls only keep the forking thread
1329 * running in the child. Therefore, we don't want to call fork or clone
1330 * in the middle of an tracepoint or ust tracing state modification.
1331 * Holding this mutex protects these structures across fork and clone.
1333 void ust_before_fork(sigset_t
*save_sigset
)
1336 * Disable signals. This is to avoid that the child intervenes
1337 * before it is properly setup for tracing. It is safer to
1338 * disable all signals, because then we know we are not breaking
1339 * anything by restoring the original mask.
1344 if (URCU_TLS(lttng_ust_nest_count
))
1346 /* Disable signals */
1347 sigfillset(&all_sigs
);
1348 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1350 PERROR("sigprocmask");
1353 rcu_bp_before_fork();
1356 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1360 DBG("process %d", getpid());
1362 /* Restore signals */
1363 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1365 PERROR("sigprocmask");
1369 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1371 if (URCU_TLS(lttng_ust_nest_count
))
1373 DBG("process %d", getpid());
1374 rcu_bp_after_fork_parent();
1375 /* Release mutexes and reenable signals */
1376 ust_after_fork_common(restore_sigset
);
1380 * After fork, in the child, we need to cleanup all the leftover state,
1381 * except the worker thread which already magically disappeared thanks
1382 * to the weird Linux fork semantics. After tyding up, we call
1383 * lttng_ust_init() again to start over as a new PID.
1385 * This is meant for forks() that have tracing in the child between the
1386 * fork and following exec call (if there is any).
1388 void ust_after_fork_child(sigset_t
*restore_sigset
)
1390 if (URCU_TLS(lttng_ust_nest_count
))
1392 DBG("process %d", getpid());
1393 /* Release urcu mutexes */
1394 rcu_bp_after_fork_child();
1395 lttng_ust_cleanup(0);
1396 lttng_context_vtid_reset();
1397 /* Release mutexes and reenable signals */
1398 ust_after_fork_common(restore_sigset
);