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-thread.h>
40 #include <lttng/ust-tracer.h>
41 #include <urcu/tls-compat.h>
44 #include <usterr-signal-safe.h>
45 #include <ust-helper.h>
46 #include "tracepoint-internal.h"
47 #include "lttng-tracer-core.h"
49 #include "../libringbuffer/rb-init.h"
50 #include "lttng-ust-statedump.h"
52 #include "../libringbuffer/getcpu.h"
54 #include "ust-events-internal.h"
55 #include "context-internal.h"
56 #include "ust-compat.h"
57 #include "lttng-counter-client.h"
58 #include "lttng-rb-clients.h"
61 * Has lttng ust comm constructor been called ?
63 static int initialized
;
66 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
67 * Held when handling a command, also held by fork() to deal with
68 * removal of threads, and by exit path.
70 * The UST lock is the centralized mutex across UST tracing control and
73 * ust_exit_mutex must never nest in ust_mutex.
75 * ust_fork_mutex must never nest in ust_mutex.
77 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
78 * counter lazy initialization called by events within the statedump,
79 * which traces while the ust_mutex is held.
81 * ust_lock nests within the dynamic loader lock (within glibc) because
82 * it is taken within the library constructor.
84 * The ust fd tracker lock nests within the ust_mutex.
86 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 /* Allow nesting the ust_mutex within the same thread. */
89 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
92 * ust_exit_mutex protects thread_active variable wrt thread exit. It
93 * cannot be done by ust_mutex because pthread_cancel(), which takes an
94 * internal libc lock, cannot nest within ust_mutex.
96 * It never nests within a ust_mutex.
98 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
101 * ust_fork_mutex protects base address statedump tracing against forks. It
102 * prevents the dynamic loader lock to be taken (by base address statedump
103 * tracing) while a fork is happening, thus preventing deadlock issues with
104 * the dynamic loader lock.
106 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 /* Should the ust comm thread quit ? */
109 static int lttng_ust_comm_should_quit
;
112 * This variable can be tested by applications to check whether
113 * lttng-ust is loaded. They simply have to define their own
114 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
115 * library constructor.
117 int lttng_ust_loaded
__attribute__((weak
));
120 * Return 0 on success, -1 if should quit.
121 * The lock is taken in both cases.
126 sigset_t sig_all_blocked
, orig_mask
;
129 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
131 ERR("pthread_setcancelstate: %s", strerror(ret
));
133 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
134 ERR("pthread_setcancelstate: unexpected oldstate");
136 sigfillset(&sig_all_blocked
);
137 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
139 ERR("pthread_sigmask: %s", strerror(ret
));
141 if (!URCU_TLS(ust_mutex_nest
)++)
142 pthread_mutex_lock(&ust_mutex
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (lttng_ust_comm_should_quit
) {
155 * ust_lock_nocheck() can be used in constructors/destructors, because
156 * they are already nested within the dynamic loader lock, and therefore
157 * have exclusive access against execution of liblttng-ust destructor.
160 void ust_lock_nocheck(void)
162 sigset_t sig_all_blocked
, orig_mask
;
165 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
167 ERR("pthread_setcancelstate: %s", strerror(ret
));
169 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
170 ERR("pthread_setcancelstate: unexpected oldstate");
172 sigfillset(&sig_all_blocked
);
173 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
175 ERR("pthread_sigmask: %s", strerror(ret
));
177 if (!URCU_TLS(ust_mutex_nest
)++)
178 pthread_mutex_lock(&ust_mutex
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
188 void ust_unlock(void)
190 sigset_t sig_all_blocked
, orig_mask
;
193 sigfillset(&sig_all_blocked
);
194 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
196 ERR("pthread_sigmask: %s", strerror(ret
));
198 if (!--URCU_TLS(ust_mutex_nest
))
199 pthread_mutex_unlock(&ust_mutex
);
200 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
202 ERR("pthread_sigmask: %s", strerror(ret
));
204 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
206 ERR("pthread_setcancelstate: %s", strerror(ret
));
208 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
209 ERR("pthread_setcancelstate: unexpected oldstate");
214 * Wait for either of these before continuing to the main
216 * - the register_done message from sessiond daemon
217 * (will let the sessiond daemon enable sessions before main
219 * - sessiond daemon is not reachable.
220 * - timeout (ensuring applications are resilient to session
223 static sem_t constructor_wait
;
225 * Doing this for both the global and local sessiond.
228 sem_count_initial_value
= 4,
231 static int sem_count
= sem_count_initial_value
;
234 * Counting nesting within lttng-ust. Used to ensure that calling fork()
235 * from liblttng-ust does not execute the pre/post fork handlers.
237 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
240 * Info about socket and associated listener thread.
244 pthread_t ust_listener
; /* listener thread */
246 int registration_done
;
251 char sock_path
[PATH_MAX
];
255 char wait_shm_path
[PATH_MAX
];
257 /* Keep track of lazy state dump not performed yet. */
258 int statedump_pending
;
259 int initial_statedump_done
;
260 /* Keep procname for statedump */
261 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
264 /* Socket from app (connect) to session daemon (listen) for communication */
265 struct sock_info global_apps
= {
270 .registration_done
= 0,
274 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
278 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
280 .statedump_pending
= 0,
281 .initial_statedump_done
= 0,
285 /* TODO: allow global_apps_sock_path override */
287 struct sock_info local_apps
= {
291 .registration_done
= 0,
292 .allowed
= 0, /* Check setuid bit first */
298 .statedump_pending
= 0,
299 .initial_statedump_done
= 0,
303 static int wait_poll_fallback
;
305 static const char *cmd_name_mapping
[] = {
306 [ LTTNG_UST_ABI_RELEASE
] = "Release",
307 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
308 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
310 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
311 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
312 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
313 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
315 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
317 /* Session FD commands */
318 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
319 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
320 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
322 /* Channel FD commands */
323 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
324 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
326 /* Event and Channel FD commands */
327 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
328 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
330 /* Event, Channel and Session commands */
331 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
332 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
334 /* Tracepoint list commands */
335 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
336 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
338 /* Event FD commands */
339 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
340 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
342 /* Event notifier group commands */
343 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
345 /* Session and event notifier group commands */
346 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
348 /* Counter commands */
349 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
350 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
353 static const char *str_timeout
;
354 static int got_timeout_env
;
356 static char *get_map_shm(struct sock_info
*sock_info
);
358 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
361 size_t copied
= 0, to_copy
= len
;
364 ret
= read(fd
, buf
+ copied
, to_copy
);
369 } while ((ret
> 0 && to_copy
> 0)
370 || (ret
< 0 && errno
== EINTR
));
377 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
381 const char *get_lttng_home_dir(void)
385 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
389 return (const char *) lttng_ust_getenv("HOME");
393 * Force a read (imply TLS fixup for dlopen) of TLS variables.
396 void lttng_fixup_nest_count_tls(void)
398 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
402 void lttng_fixup_ust_mutex_nest_tls(void)
404 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
408 * Fixup lttng-ust urcu TLS.
411 void lttng_fixup_lttng_ust_urcu_tls(void)
413 (void) lttng_ust_urcu_read_ongoing();
416 void lttng_ust_fixup_tls(void)
418 lttng_fixup_lttng_ust_urcu_tls();
419 lttng_fixup_ringbuffer_tls();
420 lttng_fixup_vtid_tls();
421 lttng_fixup_nest_count_tls();
422 lttng_fixup_procname_tls();
423 lttng_fixup_ust_mutex_nest_tls();
424 lttng_ust_fixup_perf_counter_tls();
425 lttng_ust_fixup_fd_tracker_tls();
426 lttng_fixup_cgroup_ns_tls();
427 lttng_fixup_ipc_ns_tls();
428 lttng_fixup_net_ns_tls();
429 lttng_fixup_time_ns_tls();
430 lttng_fixup_uts_ns_tls();
431 lttng_ust_fixup_ring_buffer_client_discard_tls();
432 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
433 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
434 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
438 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
439 * model because many versions of glibc don't preallocate a pool large
440 * enough for TLS variables IE model defined in other shared libraries,
441 * and causes issues when using LTTng-UST for Java tracing.
443 * Because of this use of Global Dynamic TLS variables, users wishing to
444 * trace from signal handlers need to explicitly trigger the lazy
445 * allocation of those variables for each thread before using them.
446 * This can be triggered by calling lttng_ust_init_thread().
448 void lttng_ust_init_thread(void)
451 * Because those TLS variables are global dynamic, we need to
452 * ensure those are initialized before a signal handler nesting over
453 * this thread attempts to use them.
455 lttng_ust_fixup_tls();
458 int lttng_get_notify_socket(void *owner
)
460 struct sock_info
*info
= owner
;
462 return info
->notify_socket
;
466 char* lttng_ust_sockinfo_get_procname(void *owner
)
468 struct sock_info
*info
= owner
;
470 return info
->procname
;
474 void print_cmd(int cmd
, int handle
)
476 const char *cmd_name
= "Unknown";
478 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
479 && cmd_name_mapping
[cmd
]) {
480 cmd_name
= cmd_name_mapping
[cmd
];
482 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
484 lttng_ust_obj_get_name(handle
), handle
);
488 int setup_global_apps(void)
491 assert(!global_apps
.wait_shm_mmap
);
493 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
494 if (!global_apps
.wait_shm_mmap
) {
495 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
496 global_apps
.allowed
= 0;
501 global_apps
.allowed
= 1;
502 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
507 int setup_local_apps(void)
510 const char *home_dir
;
513 assert(!local_apps
.wait_shm_mmap
);
517 * Disallow per-user tracing for setuid binaries.
519 if (uid
!= geteuid()) {
520 assert(local_apps
.allowed
== 0);
524 home_dir
= get_lttng_home_dir();
526 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
527 assert(local_apps
.allowed
== 0);
531 local_apps
.allowed
= 1;
532 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
534 LTTNG_DEFAULT_HOME_RUNDIR
,
535 LTTNG_UST_SOCK_FILENAME
);
536 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
537 LTTNG_UST_WAIT_FILENAME
,
540 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
541 if (!local_apps
.wait_shm_mmap
) {
542 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
543 local_apps
.allowed
= 0;
548 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
554 * Get socket timeout, in ms.
555 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
558 long get_timeout(void)
560 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
562 if (!got_timeout_env
) {
563 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
567 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
568 /* All negative values are considered as "-1". */
569 if (constructor_delay_ms
< -1)
570 constructor_delay_ms
= -1;
571 return constructor_delay_ms
;
574 /* Timeout for notify socket send and recv. */
576 long get_notify_sock_timeout(void)
578 return get_timeout();
581 /* Timeout for connecting to cmd and notify sockets. */
583 long get_connect_sock_timeout(void)
585 return get_timeout();
589 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
592 int get_constructor_timeout(struct timespec
*constructor_timeout
)
594 long constructor_delay_ms
;
597 constructor_delay_ms
= get_timeout();
599 switch (constructor_delay_ms
) {
600 case -1:/* fall-through */
602 return constructor_delay_ms
;
608 * If we are unable to find the current time, don't wait.
610 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
615 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
616 constructor_timeout
->tv_nsec
+=
617 (constructor_delay_ms
% 1000UL) * 1000000UL;
618 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
619 constructor_timeout
->tv_sec
++;
620 constructor_timeout
->tv_nsec
-= 1000000000UL;
622 /* Timeout wait (constructor_delay_ms). */
627 void get_allow_blocking(void)
629 const char *str_allow_blocking
=
630 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
632 if (str_allow_blocking
) {
633 DBG("%s environment variable is set",
634 "LTTNG_UST_ALLOW_BLOCKING");
635 lttng_ust_ringbuffer_set_allow_blocking();
640 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
642 return ustcomm_send_reg_msg(socket
,
645 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
646 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
647 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
648 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
649 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
653 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
657 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
660 DBG("message successfully sent");
663 if (len
== -ECONNRESET
) {
664 DBG("remote end closed connection");
669 DBG("incorrect message size: %zd", len
);
675 void decrement_sem_count(unsigned int count
)
679 assert(uatomic_read(&sem_count
) >= count
);
681 if (uatomic_read(&sem_count
) <= 0) {
685 ret
= uatomic_add_return(&sem_count
, -count
);
687 ret
= sem_post(&constructor_wait
);
693 int handle_register_done(struct sock_info
*sock_info
)
695 if (sock_info
->registration_done
)
697 sock_info
->registration_done
= 1;
699 decrement_sem_count(1);
700 if (!sock_info
->statedump_pending
) {
701 sock_info
->initial_statedump_done
= 1;
702 decrement_sem_count(1);
709 int handle_register_failed(struct sock_info
*sock_info
)
711 if (sock_info
->registration_done
)
713 sock_info
->registration_done
= 1;
714 sock_info
->initial_statedump_done
= 1;
716 decrement_sem_count(2);
722 * Only execute pending statedump after the constructor semaphore has
723 * been posted by the current listener thread. This means statedump will
724 * only be performed after the "registration done" command is received
725 * from this thread's session daemon.
727 * This ensures we don't run into deadlock issues with the dynamic
728 * loader mutex, which is held while the constructor is called and
729 * waiting on the constructor semaphore. All operations requiring this
730 * dynamic loader lock need to be postponed using this mechanism.
732 * In a scenario with two session daemons connected to the application,
733 * it is possible that the first listener thread which receives the
734 * registration done command issues its statedump while the dynamic
735 * loader lock is still held by the application constructor waiting on
736 * the semaphore. It will however be allowed to proceed when the
737 * second session daemon sends the registration done command to the
738 * second listener thread. This situation therefore does not produce
742 void handle_pending_statedump(struct sock_info
*sock_info
)
744 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
745 sock_info
->statedump_pending
= 0;
746 pthread_mutex_lock(&ust_fork_mutex
);
747 lttng_handle_pending_statedump(sock_info
);
748 pthread_mutex_unlock(&ust_fork_mutex
);
750 if (!sock_info
->initial_statedump_done
) {
751 sock_info
->initial_statedump_done
= 1;
752 decrement_sem_count(1);
758 const char *bytecode_type_str(uint32_t cmd
)
761 case LTTNG_UST_ABI_CAPTURE
:
763 case LTTNG_UST_ABI_FILTER
:
771 int handle_bytecode_recv(struct sock_info
*sock_info
,
772 int sock
, struct ustcomm_ust_msg
*lum
)
774 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
775 enum lttng_ust_bytecode_type type
;
776 const struct lttng_ust_abi_objd_ops
*ops
;
777 uint32_t data_size
, data_size_max
, reloc_offset
;
783 case LTTNG_UST_ABI_FILTER
:
784 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
785 data_size
= lum
->u
.filter
.data_size
;
786 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
787 reloc_offset
= lum
->u
.filter
.reloc_offset
;
788 seqnum
= lum
->u
.filter
.seqnum
;
790 case LTTNG_UST_ABI_CAPTURE
:
791 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
792 data_size
= lum
->u
.capture
.data_size
;
793 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
794 reloc_offset
= lum
->u
.capture
.reloc_offset
;
795 seqnum
= lum
->u
.capture
.seqnum
;
801 if (data_size
> data_size_max
) {
802 ERR("Bytecode %s data size is too large: %u bytes",
803 bytecode_type_str(lum
->cmd
), data_size
);
808 if (reloc_offset
> data_size
) {
809 ERR("Bytecode %s reloc offset %u is not within data",
810 bytecode_type_str(lum
->cmd
), reloc_offset
);
815 /* Allocate the structure AND the `data[]` field. */
816 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
822 bytecode
->bc
.len
= data_size
;
823 bytecode
->bc
.reloc_offset
= reloc_offset
;
824 bytecode
->bc
.seqnum
= seqnum
;
825 bytecode
->type
= type
;
827 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
829 case 0: /* orderly shutdown */
833 if (len
== bytecode
->bc
.len
) {
834 DBG("Bytecode %s data received",
835 bytecode_type_str(lum
->cmd
));
837 } else if (len
< 0) {
838 DBG("Receive failed from lttng-sessiond with errno %d",
840 if (len
== -ECONNRESET
) {
841 ERR("%s remote end closed connection",
849 DBG("Incorrect %s bytecode data message size: %zd",
850 bytecode_type_str(lum
->cmd
), len
);
856 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
863 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
864 (unsigned long) &bytecode
,
875 int handle_message(struct sock_info
*sock_info
,
876 int sock
, struct ustcomm_ust_msg
*lum
)
879 const struct lttng_ust_abi_objd_ops
*ops
;
880 struct ustcomm_ust_reply lur
;
881 union lttng_ust_abi_args args
;
882 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
885 memset(&lur
, 0, sizeof(lur
));
888 ret
= -LTTNG_UST_ERR_EXITING
;
892 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
899 case LTTNG_UST_ABI_REGISTER_DONE
:
900 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
901 ret
= handle_register_done(sock_info
);
905 case LTTNG_UST_ABI_RELEASE
:
906 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
909 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
911 case LTTNG_UST_ABI_CAPTURE
:
912 case LTTNG_UST_ABI_FILTER
:
913 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
917 case LTTNG_UST_ABI_EXCLUSION
:
919 /* Receive exclusion names */
920 struct lttng_ust_excluder_node
*node
;
923 count
= lum
->u
.exclusion
.count
;
925 /* There are no names to read */
929 node
= zmalloc(sizeof(*node
) +
930 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
935 node
->excluder
.count
= count
;
936 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
937 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
939 case 0: /* orderly shutdown */
944 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
945 DBG("Exclusion data received");
947 } else if (len
< 0) {
948 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
949 if (len
== -ECONNRESET
) {
950 ERR("%s remote end closed connection", sock_info
->name
);
959 DBG("Incorrect exclusion data message size: %zd", len
);
966 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
967 (unsigned long) &node
,
974 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
976 int event_notifier_notif_fd
, close_ret
;
978 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
979 &event_notifier_notif_fd
);
981 case 0: /* orderly shutdown */
988 DBG("Receive failed from lttng-sessiond with errno %d",
990 if (len
== -ECONNRESET
) {
991 ERR("%s remote end closed connection",
999 DBG("Incorrect event notifier fd message size: %zd",
1005 args
.event_notifier_handle
.event_notifier_notif_fd
=
1006 event_notifier_notif_fd
;
1008 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1009 (unsigned long) &lum
->u
,
1013 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1014 lttng_ust_lock_fd_tracker();
1015 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1016 lttng_ust_unlock_fd_tracker();
1022 case LTTNG_UST_ABI_CHANNEL
:
1027 len
= ustcomm_recv_channel_from_sessiond(sock
,
1028 &chan_data
, lum
->u
.channel
.len
,
1031 case 0: /* orderly shutdown */
1035 if (len
== lum
->u
.channel
.len
) {
1036 DBG("channel data received");
1038 } else if (len
< 0) {
1039 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1040 if (len
== -ECONNRESET
) {
1041 ERR("%s remote end closed connection", sock_info
->name
);
1048 DBG("incorrect channel data message size: %zd", len
);
1053 args
.channel
.chan_data
= chan_data
;
1054 args
.channel
.wakeup_fd
= wakeup_fd
;
1056 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1057 (unsigned long) &lum
->u
,
1061 if (args
.channel
.wakeup_fd
>= 0) {
1064 lttng_ust_lock_fd_tracker();
1065 close_ret
= close(args
.channel
.wakeup_fd
);
1066 lttng_ust_unlock_fd_tracker();
1067 args
.channel
.wakeup_fd
= -1;
1071 free(args
.channel
.chan_data
);
1074 case LTTNG_UST_ABI_STREAM
:
1078 /* Receive shm_fd, wakeup_fd */
1079 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1081 &args
.stream
.shm_fd
,
1082 &args
.stream
.wakeup_fd
);
1088 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1089 (unsigned long) &lum
->u
,
1093 if (args
.stream
.shm_fd
>= 0) {
1094 lttng_ust_lock_fd_tracker();
1095 close_ret
= close(args
.stream
.shm_fd
);
1096 lttng_ust_unlock_fd_tracker();
1097 args
.stream
.shm_fd
= -1;
1101 if (args
.stream
.wakeup_fd
>= 0) {
1102 lttng_ust_lock_fd_tracker();
1103 close_ret
= close(args
.stream
.wakeup_fd
);
1104 lttng_ust_unlock_fd_tracker();
1105 args
.stream
.wakeup_fd
= -1;
1111 case LTTNG_UST_ABI_CONTEXT
:
1112 switch (lum
->u
.context
.ctx
) {
1113 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1116 size_t ctxlen
, recvlen
;
1118 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1119 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1120 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1121 ERR("Application context string length size is too large: %zu bytes",
1126 strcpy(ctxstr
, "$app.");
1127 p
= &ctxstr
[strlen("$app.")];
1128 recvlen
= ctxlen
- strlen("$app.");
1129 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1131 case 0: /* orderly shutdown */
1135 if (len
== recvlen
) {
1136 DBG("app context data received");
1138 } else if (len
< 0) {
1139 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1140 if (len
== -ECONNRESET
) {
1141 ERR("%s remote end closed connection", sock_info
->name
);
1148 DBG("incorrect app context data message size: %zd", len
);
1153 /* Put : between provider and ctxname. */
1154 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1155 args
.app_context
.ctxname
= ctxstr
;
1162 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1163 (unsigned long) &lum
->u
,
1169 case LTTNG_UST_ABI_COUNTER
:
1173 len
= ustcomm_recv_counter_from_sessiond(sock
,
1174 &counter_data
, lum
->u
.counter
.len
);
1176 case 0: /* orderly shutdown */
1180 if (len
== lum
->u
.counter
.len
) {
1181 DBG("counter data received");
1183 } else if (len
< 0) {
1184 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1185 if (len
== -ECONNRESET
) {
1186 ERR("%s remote end closed connection", sock_info
->name
);
1193 DBG("incorrect counter data message size: %zd", len
);
1198 args
.counter
.counter_data
= counter_data
;
1200 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1201 (unsigned long) &lum
->u
,
1205 free(args
.counter
.counter_data
);
1208 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1210 /* Receive shm_fd */
1211 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1212 &args
.counter_shm
.shm_fd
);
1218 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1219 (unsigned long) &lum
->u
,
1223 if (args
.counter_shm
.shm_fd
>= 0) {
1226 lttng_ust_lock_fd_tracker();
1227 close_ret
= close(args
.counter_shm
.shm_fd
);
1228 lttng_ust_unlock_fd_tracker();
1229 args
.counter_shm
.shm_fd
= -1;
1235 case LTTNG_UST_ABI_COUNTER_CPU
:
1237 /* Receive shm_fd */
1238 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1239 &args
.counter_shm
.shm_fd
);
1245 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1246 (unsigned long) &lum
->u
,
1250 if (args
.counter_shm
.shm_fd
>= 0) {
1253 lttng_ust_lock_fd_tracker();
1254 close_ret
= close(args
.counter_shm
.shm_fd
);
1255 lttng_ust_unlock_fd_tracker();
1256 args
.counter_shm
.shm_fd
= -1;
1262 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1264 /* Receive struct lttng_ust_event_notifier */
1265 struct lttng_ust_abi_event_notifier event_notifier
;
1267 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1268 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1272 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1274 case 0: /* orderly shutdown */
1278 if (len
== sizeof(event_notifier
)) {
1279 DBG("event notifier data received");
1281 } else if (len
< 0) {
1282 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1283 if (len
== -ECONNRESET
) {
1284 ERR("%s remote end closed connection", sock_info
->name
);
1291 DBG("incorrect event notifier data message size: %zd", len
);
1297 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1298 (unsigned long) &event_notifier
,
1307 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1308 (unsigned long) &lum
->u
,
1315 lur
.handle
= lum
->handle
;
1319 lur
.ret_code
= LTTNG_UST_OK
;
1322 * Use -LTTNG_UST_ERR as wildcard for UST internal
1323 * error that are not caused by the transport, except if
1324 * we already have a more precise error message to
1327 if (ret
> -LTTNG_UST_ERR
) {
1328 /* Translate code to UST error. */
1331 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1334 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1337 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1340 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1343 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1346 lur
.ret_code
= -LTTNG_UST_ERR
;
1355 case LTTNG_UST_ABI_TRACER_VERSION
:
1356 lur
.u
.version
= lum
->u
.version
;
1358 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1359 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1363 DBG("Return value: %d", lur
.ret_val
);
1368 * Performed delayed statedump operations outside of the UST
1369 * lock. We need to take the dynamic loader lock before we take
1370 * the UST lock internally within handle_pending_statedump().
1372 handle_pending_statedump(sock_info
);
1375 ret
= -LTTNG_UST_ERR_EXITING
;
1379 ret
= send_reply(sock
, &lur
);
1381 DBG("error sending reply");
1386 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1389 if (lur
.ret_code
== LTTNG_UST_OK
) {
1391 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1392 len
= ustcomm_send_unix_sock(sock
,
1393 &args
.field_list
.entry
,
1394 sizeof(args
.field_list
.entry
));
1399 if (len
!= sizeof(args
.field_list
.entry
)) {
1413 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1417 if (sock_info
->root_handle
!= -1) {
1418 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1420 ERR("Error unref root handle");
1422 sock_info
->root_handle
= -1;
1424 sock_info
->registration_done
= 0;
1425 sock_info
->initial_statedump_done
= 0;
1428 * wait_shm_mmap, socket and notify socket are used by listener
1429 * threads outside of the ust lock, so we cannot tear them down
1430 * ourselves, because we cannot join on these threads. Leave
1431 * responsibility of cleaning up these resources to the OS
1437 if (sock_info
->socket
!= -1) {
1438 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1440 ERR("Error closing ust cmd socket");
1442 sock_info
->socket
= -1;
1444 if (sock_info
->notify_socket
!= -1) {
1445 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1447 ERR("Error closing ust notify socket");
1449 sock_info
->notify_socket
= -1;
1451 if (sock_info
->wait_shm_mmap
) {
1454 page_size
= LTTNG_UST_PAGE_SIZE
;
1455 if (page_size
<= 0) {
1459 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1461 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1463 ERR("Error unmapping wait shm");
1466 sock_info
->wait_shm_mmap
= NULL
;
1471 * Using fork to set umask in the child process (not multi-thread safe).
1472 * We deal with the shm_open vs ftruncate race (happening when the
1473 * sessiond owns the shm and does not let everybody modify it, to ensure
1474 * safety against shm_unlink) by simply letting the mmap fail and
1475 * retrying after a few seconds.
1476 * For global shm, everybody has rw access to it until the sessiond
1480 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1482 int wait_shm_fd
, ret
;
1486 * Try to open read-only.
1488 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1489 if (wait_shm_fd
>= 0) {
1492 size_t bytes_read
= 0;
1495 * Try to read the fd. If unable to do so, try opening
1499 len
= read(wait_shm_fd
,
1500 &((char *) &tmp_read
)[bytes_read
],
1501 sizeof(tmp_read
) - bytes_read
);
1505 } while ((len
< 0 && errno
== EINTR
)
1506 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1507 if (bytes_read
!= sizeof(tmp_read
)) {
1508 ret
= close(wait_shm_fd
);
1510 ERR("close wait_shm_fd");
1515 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1517 * Real-only open did not work, and it's not because the
1518 * entry was not present. It's a failure that prohibits
1521 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1527 * If the open failed because the file did not exist, or because
1528 * the file was not truncated yet, try creating it ourself.
1530 URCU_TLS(lttng_ust_nest_count
)++;
1532 URCU_TLS(lttng_ust_nest_count
)--;
1537 * Parent: wait for child to return, in which case the
1538 * shared memory map will have been created.
1540 pid
= wait(&status
);
1541 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1546 * Try to open read-only again after creation.
1548 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1549 if (wait_shm_fd
< 0) {
1551 * Real-only open did not work. It's a failure
1552 * that prohibits using shm.
1554 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1558 } else if (pid
== 0) {
1562 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1563 if (sock_info
->global
)
1564 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1566 * We're alone in a child process, so we can modify the
1567 * process-wide umask.
1569 umask(~create_mode
);
1571 * Try creating shm (or get rw access).
1572 * We don't do an exclusive open, because we allow other
1573 * processes to create+ftruncate it concurrently.
1575 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1576 O_RDWR
| O_CREAT
, create_mode
);
1577 if (wait_shm_fd
>= 0) {
1578 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1580 PERROR("ftruncate");
1581 _exit(EXIT_FAILURE
);
1583 _exit(EXIT_SUCCESS
);
1586 * For local shm, we need to have rw access to accept
1587 * opening it: this means the local sessiond will be
1588 * able to wake us up. For global shm, we open it even
1589 * if rw access is not granted, because the root.root
1590 * sessiond will be able to override all rights and wake
1593 if (!sock_info
->global
&& errno
!= EACCES
) {
1594 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1595 _exit(EXIT_FAILURE
);
1598 * The shm exists, but we cannot open it RW. Report
1601 _exit(EXIT_SUCCESS
);
1606 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1607 struct stat statbuf
;
1610 * Ensure that our user is the owner of the shm file for
1611 * local shm. If we do not own the file, it means our
1612 * sessiond will not have access to wake us up (there is
1613 * probably a rogue process trying to fake our
1614 * sessiond). Fallback to polling method in this case.
1616 ret
= fstat(wait_shm_fd
, &statbuf
);
1621 if (statbuf
.st_uid
!= getuid())
1627 ret
= close(wait_shm_fd
);
1629 PERROR("Error closing fd");
1635 char *get_map_shm(struct sock_info
*sock_info
)
1638 int wait_shm_fd
, ret
;
1639 char *wait_shm_mmap
;
1641 page_size
= sysconf(_SC_PAGE_SIZE
);
1642 if (page_size
<= 0) {
1646 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1650 lttng_ust_lock_fd_tracker();
1651 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1652 if (wait_shm_fd
< 0) {
1653 lttng_ust_unlock_fd_tracker();
1657 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1659 ret
= close(wait_shm_fd
);
1661 PERROR("Error closing fd");
1663 lttng_ust_unlock_fd_tracker();
1668 lttng_ust_unlock_fd_tracker();
1670 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1671 MAP_SHARED
, wait_shm_fd
, 0);
1673 /* close shm fd immediately after taking the mmap reference */
1674 lttng_ust_lock_fd_tracker();
1675 ret
= close(wait_shm_fd
);
1677 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1679 PERROR("Error closing fd");
1681 lttng_ust_unlock_fd_tracker();
1683 if (wait_shm_mmap
== MAP_FAILED
) {
1684 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1687 return wait_shm_mmap
;
1694 void wait_for_sessiond(struct sock_info
*sock_info
)
1696 /* Use ust_lock to check if we should quit. */
1700 if (wait_poll_fallback
) {
1705 assert(sock_info
->wait_shm_mmap
);
1707 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1708 /* Wait for futex wakeup */
1709 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1712 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1713 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1716 /* Value already changed. */
1719 /* Retry if interrupted by signal. */
1720 break; /* Get out of switch. */
1722 wait_poll_fallback
= 1;
1724 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1725 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1726 "Please upgrade your kernel "
1727 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1728 "mainline). LTTng-UST will use polling mode fallback.");
1729 if (ust_err_debug_enabled())
1747 * This thread does not allocate any resource, except within
1748 * handle_message, within mutex protection. This mutex protects against
1750 * The other moment it allocates resources is at socket connection, which
1751 * is also protected by the mutex.
1754 void *ust_listener_thread(void *arg
)
1756 struct sock_info
*sock_info
= arg
;
1757 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1760 lttng_ust_fixup_tls();
1762 * If available, add '-ust' to the end of this thread's
1765 ret
= lttng_ust_setustprocname();
1767 ERR("Unable to set UST process name");
1770 /* Restart trying to connect to the session daemon */
1772 if (prev_connect_failed
) {
1773 /* Wait for sessiond availability with pipe */
1774 wait_for_sessiond(sock_info
);
1778 * Sleep for 5 seconds before retrying after a
1779 * sequence of failure / wait / failure. This
1780 * deals with a killed or broken session daemon.
1786 prev_connect_failed
= 0;
1793 if (sock_info
->socket
!= -1) {
1794 /* FD tracker is updated by ustcomm_close_unix_sock() */
1795 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1797 ERR("Error closing %s ust cmd socket",
1800 sock_info
->socket
= -1;
1802 if (sock_info
->notify_socket
!= -1) {
1803 /* FD tracker is updated by ustcomm_close_unix_sock() */
1804 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1806 ERR("Error closing %s ust notify socket",
1809 sock_info
->notify_socket
= -1;
1814 * Register. We need to perform both connect and sending
1815 * registration message before doing the next connect otherwise
1816 * we may reach unix socket connect queue max limits and block
1817 * on the 2nd connect while the session daemon is awaiting the
1818 * first connect registration message.
1820 /* Connect cmd socket */
1821 lttng_ust_lock_fd_tracker();
1822 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1823 get_connect_sock_timeout());
1825 lttng_ust_unlock_fd_tracker();
1826 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1827 prev_connect_failed
= 1;
1830 * If we cannot find the sessiond daemon, don't delay
1831 * constructor execution.
1833 ret
= handle_register_failed(sock_info
);
1839 ret
= lttng_ust_add_fd_to_tracker(fd
);
1843 PERROR("close on sock_info->socket");
1846 lttng_ust_unlock_fd_tracker();
1851 sock_info
->socket
= ret
;
1852 lttng_ust_unlock_fd_tracker();
1856 * Unlock/relock ust lock because connect is blocking (with
1857 * timeout). Don't delay constructors on the ust lock for too
1865 * Create only one root handle per listener thread for the whole
1866 * process lifetime, so we ensure we get ID which is statically
1867 * assigned to the root handle.
1869 if (sock_info
->root_handle
== -1) {
1870 ret
= lttng_abi_create_root_handle();
1872 ERR("Error creating root handle");
1875 sock_info
->root_handle
= ret
;
1878 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1880 ERR("Error registering to %s ust cmd socket",
1882 prev_connect_failed
= 1;
1884 * If we cannot register to the sessiond daemon, don't
1885 * delay constructor execution.
1887 ret
= handle_register_failed(sock_info
);
1895 * Unlock/relock ust lock because connect is blocking (with
1896 * timeout). Don't delay constructors on the ust lock for too
1903 /* Connect notify socket */
1904 lttng_ust_lock_fd_tracker();
1905 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1906 get_connect_sock_timeout());
1908 lttng_ust_unlock_fd_tracker();
1909 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1910 prev_connect_failed
= 1;
1913 * If we cannot find the sessiond daemon, don't delay
1914 * constructor execution.
1916 ret
= handle_register_failed(sock_info
);
1923 ret
= lttng_ust_add_fd_to_tracker(fd
);
1927 PERROR("close on sock_info->notify_socket");
1930 lttng_ust_unlock_fd_tracker();
1935 sock_info
->notify_socket
= ret
;
1936 lttng_ust_unlock_fd_tracker();
1940 * Unlock/relock ust lock because connect is blocking (with
1941 * timeout). Don't delay constructors on the ust lock for too
1948 timeout
= get_notify_sock_timeout();
1951 * Give at least 10ms to sessiond to reply to
1956 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1959 WARN("Error setting socket receive timeout");
1961 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1964 WARN("Error setting socket send timeout");
1966 } else if (timeout
< -1) {
1967 WARN("Unsupported timeout value %ld", timeout
);
1970 ret
= register_to_sessiond(sock_info
->notify_socket
,
1971 USTCTL_SOCKET_NOTIFY
);
1973 ERR("Error registering to %s ust notify socket",
1975 prev_connect_failed
= 1;
1977 * If we cannot register to the sessiond daemon, don't
1978 * delay constructor execution.
1980 ret
= handle_register_failed(sock_info
);
1985 sock
= sock_info
->socket
;
1991 struct ustcomm_ust_msg lum
;
1993 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1995 case 0: /* orderly shutdown */
1996 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
2001 * Either sessiond has shutdown or refused us by closing the socket.
2002 * In either case, we don't want to delay construction execution,
2003 * and we need to wait before retry.
2005 prev_connect_failed
= 1;
2007 * If we cannot register to the sessiond daemon, don't
2008 * delay constructor execution.
2010 ret
= handle_register_failed(sock_info
);
2015 print_cmd(lum
.cmd
, lum
.handle
);
2016 ret
= handle_message(sock_info
, sock
, &lum
);
2018 ERR("Error handling message for %s socket",
2021 * Close socket if protocol error is
2029 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2031 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2033 if (len
== -ECONNRESET
) {
2034 DBG("%s remote end closed connection", sock_info
->name
);
2045 /* Cleanup socket handles before trying to reconnect */
2046 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2048 goto restart
; /* try to reconnect */
2053 pthread_mutex_lock(&ust_exit_mutex
);
2054 sock_info
->thread_active
= 0;
2055 pthread_mutex_unlock(&ust_exit_mutex
);
2060 * Weak symbol to call when the ust malloc wrapper is not loaded.
2062 __attribute__((weak
))
2063 void lttng_ust_libc_wrapper_malloc_init(void)
2068 * sessiond monitoring thread: monitor presence of global and per-user
2069 * sessiond by polling the application common named pipe.
2072 void lttng_ust_init(void)
2073 __attribute__((constructor
));
2075 void lttng_ust_init(void)
2077 struct timespec constructor_timeout
;
2078 sigset_t sig_all_blocked
, orig_parent_mask
;
2079 pthread_attr_t thread_attr
;
2084 if (uatomic_xchg(&initialized
, 1) == 1)
2088 * Fixup interdependency between TLS fixup mutex (which happens
2089 * to be the dynamic linker mutex) and ust_lock, taken within
2092 lttng_ust_fixup_tls();
2094 lttng_ust_loaded
= 1;
2097 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2098 * the unloading of code used by the ust_listener_threads as we can not
2099 * reliably know when they exited. To do that, manually load
2100 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2101 * this library so it never becomes zero, thus never gets unloaded from the
2102 * address space of the process. Since we are already running in the
2103 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2104 * simply increment the refcount and no additionnal work is needed by the
2105 * dynamic loader as the shared library is already loaded in the address
2106 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2107 * unloading of the UST library if its refcount becomes zero (which should
2108 * never happen). Do the return value check but discard the handle at the
2109 * end of the function as it's not needed.
2111 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2113 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2117 * We want precise control over the order in which we construct
2118 * our sub-libraries vs starting to receive commands from
2119 * sessiond (otherwise leading to errors when trying to create
2120 * sessiond before the init functions are completed).
2123 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2124 lttng_ust_tp_init();
2125 lttng_ust_init_fd_tracker();
2126 lttng_ust_clock_init();
2127 lttng_ust_getcpu_init();
2128 lttng_ust_statedump_init();
2129 lttng_ust_ring_buffer_clients_init();
2130 lttng_ust_counter_clients_init();
2131 lttng_perf_counter_init();
2133 * Invoke ust malloc wrapper init before starting other threads.
2135 lttng_ust_libc_wrapper_malloc_init();
2137 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2139 get_allow_blocking();
2141 ret
= sem_init(&constructor_wait
, 0, 0);
2146 ret
= setup_global_apps();
2148 assert(global_apps
.allowed
== 0);
2149 DBG("global apps setup returned %d", ret
);
2152 ret
= setup_local_apps();
2154 assert(local_apps
.allowed
== 0);
2155 DBG("local apps setup returned %d", ret
);
2158 /* A new thread created by pthread_create inherits the signal mask
2159 * from the parent. To avoid any signal being received by the
2160 * listener thread, we block all signals temporarily in the parent,
2161 * while we create the listener thread.
2163 sigfillset(&sig_all_blocked
);
2164 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2166 ERR("pthread_sigmask: %s", strerror(ret
));
2169 ret
= pthread_attr_init(&thread_attr
);
2171 ERR("pthread_attr_init: %s", strerror(ret
));
2173 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2175 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2178 if (global_apps
.allowed
) {
2179 pthread_mutex_lock(&ust_exit_mutex
);
2180 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2181 ust_listener_thread
, &global_apps
);
2183 ERR("pthread_create global: %s", strerror(ret
));
2185 global_apps
.thread_active
= 1;
2186 pthread_mutex_unlock(&ust_exit_mutex
);
2188 handle_register_done(&global_apps
);
2191 if (local_apps
.allowed
) {
2192 pthread_mutex_lock(&ust_exit_mutex
);
2193 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2194 ust_listener_thread
, &local_apps
);
2196 ERR("pthread_create local: %s", strerror(ret
));
2198 local_apps
.thread_active
= 1;
2199 pthread_mutex_unlock(&ust_exit_mutex
);
2201 handle_register_done(&local_apps
);
2203 ret
= pthread_attr_destroy(&thread_attr
);
2205 ERR("pthread_attr_destroy: %s", strerror(ret
));
2208 /* Restore original signal mask in parent */
2209 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2211 ERR("pthread_sigmask: %s", strerror(ret
));
2214 switch (timeout_mode
) {
2215 case 1: /* timeout wait */
2217 ret
= sem_timedwait(&constructor_wait
,
2218 &constructor_timeout
);
2219 } while (ret
< 0 && errno
== EINTR
);
2223 ERR("Timed out waiting for lttng-sessiond");
2226 PERROR("sem_timedwait");
2229 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2234 case -1:/* wait forever */
2236 ret
= sem_wait(&constructor_wait
);
2237 } while (ret
< 0 && errno
== EINTR
);
2244 ERR("Unexpected error \"%s\" returned by sem_wait",
2249 case 0: /* no timeout */
2255 void lttng_ust_cleanup(int exiting
)
2257 cleanup_sock_info(&global_apps
, exiting
);
2258 cleanup_sock_info(&local_apps
, exiting
);
2259 local_apps
.allowed
= 0;
2260 global_apps
.allowed
= 0;
2262 * The teardown in this function all affect data structures
2263 * accessed under the UST lock by the listener thread. This
2264 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2265 * that none of these threads are accessing this data at this
2268 lttng_ust_abi_exit();
2269 lttng_ust_abi_events_exit();
2270 lttng_perf_counter_exit();
2271 lttng_ust_ring_buffer_clients_exit();
2272 lttng_ust_counter_clients_exit();
2273 lttng_ust_statedump_destroy();
2274 lttng_ust_tp_exit();
2276 /* Reinitialize values for fork */
2277 sem_count
= sem_count_initial_value
;
2278 lttng_ust_comm_should_quit
= 0;
2284 void lttng_ust_exit(void)
2285 __attribute__((destructor
));
2287 void lttng_ust_exit(void)
2292 * Using pthread_cancel here because:
2293 * A) we don't want to hang application teardown.
2294 * B) the thread is not allocating any resource.
2298 * Require the communication thread to quit. Synchronize with
2299 * mutexes to ensure it is not in a mutex critical section when
2300 * pthread_cancel is later called.
2303 lttng_ust_comm_should_quit
= 1;
2306 pthread_mutex_lock(&ust_exit_mutex
);
2307 /* cancel threads */
2308 if (global_apps
.thread_active
) {
2309 ret
= pthread_cancel(global_apps
.ust_listener
);
2311 ERR("Error cancelling global ust listener thread: %s",
2314 global_apps
.thread_active
= 0;
2317 if (local_apps
.thread_active
) {
2318 ret
= pthread_cancel(local_apps
.ust_listener
);
2320 ERR("Error cancelling local ust listener thread: %s",
2323 local_apps
.thread_active
= 0;
2326 pthread_mutex_unlock(&ust_exit_mutex
);
2329 * Do NOT join threads: use of sys_futex makes it impossible to
2330 * join the threads without using async-cancel, but async-cancel
2331 * is delivered by a signal, which could hit the target thread
2332 * anywhere in its code path, including while the ust_lock() is
2333 * held, causing a deadlock for the other thread. Let the OS
2334 * cleanup the threads if there are stalled in a syscall.
2336 lttng_ust_cleanup(1);
2340 void ust_context_ns_reset(void)
2342 lttng_context_pid_ns_reset();
2343 lttng_context_cgroup_ns_reset();
2344 lttng_context_ipc_ns_reset();
2345 lttng_context_mnt_ns_reset();
2346 lttng_context_net_ns_reset();
2347 lttng_context_user_ns_reset();
2348 lttng_context_time_ns_reset();
2349 lttng_context_uts_ns_reset();
2353 void ust_context_vuids_reset(void)
2355 lttng_context_vuid_reset();
2356 lttng_context_veuid_reset();
2357 lttng_context_vsuid_reset();
2361 void ust_context_vgids_reset(void)
2363 lttng_context_vgid_reset();
2364 lttng_context_vegid_reset();
2365 lttng_context_vsgid_reset();
2369 * We exclude the worker threads across fork and clone (except
2370 * CLONE_VM), because these system calls only keep the forking thread
2371 * running in the child. Therefore, we don't want to call fork or clone
2372 * in the middle of an tracepoint or ust tracing state modification.
2373 * Holding this mutex protects these structures across fork and clone.
2375 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2378 * Disable signals. This is to avoid that the child intervenes
2379 * before it is properly setup for tracing. It is safer to
2380 * disable all signals, because then we know we are not breaking
2381 * anything by restoring the original mask.
2386 /* Fixup lttng-ust TLS. */
2387 lttng_ust_fixup_tls();
2389 if (URCU_TLS(lttng_ust_nest_count
))
2391 /* Disable signals */
2392 sigfillset(&all_sigs
);
2393 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2395 PERROR("sigprocmask");
2398 pthread_mutex_lock(&ust_fork_mutex
);
2401 lttng_ust_urcu_before_fork();
2402 lttng_ust_lock_fd_tracker();
2406 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2410 DBG("process %d", getpid());
2411 lttng_perf_unlock();
2412 lttng_ust_unlock_fd_tracker();
2415 pthread_mutex_unlock(&ust_fork_mutex
);
2417 /* Restore signals */
2418 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2420 PERROR("sigprocmask");
2424 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2426 if (URCU_TLS(lttng_ust_nest_count
))
2428 DBG("process %d", getpid());
2429 lttng_ust_urcu_after_fork_parent();
2430 /* Release mutexes and reenable signals */
2431 ust_after_fork_common(restore_sigset
);
2435 * After fork, in the child, we need to cleanup all the leftover state,
2436 * except the worker thread which already magically disappeared thanks
2437 * to the weird Linux fork semantics. After tyding up, we call
2438 * lttng_ust_init() again to start over as a new PID.
2440 * This is meant for forks() that have tracing in the child between the
2441 * fork and following exec call (if there is any).
2443 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2445 if (URCU_TLS(lttng_ust_nest_count
))
2447 lttng_context_vpid_reset();
2448 lttng_context_vtid_reset();
2449 lttng_ust_context_procname_reset();
2450 ust_context_ns_reset();
2451 ust_context_vuids_reset();
2452 ust_context_vgids_reset();
2453 DBG("process %d", getpid());
2454 /* Release urcu mutexes */
2455 lttng_ust_urcu_after_fork_child();
2456 lttng_ust_cleanup(0);
2457 /* Release mutexes and reenable signals */
2458 ust_after_fork_common(restore_sigset
);
2462 void lttng_ust_after_setns(void)
2464 ust_context_ns_reset();
2465 ust_context_vuids_reset();
2466 ust_context_vgids_reset();
2469 void lttng_ust_after_unshare(void)
2471 ust_context_ns_reset();
2472 ust_context_vuids_reset();
2473 ust_context_vgids_reset();
2476 void lttng_ust_after_setuid(void)
2478 ust_context_vuids_reset();
2481 void lttng_ust_after_seteuid(void)
2483 ust_context_vuids_reset();
2486 void lttng_ust_after_setreuid(void)
2488 ust_context_vuids_reset();
2491 void lttng_ust_after_setresuid(void)
2493 ust_context_vuids_reset();
2496 void lttng_ust_after_setgid(void)
2498 ust_context_vgids_reset();
2501 void lttng_ust_after_setegid(void)
2503 ust_context_vgids_reset();
2506 void lttng_ust_after_setregid(void)
2508 ust_context_vgids_reset();
2511 void lttng_ust_after_setresgid(void)
2513 ust_context_vgids_reset();
2516 void lttng_ust_sockinfo_session_enabled(void *owner
)
2518 struct sock_info
*sock_info
= owner
;
2519 sock_info
->statedump_pending
= 1;