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-utils.h>
33 #include <lttng/ust-events.h>
34 #include <lttng/ust-abi.h>
35 #include <lttng/ust-fork.h>
36 #include <lttng/ust-error.h>
37 #include <lttng/ust-ctl.h>
38 #include <lttng/ust-libc-wrapper.h>
39 #include <lttng/ust-tracer.h>
40 #include <urcu/tls-compat.h>
43 #include <usterr-signal-safe.h>
44 #include <ust-helper.h>
45 #include "tracepoint-internal.h"
46 #include "lttng-tracer-core.h"
48 #include "../libringbuffer/rb-init.h"
49 #include "lttng-ust-statedump.h"
51 #include "../libringbuffer/getcpu.h"
53 #include "ust-events-internal.h"
54 #include "context-internal.h"
55 #include "ust-compat.h"
56 #include "lttng-counter-client.h"
59 * Has lttng ust comm constructor been called ?
61 static int initialized
;
64 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
65 * Held when handling a command, also held by fork() to deal with
66 * removal of threads, and by exit path.
68 * The UST lock is the centralized mutex across UST tracing control and
71 * ust_exit_mutex must never nest in ust_mutex.
73 * ust_fork_mutex must never nest in ust_mutex.
75 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
76 * counter lazy initialization called by events within the statedump,
77 * which traces while the ust_mutex is held.
79 * ust_lock nests within the dynamic loader lock (within glibc) because
80 * it is taken within the library constructor.
82 * The ust fd tracker lock nests within the ust_mutex.
84 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
86 /* Allow nesting the ust_mutex within the same thread. */
87 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
90 * ust_exit_mutex protects thread_active variable wrt thread exit. It
91 * cannot be done by ust_mutex because pthread_cancel(), which takes an
92 * internal libc lock, cannot nest within ust_mutex.
94 * It never nests within a ust_mutex.
96 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
99 * ust_fork_mutex protects base address statedump tracing against forks. It
100 * prevents the dynamic loader lock to be taken (by base address statedump
101 * tracing) while a fork is happening, thus preventing deadlock issues with
102 * the dynamic loader lock.
104 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
106 /* Should the ust comm thread quit ? */
107 static int lttng_ust_comm_should_quit
;
110 * This variable can be tested by applications to check whether
111 * lttng-ust is loaded. They simply have to define their own
112 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
113 * library constructor.
115 int lttng_ust_loaded
__attribute__((weak
));
118 * Return 0 on success, -1 if should quit.
119 * The lock is taken in both cases.
124 sigset_t sig_all_blocked
, orig_mask
;
127 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
129 ERR("pthread_setcancelstate: %s", strerror(ret
));
131 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
132 ERR("pthread_setcancelstate: unexpected oldstate");
134 sigfillset(&sig_all_blocked
);
135 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
137 ERR("pthread_sigmask: %s", strerror(ret
));
139 if (!URCU_TLS(ust_mutex_nest
)++)
140 pthread_mutex_lock(&ust_mutex
);
141 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
143 ERR("pthread_sigmask: %s", strerror(ret
));
145 if (lttng_ust_comm_should_quit
) {
153 * ust_lock_nocheck() can be used in constructors/destructors, because
154 * they are already nested within the dynamic loader lock, and therefore
155 * have exclusive access against execution of liblttng-ust destructor.
158 void ust_lock_nocheck(void)
160 sigset_t sig_all_blocked
, orig_mask
;
163 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
165 ERR("pthread_setcancelstate: %s", strerror(ret
));
167 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
168 ERR("pthread_setcancelstate: unexpected oldstate");
170 sigfillset(&sig_all_blocked
);
171 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
173 ERR("pthread_sigmask: %s", strerror(ret
));
175 if (!URCU_TLS(ust_mutex_nest
)++)
176 pthread_mutex_lock(&ust_mutex
);
177 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
179 ERR("pthread_sigmask: %s", strerror(ret
));
186 void ust_unlock(void)
188 sigset_t sig_all_blocked
, orig_mask
;
191 sigfillset(&sig_all_blocked
);
192 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
194 ERR("pthread_sigmask: %s", strerror(ret
));
196 if (!--URCU_TLS(ust_mutex_nest
))
197 pthread_mutex_unlock(&ust_mutex
);
198 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
200 ERR("pthread_sigmask: %s", strerror(ret
));
202 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
204 ERR("pthread_setcancelstate: %s", strerror(ret
));
206 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
207 ERR("pthread_setcancelstate: unexpected oldstate");
212 * Wait for either of these before continuing to the main
214 * - the register_done message from sessiond daemon
215 * (will let the sessiond daemon enable sessions before main
217 * - sessiond daemon is not reachable.
218 * - timeout (ensuring applications are resilient to session
221 static sem_t constructor_wait
;
223 * Doing this for both the global and local sessiond.
226 sem_count_initial_value
= 4,
229 static int sem_count
= sem_count_initial_value
;
232 * Counting nesting within lttng-ust. Used to ensure that calling fork()
233 * from liblttng-ust does not execute the pre/post fork handlers.
235 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
238 * Info about socket and associated listener thread.
242 pthread_t ust_listener
; /* listener thread */
244 int registration_done
;
249 char sock_path
[PATH_MAX
];
253 char wait_shm_path
[PATH_MAX
];
255 /* Keep track of lazy state dump not performed yet. */
256 int statedump_pending
;
257 int initial_statedump_done
;
258 /* Keep procname for statedump */
259 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
262 /* Socket from app (connect) to session daemon (listen) for communication */
263 struct sock_info global_apps
= {
268 .registration_done
= 0,
272 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
276 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
278 .statedump_pending
= 0,
279 .initial_statedump_done
= 0,
283 /* TODO: allow global_apps_sock_path override */
285 struct sock_info local_apps
= {
289 .registration_done
= 0,
290 .allowed
= 0, /* Check setuid bit first */
296 .statedump_pending
= 0,
297 .initial_statedump_done
= 0,
301 static int wait_poll_fallback
;
303 static const char *cmd_name_mapping
[] = {
304 [ LTTNG_UST_ABI_RELEASE
] = "Release",
305 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
306 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
308 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
309 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
310 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
311 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
313 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
315 /* Session FD commands */
316 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
317 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
318 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
320 /* Channel FD commands */
321 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
322 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
324 /* Event and Channel FD commands */
325 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
326 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
328 /* Event, Channel and Session commands */
329 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
330 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
332 /* Tracepoint list commands */
333 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
334 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
336 /* Event FD commands */
337 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
338 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
340 /* Event notifier group commands */
341 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
343 /* Session and event notifier group commands */
344 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
346 /* Counter commands */
347 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
348 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
351 static const char *str_timeout
;
352 static int got_timeout_env
;
354 static char *get_map_shm(struct sock_info
*sock_info
);
356 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
359 size_t copied
= 0, to_copy
= len
;
362 ret
= read(fd
, buf
+ copied
, to_copy
);
367 } while ((ret
> 0 && to_copy
> 0)
368 || (ret
< 0 && errno
== EINTR
));
375 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
379 const char *get_lttng_home_dir(void)
383 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
387 return (const char *) lttng_ust_getenv("HOME");
391 * Force a read (imply TLS fixup for dlopen) of TLS variables.
394 void lttng_fixup_nest_count_tls(void)
396 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
400 void lttng_fixup_ust_mutex_nest_tls(void)
402 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
406 * Fixup lttng-ust urcu TLS.
409 void lttng_fixup_lttng_ust_urcu_tls(void)
411 (void) lttng_ust_urcu_read_ongoing();
414 void lttng_ust_fixup_tls(void)
416 lttng_fixup_lttng_ust_urcu_tls();
417 lttng_fixup_ringbuffer_tls();
418 lttng_fixup_vtid_tls();
419 lttng_fixup_nest_count_tls();
420 lttng_fixup_procname_tls();
421 lttng_fixup_ust_mutex_nest_tls();
422 lttng_ust_fixup_perf_counter_tls();
423 lttng_ust_fixup_fd_tracker_tls();
424 lttng_fixup_cgroup_ns_tls();
425 lttng_fixup_ipc_ns_tls();
426 lttng_fixup_net_ns_tls();
427 lttng_fixup_time_ns_tls();
428 lttng_fixup_uts_ns_tls();
429 lttng_ust_fixup_ring_buffer_client_discard_tls();
430 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
431 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
432 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
436 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
437 * model because many versions of glibc don't preallocate a pool large
438 * enough for TLS variables IE model defined in other shared libraries,
439 * and causes issues when using LTTng-UST for Java tracing.
441 * Because of this use of Global Dynamic TLS variables, users wishing to
442 * trace from signal handlers need to explicitly trigger the lazy
443 * allocation of those variables for each thread before using them.
444 * This can be triggered by calling lttng_ust_init_thread().
446 void lttng_ust_init_thread(void)
449 * Because those TLS variables are global dynamic, we need to
450 * ensure those are initialized before a signal handler nesting over
451 * this thread attempts to use them.
453 lttng_ust_fixup_tls();
456 int lttng_get_notify_socket(void *owner
)
458 struct sock_info
*info
= owner
;
460 return info
->notify_socket
;
464 char* lttng_ust_sockinfo_get_procname(void *owner
)
466 struct sock_info
*info
= owner
;
468 return info
->procname
;
472 void print_cmd(int cmd
, int handle
)
474 const char *cmd_name
= "Unknown";
476 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
477 && cmd_name_mapping
[cmd
]) {
478 cmd_name
= cmd_name_mapping
[cmd
];
480 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
482 lttng_ust_obj_get_name(handle
), handle
);
486 int setup_global_apps(void)
489 assert(!global_apps
.wait_shm_mmap
);
491 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
492 if (!global_apps
.wait_shm_mmap
) {
493 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
494 global_apps
.allowed
= 0;
499 global_apps
.allowed
= 1;
500 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
505 int setup_local_apps(void)
508 const char *home_dir
;
511 assert(!local_apps
.wait_shm_mmap
);
515 * Disallow per-user tracing for setuid binaries.
517 if (uid
!= geteuid()) {
518 assert(local_apps
.allowed
== 0);
522 home_dir
= get_lttng_home_dir();
524 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
525 assert(local_apps
.allowed
== 0);
529 local_apps
.allowed
= 1;
530 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
532 LTTNG_DEFAULT_HOME_RUNDIR
,
533 LTTNG_UST_SOCK_FILENAME
);
534 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
535 LTTNG_UST_WAIT_FILENAME
,
538 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
539 if (!local_apps
.wait_shm_mmap
) {
540 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
541 local_apps
.allowed
= 0;
546 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
552 * Get socket timeout, in ms.
553 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
556 long get_timeout(void)
558 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
560 if (!got_timeout_env
) {
561 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
565 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
566 /* All negative values are considered as "-1". */
567 if (constructor_delay_ms
< -1)
568 constructor_delay_ms
= -1;
569 return constructor_delay_ms
;
572 /* Timeout for notify socket send and recv. */
574 long get_notify_sock_timeout(void)
576 return get_timeout();
579 /* Timeout for connecting to cmd and notify sockets. */
581 long get_connect_sock_timeout(void)
583 return get_timeout();
587 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
590 int get_constructor_timeout(struct timespec
*constructor_timeout
)
592 long constructor_delay_ms
;
595 constructor_delay_ms
= get_timeout();
597 switch (constructor_delay_ms
) {
598 case -1:/* fall-through */
600 return constructor_delay_ms
;
606 * If we are unable to find the current time, don't wait.
608 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
613 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
614 constructor_timeout
->tv_nsec
+=
615 (constructor_delay_ms
% 1000UL) * 1000000UL;
616 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
617 constructor_timeout
->tv_sec
++;
618 constructor_timeout
->tv_nsec
-= 1000000000UL;
620 /* Timeout wait (constructor_delay_ms). */
625 void get_allow_blocking(void)
627 const char *str_allow_blocking
=
628 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
630 if (str_allow_blocking
) {
631 DBG("%s environment variable is set",
632 "LTTNG_UST_ALLOW_BLOCKING");
633 lttng_ust_ringbuffer_set_allow_blocking();
638 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
640 return ustcomm_send_reg_msg(socket
,
643 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
644 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
645 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
646 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
647 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
651 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
655 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
658 DBG("message successfully sent");
661 if (len
== -ECONNRESET
) {
662 DBG("remote end closed connection");
667 DBG("incorrect message size: %zd", len
);
673 void decrement_sem_count(unsigned int count
)
677 assert(uatomic_read(&sem_count
) >= count
);
679 if (uatomic_read(&sem_count
) <= 0) {
683 ret
= uatomic_add_return(&sem_count
, -count
);
685 ret
= sem_post(&constructor_wait
);
691 int handle_register_done(struct sock_info
*sock_info
)
693 if (sock_info
->registration_done
)
695 sock_info
->registration_done
= 1;
697 decrement_sem_count(1);
698 if (!sock_info
->statedump_pending
) {
699 sock_info
->initial_statedump_done
= 1;
700 decrement_sem_count(1);
707 int handle_register_failed(struct sock_info
*sock_info
)
709 if (sock_info
->registration_done
)
711 sock_info
->registration_done
= 1;
712 sock_info
->initial_statedump_done
= 1;
714 decrement_sem_count(2);
720 * Only execute pending statedump after the constructor semaphore has
721 * been posted by the current listener thread. This means statedump will
722 * only be performed after the "registration done" command is received
723 * from this thread's session daemon.
725 * This ensures we don't run into deadlock issues with the dynamic
726 * loader mutex, which is held while the constructor is called and
727 * waiting on the constructor semaphore. All operations requiring this
728 * dynamic loader lock need to be postponed using this mechanism.
730 * In a scenario with two session daemons connected to the application,
731 * it is possible that the first listener thread which receives the
732 * registration done command issues its statedump while the dynamic
733 * loader lock is still held by the application constructor waiting on
734 * the semaphore. It will however be allowed to proceed when the
735 * second session daemon sends the registration done command to the
736 * second listener thread. This situation therefore does not produce
740 void handle_pending_statedump(struct sock_info
*sock_info
)
742 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
743 sock_info
->statedump_pending
= 0;
744 pthread_mutex_lock(&ust_fork_mutex
);
745 lttng_handle_pending_statedump(sock_info
);
746 pthread_mutex_unlock(&ust_fork_mutex
);
748 if (!sock_info
->initial_statedump_done
) {
749 sock_info
->initial_statedump_done
= 1;
750 decrement_sem_count(1);
756 const char *bytecode_type_str(uint32_t cmd
)
759 case LTTNG_UST_ABI_CAPTURE
:
761 case LTTNG_UST_ABI_FILTER
:
769 int handle_bytecode_recv(struct sock_info
*sock_info
,
770 int sock
, struct ustcomm_ust_msg
*lum
)
772 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
773 enum lttng_ust_bytecode_type type
;
774 const struct lttng_ust_abi_objd_ops
*ops
;
775 uint32_t data_size
, data_size_max
, reloc_offset
;
781 case LTTNG_UST_ABI_FILTER
:
782 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
783 data_size
= lum
->u
.filter
.data_size
;
784 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
785 reloc_offset
= lum
->u
.filter
.reloc_offset
;
786 seqnum
= lum
->u
.filter
.seqnum
;
788 case LTTNG_UST_ABI_CAPTURE
:
789 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
790 data_size
= lum
->u
.capture
.data_size
;
791 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
792 reloc_offset
= lum
->u
.capture
.reloc_offset
;
793 seqnum
= lum
->u
.capture
.seqnum
;
799 if (data_size
> data_size_max
) {
800 ERR("Bytecode %s data size is too large: %u bytes",
801 bytecode_type_str(lum
->cmd
), data_size
);
806 if (reloc_offset
> data_size
) {
807 ERR("Bytecode %s reloc offset %u is not within data",
808 bytecode_type_str(lum
->cmd
), reloc_offset
);
813 /* Allocate the structure AND the `data[]` field. */
814 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
820 bytecode
->bc
.len
= data_size
;
821 bytecode
->bc
.reloc_offset
= reloc_offset
;
822 bytecode
->bc
.seqnum
= seqnum
;
823 bytecode
->type
= type
;
825 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
827 case 0: /* orderly shutdown */
831 if (len
== bytecode
->bc
.len
) {
832 DBG("Bytecode %s data received",
833 bytecode_type_str(lum
->cmd
));
835 } else if (len
< 0) {
836 DBG("Receive failed from lttng-sessiond with errno %d",
838 if (len
== -ECONNRESET
) {
839 ERR("%s remote end closed connection",
847 DBG("Incorrect %s bytecode data message size: %zd",
848 bytecode_type_str(lum
->cmd
), len
);
854 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
861 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
862 (unsigned long) &bytecode
,
873 int handle_message(struct sock_info
*sock_info
,
874 int sock
, struct ustcomm_ust_msg
*lum
)
877 const struct lttng_ust_abi_objd_ops
*ops
;
878 struct ustcomm_ust_reply lur
;
879 union lttng_ust_abi_args args
;
880 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
883 memset(&lur
, 0, sizeof(lur
));
886 ret
= -LTTNG_UST_ERR_EXITING
;
890 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
897 case LTTNG_UST_ABI_REGISTER_DONE
:
898 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
899 ret
= handle_register_done(sock_info
);
903 case LTTNG_UST_ABI_RELEASE
:
904 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
907 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
909 case LTTNG_UST_ABI_CAPTURE
:
910 case LTTNG_UST_ABI_FILTER
:
911 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
915 case LTTNG_UST_ABI_EXCLUSION
:
917 /* Receive exclusion names */
918 struct lttng_ust_excluder_node
*node
;
921 count
= lum
->u
.exclusion
.count
;
923 /* There are no names to read */
927 node
= zmalloc(sizeof(*node
) +
928 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
933 node
->excluder
.count
= count
;
934 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
935 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
937 case 0: /* orderly shutdown */
942 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
943 DBG("Exclusion data received");
945 } else if (len
< 0) {
946 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
947 if (len
== -ECONNRESET
) {
948 ERR("%s remote end closed connection", sock_info
->name
);
957 DBG("Incorrect exclusion data message size: %zd", len
);
964 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
965 (unsigned long) &node
,
972 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
974 int event_notifier_notif_fd
, close_ret
;
976 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
977 &event_notifier_notif_fd
);
979 case 0: /* orderly shutdown */
986 DBG("Receive failed from lttng-sessiond with errno %d",
988 if (len
== -ECONNRESET
) {
989 ERR("%s remote end closed connection",
997 DBG("Incorrect event notifier fd message size: %zd",
1003 args
.event_notifier_handle
.event_notifier_notif_fd
=
1004 event_notifier_notif_fd
;
1006 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1007 (unsigned long) &lum
->u
,
1011 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1012 lttng_ust_lock_fd_tracker();
1013 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1014 lttng_ust_unlock_fd_tracker();
1020 case LTTNG_UST_ABI_CHANNEL
:
1025 len
= ustcomm_recv_channel_from_sessiond(sock
,
1026 &chan_data
, lum
->u
.channel
.len
,
1029 case 0: /* orderly shutdown */
1033 if (len
== lum
->u
.channel
.len
) {
1034 DBG("channel data received");
1036 } else if (len
< 0) {
1037 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1038 if (len
== -ECONNRESET
) {
1039 ERR("%s remote end closed connection", sock_info
->name
);
1046 DBG("incorrect channel data message size: %zd", len
);
1051 args
.channel
.chan_data
= chan_data
;
1052 args
.channel
.wakeup_fd
= wakeup_fd
;
1054 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1055 (unsigned long) &lum
->u
,
1059 if (args
.channel
.wakeup_fd
>= 0) {
1062 lttng_ust_lock_fd_tracker();
1063 close_ret
= close(args
.channel
.wakeup_fd
);
1064 lttng_ust_unlock_fd_tracker();
1065 args
.channel
.wakeup_fd
= -1;
1069 free(args
.channel
.chan_data
);
1072 case LTTNG_UST_ABI_STREAM
:
1076 /* Receive shm_fd, wakeup_fd */
1077 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1079 &args
.stream
.shm_fd
,
1080 &args
.stream
.wakeup_fd
);
1086 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1087 (unsigned long) &lum
->u
,
1091 if (args
.stream
.shm_fd
>= 0) {
1092 lttng_ust_lock_fd_tracker();
1093 close_ret
= close(args
.stream
.shm_fd
);
1094 lttng_ust_unlock_fd_tracker();
1095 args
.stream
.shm_fd
= -1;
1099 if (args
.stream
.wakeup_fd
>= 0) {
1100 lttng_ust_lock_fd_tracker();
1101 close_ret
= close(args
.stream
.wakeup_fd
);
1102 lttng_ust_unlock_fd_tracker();
1103 args
.stream
.wakeup_fd
= -1;
1109 case LTTNG_UST_ABI_CONTEXT
:
1110 switch (lum
->u
.context
.ctx
) {
1111 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1114 size_t ctxlen
, recvlen
;
1116 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1117 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1118 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1119 ERR("Application context string length size is too large: %zu bytes",
1124 strcpy(ctxstr
, "$app.");
1125 p
= &ctxstr
[strlen("$app.")];
1126 recvlen
= ctxlen
- strlen("$app.");
1127 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1129 case 0: /* orderly shutdown */
1133 if (len
== recvlen
) {
1134 DBG("app context data received");
1136 } else if (len
< 0) {
1137 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1138 if (len
== -ECONNRESET
) {
1139 ERR("%s remote end closed connection", sock_info
->name
);
1146 DBG("incorrect app context data message size: %zd", len
);
1151 /* Put : between provider and ctxname. */
1152 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1153 args
.app_context
.ctxname
= ctxstr
;
1160 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1161 (unsigned long) &lum
->u
,
1167 case LTTNG_UST_ABI_COUNTER
:
1171 len
= ustcomm_recv_counter_from_sessiond(sock
,
1172 &counter_data
, lum
->u
.counter
.len
);
1174 case 0: /* orderly shutdown */
1178 if (len
== lum
->u
.counter
.len
) {
1179 DBG("counter data received");
1181 } else if (len
< 0) {
1182 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1183 if (len
== -ECONNRESET
) {
1184 ERR("%s remote end closed connection", sock_info
->name
);
1191 DBG("incorrect counter data message size: %zd", len
);
1196 args
.counter
.counter_data
= counter_data
;
1198 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1199 (unsigned long) &lum
->u
,
1203 free(args
.counter
.counter_data
);
1206 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1208 /* Receive shm_fd */
1209 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1210 &args
.counter_shm
.shm_fd
);
1216 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1217 (unsigned long) &lum
->u
,
1221 if (args
.counter_shm
.shm_fd
>= 0) {
1224 lttng_ust_lock_fd_tracker();
1225 close_ret
= close(args
.counter_shm
.shm_fd
);
1226 lttng_ust_unlock_fd_tracker();
1227 args
.counter_shm
.shm_fd
= -1;
1233 case LTTNG_UST_ABI_COUNTER_CPU
:
1235 /* Receive shm_fd */
1236 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1237 &args
.counter_shm
.shm_fd
);
1243 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1244 (unsigned long) &lum
->u
,
1248 if (args
.counter_shm
.shm_fd
>= 0) {
1251 lttng_ust_lock_fd_tracker();
1252 close_ret
= close(args
.counter_shm
.shm_fd
);
1253 lttng_ust_unlock_fd_tracker();
1254 args
.counter_shm
.shm_fd
= -1;
1260 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1262 /* Receive struct lttng_ust_event_notifier */
1263 struct lttng_ust_abi_event_notifier event_notifier
;
1265 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1266 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1270 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1272 case 0: /* orderly shutdown */
1276 if (len
== sizeof(event_notifier
)) {
1277 DBG("event notifier data received");
1279 } else if (len
< 0) {
1280 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1281 if (len
== -ECONNRESET
) {
1282 ERR("%s remote end closed connection", sock_info
->name
);
1289 DBG("incorrect event notifier data message size: %zd", len
);
1295 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1296 (unsigned long) &event_notifier
,
1305 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1306 (unsigned long) &lum
->u
,
1313 lur
.handle
= lum
->handle
;
1317 lur
.ret_code
= LTTNG_UST_OK
;
1320 * Use -LTTNG_UST_ERR as wildcard for UST internal
1321 * error that are not caused by the transport, except if
1322 * we already have a more precise error message to
1325 if (ret
> -LTTNG_UST_ERR
) {
1326 /* Translate code to UST error. */
1329 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1332 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1335 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1338 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1341 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1344 lur
.ret_code
= -LTTNG_UST_ERR
;
1353 case LTTNG_UST_ABI_TRACER_VERSION
:
1354 lur
.u
.version
= lum
->u
.version
;
1356 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1357 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1361 DBG("Return value: %d", lur
.ret_val
);
1366 * Performed delayed statedump operations outside of the UST
1367 * lock. We need to take the dynamic loader lock before we take
1368 * the UST lock internally within handle_pending_statedump().
1370 handle_pending_statedump(sock_info
);
1373 ret
= -LTTNG_UST_ERR_EXITING
;
1377 ret
= send_reply(sock
, &lur
);
1379 DBG("error sending reply");
1384 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1387 if (lur
.ret_code
== LTTNG_UST_OK
) {
1389 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1390 len
= ustcomm_send_unix_sock(sock
,
1391 &args
.field_list
.entry
,
1392 sizeof(args
.field_list
.entry
));
1397 if (len
!= sizeof(args
.field_list
.entry
)) {
1411 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1415 if (sock_info
->root_handle
!= -1) {
1416 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1418 ERR("Error unref root handle");
1420 sock_info
->root_handle
= -1;
1422 sock_info
->registration_done
= 0;
1423 sock_info
->initial_statedump_done
= 0;
1426 * wait_shm_mmap, socket and notify socket are used by listener
1427 * threads outside of the ust lock, so we cannot tear them down
1428 * ourselves, because we cannot join on these threads. Leave
1429 * responsibility of cleaning up these resources to the OS
1435 if (sock_info
->socket
!= -1) {
1436 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1438 ERR("Error closing ust cmd socket");
1440 sock_info
->socket
= -1;
1442 if (sock_info
->notify_socket
!= -1) {
1443 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1445 ERR("Error closing ust notify socket");
1447 sock_info
->notify_socket
= -1;
1449 if (sock_info
->wait_shm_mmap
) {
1452 page_size
= LTTNG_UST_PAGE_SIZE
;
1453 if (page_size
<= 0) {
1457 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1459 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1461 ERR("Error unmapping wait shm");
1464 sock_info
->wait_shm_mmap
= NULL
;
1469 * Using fork to set umask in the child process (not multi-thread safe).
1470 * We deal with the shm_open vs ftruncate race (happening when the
1471 * sessiond owns the shm and does not let everybody modify it, to ensure
1472 * safety against shm_unlink) by simply letting the mmap fail and
1473 * retrying after a few seconds.
1474 * For global shm, everybody has rw access to it until the sessiond
1478 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1480 int wait_shm_fd
, ret
;
1484 * Try to open read-only.
1486 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1487 if (wait_shm_fd
>= 0) {
1490 size_t bytes_read
= 0;
1493 * Try to read the fd. If unable to do so, try opening
1497 len
= read(wait_shm_fd
,
1498 &((char *) &tmp_read
)[bytes_read
],
1499 sizeof(tmp_read
) - bytes_read
);
1503 } while ((len
< 0 && errno
== EINTR
)
1504 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1505 if (bytes_read
!= sizeof(tmp_read
)) {
1506 ret
= close(wait_shm_fd
);
1508 ERR("close wait_shm_fd");
1513 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1515 * Real-only open did not work, and it's not because the
1516 * entry was not present. It's a failure that prohibits
1519 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1525 * If the open failed because the file did not exist, or because
1526 * the file was not truncated yet, try creating it ourself.
1528 URCU_TLS(lttng_ust_nest_count
)++;
1530 URCU_TLS(lttng_ust_nest_count
)--;
1535 * Parent: wait for child to return, in which case the
1536 * shared memory map will have been created.
1538 pid
= wait(&status
);
1539 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1544 * Try to open read-only again after creation.
1546 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1547 if (wait_shm_fd
< 0) {
1549 * Real-only open did not work. It's a failure
1550 * that prohibits using shm.
1552 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1556 } else if (pid
== 0) {
1560 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1561 if (sock_info
->global
)
1562 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1564 * We're alone in a child process, so we can modify the
1565 * process-wide umask.
1567 umask(~create_mode
);
1569 * Try creating shm (or get rw access).
1570 * We don't do an exclusive open, because we allow other
1571 * processes to create+ftruncate it concurrently.
1573 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1574 O_RDWR
| O_CREAT
, create_mode
);
1575 if (wait_shm_fd
>= 0) {
1576 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1578 PERROR("ftruncate");
1579 _exit(EXIT_FAILURE
);
1581 _exit(EXIT_SUCCESS
);
1584 * For local shm, we need to have rw access to accept
1585 * opening it: this means the local sessiond will be
1586 * able to wake us up. For global shm, we open it even
1587 * if rw access is not granted, because the root.root
1588 * sessiond will be able to override all rights and wake
1591 if (!sock_info
->global
&& errno
!= EACCES
) {
1592 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1593 _exit(EXIT_FAILURE
);
1596 * The shm exists, but we cannot open it RW. Report
1599 _exit(EXIT_SUCCESS
);
1604 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1605 struct stat statbuf
;
1608 * Ensure that our user is the owner of the shm file for
1609 * local shm. If we do not own the file, it means our
1610 * sessiond will not have access to wake us up (there is
1611 * probably a rogue process trying to fake our
1612 * sessiond). Fallback to polling method in this case.
1614 ret
= fstat(wait_shm_fd
, &statbuf
);
1619 if (statbuf
.st_uid
!= getuid())
1625 ret
= close(wait_shm_fd
);
1627 PERROR("Error closing fd");
1633 char *get_map_shm(struct sock_info
*sock_info
)
1636 int wait_shm_fd
, ret
;
1637 char *wait_shm_mmap
;
1639 page_size
= sysconf(_SC_PAGE_SIZE
);
1640 if (page_size
<= 0) {
1644 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1648 lttng_ust_lock_fd_tracker();
1649 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1650 if (wait_shm_fd
< 0) {
1651 lttng_ust_unlock_fd_tracker();
1655 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1657 ret
= close(wait_shm_fd
);
1659 PERROR("Error closing fd");
1661 lttng_ust_unlock_fd_tracker();
1666 lttng_ust_unlock_fd_tracker();
1668 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1669 MAP_SHARED
, wait_shm_fd
, 0);
1671 /* close shm fd immediately after taking the mmap reference */
1672 lttng_ust_lock_fd_tracker();
1673 ret
= close(wait_shm_fd
);
1675 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1677 PERROR("Error closing fd");
1679 lttng_ust_unlock_fd_tracker();
1681 if (wait_shm_mmap
== MAP_FAILED
) {
1682 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1685 return wait_shm_mmap
;
1692 void wait_for_sessiond(struct sock_info
*sock_info
)
1694 /* Use ust_lock to check if we should quit. */
1698 if (wait_poll_fallback
) {
1703 assert(sock_info
->wait_shm_mmap
);
1705 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1706 /* Wait for futex wakeup */
1707 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1710 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1711 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1714 /* Value already changed. */
1717 /* Retry if interrupted by signal. */
1718 break; /* Get out of switch. */
1720 wait_poll_fallback
= 1;
1722 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1723 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1724 "Please upgrade your kernel "
1725 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1726 "mainline). LTTng-UST will use polling mode fallback.");
1727 if (ust_err_debug_enabled())
1745 * This thread does not allocate any resource, except within
1746 * handle_message, within mutex protection. This mutex protects against
1748 * The other moment it allocates resources is at socket connection, which
1749 * is also protected by the mutex.
1752 void *ust_listener_thread(void *arg
)
1754 struct sock_info
*sock_info
= arg
;
1755 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1758 lttng_ust_fixup_tls();
1760 * If available, add '-ust' to the end of this thread's
1763 ret
= lttng_ust_setustprocname();
1765 ERR("Unable to set UST process name");
1768 /* Restart trying to connect to the session daemon */
1770 if (prev_connect_failed
) {
1771 /* Wait for sessiond availability with pipe */
1772 wait_for_sessiond(sock_info
);
1776 * Sleep for 5 seconds before retrying after a
1777 * sequence of failure / wait / failure. This
1778 * deals with a killed or broken session daemon.
1784 prev_connect_failed
= 0;
1791 if (sock_info
->socket
!= -1) {
1792 /* FD tracker is updated by ustcomm_close_unix_sock() */
1793 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1795 ERR("Error closing %s ust cmd socket",
1798 sock_info
->socket
= -1;
1800 if (sock_info
->notify_socket
!= -1) {
1801 /* FD tracker is updated by ustcomm_close_unix_sock() */
1802 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1804 ERR("Error closing %s ust notify socket",
1807 sock_info
->notify_socket
= -1;
1812 * Register. We need to perform both connect and sending
1813 * registration message before doing the next connect otherwise
1814 * we may reach unix socket connect queue max limits and block
1815 * on the 2nd connect while the session daemon is awaiting the
1816 * first connect registration message.
1818 /* Connect cmd socket */
1819 lttng_ust_lock_fd_tracker();
1820 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1821 get_connect_sock_timeout());
1823 lttng_ust_unlock_fd_tracker();
1824 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1825 prev_connect_failed
= 1;
1828 * If we cannot find the sessiond daemon, don't delay
1829 * constructor execution.
1831 ret
= handle_register_failed(sock_info
);
1837 ret
= lttng_ust_add_fd_to_tracker(fd
);
1841 PERROR("close on sock_info->socket");
1844 lttng_ust_unlock_fd_tracker();
1849 sock_info
->socket
= ret
;
1850 lttng_ust_unlock_fd_tracker();
1854 * Unlock/relock ust lock because connect is blocking (with
1855 * timeout). Don't delay constructors on the ust lock for too
1863 * Create only one root handle per listener thread for the whole
1864 * process lifetime, so we ensure we get ID which is statically
1865 * assigned to the root handle.
1867 if (sock_info
->root_handle
== -1) {
1868 ret
= lttng_abi_create_root_handle();
1870 ERR("Error creating root handle");
1873 sock_info
->root_handle
= ret
;
1876 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1878 ERR("Error registering to %s ust cmd socket",
1880 prev_connect_failed
= 1;
1882 * If we cannot register to the sessiond daemon, don't
1883 * delay constructor execution.
1885 ret
= handle_register_failed(sock_info
);
1893 * Unlock/relock ust lock because connect is blocking (with
1894 * timeout). Don't delay constructors on the ust lock for too
1901 /* Connect notify socket */
1902 lttng_ust_lock_fd_tracker();
1903 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1904 get_connect_sock_timeout());
1906 lttng_ust_unlock_fd_tracker();
1907 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1908 prev_connect_failed
= 1;
1911 * If we cannot find the sessiond daemon, don't delay
1912 * constructor execution.
1914 ret
= handle_register_failed(sock_info
);
1921 ret
= lttng_ust_add_fd_to_tracker(fd
);
1925 PERROR("close on sock_info->notify_socket");
1928 lttng_ust_unlock_fd_tracker();
1933 sock_info
->notify_socket
= ret
;
1934 lttng_ust_unlock_fd_tracker();
1938 * Unlock/relock ust lock because connect is blocking (with
1939 * timeout). Don't delay constructors on the ust lock for too
1946 timeout
= get_notify_sock_timeout();
1949 * Give at least 10ms to sessiond to reply to
1954 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1957 WARN("Error setting socket receive timeout");
1959 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1962 WARN("Error setting socket send timeout");
1964 } else if (timeout
< -1) {
1965 WARN("Unsupported timeout value %ld", timeout
);
1968 ret
= register_to_sessiond(sock_info
->notify_socket
,
1969 USTCTL_SOCKET_NOTIFY
);
1971 ERR("Error registering to %s ust notify socket",
1973 prev_connect_failed
= 1;
1975 * If we cannot register to the sessiond daemon, don't
1976 * delay constructor execution.
1978 ret
= handle_register_failed(sock_info
);
1983 sock
= sock_info
->socket
;
1989 struct ustcomm_ust_msg lum
;
1991 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1993 case 0: /* orderly shutdown */
1994 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1999 * Either sessiond has shutdown or refused us by closing the socket.
2000 * In either case, we don't want to delay construction execution,
2001 * and we need to wait before retry.
2003 prev_connect_failed
= 1;
2005 * If we cannot register to the sessiond daemon, don't
2006 * delay constructor execution.
2008 ret
= handle_register_failed(sock_info
);
2013 print_cmd(lum
.cmd
, lum
.handle
);
2014 ret
= handle_message(sock_info
, sock
, &lum
);
2016 ERR("Error handling message for %s socket",
2019 * Close socket if protocol error is
2027 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2029 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2031 if (len
== -ECONNRESET
) {
2032 DBG("%s remote end closed connection", sock_info
->name
);
2043 /* Cleanup socket handles before trying to reconnect */
2044 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2046 goto restart
; /* try to reconnect */
2051 pthread_mutex_lock(&ust_exit_mutex
);
2052 sock_info
->thread_active
= 0;
2053 pthread_mutex_unlock(&ust_exit_mutex
);
2058 * Weak symbol to call when the ust malloc wrapper is not loaded.
2060 __attribute__((weak
))
2061 void lttng_ust_libc_wrapper_malloc_init(void)
2065 void lttng_ust_ring_buffer_clients_init(void)
2067 lttng_ring_buffer_metadata_client_init();
2068 lttng_ring_buffer_client_overwrite_init();
2069 lttng_ring_buffer_client_overwrite_rt_init();
2070 lttng_ring_buffer_client_discard_init();
2071 lttng_ring_buffer_client_discard_rt_init();
2074 void lttng_ust_ring_buffer_clients_exit(void)
2076 lttng_ring_buffer_client_discard_rt_exit();
2077 lttng_ring_buffer_client_discard_exit();
2078 lttng_ring_buffer_client_overwrite_rt_exit();
2079 lttng_ring_buffer_client_overwrite_exit();
2080 lttng_ring_buffer_metadata_client_exit();
2083 void lttng_ust_counter_clients_init(void)
2085 lttng_counter_client_percpu_64_modular_init();
2086 lttng_counter_client_percpu_32_modular_init();
2089 void lttng_ust_counter_clients_exit(void)
2091 lttng_counter_client_percpu_32_modular_exit();
2092 lttng_counter_client_percpu_64_modular_exit();
2096 * sessiond monitoring thread: monitor presence of global and per-user
2097 * sessiond by polling the application common named pipe.
2099 void __attribute__((constructor
)) lttng_ust_init(void)
2101 struct timespec constructor_timeout
;
2102 sigset_t sig_all_blocked
, orig_parent_mask
;
2103 pthread_attr_t thread_attr
;
2108 if (uatomic_xchg(&initialized
, 1) == 1)
2112 * Fixup interdependency between TLS fixup mutex (which happens
2113 * to be the dynamic linker mutex) and ust_lock, taken within
2116 lttng_ust_fixup_tls();
2118 lttng_ust_loaded
= 1;
2121 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2122 * the unloading of code used by the ust_listener_threads as we can not
2123 * reliably know when they exited. To do that, manually load
2124 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2125 * this library so it never becomes zero, thus never gets unloaded from the
2126 * address space of the process. Since we are already running in the
2127 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2128 * simply increment the refcount and no additionnal work is needed by the
2129 * dynamic loader as the shared library is already loaded in the address
2130 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2131 * unloading of the UST library if its refcount becomes zero (which should
2132 * never happen). Do the return value check but discard the handle at the
2133 * end of the function as it's not needed.
2135 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2137 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2141 * We want precise control over the order in which we construct
2142 * our sub-libraries vs starting to receive commands from
2143 * sessiond (otherwise leading to errors when trying to create
2144 * sessiond before the init functions are completed).
2147 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2148 lttng_ust_tp_init();
2149 lttng_ust_init_fd_tracker();
2150 lttng_ust_clock_init();
2151 lttng_ust_getcpu_init();
2152 lttng_ust_statedump_init();
2153 lttng_ust_ring_buffer_clients_init();
2154 lttng_ust_counter_clients_init();
2155 lttng_perf_counter_init();
2157 * Invoke ust malloc wrapper init before starting other threads.
2159 lttng_ust_libc_wrapper_malloc_init();
2161 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2163 get_allow_blocking();
2165 ret
= sem_init(&constructor_wait
, 0, 0);
2170 ret
= setup_global_apps();
2172 assert(global_apps
.allowed
== 0);
2173 DBG("global apps setup returned %d", ret
);
2176 ret
= setup_local_apps();
2178 assert(local_apps
.allowed
== 0);
2179 DBG("local apps setup returned %d", ret
);
2182 /* A new thread created by pthread_create inherits the signal mask
2183 * from the parent. To avoid any signal being received by the
2184 * listener thread, we block all signals temporarily in the parent,
2185 * while we create the listener thread.
2187 sigfillset(&sig_all_blocked
);
2188 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2190 ERR("pthread_sigmask: %s", strerror(ret
));
2193 ret
= pthread_attr_init(&thread_attr
);
2195 ERR("pthread_attr_init: %s", strerror(ret
));
2197 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2199 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2202 if (global_apps
.allowed
) {
2203 pthread_mutex_lock(&ust_exit_mutex
);
2204 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2205 ust_listener_thread
, &global_apps
);
2207 ERR("pthread_create global: %s", strerror(ret
));
2209 global_apps
.thread_active
= 1;
2210 pthread_mutex_unlock(&ust_exit_mutex
);
2212 handle_register_done(&global_apps
);
2215 if (local_apps
.allowed
) {
2216 pthread_mutex_lock(&ust_exit_mutex
);
2217 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2218 ust_listener_thread
, &local_apps
);
2220 ERR("pthread_create local: %s", strerror(ret
));
2222 local_apps
.thread_active
= 1;
2223 pthread_mutex_unlock(&ust_exit_mutex
);
2225 handle_register_done(&local_apps
);
2227 ret
= pthread_attr_destroy(&thread_attr
);
2229 ERR("pthread_attr_destroy: %s", strerror(ret
));
2232 /* Restore original signal mask in parent */
2233 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2235 ERR("pthread_sigmask: %s", strerror(ret
));
2238 switch (timeout_mode
) {
2239 case 1: /* timeout wait */
2241 ret
= sem_timedwait(&constructor_wait
,
2242 &constructor_timeout
);
2243 } while (ret
< 0 && errno
== EINTR
);
2247 ERR("Timed out waiting for lttng-sessiond");
2250 PERROR("sem_timedwait");
2253 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2258 case -1:/* wait forever */
2260 ret
= sem_wait(&constructor_wait
);
2261 } while (ret
< 0 && errno
== EINTR
);
2268 ERR("Unexpected error \"%s\" returned by sem_wait",
2273 case 0: /* no timeout */
2279 void lttng_ust_cleanup(int exiting
)
2281 cleanup_sock_info(&global_apps
, exiting
);
2282 cleanup_sock_info(&local_apps
, exiting
);
2283 local_apps
.allowed
= 0;
2284 global_apps
.allowed
= 0;
2286 * The teardown in this function all affect data structures
2287 * accessed under the UST lock by the listener thread. This
2288 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2289 * that none of these threads are accessing this data at this
2292 lttng_ust_abi_exit();
2293 lttng_ust_abi_events_exit();
2294 lttng_perf_counter_exit();
2295 lttng_ust_ring_buffer_clients_exit();
2296 lttng_ust_counter_clients_exit();
2297 lttng_ust_statedump_destroy();
2298 lttng_ust_tp_exit();
2300 /* Reinitialize values for fork */
2301 sem_count
= sem_count_initial_value
;
2302 lttng_ust_comm_should_quit
= 0;
2307 void __attribute__((destructor
)) lttng_ust_exit(void)
2312 * Using pthread_cancel here because:
2313 * A) we don't want to hang application teardown.
2314 * B) the thread is not allocating any resource.
2318 * Require the communication thread to quit. Synchronize with
2319 * mutexes to ensure it is not in a mutex critical section when
2320 * pthread_cancel is later called.
2323 lttng_ust_comm_should_quit
= 1;
2326 pthread_mutex_lock(&ust_exit_mutex
);
2327 /* cancel threads */
2328 if (global_apps
.thread_active
) {
2329 ret
= pthread_cancel(global_apps
.ust_listener
);
2331 ERR("Error cancelling global ust listener thread: %s",
2334 global_apps
.thread_active
= 0;
2337 if (local_apps
.thread_active
) {
2338 ret
= pthread_cancel(local_apps
.ust_listener
);
2340 ERR("Error cancelling local ust listener thread: %s",
2343 local_apps
.thread_active
= 0;
2346 pthread_mutex_unlock(&ust_exit_mutex
);
2349 * Do NOT join threads: use of sys_futex makes it impossible to
2350 * join the threads without using async-cancel, but async-cancel
2351 * is delivered by a signal, which could hit the target thread
2352 * anywhere in its code path, including while the ust_lock() is
2353 * held, causing a deadlock for the other thread. Let the OS
2354 * cleanup the threads if there are stalled in a syscall.
2356 lttng_ust_cleanup(1);
2360 void ust_context_ns_reset(void)
2362 lttng_context_pid_ns_reset();
2363 lttng_context_cgroup_ns_reset();
2364 lttng_context_ipc_ns_reset();
2365 lttng_context_mnt_ns_reset();
2366 lttng_context_net_ns_reset();
2367 lttng_context_user_ns_reset();
2368 lttng_context_time_ns_reset();
2369 lttng_context_uts_ns_reset();
2373 void ust_context_vuids_reset(void)
2375 lttng_context_vuid_reset();
2376 lttng_context_veuid_reset();
2377 lttng_context_vsuid_reset();
2381 void ust_context_vgids_reset(void)
2383 lttng_context_vgid_reset();
2384 lttng_context_vegid_reset();
2385 lttng_context_vsgid_reset();
2389 * We exclude the worker threads across fork and clone (except
2390 * CLONE_VM), because these system calls only keep the forking thread
2391 * running in the child. Therefore, we don't want to call fork or clone
2392 * in the middle of an tracepoint or ust tracing state modification.
2393 * Holding this mutex protects these structures across fork and clone.
2395 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2398 * Disable signals. This is to avoid that the child intervenes
2399 * before it is properly setup for tracing. It is safer to
2400 * disable all signals, because then we know we are not breaking
2401 * anything by restoring the original mask.
2406 /* Fixup lttng-ust TLS. */
2407 lttng_ust_fixup_tls();
2409 if (URCU_TLS(lttng_ust_nest_count
))
2411 /* Disable signals */
2412 sigfillset(&all_sigs
);
2413 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2415 PERROR("sigprocmask");
2418 pthread_mutex_lock(&ust_fork_mutex
);
2421 lttng_ust_urcu_before_fork();
2422 lttng_ust_lock_fd_tracker();
2426 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2430 DBG("process %d", getpid());
2431 lttng_perf_unlock();
2432 lttng_ust_unlock_fd_tracker();
2435 pthread_mutex_unlock(&ust_fork_mutex
);
2437 /* Restore signals */
2438 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2440 PERROR("sigprocmask");
2444 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2446 if (URCU_TLS(lttng_ust_nest_count
))
2448 DBG("process %d", getpid());
2449 lttng_ust_urcu_after_fork_parent();
2450 /* Release mutexes and reenable signals */
2451 ust_after_fork_common(restore_sigset
);
2455 * After fork, in the child, we need to cleanup all the leftover state,
2456 * except the worker thread which already magically disappeared thanks
2457 * to the weird Linux fork semantics. After tyding up, we call
2458 * lttng_ust_init() again to start over as a new PID.
2460 * This is meant for forks() that have tracing in the child between the
2461 * fork and following exec call (if there is any).
2463 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2465 if (URCU_TLS(lttng_ust_nest_count
))
2467 lttng_context_vpid_reset();
2468 lttng_context_vtid_reset();
2469 lttng_ust_context_procname_reset();
2470 ust_context_ns_reset();
2471 ust_context_vuids_reset();
2472 ust_context_vgids_reset();
2473 DBG("process %d", getpid());
2474 /* Release urcu mutexes */
2475 lttng_ust_urcu_after_fork_child();
2476 lttng_ust_cleanup(0);
2477 /* Release mutexes and reenable signals */
2478 ust_after_fork_common(restore_sigset
);
2482 void lttng_ust_after_setns(void)
2484 ust_context_ns_reset();
2485 ust_context_vuids_reset();
2486 ust_context_vgids_reset();
2489 void lttng_ust_after_unshare(void)
2491 ust_context_ns_reset();
2492 ust_context_vuids_reset();
2493 ust_context_vgids_reset();
2496 void lttng_ust_after_setuid(void)
2498 ust_context_vuids_reset();
2501 void lttng_ust_after_seteuid(void)
2503 ust_context_vuids_reset();
2506 void lttng_ust_after_setreuid(void)
2508 ust_context_vuids_reset();
2511 void lttng_ust_after_setresuid(void)
2513 ust_context_vuids_reset();
2516 void lttng_ust_after_setgid(void)
2518 ust_context_vgids_reset();
2521 void lttng_ust_after_setegid(void)
2523 ust_context_vgids_reset();
2526 void lttng_ust_after_setregid(void)
2528 ust_context_vgids_reset();
2531 void lttng_ust_after_setresgid(void)
2533 ust_context_vgids_reset();
2536 void lttng_ust_sockinfo_session_enabled(void *owner
)
2538 struct sock_info
*sock_info
= owner
;
2539 sock_info
->statedump_pending
= 1;