2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
11 #include <sys/types.h>
12 #include <sys/socket.h>
15 #include <sys/types.h>
22 #include <semaphore.h>
27 #include <urcu/uatomic.h>
29 #include <urcu/compiler.h>
30 #include <lttng/urcu/urcu-ust.h>
32 #include <lttng/ust-events.h>
33 #include <lttng/ust-abi.h>
34 #include <lttng/ust.h>
35 #include <lttng/ust-error.h>
36 #include <lttng/ust-ctl.h>
37 #include <urcu/tls-compat.h>
40 #include <usterr-signal-safe.h>
41 #include <ust-helper.h>
42 #include "tracepoint-internal.h"
43 #include "lttng-tracer-core.h"
45 #include "../libringbuffer/rb-init.h"
46 #include "lttng-ust-statedump.h"
48 #include "../libringbuffer/getcpu.h"
50 #include "ust-events-internal.h"
51 #include "context-internal.h"
52 #include "ust-compat.h"
54 /* Concatenate lttng ust shared library name with its major version number. */
55 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
58 * Has lttng ust comm constructor been called ?
60 static int initialized
;
63 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
64 * Held when handling a command, also held by fork() to deal with
65 * removal of threads, and by exit path.
67 * The UST lock is the centralized mutex across UST tracing control and
70 * ust_exit_mutex must never nest in ust_mutex.
72 * ust_fork_mutex must never nest in ust_mutex.
74 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
75 * counter lazy initialization called by events within the statedump,
76 * which traces while the ust_mutex is held.
78 * ust_lock nests within the dynamic loader lock (within glibc) because
79 * it is taken within the library constructor.
81 * The ust fd tracker lock nests within the ust_mutex.
83 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
85 /* Allow nesting the ust_mutex within the same thread. */
86 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
89 * ust_exit_mutex protects thread_active variable wrt thread exit. It
90 * cannot be done by ust_mutex because pthread_cancel(), which takes an
91 * internal libc lock, cannot nest within ust_mutex.
93 * It never nests within a ust_mutex.
95 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
98 * ust_fork_mutex protects base address statedump tracing against forks. It
99 * prevents the dynamic loader lock to be taken (by base address statedump
100 * tracing) while a fork is happening, thus preventing deadlock issues with
101 * the dynamic loader lock.
103 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 /* Should the ust comm thread quit ? */
106 static int lttng_ust_comm_should_quit
;
109 * This variable can be tested by applications to check whether
110 * lttng-ust is loaded. They simply have to define their own
111 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
112 * library constructor.
114 int lttng_ust_loaded
__attribute__((weak
));
117 * Return 0 on success, -1 if should quit.
118 * The lock is taken in both cases.
123 sigset_t sig_all_blocked
, orig_mask
;
126 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
128 ERR("pthread_setcancelstate: %s", strerror(ret
));
130 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
131 ERR("pthread_setcancelstate: unexpected oldstate");
133 sigfillset(&sig_all_blocked
);
134 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
136 ERR("pthread_sigmask: %s", strerror(ret
));
138 if (!URCU_TLS(ust_mutex_nest
)++)
139 pthread_mutex_lock(&ust_mutex
);
140 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
142 ERR("pthread_sigmask: %s", strerror(ret
));
144 if (lttng_ust_comm_should_quit
) {
152 * ust_lock_nocheck() can be used in constructors/destructors, because
153 * they are already nested within the dynamic loader lock, and therefore
154 * have exclusive access against execution of liblttng-ust destructor.
157 void ust_lock_nocheck(void)
159 sigset_t sig_all_blocked
, orig_mask
;
162 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
164 ERR("pthread_setcancelstate: %s", strerror(ret
));
166 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
167 ERR("pthread_setcancelstate: unexpected oldstate");
169 sigfillset(&sig_all_blocked
);
170 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
172 ERR("pthread_sigmask: %s", strerror(ret
));
174 if (!URCU_TLS(ust_mutex_nest
)++)
175 pthread_mutex_lock(&ust_mutex
);
176 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
178 ERR("pthread_sigmask: %s", strerror(ret
));
185 void ust_unlock(void)
187 sigset_t sig_all_blocked
, orig_mask
;
190 sigfillset(&sig_all_blocked
);
191 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
193 ERR("pthread_sigmask: %s", strerror(ret
));
195 if (!--URCU_TLS(ust_mutex_nest
))
196 pthread_mutex_unlock(&ust_mutex
);
197 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
199 ERR("pthread_sigmask: %s", strerror(ret
));
201 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
203 ERR("pthread_setcancelstate: %s", strerror(ret
));
205 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
206 ERR("pthread_setcancelstate: unexpected oldstate");
211 * Wait for either of these before continuing to the main
213 * - the register_done message from sessiond daemon
214 * (will let the sessiond daemon enable sessions before main
216 * - sessiond daemon is not reachable.
217 * - timeout (ensuring applications are resilient to session
220 static sem_t constructor_wait
;
222 * Doing this for both the global and local sessiond.
225 sem_count_initial_value
= 4,
228 static int sem_count
= sem_count_initial_value
;
231 * Counting nesting within lttng-ust. Used to ensure that calling fork()
232 * from liblttng-ust does not execute the pre/post fork handlers.
234 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
237 * Info about socket and associated listener thread.
241 pthread_t ust_listener
; /* listener thread */
243 int registration_done
;
248 char sock_path
[PATH_MAX
];
252 char wait_shm_path
[PATH_MAX
];
254 /* Keep track of lazy state dump not performed yet. */
255 int statedump_pending
;
256 int initial_statedump_done
;
257 /* Keep procname for statedump */
258 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
261 /* Socket from app (connect) to session daemon (listen) for communication */
262 struct sock_info global_apps
= {
267 .registration_done
= 0,
271 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
275 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
277 .statedump_pending
= 0,
278 .initial_statedump_done
= 0,
282 /* TODO: allow global_apps_sock_path override */
284 struct sock_info local_apps
= {
288 .registration_done
= 0,
289 .allowed
= 0, /* Check setuid bit first */
295 .statedump_pending
= 0,
296 .initial_statedump_done
= 0,
300 static int wait_poll_fallback
;
302 static const char *cmd_name_mapping
[] = {
303 [ LTTNG_UST_RELEASE
] = "Release",
304 [ LTTNG_UST_SESSION
] = "Create Session",
305 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
307 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
308 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
309 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
310 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
312 [ LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
314 /* Session FD commands */
315 [ LTTNG_UST_CHANNEL
] = "Create Channel",
316 [ LTTNG_UST_SESSION_START
] = "Start Session",
317 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
319 /* Channel FD commands */
320 [ LTTNG_UST_STREAM
] = "Create Stream",
321 [ LTTNG_UST_EVENT
] = "Create Event",
323 /* Event and Channel FD commands */
324 [ LTTNG_UST_CONTEXT
] = "Create Context",
325 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
327 /* Event, Channel and Session commands */
328 [ LTTNG_UST_ENABLE
] = "Enable",
329 [ LTTNG_UST_DISABLE
] = "Disable",
331 /* Tracepoint list commands */
332 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
333 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
335 /* Event FD commands */
336 [ LTTNG_UST_FILTER
] = "Create Filter",
337 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
339 /* Event notifier group commands */
340 [ LTTNG_UST_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
342 /* Session and event notifier group commands */
343 [ LTTNG_UST_COUNTER
] = "Create Counter",
345 /* Counter commands */
346 [ LTTNG_UST_COUNTER_GLOBAL
] = "Create Counter Global",
347 [ LTTNG_UST_COUNTER_CPU
] = "Create Counter CPU",
350 static const char *str_timeout
;
351 static int got_timeout_env
;
353 extern void lttng_ring_buffer_client_overwrite_init(void);
354 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
355 extern void lttng_ring_buffer_client_discard_init(void);
356 extern void lttng_ring_buffer_client_discard_rt_init(void);
357 extern void lttng_ring_buffer_metadata_client_init(void);
358 extern void lttng_ring_buffer_client_overwrite_exit(void);
359 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
360 extern void lttng_ring_buffer_client_discard_exit(void);
361 extern void lttng_ring_buffer_client_discard_rt_exit(void);
362 extern void lttng_ring_buffer_metadata_client_exit(void);
364 extern void lttng_counter_client_percpu_32_modular_init(void);
366 extern void lttng_counter_client_percpu_32_modular_exit(void);
368 extern void lttng_counter_client_percpu_64_modular_init(void);
370 extern void lttng_counter_client_percpu_64_modular_exit(void);
372 static char *get_map_shm(struct sock_info
*sock_info
);
374 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
377 size_t copied
= 0, to_copy
= len
;
380 ret
= read(fd
, buf
+ copied
, to_copy
);
385 } while ((ret
> 0 && to_copy
> 0)
386 || (ret
< 0 && errno
== EINTR
));
393 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
397 const char *get_lttng_home_dir(void)
401 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
405 return (const char *) lttng_ust_getenv("HOME");
409 * Force a read (imply TLS fixup for dlopen) of TLS variables.
412 void lttng_fixup_nest_count_tls(void)
414 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
418 void lttng_fixup_ust_mutex_nest_tls(void)
420 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
424 * Fixup lttng-ust urcu TLS.
427 void lttng_fixup_lttng_ust_urcu_tls(void)
429 (void) lttng_ust_urcu_read_ongoing();
432 void lttng_ust_fixup_tls(void)
434 lttng_fixup_lttng_ust_urcu_tls();
435 lttng_fixup_ringbuffer_tls();
436 lttng_fixup_vtid_tls();
437 lttng_fixup_nest_count_tls();
438 lttng_fixup_procname_tls();
439 lttng_fixup_ust_mutex_nest_tls();
440 lttng_ust_fixup_perf_counter_tls();
441 lttng_ust_fixup_fd_tracker_tls();
442 lttng_fixup_cgroup_ns_tls();
443 lttng_fixup_ipc_ns_tls();
444 lttng_fixup_net_ns_tls();
445 lttng_fixup_time_ns_tls();
446 lttng_fixup_uts_ns_tls();
449 int lttng_get_notify_socket(void *owner
)
451 struct sock_info
*info
= owner
;
453 return info
->notify_socket
;
458 char* lttng_ust_sockinfo_get_procname(void *owner
)
460 struct sock_info
*info
= owner
;
462 return info
->procname
;
466 void print_cmd(int cmd
, int handle
)
468 const char *cmd_name
= "Unknown";
470 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
471 && cmd_name_mapping
[cmd
]) {
472 cmd_name
= cmd_name_mapping
[cmd
];
474 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
476 lttng_ust_obj_get_name(handle
), handle
);
480 int setup_global_apps(void)
483 assert(!global_apps
.wait_shm_mmap
);
485 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
486 if (!global_apps
.wait_shm_mmap
) {
487 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
488 global_apps
.allowed
= 0;
493 global_apps
.allowed
= 1;
494 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
499 int setup_local_apps(void)
502 const char *home_dir
;
505 assert(!local_apps
.wait_shm_mmap
);
509 * Disallow per-user tracing for setuid binaries.
511 if (uid
!= geteuid()) {
512 assert(local_apps
.allowed
== 0);
516 home_dir
= get_lttng_home_dir();
518 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
519 assert(local_apps
.allowed
== 0);
523 local_apps
.allowed
= 1;
524 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
526 LTTNG_DEFAULT_HOME_RUNDIR
,
527 LTTNG_UST_SOCK_FILENAME
);
528 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
529 LTTNG_UST_WAIT_FILENAME
,
532 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
533 if (!local_apps
.wait_shm_mmap
) {
534 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
535 local_apps
.allowed
= 0;
540 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
546 * Get socket timeout, in ms.
547 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
550 long get_timeout(void)
552 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
554 if (!got_timeout_env
) {
555 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
559 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
560 /* All negative values are considered as "-1". */
561 if (constructor_delay_ms
< -1)
562 constructor_delay_ms
= -1;
563 return constructor_delay_ms
;
566 /* Timeout for notify socket send and recv. */
568 long get_notify_sock_timeout(void)
570 return get_timeout();
573 /* Timeout for connecting to cmd and notify sockets. */
575 long get_connect_sock_timeout(void)
577 return get_timeout();
581 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
584 int get_constructor_timeout(struct timespec
*constructor_timeout
)
586 long constructor_delay_ms
;
589 constructor_delay_ms
= get_timeout();
591 switch (constructor_delay_ms
) {
592 case -1:/* fall-through */
594 return constructor_delay_ms
;
600 * If we are unable to find the current time, don't wait.
602 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
607 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
608 constructor_timeout
->tv_nsec
+=
609 (constructor_delay_ms
% 1000UL) * 1000000UL;
610 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
611 constructor_timeout
->tv_sec
++;
612 constructor_timeout
->tv_nsec
-= 1000000000UL;
614 /* Timeout wait (constructor_delay_ms). */
619 void get_allow_blocking(void)
621 const char *str_allow_blocking
=
622 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
624 if (str_allow_blocking
) {
625 DBG("%s environment variable is set",
626 "LTTNG_UST_ALLOW_BLOCKING");
627 lttng_ust_ringbuffer_set_allow_blocking();
632 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
634 return ustcomm_send_reg_msg(socket
,
637 lttng_alignof(uint8_t) * CHAR_BIT
,
638 lttng_alignof(uint16_t) * CHAR_BIT
,
639 lttng_alignof(uint32_t) * CHAR_BIT
,
640 lttng_alignof(uint64_t) * CHAR_BIT
,
641 lttng_alignof(unsigned long) * CHAR_BIT
);
645 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
649 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
652 DBG("message successfully sent");
655 if (len
== -ECONNRESET
) {
656 DBG("remote end closed connection");
661 DBG("incorrect message size: %zd", len
);
667 void decrement_sem_count(unsigned int count
)
671 assert(uatomic_read(&sem_count
) >= count
);
673 if (uatomic_read(&sem_count
) <= 0) {
677 ret
= uatomic_add_return(&sem_count
, -count
);
679 ret
= sem_post(&constructor_wait
);
685 int handle_register_done(struct sock_info
*sock_info
)
687 if (sock_info
->registration_done
)
689 sock_info
->registration_done
= 1;
691 decrement_sem_count(1);
692 if (!sock_info
->statedump_pending
) {
693 sock_info
->initial_statedump_done
= 1;
694 decrement_sem_count(1);
701 int handle_register_failed(struct sock_info
*sock_info
)
703 if (sock_info
->registration_done
)
705 sock_info
->registration_done
= 1;
706 sock_info
->initial_statedump_done
= 1;
708 decrement_sem_count(2);
714 * Only execute pending statedump after the constructor semaphore has
715 * been posted by the current listener thread. This means statedump will
716 * only be performed after the "registration done" command is received
717 * from this thread's session daemon.
719 * This ensures we don't run into deadlock issues with the dynamic
720 * loader mutex, which is held while the constructor is called and
721 * waiting on the constructor semaphore. All operations requiring this
722 * dynamic loader lock need to be postponed using this mechanism.
724 * In a scenario with two session daemons connected to the application,
725 * it is possible that the first listener thread which receives the
726 * registration done command issues its statedump while the dynamic
727 * loader lock is still held by the application constructor waiting on
728 * the semaphore. It will however be allowed to proceed when the
729 * second session daemon sends the registration done command to the
730 * second listener thread. This situation therefore does not produce
734 void handle_pending_statedump(struct sock_info
*sock_info
)
736 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
737 sock_info
->statedump_pending
= 0;
738 pthread_mutex_lock(&ust_fork_mutex
);
739 lttng_handle_pending_statedump(sock_info
);
740 pthread_mutex_unlock(&ust_fork_mutex
);
742 if (!sock_info
->initial_statedump_done
) {
743 sock_info
->initial_statedump_done
= 1;
744 decrement_sem_count(1);
750 const char *bytecode_type_str(uint32_t cmd
)
753 case LTTNG_UST_CAPTURE
:
755 case LTTNG_UST_FILTER
:
763 int handle_bytecode_recv(struct sock_info
*sock_info
,
764 int sock
, struct ustcomm_ust_msg
*lum
)
766 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
767 enum lttng_ust_bytecode_node_type type
;
768 const struct lttng_ust_objd_ops
*ops
;
769 uint32_t data_size
, data_size_max
, reloc_offset
;
775 case LTTNG_UST_FILTER
:
776 type
= LTTNG_UST_BYTECODE_NODE_TYPE_FILTER
;
777 data_size
= lum
->u
.filter
.data_size
;
778 data_size_max
= FILTER_BYTECODE_MAX_LEN
;
779 reloc_offset
= lum
->u
.filter
.reloc_offset
;
780 seqnum
= lum
->u
.filter
.seqnum
;
782 case LTTNG_UST_CAPTURE
:
783 type
= LTTNG_UST_BYTECODE_NODE_TYPE_CAPTURE
;
784 data_size
= lum
->u
.capture
.data_size
;
785 data_size_max
= CAPTURE_BYTECODE_MAX_LEN
;
786 reloc_offset
= lum
->u
.capture
.reloc_offset
;
787 seqnum
= lum
->u
.capture
.seqnum
;
793 if (data_size
> data_size_max
) {
794 ERR("Bytecode %s data size is too large: %u bytes",
795 bytecode_type_str(lum
->cmd
), data_size
);
800 if (reloc_offset
> data_size
) {
801 ERR("Bytecode %s reloc offset %u is not within data",
802 bytecode_type_str(lum
->cmd
), reloc_offset
);
807 /* Allocate the structure AND the `data[]` field. */
808 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
814 bytecode
->bc
.len
= data_size
;
815 bytecode
->bc
.reloc_offset
= reloc_offset
;
816 bytecode
->bc
.seqnum
= seqnum
;
817 bytecode
->type
= type
;
819 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
821 case 0: /* orderly shutdown */
825 if (len
== bytecode
->bc
.len
) {
826 DBG("Bytecode %s data received",
827 bytecode_type_str(lum
->cmd
));
829 } else if (len
< 0) {
830 DBG("Receive failed from lttng-sessiond with errno %d",
832 if (len
== -ECONNRESET
) {
833 ERR("%s remote end closed connection",
841 DBG("Incorrect %s bytecode data message size: %zd",
842 bytecode_type_str(lum
->cmd
), len
);
848 ops
= objd_ops(lum
->handle
);
855 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
856 (unsigned long) &bytecode
,
867 int handle_message(struct sock_info
*sock_info
,
868 int sock
, struct ustcomm_ust_msg
*lum
)
871 const struct lttng_ust_objd_ops
*ops
;
872 struct ustcomm_ust_reply lur
;
874 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
877 memset(&lur
, 0, sizeof(lur
));
880 ret
= -LTTNG_UST_ERR_EXITING
;
884 ops
= objd_ops(lum
->handle
);
891 case LTTNG_UST_REGISTER_DONE
:
892 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
893 ret
= handle_register_done(sock_info
);
897 case LTTNG_UST_RELEASE
:
898 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
901 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
903 case LTTNG_UST_CAPTURE
:
904 case LTTNG_UST_FILTER
:
905 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
909 case LTTNG_UST_EXCLUSION
:
911 /* Receive exclusion names */
912 struct lttng_ust_excluder_node
*node
;
915 count
= lum
->u
.exclusion
.count
;
917 /* There are no names to read */
921 node
= zmalloc(sizeof(*node
) +
922 count
* LTTNG_UST_SYM_NAME_LEN
);
927 node
->excluder
.count
= count
;
928 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
929 count
* LTTNG_UST_SYM_NAME_LEN
);
931 case 0: /* orderly shutdown */
936 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
937 DBG("Exclusion data received");
939 } else if (len
< 0) {
940 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
941 if (len
== -ECONNRESET
) {
942 ERR("%s remote end closed connection", sock_info
->name
);
951 DBG("Incorrect exclusion data message size: %zd", len
);
958 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
959 (unsigned long) &node
,
966 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
:
968 int event_notifier_notif_fd
, close_ret
;
970 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
971 &event_notifier_notif_fd
);
973 case 0: /* orderly shutdown */
980 DBG("Receive failed from lttng-sessiond with errno %d",
982 if (len
== -ECONNRESET
) {
983 ERR("%s remote end closed connection",
991 DBG("Incorrect event notifier fd message size: %zd",
997 args
.event_notifier_handle
.event_notifier_notif_fd
=
998 event_notifier_notif_fd
;
1000 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1001 (unsigned long) &lum
->u
,
1005 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1006 lttng_ust_lock_fd_tracker();
1007 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1008 lttng_ust_unlock_fd_tracker();
1014 case LTTNG_UST_CHANNEL
:
1019 len
= ustcomm_recv_channel_from_sessiond(sock
,
1020 &chan_data
, lum
->u
.channel
.len
,
1023 case 0: /* orderly shutdown */
1027 if (len
== lum
->u
.channel
.len
) {
1028 DBG("channel data received");
1030 } else if (len
< 0) {
1031 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1032 if (len
== -ECONNRESET
) {
1033 ERR("%s remote end closed connection", sock_info
->name
);
1040 DBG("incorrect channel data message size: %zd", len
);
1045 args
.channel
.chan_data
= chan_data
;
1046 args
.channel
.wakeup_fd
= wakeup_fd
;
1048 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1049 (unsigned long) &lum
->u
,
1053 if (args
.channel
.wakeup_fd
>= 0) {
1056 lttng_ust_lock_fd_tracker();
1057 close_ret
= close(args
.channel
.wakeup_fd
);
1058 lttng_ust_unlock_fd_tracker();
1059 args
.channel
.wakeup_fd
= -1;
1063 free(args
.channel
.chan_data
);
1066 case LTTNG_UST_STREAM
:
1070 /* Receive shm_fd, wakeup_fd */
1071 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1073 &args
.stream
.shm_fd
,
1074 &args
.stream
.wakeup_fd
);
1080 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1081 (unsigned long) &lum
->u
,
1085 if (args
.stream
.shm_fd
>= 0) {
1086 lttng_ust_lock_fd_tracker();
1087 close_ret
= close(args
.stream
.shm_fd
);
1088 lttng_ust_unlock_fd_tracker();
1089 args
.stream
.shm_fd
= -1;
1093 if (args
.stream
.wakeup_fd
>= 0) {
1094 lttng_ust_lock_fd_tracker();
1095 close_ret
= close(args
.stream
.wakeup_fd
);
1096 lttng_ust_unlock_fd_tracker();
1097 args
.stream
.wakeup_fd
= -1;
1103 case LTTNG_UST_CONTEXT
:
1104 switch (lum
->u
.context
.ctx
) {
1105 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1108 size_t ctxlen
, recvlen
;
1110 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1111 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1112 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
1113 ERR("Application context string length size is too large: %zu bytes",
1118 strcpy(ctxstr
, "$app.");
1119 p
= &ctxstr
[strlen("$app.")];
1120 recvlen
= ctxlen
- strlen("$app.");
1121 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1123 case 0: /* orderly shutdown */
1127 if (len
== recvlen
) {
1128 DBG("app context data received");
1130 } else if (len
< 0) {
1131 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1132 if (len
== -ECONNRESET
) {
1133 ERR("%s remote end closed connection", sock_info
->name
);
1140 DBG("incorrect app context data message size: %zd", len
);
1145 /* Put : between provider and ctxname. */
1146 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1147 args
.app_context
.ctxname
= ctxstr
;
1154 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1155 (unsigned long) &lum
->u
,
1161 case LTTNG_UST_COUNTER
:
1165 len
= ustcomm_recv_counter_from_sessiond(sock
,
1166 &counter_data
, lum
->u
.counter
.len
);
1168 case 0: /* orderly shutdown */
1172 if (len
== lum
->u
.counter
.len
) {
1173 DBG("counter data received");
1175 } else if (len
< 0) {
1176 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1177 if (len
== -ECONNRESET
) {
1178 ERR("%s remote end closed connection", sock_info
->name
);
1185 DBG("incorrect counter data message size: %zd", len
);
1190 args
.counter
.counter_data
= counter_data
;
1192 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1193 (unsigned long) &lum
->u
,
1197 free(args
.counter
.counter_data
);
1200 case LTTNG_UST_COUNTER_GLOBAL
:
1202 /* Receive shm_fd */
1203 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1204 &args
.counter_shm
.shm_fd
);
1210 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1211 (unsigned long) &lum
->u
,
1215 if (args
.counter_shm
.shm_fd
>= 0) {
1218 lttng_ust_lock_fd_tracker();
1219 close_ret
= close(args
.counter_shm
.shm_fd
);
1220 lttng_ust_unlock_fd_tracker();
1221 args
.counter_shm
.shm_fd
= -1;
1227 case LTTNG_UST_COUNTER_CPU
:
1229 /* Receive shm_fd */
1230 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1231 &args
.counter_shm
.shm_fd
);
1237 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1238 (unsigned long) &lum
->u
,
1242 if (args
.counter_shm
.shm_fd
>= 0) {
1245 lttng_ust_lock_fd_tracker();
1246 close_ret
= close(args
.counter_shm
.shm_fd
);
1247 lttng_ust_unlock_fd_tracker();
1248 args
.counter_shm
.shm_fd
= -1;
1254 case LTTNG_UST_EVENT_NOTIFIER_CREATE
:
1256 /* Receive struct lttng_ust_event_notifier */
1257 struct lttng_ust_event_notifier event_notifier
;
1259 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1260 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1264 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1266 case 0: /* orderly shutdown */
1270 if (len
== sizeof(event_notifier
)) {
1271 DBG("event notifier data received");
1273 } else if (len
< 0) {
1274 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1275 if (len
== -ECONNRESET
) {
1276 ERR("%s remote end closed connection", sock_info
->name
);
1283 DBG("incorrect event notifier data message size: %zd", len
);
1289 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1290 (unsigned long) &event_notifier
,
1299 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1300 (unsigned long) &lum
->u
,
1307 lur
.handle
= lum
->handle
;
1311 lur
.ret_code
= LTTNG_UST_OK
;
1314 * Use -LTTNG_UST_ERR as wildcard for UST internal
1315 * error that are not caused by the transport, except if
1316 * we already have a more precise error message to
1319 if (ret
> -LTTNG_UST_ERR
) {
1320 /* Translate code to UST error. */
1323 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1326 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1329 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1332 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1335 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1338 lur
.ret_code
= -LTTNG_UST_ERR
;
1347 case LTTNG_UST_TRACER_VERSION
:
1348 lur
.u
.version
= lum
->u
.version
;
1350 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1351 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1355 DBG("Return value: %d", lur
.ret_val
);
1360 * Performed delayed statedump operations outside of the UST
1361 * lock. We need to take the dynamic loader lock before we take
1362 * the UST lock internally within handle_pending_statedump().
1364 handle_pending_statedump(sock_info
);
1367 ret
= -LTTNG_UST_ERR_EXITING
;
1371 ret
= send_reply(sock
, &lur
);
1373 DBG("error sending reply");
1378 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1381 if (lur
.ret_code
== LTTNG_UST_OK
) {
1383 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1384 len
= ustcomm_send_unix_sock(sock
,
1385 &args
.field_list
.entry
,
1386 sizeof(args
.field_list
.entry
));
1391 if (len
!= sizeof(args
.field_list
.entry
)) {
1405 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1409 if (sock_info
->root_handle
!= -1) {
1410 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1412 ERR("Error unref root handle");
1414 sock_info
->root_handle
= -1;
1416 sock_info
->registration_done
= 0;
1417 sock_info
->initial_statedump_done
= 0;
1420 * wait_shm_mmap, socket and notify socket are used by listener
1421 * threads outside of the ust lock, so we cannot tear them down
1422 * ourselves, because we cannot join on these threads. Leave
1423 * responsibility of cleaning up these resources to the OS
1429 if (sock_info
->socket
!= -1) {
1430 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1432 ERR("Error closing ust cmd socket");
1434 sock_info
->socket
= -1;
1436 if (sock_info
->notify_socket
!= -1) {
1437 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1439 ERR("Error closing ust notify socket");
1441 sock_info
->notify_socket
= -1;
1443 if (sock_info
->wait_shm_mmap
) {
1446 page_size
= LTTNG_UST_PAGE_SIZE
;
1447 if (page_size
<= 0) {
1451 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1453 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1455 ERR("Error unmapping wait shm");
1458 sock_info
->wait_shm_mmap
= NULL
;
1463 * Using fork to set umask in the child process (not multi-thread safe).
1464 * We deal with the shm_open vs ftruncate race (happening when the
1465 * sessiond owns the shm and does not let everybody modify it, to ensure
1466 * safety against shm_unlink) by simply letting the mmap fail and
1467 * retrying after a few seconds.
1468 * For global shm, everybody has rw access to it until the sessiond
1472 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1474 int wait_shm_fd
, ret
;
1478 * Try to open read-only.
1480 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1481 if (wait_shm_fd
>= 0) {
1484 size_t bytes_read
= 0;
1487 * Try to read the fd. If unable to do so, try opening
1491 len
= read(wait_shm_fd
,
1492 &((char *) &tmp_read
)[bytes_read
],
1493 sizeof(tmp_read
) - bytes_read
);
1497 } while ((len
< 0 && errno
== EINTR
)
1498 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1499 if (bytes_read
!= sizeof(tmp_read
)) {
1500 ret
= close(wait_shm_fd
);
1502 ERR("close wait_shm_fd");
1507 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1509 * Real-only open did not work, and it's not because the
1510 * entry was not present. It's a failure that prohibits
1513 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1519 * If the open failed because the file did not exist, or because
1520 * the file was not truncated yet, try creating it ourself.
1522 URCU_TLS(lttng_ust_nest_count
)++;
1524 URCU_TLS(lttng_ust_nest_count
)--;
1529 * Parent: wait for child to return, in which case the
1530 * shared memory map will have been created.
1532 pid
= wait(&status
);
1533 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1538 * Try to open read-only again after creation.
1540 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1541 if (wait_shm_fd
< 0) {
1543 * Real-only open did not work. It's a failure
1544 * that prohibits using shm.
1546 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1550 } else if (pid
== 0) {
1554 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1555 if (sock_info
->global
)
1556 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1558 * We're alone in a child process, so we can modify the
1559 * process-wide umask.
1561 umask(~create_mode
);
1563 * Try creating shm (or get rw access).
1564 * We don't do an exclusive open, because we allow other
1565 * processes to create+ftruncate it concurrently.
1567 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1568 O_RDWR
| O_CREAT
, create_mode
);
1569 if (wait_shm_fd
>= 0) {
1570 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1572 PERROR("ftruncate");
1573 _exit(EXIT_FAILURE
);
1575 _exit(EXIT_SUCCESS
);
1578 * For local shm, we need to have rw access to accept
1579 * opening it: this means the local sessiond will be
1580 * able to wake us up. For global shm, we open it even
1581 * if rw access is not granted, because the root.root
1582 * sessiond will be able to override all rights and wake
1585 if (!sock_info
->global
&& errno
!= EACCES
) {
1586 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1587 _exit(EXIT_FAILURE
);
1590 * The shm exists, but we cannot open it RW. Report
1593 _exit(EXIT_SUCCESS
);
1598 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1599 struct stat statbuf
;
1602 * Ensure that our user is the owner of the shm file for
1603 * local shm. If we do not own the file, it means our
1604 * sessiond will not have access to wake us up (there is
1605 * probably a rogue process trying to fake our
1606 * sessiond). Fallback to polling method in this case.
1608 ret
= fstat(wait_shm_fd
, &statbuf
);
1613 if (statbuf
.st_uid
!= getuid())
1619 ret
= close(wait_shm_fd
);
1621 PERROR("Error closing fd");
1627 char *get_map_shm(struct sock_info
*sock_info
)
1630 int wait_shm_fd
, ret
;
1631 char *wait_shm_mmap
;
1633 page_size
= sysconf(_SC_PAGE_SIZE
);
1634 if (page_size
<= 0) {
1638 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1642 lttng_ust_lock_fd_tracker();
1643 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1644 if (wait_shm_fd
< 0) {
1645 lttng_ust_unlock_fd_tracker();
1649 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1651 ret
= close(wait_shm_fd
);
1653 PERROR("Error closing fd");
1655 lttng_ust_unlock_fd_tracker();
1660 lttng_ust_unlock_fd_tracker();
1662 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1663 MAP_SHARED
, wait_shm_fd
, 0);
1665 /* close shm fd immediately after taking the mmap reference */
1666 lttng_ust_lock_fd_tracker();
1667 ret
= close(wait_shm_fd
);
1669 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1671 PERROR("Error closing fd");
1673 lttng_ust_unlock_fd_tracker();
1675 if (wait_shm_mmap
== MAP_FAILED
) {
1676 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1679 return wait_shm_mmap
;
1686 void wait_for_sessiond(struct sock_info
*sock_info
)
1688 /* Use ust_lock to check if we should quit. */
1692 if (wait_poll_fallback
) {
1697 assert(sock_info
->wait_shm_mmap
);
1699 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1700 /* Wait for futex wakeup */
1701 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1704 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1705 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1708 /* Value already changed. */
1711 /* Retry if interrupted by signal. */
1712 break; /* Get out of switch. */
1714 wait_poll_fallback
= 1;
1716 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1717 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1718 "Please upgrade your kernel "
1719 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1720 "mainline). LTTng-UST will use polling mode fallback.");
1721 if (ust_err_debug_enabled())
1739 * This thread does not allocate any resource, except within
1740 * handle_message, within mutex protection. This mutex protects against
1742 * The other moment it allocates resources is at socket connection, which
1743 * is also protected by the mutex.
1746 void *ust_listener_thread(void *arg
)
1748 struct sock_info
*sock_info
= arg
;
1749 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1752 lttng_ust_fixup_tls();
1754 * If available, add '-ust' to the end of this thread's
1757 ret
= lttng_ust_setustprocname();
1759 ERR("Unable to set UST process name");
1762 /* Restart trying to connect to the session daemon */
1764 if (prev_connect_failed
) {
1765 /* Wait for sessiond availability with pipe */
1766 wait_for_sessiond(sock_info
);
1770 * Sleep for 5 seconds before retrying after a
1771 * sequence of failure / wait / failure. This
1772 * deals with a killed or broken session daemon.
1778 prev_connect_failed
= 0;
1785 if (sock_info
->socket
!= -1) {
1786 /* FD tracker is updated by ustcomm_close_unix_sock() */
1787 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1789 ERR("Error closing %s ust cmd socket",
1792 sock_info
->socket
= -1;
1794 if (sock_info
->notify_socket
!= -1) {
1795 /* FD tracker is updated by ustcomm_close_unix_sock() */
1796 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1798 ERR("Error closing %s ust notify socket",
1801 sock_info
->notify_socket
= -1;
1806 * Register. We need to perform both connect and sending
1807 * registration message before doing the next connect otherwise
1808 * we may reach unix socket connect queue max limits and block
1809 * on the 2nd connect while the session daemon is awaiting the
1810 * first connect registration message.
1812 /* Connect cmd socket */
1813 lttng_ust_lock_fd_tracker();
1814 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1815 get_connect_sock_timeout());
1817 lttng_ust_unlock_fd_tracker();
1818 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1819 prev_connect_failed
= 1;
1822 * If we cannot find the sessiond daemon, don't delay
1823 * constructor execution.
1825 ret
= handle_register_failed(sock_info
);
1831 ret
= lttng_ust_add_fd_to_tracker(fd
);
1835 PERROR("close on sock_info->socket");
1838 lttng_ust_unlock_fd_tracker();
1843 sock_info
->socket
= ret
;
1844 lttng_ust_unlock_fd_tracker();
1848 * Unlock/relock ust lock because connect is blocking (with
1849 * timeout). Don't delay constructors on the ust lock for too
1857 * Create only one root handle per listener thread for the whole
1858 * process lifetime, so we ensure we get ID which is statically
1859 * assigned to the root handle.
1861 if (sock_info
->root_handle
== -1) {
1862 ret
= lttng_abi_create_root_handle();
1864 ERR("Error creating root handle");
1867 sock_info
->root_handle
= ret
;
1870 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1872 ERR("Error registering to %s ust cmd socket",
1874 prev_connect_failed
= 1;
1876 * If we cannot register to the sessiond daemon, don't
1877 * delay constructor execution.
1879 ret
= handle_register_failed(sock_info
);
1887 * Unlock/relock ust lock because connect is blocking (with
1888 * timeout). Don't delay constructors on the ust lock for too
1895 /* Connect notify socket */
1896 lttng_ust_lock_fd_tracker();
1897 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1898 get_connect_sock_timeout());
1900 lttng_ust_unlock_fd_tracker();
1901 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1902 prev_connect_failed
= 1;
1905 * If we cannot find the sessiond daemon, don't delay
1906 * constructor execution.
1908 ret
= handle_register_failed(sock_info
);
1915 ret
= lttng_ust_add_fd_to_tracker(fd
);
1919 PERROR("close on sock_info->notify_socket");
1922 lttng_ust_unlock_fd_tracker();
1927 sock_info
->notify_socket
= ret
;
1928 lttng_ust_unlock_fd_tracker();
1932 * Unlock/relock ust lock because connect is blocking (with
1933 * timeout). Don't delay constructors on the ust lock for too
1940 timeout
= get_notify_sock_timeout();
1943 * Give at least 10ms to sessiond to reply to
1948 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1951 WARN("Error setting socket receive timeout");
1953 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1956 WARN("Error setting socket send timeout");
1958 } else if (timeout
< -1) {
1959 WARN("Unsupported timeout value %ld", timeout
);
1962 ret
= register_to_sessiond(sock_info
->notify_socket
,
1963 USTCTL_SOCKET_NOTIFY
);
1965 ERR("Error registering to %s ust notify socket",
1967 prev_connect_failed
= 1;
1969 * If we cannot register to the sessiond daemon, don't
1970 * delay constructor execution.
1972 ret
= handle_register_failed(sock_info
);
1977 sock
= sock_info
->socket
;
1983 struct ustcomm_ust_msg lum
;
1985 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1987 case 0: /* orderly shutdown */
1988 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1993 * Either sessiond has shutdown or refused us by closing the socket.
1994 * In either case, we don't want to delay construction execution,
1995 * and we need to wait before retry.
1997 prev_connect_failed
= 1;
1999 * If we cannot register to the sessiond daemon, don't
2000 * delay constructor execution.
2002 ret
= handle_register_failed(sock_info
);
2007 print_cmd(lum
.cmd
, lum
.handle
);
2008 ret
= handle_message(sock_info
, sock
, &lum
);
2010 ERR("Error handling message for %s socket",
2013 * Close socket if protocol error is
2021 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2023 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2025 if (len
== -ECONNRESET
) {
2026 DBG("%s remote end closed connection", sock_info
->name
);
2037 /* Cleanup socket handles before trying to reconnect */
2038 lttng_ust_objd_table_owner_cleanup(sock_info
);
2040 goto restart
; /* try to reconnect */
2045 pthread_mutex_lock(&ust_exit_mutex
);
2046 sock_info
->thread_active
= 0;
2047 pthread_mutex_unlock(&ust_exit_mutex
);
2052 * Weak symbol to call when the ust malloc wrapper is not loaded.
2054 __attribute__((weak
))
2055 void lttng_ust_malloc_wrapper_init(void)
2060 * sessiond monitoring thread: monitor presence of global and per-user
2061 * sessiond by polling the application common named pipe.
2063 void __attribute__((constructor
)) lttng_ust_init(void)
2065 struct timespec constructor_timeout
;
2066 sigset_t sig_all_blocked
, orig_parent_mask
;
2067 pthread_attr_t thread_attr
;
2072 if (uatomic_xchg(&initialized
, 1) == 1)
2076 * Fixup interdependency between TLS fixup mutex (which happens
2077 * to be the dynamic linker mutex) and ust_lock, taken within
2080 lttng_ust_fixup_tls();
2082 lttng_ust_loaded
= 1;
2085 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2086 * the unloading of code used by the ust_listener_threads as we can not
2087 * reliably know when they exited. To do that, manually load
2088 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2089 * this library so it never becomes zero, thus never gets unloaded from the
2090 * address space of the process. Since we are already running in the
2091 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
2092 * simply increment the refcount and no additionnal work is needed by the
2093 * dynamic loader as the shared library is already loaded in the address
2094 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2095 * unloading of the UST library if its refcount becomes zero (which should
2096 * never happen). Do the return value check but discard the handle at the
2097 * end of the function as it's not needed.
2099 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
2101 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
2105 * We want precise control over the order in which we construct
2106 * our sub-libraries vs starting to receive commands from
2107 * sessiond (otherwise leading to errors when trying to create
2108 * sessiond before the init functions are completed).
2111 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2113 lttng_ust_init_fd_tracker();
2114 lttng_ust_clock_init();
2115 lttng_ust_getcpu_init();
2116 lttng_ust_statedump_init();
2117 lttng_ring_buffer_metadata_client_init();
2118 lttng_ring_buffer_client_overwrite_init();
2119 lttng_ring_buffer_client_overwrite_rt_init();
2120 lttng_ring_buffer_client_discard_init();
2121 lttng_ring_buffer_client_discard_rt_init();
2122 lttng_counter_client_percpu_32_modular_init();
2123 lttng_counter_client_percpu_64_modular_init();
2124 lttng_perf_counter_init();
2126 * Invoke ust malloc wrapper init before starting other threads.
2128 lttng_ust_malloc_wrapper_init();
2130 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2132 get_allow_blocking();
2134 ret
= sem_init(&constructor_wait
, 0, 0);
2139 ret
= setup_global_apps();
2141 assert(global_apps
.allowed
== 0);
2142 DBG("global apps setup returned %d", ret
);
2145 ret
= setup_local_apps();
2147 assert(local_apps
.allowed
== 0);
2148 DBG("local apps setup returned %d", ret
);
2151 /* A new thread created by pthread_create inherits the signal mask
2152 * from the parent. To avoid any signal being received by the
2153 * listener thread, we block all signals temporarily in the parent,
2154 * while we create the listener thread.
2156 sigfillset(&sig_all_blocked
);
2157 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2159 ERR("pthread_sigmask: %s", strerror(ret
));
2162 ret
= pthread_attr_init(&thread_attr
);
2164 ERR("pthread_attr_init: %s", strerror(ret
));
2166 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2168 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2171 if (global_apps
.allowed
) {
2172 pthread_mutex_lock(&ust_exit_mutex
);
2173 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2174 ust_listener_thread
, &global_apps
);
2176 ERR("pthread_create global: %s", strerror(ret
));
2178 global_apps
.thread_active
= 1;
2179 pthread_mutex_unlock(&ust_exit_mutex
);
2181 handle_register_done(&global_apps
);
2184 if (local_apps
.allowed
) {
2185 pthread_mutex_lock(&ust_exit_mutex
);
2186 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2187 ust_listener_thread
, &local_apps
);
2189 ERR("pthread_create local: %s", strerror(ret
));
2191 local_apps
.thread_active
= 1;
2192 pthread_mutex_unlock(&ust_exit_mutex
);
2194 handle_register_done(&local_apps
);
2196 ret
= pthread_attr_destroy(&thread_attr
);
2198 ERR("pthread_attr_destroy: %s", strerror(ret
));
2201 /* Restore original signal mask in parent */
2202 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2204 ERR("pthread_sigmask: %s", strerror(ret
));
2207 switch (timeout_mode
) {
2208 case 1: /* timeout wait */
2210 ret
= sem_timedwait(&constructor_wait
,
2211 &constructor_timeout
);
2212 } while (ret
< 0 && errno
== EINTR
);
2216 ERR("Timed out waiting for lttng-sessiond");
2219 PERROR("sem_timedwait");
2222 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2227 case -1:/* wait forever */
2229 ret
= sem_wait(&constructor_wait
);
2230 } while (ret
< 0 && errno
== EINTR
);
2237 ERR("Unexpected error \"%s\" returned by sem_wait",
2242 case 0: /* no timeout */
2248 void lttng_ust_cleanup(int exiting
)
2250 cleanup_sock_info(&global_apps
, exiting
);
2251 cleanup_sock_info(&local_apps
, exiting
);
2252 local_apps
.allowed
= 0;
2253 global_apps
.allowed
= 0;
2255 * The teardown in this function all affect data structures
2256 * accessed under the UST lock by the listener thread. This
2257 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2258 * that none of these threads are accessing this data at this
2261 lttng_ust_abi_exit();
2262 lttng_ust_events_exit();
2263 lttng_perf_counter_exit();
2264 lttng_ring_buffer_client_discard_rt_exit();
2265 lttng_ring_buffer_client_discard_exit();
2266 lttng_ring_buffer_client_overwrite_rt_exit();
2267 lttng_ring_buffer_client_overwrite_exit();
2268 lttng_ring_buffer_metadata_client_exit();
2269 lttng_counter_client_percpu_32_modular_exit();
2270 lttng_counter_client_percpu_64_modular_exit();
2271 lttng_ust_statedump_destroy();
2274 /* Reinitialize values for fork */
2275 sem_count
= sem_count_initial_value
;
2276 lttng_ust_comm_should_quit
= 0;
2281 void __attribute__((destructor
)) lttng_ust_exit(void)
2286 * Using pthread_cancel here because:
2287 * A) we don't want to hang application teardown.
2288 * B) the thread is not allocating any resource.
2292 * Require the communication thread to quit. Synchronize with
2293 * mutexes to ensure it is not in a mutex critical section when
2294 * pthread_cancel is later called.
2297 lttng_ust_comm_should_quit
= 1;
2300 pthread_mutex_lock(&ust_exit_mutex
);
2301 /* cancel threads */
2302 if (global_apps
.thread_active
) {
2303 ret
= pthread_cancel(global_apps
.ust_listener
);
2305 ERR("Error cancelling global ust listener thread: %s",
2308 global_apps
.thread_active
= 0;
2311 if (local_apps
.thread_active
) {
2312 ret
= pthread_cancel(local_apps
.ust_listener
);
2314 ERR("Error cancelling local ust listener thread: %s",
2317 local_apps
.thread_active
= 0;
2320 pthread_mutex_unlock(&ust_exit_mutex
);
2323 * Do NOT join threads: use of sys_futex makes it impossible to
2324 * join the threads without using async-cancel, but async-cancel
2325 * is delivered by a signal, which could hit the target thread
2326 * anywhere in its code path, including while the ust_lock() is
2327 * held, causing a deadlock for the other thread. Let the OS
2328 * cleanup the threads if there are stalled in a syscall.
2330 lttng_ust_cleanup(1);
2334 void ust_context_ns_reset(void)
2336 lttng_context_pid_ns_reset();
2337 lttng_context_cgroup_ns_reset();
2338 lttng_context_ipc_ns_reset();
2339 lttng_context_mnt_ns_reset();
2340 lttng_context_net_ns_reset();
2341 lttng_context_user_ns_reset();
2342 lttng_context_time_ns_reset();
2343 lttng_context_uts_ns_reset();
2347 void ust_context_vuids_reset(void)
2349 lttng_context_vuid_reset();
2350 lttng_context_veuid_reset();
2351 lttng_context_vsuid_reset();
2355 void ust_context_vgids_reset(void)
2357 lttng_context_vgid_reset();
2358 lttng_context_vegid_reset();
2359 lttng_context_vsgid_reset();
2363 * We exclude the worker threads across fork and clone (except
2364 * CLONE_VM), because these system calls only keep the forking thread
2365 * running in the child. Therefore, we don't want to call fork or clone
2366 * in the middle of an tracepoint or ust tracing state modification.
2367 * Holding this mutex protects these structures across fork and clone.
2369 void ust_before_fork(sigset_t
*save_sigset
)
2372 * Disable signals. This is to avoid that the child intervenes
2373 * before it is properly setup for tracing. It is safer to
2374 * disable all signals, because then we know we are not breaking
2375 * anything by restoring the original mask.
2380 /* Fixup lttng-ust TLS. */
2381 lttng_ust_fixup_tls();
2383 if (URCU_TLS(lttng_ust_nest_count
))
2385 /* Disable signals */
2386 sigfillset(&all_sigs
);
2387 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2389 PERROR("sigprocmask");
2392 pthread_mutex_lock(&ust_fork_mutex
);
2395 lttng_ust_urcu_before_fork();
2396 lttng_ust_lock_fd_tracker();
2400 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2404 DBG("process %d", getpid());
2405 lttng_perf_unlock();
2406 lttng_ust_unlock_fd_tracker();
2409 pthread_mutex_unlock(&ust_fork_mutex
);
2411 /* Restore signals */
2412 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2414 PERROR("sigprocmask");
2418 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2420 if (URCU_TLS(lttng_ust_nest_count
))
2422 DBG("process %d", getpid());
2423 lttng_ust_urcu_after_fork_parent();
2424 /* Release mutexes and reenable signals */
2425 ust_after_fork_common(restore_sigset
);
2429 * After fork, in the child, we need to cleanup all the leftover state,
2430 * except the worker thread which already magically disappeared thanks
2431 * to the weird Linux fork semantics. After tyding up, we call
2432 * lttng_ust_init() again to start over as a new PID.
2434 * This is meant for forks() that have tracing in the child between the
2435 * fork and following exec call (if there is any).
2437 void ust_after_fork_child(sigset_t
*restore_sigset
)
2439 if (URCU_TLS(lttng_ust_nest_count
))
2441 lttng_context_vpid_reset();
2442 lttng_context_vtid_reset();
2443 lttng_context_procname_reset();
2444 ust_context_ns_reset();
2445 ust_context_vuids_reset();
2446 ust_context_vgids_reset();
2447 DBG("process %d", getpid());
2448 /* Release urcu mutexes */
2449 lttng_ust_urcu_after_fork_child();
2450 lttng_ust_cleanup(0);
2451 /* Release mutexes and reenable signals */
2452 ust_after_fork_common(restore_sigset
);
2456 void ust_after_setns(void)
2458 ust_context_ns_reset();
2459 ust_context_vuids_reset();
2460 ust_context_vgids_reset();
2463 void ust_after_unshare(void)
2465 ust_context_ns_reset();
2466 ust_context_vuids_reset();
2467 ust_context_vgids_reset();
2470 void ust_after_setuid(void)
2472 ust_context_vuids_reset();
2475 void ust_after_seteuid(void)
2477 ust_context_vuids_reset();
2480 void ust_after_setreuid(void)
2482 ust_context_vuids_reset();
2485 void ust_after_setresuid(void)
2487 ust_context_vuids_reset();
2490 void ust_after_setgid(void)
2492 ust_context_vgids_reset();
2495 void ust_after_setegid(void)
2497 ust_context_vgids_reset();
2500 void ust_after_setregid(void)
2502 ust_context_vgids_reset();
2505 void ust_after_setresgid(void)
2507 ust_context_vgids_reset();
2510 void lttng_ust_sockinfo_session_enabled(void *owner
)
2512 struct sock_info
*sock_info
= owner
;
2513 sock_info
->statedump_pending
= 1;