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"
57 #include "lttng-rb-clients.h"
60 * Has lttng ust comm constructor been called ?
62 static int initialized
;
65 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
66 * Held when handling a command, also held by fork() to deal with
67 * removal of threads, and by exit path.
69 * The UST lock is the centralized mutex across UST tracing control and
72 * ust_exit_mutex must never nest in ust_mutex.
74 * ust_fork_mutex must never nest in ust_mutex.
76 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
77 * counter lazy initialization called by events within the statedump,
78 * which traces while the ust_mutex is held.
80 * ust_lock nests within the dynamic loader lock (within glibc) because
81 * it is taken within the library constructor.
83 * The ust fd tracker lock nests within the ust_mutex.
85 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
87 /* Allow nesting the ust_mutex within the same thread. */
88 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
91 * ust_exit_mutex protects thread_active variable wrt thread exit. It
92 * cannot be done by ust_mutex because pthread_cancel(), which takes an
93 * internal libc lock, cannot nest within ust_mutex.
95 * It never nests within a ust_mutex.
97 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
100 * ust_fork_mutex protects base address statedump tracing against forks. It
101 * prevents the dynamic loader lock to be taken (by base address statedump
102 * tracing) while a fork is happening, thus preventing deadlock issues with
103 * the dynamic loader lock.
105 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 /* Should the ust comm thread quit ? */
108 static int lttng_ust_comm_should_quit
;
111 * This variable can be tested by applications to check whether
112 * lttng-ust is loaded. They simply have to define their own
113 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
114 * library constructor.
116 int lttng_ust_loaded
__attribute__((weak
));
119 * Return 0 on success, -1 if should quit.
120 * The lock is taken in both cases.
125 sigset_t sig_all_blocked
, orig_mask
;
128 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
130 ERR("pthread_setcancelstate: %s", strerror(ret
));
132 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
133 ERR("pthread_setcancelstate: unexpected oldstate");
135 sigfillset(&sig_all_blocked
);
136 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
138 ERR("pthread_sigmask: %s", strerror(ret
));
140 if (!URCU_TLS(ust_mutex_nest
)++)
141 pthread_mutex_lock(&ust_mutex
);
142 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
144 ERR("pthread_sigmask: %s", strerror(ret
));
146 if (lttng_ust_comm_should_quit
) {
154 * ust_lock_nocheck() can be used in constructors/destructors, because
155 * they are already nested within the dynamic loader lock, and therefore
156 * have exclusive access against execution of liblttng-ust destructor.
159 void ust_lock_nocheck(void)
161 sigset_t sig_all_blocked
, orig_mask
;
164 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
166 ERR("pthread_setcancelstate: %s", strerror(ret
));
168 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
169 ERR("pthread_setcancelstate: unexpected oldstate");
171 sigfillset(&sig_all_blocked
);
172 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
174 ERR("pthread_sigmask: %s", strerror(ret
));
176 if (!URCU_TLS(ust_mutex_nest
)++)
177 pthread_mutex_lock(&ust_mutex
);
178 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
180 ERR("pthread_sigmask: %s", strerror(ret
));
187 void ust_unlock(void)
189 sigset_t sig_all_blocked
, orig_mask
;
192 sigfillset(&sig_all_blocked
);
193 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
195 ERR("pthread_sigmask: %s", strerror(ret
));
197 if (!--URCU_TLS(ust_mutex_nest
))
198 pthread_mutex_unlock(&ust_mutex
);
199 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
201 ERR("pthread_sigmask: %s", strerror(ret
));
203 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
205 ERR("pthread_setcancelstate: %s", strerror(ret
));
207 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
208 ERR("pthread_setcancelstate: unexpected oldstate");
213 * Wait for either of these before continuing to the main
215 * - the register_done message from sessiond daemon
216 * (will let the sessiond daemon enable sessions before main
218 * - sessiond daemon is not reachable.
219 * - timeout (ensuring applications are resilient to session
222 static sem_t constructor_wait
;
224 * Doing this for both the global and local sessiond.
227 sem_count_initial_value
= 4,
230 static int sem_count
= sem_count_initial_value
;
233 * Counting nesting within lttng-ust. Used to ensure that calling fork()
234 * from liblttng-ust does not execute the pre/post fork handlers.
236 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
239 * Info about socket and associated listener thread.
243 pthread_t ust_listener
; /* listener thread */
245 int registration_done
;
250 char sock_path
[PATH_MAX
];
254 char wait_shm_path
[PATH_MAX
];
256 /* Keep track of lazy state dump not performed yet. */
257 int statedump_pending
;
258 int initial_statedump_done
;
259 /* Keep procname for statedump */
260 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
263 /* Socket from app (connect) to session daemon (listen) for communication */
264 struct sock_info global_apps
= {
269 .registration_done
= 0,
273 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
277 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
279 .statedump_pending
= 0,
280 .initial_statedump_done
= 0,
284 /* TODO: allow global_apps_sock_path override */
286 struct sock_info local_apps
= {
290 .registration_done
= 0,
291 .allowed
= 0, /* Check setuid bit first */
297 .statedump_pending
= 0,
298 .initial_statedump_done
= 0,
302 static int wait_poll_fallback
;
304 static const char *cmd_name_mapping
[] = {
305 [ LTTNG_UST_ABI_RELEASE
] = "Release",
306 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
307 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
309 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
310 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
311 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
312 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
314 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
316 /* Session FD commands */
317 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
318 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
319 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
321 /* Channel FD commands */
322 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
323 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
325 /* Event and Channel FD commands */
326 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
327 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
329 /* Event, Channel and Session commands */
330 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
331 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
333 /* Tracepoint list commands */
334 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
335 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
337 /* Event FD commands */
338 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
339 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
341 /* Event notifier group commands */
342 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
344 /* Session and event notifier group commands */
345 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
347 /* Counter commands */
348 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
349 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
352 static const char *str_timeout
;
353 static int got_timeout_env
;
355 static char *get_map_shm(struct sock_info
*sock_info
);
357 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
360 size_t copied
= 0, to_copy
= len
;
363 ret
= read(fd
, buf
+ copied
, to_copy
);
368 } while ((ret
> 0 && to_copy
> 0)
369 || (ret
< 0 && errno
== EINTR
));
376 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
380 const char *get_lttng_home_dir(void)
384 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
388 return (const char *) lttng_ust_getenv("HOME");
392 * Force a read (imply TLS fixup for dlopen) of TLS variables.
395 void lttng_fixup_nest_count_tls(void)
397 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
401 void lttng_fixup_ust_mutex_nest_tls(void)
403 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
407 * Fixup lttng-ust urcu TLS.
410 void lttng_fixup_lttng_ust_urcu_tls(void)
412 (void) lttng_ust_urcu_read_ongoing();
415 void lttng_ust_fixup_tls(void)
417 lttng_fixup_lttng_ust_urcu_tls();
418 lttng_fixup_ringbuffer_tls();
419 lttng_fixup_vtid_tls();
420 lttng_fixup_nest_count_tls();
421 lttng_fixup_procname_tls();
422 lttng_fixup_ust_mutex_nest_tls();
423 lttng_ust_fixup_perf_counter_tls();
424 lttng_ust_fixup_fd_tracker_tls();
425 lttng_fixup_cgroup_ns_tls();
426 lttng_fixup_ipc_ns_tls();
427 lttng_fixup_net_ns_tls();
428 lttng_fixup_time_ns_tls();
429 lttng_fixup_uts_ns_tls();
430 lttng_ust_fixup_ring_buffer_client_discard_tls();
431 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
432 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
433 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
437 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
438 * model because many versions of glibc don't preallocate a pool large
439 * enough for TLS variables IE model defined in other shared libraries,
440 * and causes issues when using LTTng-UST for Java tracing.
442 * Because of this use of Global Dynamic TLS variables, users wishing to
443 * trace from signal handlers need to explicitly trigger the lazy
444 * allocation of those variables for each thread before using them.
445 * This can be triggered by calling lttng_ust_init_thread().
447 void lttng_ust_init_thread(void)
450 * Because those TLS variables are global dynamic, we need to
451 * ensure those are initialized before a signal handler nesting over
452 * this thread attempts to use them.
454 lttng_ust_fixup_tls();
457 int lttng_get_notify_socket(void *owner
)
459 struct sock_info
*info
= owner
;
461 return info
->notify_socket
;
465 char* lttng_ust_sockinfo_get_procname(void *owner
)
467 struct sock_info
*info
= owner
;
469 return info
->procname
;
473 void print_cmd(int cmd
, int handle
)
475 const char *cmd_name
= "Unknown";
477 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
478 && cmd_name_mapping
[cmd
]) {
479 cmd_name
= cmd_name_mapping
[cmd
];
481 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
483 lttng_ust_obj_get_name(handle
), handle
);
487 int setup_global_apps(void)
490 assert(!global_apps
.wait_shm_mmap
);
492 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
493 if (!global_apps
.wait_shm_mmap
) {
494 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
495 global_apps
.allowed
= 0;
500 global_apps
.allowed
= 1;
501 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
506 int setup_local_apps(void)
509 const char *home_dir
;
512 assert(!local_apps
.wait_shm_mmap
);
516 * Disallow per-user tracing for setuid binaries.
518 if (uid
!= geteuid()) {
519 assert(local_apps
.allowed
== 0);
523 home_dir
= get_lttng_home_dir();
525 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
526 assert(local_apps
.allowed
== 0);
530 local_apps
.allowed
= 1;
531 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
533 LTTNG_DEFAULT_HOME_RUNDIR
,
534 LTTNG_UST_SOCK_FILENAME
);
535 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
536 LTTNG_UST_WAIT_FILENAME
,
539 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
540 if (!local_apps
.wait_shm_mmap
) {
541 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
542 local_apps
.allowed
= 0;
547 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
553 * Get socket timeout, in ms.
554 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
557 long get_timeout(void)
559 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
561 if (!got_timeout_env
) {
562 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
566 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
567 /* All negative values are considered as "-1". */
568 if (constructor_delay_ms
< -1)
569 constructor_delay_ms
= -1;
570 return constructor_delay_ms
;
573 /* Timeout for notify socket send and recv. */
575 long get_notify_sock_timeout(void)
577 return get_timeout();
580 /* Timeout for connecting to cmd and notify sockets. */
582 long get_connect_sock_timeout(void)
584 return get_timeout();
588 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
591 int get_constructor_timeout(struct timespec
*constructor_timeout
)
593 long constructor_delay_ms
;
596 constructor_delay_ms
= get_timeout();
598 switch (constructor_delay_ms
) {
599 case -1:/* fall-through */
601 return constructor_delay_ms
;
607 * If we are unable to find the current time, don't wait.
609 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
614 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
615 constructor_timeout
->tv_nsec
+=
616 (constructor_delay_ms
% 1000UL) * 1000000UL;
617 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
618 constructor_timeout
->tv_sec
++;
619 constructor_timeout
->tv_nsec
-= 1000000000UL;
621 /* Timeout wait (constructor_delay_ms). */
626 void get_allow_blocking(void)
628 const char *str_allow_blocking
=
629 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
631 if (str_allow_blocking
) {
632 DBG("%s environment variable is set",
633 "LTTNG_UST_ALLOW_BLOCKING");
634 lttng_ust_ringbuffer_set_allow_blocking();
639 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
641 return ustcomm_send_reg_msg(socket
,
644 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
645 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
646 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
647 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
648 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
652 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
656 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
659 DBG("message successfully sent");
662 if (len
== -ECONNRESET
) {
663 DBG("remote end closed connection");
668 DBG("incorrect message size: %zd", len
);
674 void decrement_sem_count(unsigned int count
)
678 assert(uatomic_read(&sem_count
) >= count
);
680 if (uatomic_read(&sem_count
) <= 0) {
684 ret
= uatomic_add_return(&sem_count
, -count
);
686 ret
= sem_post(&constructor_wait
);
692 int handle_register_done(struct sock_info
*sock_info
)
694 if (sock_info
->registration_done
)
696 sock_info
->registration_done
= 1;
698 decrement_sem_count(1);
699 if (!sock_info
->statedump_pending
) {
700 sock_info
->initial_statedump_done
= 1;
701 decrement_sem_count(1);
708 int handle_register_failed(struct sock_info
*sock_info
)
710 if (sock_info
->registration_done
)
712 sock_info
->registration_done
= 1;
713 sock_info
->initial_statedump_done
= 1;
715 decrement_sem_count(2);
721 * Only execute pending statedump after the constructor semaphore has
722 * been posted by the current listener thread. This means statedump will
723 * only be performed after the "registration done" command is received
724 * from this thread's session daemon.
726 * This ensures we don't run into deadlock issues with the dynamic
727 * loader mutex, which is held while the constructor is called and
728 * waiting on the constructor semaphore. All operations requiring this
729 * dynamic loader lock need to be postponed using this mechanism.
731 * In a scenario with two session daemons connected to the application,
732 * it is possible that the first listener thread which receives the
733 * registration done command issues its statedump while the dynamic
734 * loader lock is still held by the application constructor waiting on
735 * the semaphore. It will however be allowed to proceed when the
736 * second session daemon sends the registration done command to the
737 * second listener thread. This situation therefore does not produce
741 void handle_pending_statedump(struct sock_info
*sock_info
)
743 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
744 sock_info
->statedump_pending
= 0;
745 pthread_mutex_lock(&ust_fork_mutex
);
746 lttng_handle_pending_statedump(sock_info
);
747 pthread_mutex_unlock(&ust_fork_mutex
);
749 if (!sock_info
->initial_statedump_done
) {
750 sock_info
->initial_statedump_done
= 1;
751 decrement_sem_count(1);
757 const char *bytecode_type_str(uint32_t cmd
)
760 case LTTNG_UST_ABI_CAPTURE
:
762 case LTTNG_UST_ABI_FILTER
:
770 int handle_bytecode_recv(struct sock_info
*sock_info
,
771 int sock
, struct ustcomm_ust_msg
*lum
)
773 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
774 enum lttng_ust_bytecode_type type
;
775 const struct lttng_ust_abi_objd_ops
*ops
;
776 uint32_t data_size
, data_size_max
, reloc_offset
;
782 case LTTNG_UST_ABI_FILTER
:
783 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
784 data_size
= lum
->u
.filter
.data_size
;
785 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
786 reloc_offset
= lum
->u
.filter
.reloc_offset
;
787 seqnum
= lum
->u
.filter
.seqnum
;
789 case LTTNG_UST_ABI_CAPTURE
:
790 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
791 data_size
= lum
->u
.capture
.data_size
;
792 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
793 reloc_offset
= lum
->u
.capture
.reloc_offset
;
794 seqnum
= lum
->u
.capture
.seqnum
;
800 if (data_size
> data_size_max
) {
801 ERR("Bytecode %s data size is too large: %u bytes",
802 bytecode_type_str(lum
->cmd
), data_size
);
807 if (reloc_offset
> data_size
) {
808 ERR("Bytecode %s reloc offset %u is not within data",
809 bytecode_type_str(lum
->cmd
), reloc_offset
);
814 /* Allocate the structure AND the `data[]` field. */
815 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
821 bytecode
->bc
.len
= data_size
;
822 bytecode
->bc
.reloc_offset
= reloc_offset
;
823 bytecode
->bc
.seqnum
= seqnum
;
824 bytecode
->type
= type
;
826 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
828 case 0: /* orderly shutdown */
832 if (len
== bytecode
->bc
.len
) {
833 DBG("Bytecode %s data received",
834 bytecode_type_str(lum
->cmd
));
836 } else if (len
< 0) {
837 DBG("Receive failed from lttng-sessiond with errno %d",
839 if (len
== -ECONNRESET
) {
840 ERR("%s remote end closed connection",
848 DBG("Incorrect %s bytecode data message size: %zd",
849 bytecode_type_str(lum
->cmd
), len
);
855 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
862 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
863 (unsigned long) &bytecode
,
874 int handle_message(struct sock_info
*sock_info
,
875 int sock
, struct ustcomm_ust_msg
*lum
)
878 const struct lttng_ust_abi_objd_ops
*ops
;
879 struct ustcomm_ust_reply lur
;
880 union lttng_ust_abi_args args
;
881 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
884 memset(&lur
, 0, sizeof(lur
));
887 ret
= -LTTNG_UST_ERR_EXITING
;
891 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
898 case LTTNG_UST_ABI_REGISTER_DONE
:
899 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
900 ret
= handle_register_done(sock_info
);
904 case LTTNG_UST_ABI_RELEASE
:
905 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
908 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
910 case LTTNG_UST_ABI_CAPTURE
:
911 case LTTNG_UST_ABI_FILTER
:
912 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
916 case LTTNG_UST_ABI_EXCLUSION
:
918 /* Receive exclusion names */
919 struct lttng_ust_excluder_node
*node
;
922 count
= lum
->u
.exclusion
.count
;
924 /* There are no names to read */
928 node
= zmalloc(sizeof(*node
) +
929 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
934 node
->excluder
.count
= count
;
935 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
936 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
938 case 0: /* orderly shutdown */
943 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
944 DBG("Exclusion data received");
946 } else if (len
< 0) {
947 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
948 if (len
== -ECONNRESET
) {
949 ERR("%s remote end closed connection", sock_info
->name
);
958 DBG("Incorrect exclusion data message size: %zd", len
);
965 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
966 (unsigned long) &node
,
973 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
975 int event_notifier_notif_fd
, close_ret
;
977 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
978 &event_notifier_notif_fd
);
980 case 0: /* orderly shutdown */
987 DBG("Receive failed from lttng-sessiond with errno %d",
989 if (len
== -ECONNRESET
) {
990 ERR("%s remote end closed connection",
998 DBG("Incorrect event notifier fd message size: %zd",
1004 args
.event_notifier_handle
.event_notifier_notif_fd
=
1005 event_notifier_notif_fd
;
1007 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1008 (unsigned long) &lum
->u
,
1012 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1013 lttng_ust_lock_fd_tracker();
1014 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1015 lttng_ust_unlock_fd_tracker();
1021 case LTTNG_UST_ABI_CHANNEL
:
1026 len
= ustcomm_recv_channel_from_sessiond(sock
,
1027 &chan_data
, lum
->u
.channel
.len
,
1030 case 0: /* orderly shutdown */
1034 if (len
== lum
->u
.channel
.len
) {
1035 DBG("channel data received");
1037 } else if (len
< 0) {
1038 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1039 if (len
== -ECONNRESET
) {
1040 ERR("%s remote end closed connection", sock_info
->name
);
1047 DBG("incorrect channel data message size: %zd", len
);
1052 args
.channel
.chan_data
= chan_data
;
1053 args
.channel
.wakeup_fd
= wakeup_fd
;
1055 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1056 (unsigned long) &lum
->u
,
1060 if (args
.channel
.wakeup_fd
>= 0) {
1063 lttng_ust_lock_fd_tracker();
1064 close_ret
= close(args
.channel
.wakeup_fd
);
1065 lttng_ust_unlock_fd_tracker();
1066 args
.channel
.wakeup_fd
= -1;
1070 free(args
.channel
.chan_data
);
1073 case LTTNG_UST_ABI_STREAM
:
1077 /* Receive shm_fd, wakeup_fd */
1078 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1080 &args
.stream
.shm_fd
,
1081 &args
.stream
.wakeup_fd
);
1087 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1088 (unsigned long) &lum
->u
,
1092 if (args
.stream
.shm_fd
>= 0) {
1093 lttng_ust_lock_fd_tracker();
1094 close_ret
= close(args
.stream
.shm_fd
);
1095 lttng_ust_unlock_fd_tracker();
1096 args
.stream
.shm_fd
= -1;
1100 if (args
.stream
.wakeup_fd
>= 0) {
1101 lttng_ust_lock_fd_tracker();
1102 close_ret
= close(args
.stream
.wakeup_fd
);
1103 lttng_ust_unlock_fd_tracker();
1104 args
.stream
.wakeup_fd
= -1;
1110 case LTTNG_UST_ABI_CONTEXT
:
1111 switch (lum
->u
.context
.ctx
) {
1112 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1115 size_t ctxlen
, recvlen
;
1117 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1118 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1119 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1120 ERR("Application context string length size is too large: %zu bytes",
1125 strcpy(ctxstr
, "$app.");
1126 p
= &ctxstr
[strlen("$app.")];
1127 recvlen
= ctxlen
- strlen("$app.");
1128 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1130 case 0: /* orderly shutdown */
1134 if (len
== recvlen
) {
1135 DBG("app context data received");
1137 } else if (len
< 0) {
1138 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1139 if (len
== -ECONNRESET
) {
1140 ERR("%s remote end closed connection", sock_info
->name
);
1147 DBG("incorrect app context data message size: %zd", len
);
1152 /* Put : between provider and ctxname. */
1153 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1154 args
.app_context
.ctxname
= ctxstr
;
1161 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1162 (unsigned long) &lum
->u
,
1168 case LTTNG_UST_ABI_COUNTER
:
1172 len
= ustcomm_recv_counter_from_sessiond(sock
,
1173 &counter_data
, lum
->u
.counter
.len
);
1175 case 0: /* orderly shutdown */
1179 if (len
== lum
->u
.counter
.len
) {
1180 DBG("counter data received");
1182 } else if (len
< 0) {
1183 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1184 if (len
== -ECONNRESET
) {
1185 ERR("%s remote end closed connection", sock_info
->name
);
1192 DBG("incorrect counter data message size: %zd", len
);
1197 args
.counter
.counter_data
= counter_data
;
1199 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1200 (unsigned long) &lum
->u
,
1204 free(args
.counter
.counter_data
);
1207 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1209 /* Receive shm_fd */
1210 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1211 &args
.counter_shm
.shm_fd
);
1217 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1218 (unsigned long) &lum
->u
,
1222 if (args
.counter_shm
.shm_fd
>= 0) {
1225 lttng_ust_lock_fd_tracker();
1226 close_ret
= close(args
.counter_shm
.shm_fd
);
1227 lttng_ust_unlock_fd_tracker();
1228 args
.counter_shm
.shm_fd
= -1;
1234 case LTTNG_UST_ABI_COUNTER_CPU
:
1236 /* Receive shm_fd */
1237 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1238 &args
.counter_shm
.shm_fd
);
1244 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1245 (unsigned long) &lum
->u
,
1249 if (args
.counter_shm
.shm_fd
>= 0) {
1252 lttng_ust_lock_fd_tracker();
1253 close_ret
= close(args
.counter_shm
.shm_fd
);
1254 lttng_ust_unlock_fd_tracker();
1255 args
.counter_shm
.shm_fd
= -1;
1261 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1263 /* Receive struct lttng_ust_event_notifier */
1264 struct lttng_ust_abi_event_notifier event_notifier
;
1266 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1267 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1271 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1273 case 0: /* orderly shutdown */
1277 if (len
== sizeof(event_notifier
)) {
1278 DBG("event notifier data received");
1280 } else if (len
< 0) {
1281 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1282 if (len
== -ECONNRESET
) {
1283 ERR("%s remote end closed connection", sock_info
->name
);
1290 DBG("incorrect event notifier data message size: %zd", len
);
1296 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1297 (unsigned long) &event_notifier
,
1306 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1307 (unsigned long) &lum
->u
,
1314 lur
.handle
= lum
->handle
;
1318 lur
.ret_code
= LTTNG_UST_OK
;
1321 * Use -LTTNG_UST_ERR as wildcard for UST internal
1322 * error that are not caused by the transport, except if
1323 * we already have a more precise error message to
1326 if (ret
> -LTTNG_UST_ERR
) {
1327 /* Translate code to UST error. */
1330 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1333 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1336 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1339 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1342 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1345 lur
.ret_code
= -LTTNG_UST_ERR
;
1354 case LTTNG_UST_ABI_TRACER_VERSION
:
1355 lur
.u
.version
= lum
->u
.version
;
1357 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1358 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1362 DBG("Return value: %d", lur
.ret_val
);
1367 * Performed delayed statedump operations outside of the UST
1368 * lock. We need to take the dynamic loader lock before we take
1369 * the UST lock internally within handle_pending_statedump().
1371 handle_pending_statedump(sock_info
);
1374 ret
= -LTTNG_UST_ERR_EXITING
;
1378 ret
= send_reply(sock
, &lur
);
1380 DBG("error sending reply");
1385 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1388 if (lur
.ret_code
== LTTNG_UST_OK
) {
1390 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1391 len
= ustcomm_send_unix_sock(sock
,
1392 &args
.field_list
.entry
,
1393 sizeof(args
.field_list
.entry
));
1398 if (len
!= sizeof(args
.field_list
.entry
)) {
1412 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1416 if (sock_info
->root_handle
!= -1) {
1417 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1419 ERR("Error unref root handle");
1421 sock_info
->root_handle
= -1;
1423 sock_info
->registration_done
= 0;
1424 sock_info
->initial_statedump_done
= 0;
1427 * wait_shm_mmap, socket and notify socket are used by listener
1428 * threads outside of the ust lock, so we cannot tear them down
1429 * ourselves, because we cannot join on these threads. Leave
1430 * responsibility of cleaning up these resources to the OS
1436 if (sock_info
->socket
!= -1) {
1437 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1439 ERR("Error closing ust cmd socket");
1441 sock_info
->socket
= -1;
1443 if (sock_info
->notify_socket
!= -1) {
1444 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1446 ERR("Error closing ust notify socket");
1448 sock_info
->notify_socket
= -1;
1450 if (sock_info
->wait_shm_mmap
) {
1453 page_size
= LTTNG_UST_PAGE_SIZE
;
1454 if (page_size
<= 0) {
1458 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1460 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1462 ERR("Error unmapping wait shm");
1465 sock_info
->wait_shm_mmap
= NULL
;
1470 * Using fork to set umask in the child process (not multi-thread safe).
1471 * We deal with the shm_open vs ftruncate race (happening when the
1472 * sessiond owns the shm and does not let everybody modify it, to ensure
1473 * safety against shm_unlink) by simply letting the mmap fail and
1474 * retrying after a few seconds.
1475 * For global shm, everybody has rw access to it until the sessiond
1479 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1481 int wait_shm_fd
, ret
;
1485 * Try to open read-only.
1487 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1488 if (wait_shm_fd
>= 0) {
1491 size_t bytes_read
= 0;
1494 * Try to read the fd. If unable to do so, try opening
1498 len
= read(wait_shm_fd
,
1499 &((char *) &tmp_read
)[bytes_read
],
1500 sizeof(tmp_read
) - bytes_read
);
1504 } while ((len
< 0 && errno
== EINTR
)
1505 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1506 if (bytes_read
!= sizeof(tmp_read
)) {
1507 ret
= close(wait_shm_fd
);
1509 ERR("close wait_shm_fd");
1514 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1516 * Real-only open did not work, and it's not because the
1517 * entry was not present. It's a failure that prohibits
1520 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1526 * If the open failed because the file did not exist, or because
1527 * the file was not truncated yet, try creating it ourself.
1529 URCU_TLS(lttng_ust_nest_count
)++;
1531 URCU_TLS(lttng_ust_nest_count
)--;
1536 * Parent: wait for child to return, in which case the
1537 * shared memory map will have been created.
1539 pid
= wait(&status
);
1540 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1545 * Try to open read-only again after creation.
1547 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1548 if (wait_shm_fd
< 0) {
1550 * Real-only open did not work. It's a failure
1551 * that prohibits using shm.
1553 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1557 } else if (pid
== 0) {
1561 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1562 if (sock_info
->global
)
1563 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1565 * We're alone in a child process, so we can modify the
1566 * process-wide umask.
1568 umask(~create_mode
);
1570 * Try creating shm (or get rw access).
1571 * We don't do an exclusive open, because we allow other
1572 * processes to create+ftruncate it concurrently.
1574 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1575 O_RDWR
| O_CREAT
, create_mode
);
1576 if (wait_shm_fd
>= 0) {
1577 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1579 PERROR("ftruncate");
1580 _exit(EXIT_FAILURE
);
1582 _exit(EXIT_SUCCESS
);
1585 * For local shm, we need to have rw access to accept
1586 * opening it: this means the local sessiond will be
1587 * able to wake us up. For global shm, we open it even
1588 * if rw access is not granted, because the root.root
1589 * sessiond will be able to override all rights and wake
1592 if (!sock_info
->global
&& errno
!= EACCES
) {
1593 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1594 _exit(EXIT_FAILURE
);
1597 * The shm exists, but we cannot open it RW. Report
1600 _exit(EXIT_SUCCESS
);
1605 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1606 struct stat statbuf
;
1609 * Ensure that our user is the owner of the shm file for
1610 * local shm. If we do not own the file, it means our
1611 * sessiond will not have access to wake us up (there is
1612 * probably a rogue process trying to fake our
1613 * sessiond). Fallback to polling method in this case.
1615 ret
= fstat(wait_shm_fd
, &statbuf
);
1620 if (statbuf
.st_uid
!= getuid())
1626 ret
= close(wait_shm_fd
);
1628 PERROR("Error closing fd");
1634 char *get_map_shm(struct sock_info
*sock_info
)
1637 int wait_shm_fd
, ret
;
1638 char *wait_shm_mmap
;
1640 page_size
= sysconf(_SC_PAGE_SIZE
);
1641 if (page_size
<= 0) {
1645 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1649 lttng_ust_lock_fd_tracker();
1650 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1651 if (wait_shm_fd
< 0) {
1652 lttng_ust_unlock_fd_tracker();
1656 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1658 ret
= close(wait_shm_fd
);
1660 PERROR("Error closing fd");
1662 lttng_ust_unlock_fd_tracker();
1667 lttng_ust_unlock_fd_tracker();
1669 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1670 MAP_SHARED
, wait_shm_fd
, 0);
1672 /* close shm fd immediately after taking the mmap reference */
1673 lttng_ust_lock_fd_tracker();
1674 ret
= close(wait_shm_fd
);
1676 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1678 PERROR("Error closing fd");
1680 lttng_ust_unlock_fd_tracker();
1682 if (wait_shm_mmap
== MAP_FAILED
) {
1683 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1686 return wait_shm_mmap
;
1693 void wait_for_sessiond(struct sock_info
*sock_info
)
1695 /* Use ust_lock to check if we should quit. */
1699 if (wait_poll_fallback
) {
1704 assert(sock_info
->wait_shm_mmap
);
1706 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1707 /* Wait for futex wakeup */
1708 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1711 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1712 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1715 /* Value already changed. */
1718 /* Retry if interrupted by signal. */
1719 break; /* Get out of switch. */
1721 wait_poll_fallback
= 1;
1723 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1724 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1725 "Please upgrade your kernel "
1726 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1727 "mainline). LTTng-UST will use polling mode fallback.");
1728 if (ust_err_debug_enabled())
1746 * This thread does not allocate any resource, except within
1747 * handle_message, within mutex protection. This mutex protects against
1749 * The other moment it allocates resources is at socket connection, which
1750 * is also protected by the mutex.
1753 void *ust_listener_thread(void *arg
)
1755 struct sock_info
*sock_info
= arg
;
1756 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1759 lttng_ust_fixup_tls();
1761 * If available, add '-ust' to the end of this thread's
1764 ret
= lttng_ust_setustprocname();
1766 ERR("Unable to set UST process name");
1769 /* Restart trying to connect to the session daemon */
1771 if (prev_connect_failed
) {
1772 /* Wait for sessiond availability with pipe */
1773 wait_for_sessiond(sock_info
);
1777 * Sleep for 5 seconds before retrying after a
1778 * sequence of failure / wait / failure. This
1779 * deals with a killed or broken session daemon.
1785 prev_connect_failed
= 0;
1792 if (sock_info
->socket
!= -1) {
1793 /* FD tracker is updated by ustcomm_close_unix_sock() */
1794 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1796 ERR("Error closing %s ust cmd socket",
1799 sock_info
->socket
= -1;
1801 if (sock_info
->notify_socket
!= -1) {
1802 /* FD tracker is updated by ustcomm_close_unix_sock() */
1803 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1805 ERR("Error closing %s ust notify socket",
1808 sock_info
->notify_socket
= -1;
1813 * Register. We need to perform both connect and sending
1814 * registration message before doing the next connect otherwise
1815 * we may reach unix socket connect queue max limits and block
1816 * on the 2nd connect while the session daemon is awaiting the
1817 * first connect registration message.
1819 /* Connect cmd socket */
1820 lttng_ust_lock_fd_tracker();
1821 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1822 get_connect_sock_timeout());
1824 lttng_ust_unlock_fd_tracker();
1825 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1826 prev_connect_failed
= 1;
1829 * If we cannot find the sessiond daemon, don't delay
1830 * constructor execution.
1832 ret
= handle_register_failed(sock_info
);
1838 ret
= lttng_ust_add_fd_to_tracker(fd
);
1842 PERROR("close on sock_info->socket");
1845 lttng_ust_unlock_fd_tracker();
1850 sock_info
->socket
= ret
;
1851 lttng_ust_unlock_fd_tracker();
1855 * Unlock/relock ust lock because connect is blocking (with
1856 * timeout). Don't delay constructors on the ust lock for too
1864 * Create only one root handle per listener thread for the whole
1865 * process lifetime, so we ensure we get ID which is statically
1866 * assigned to the root handle.
1868 if (sock_info
->root_handle
== -1) {
1869 ret
= lttng_abi_create_root_handle();
1871 ERR("Error creating root handle");
1874 sock_info
->root_handle
= ret
;
1877 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1879 ERR("Error registering to %s ust cmd socket",
1881 prev_connect_failed
= 1;
1883 * If we cannot register to the sessiond daemon, don't
1884 * delay constructor execution.
1886 ret
= handle_register_failed(sock_info
);
1894 * Unlock/relock ust lock because connect is blocking (with
1895 * timeout). Don't delay constructors on the ust lock for too
1902 /* Connect notify socket */
1903 lttng_ust_lock_fd_tracker();
1904 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1905 get_connect_sock_timeout());
1907 lttng_ust_unlock_fd_tracker();
1908 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1909 prev_connect_failed
= 1;
1912 * If we cannot find the sessiond daemon, don't delay
1913 * constructor execution.
1915 ret
= handle_register_failed(sock_info
);
1922 ret
= lttng_ust_add_fd_to_tracker(fd
);
1926 PERROR("close on sock_info->notify_socket");
1929 lttng_ust_unlock_fd_tracker();
1934 sock_info
->notify_socket
= ret
;
1935 lttng_ust_unlock_fd_tracker();
1939 * Unlock/relock ust lock because connect is blocking (with
1940 * timeout). Don't delay constructors on the ust lock for too
1947 timeout
= get_notify_sock_timeout();
1950 * Give at least 10ms to sessiond to reply to
1955 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1958 WARN("Error setting socket receive timeout");
1960 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1963 WARN("Error setting socket send timeout");
1965 } else if (timeout
< -1) {
1966 WARN("Unsupported timeout value %ld", timeout
);
1969 ret
= register_to_sessiond(sock_info
->notify_socket
,
1970 USTCTL_SOCKET_NOTIFY
);
1972 ERR("Error registering to %s ust notify socket",
1974 prev_connect_failed
= 1;
1976 * If we cannot register to the sessiond daemon, don't
1977 * delay constructor execution.
1979 ret
= handle_register_failed(sock_info
);
1984 sock
= sock_info
->socket
;
1990 struct ustcomm_ust_msg lum
;
1992 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1994 case 0: /* orderly shutdown */
1995 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
2000 * Either sessiond has shutdown or refused us by closing the socket.
2001 * In either case, we don't want to delay construction execution,
2002 * and we need to wait before retry.
2004 prev_connect_failed
= 1;
2006 * If we cannot register to the sessiond daemon, don't
2007 * delay constructor execution.
2009 ret
= handle_register_failed(sock_info
);
2014 print_cmd(lum
.cmd
, lum
.handle
);
2015 ret
= handle_message(sock_info
, sock
, &lum
);
2017 ERR("Error handling message for %s socket",
2020 * Close socket if protocol error is
2028 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2030 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2032 if (len
== -ECONNRESET
) {
2033 DBG("%s remote end closed connection", sock_info
->name
);
2044 /* Cleanup socket handles before trying to reconnect */
2045 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2047 goto restart
; /* try to reconnect */
2052 pthread_mutex_lock(&ust_exit_mutex
);
2053 sock_info
->thread_active
= 0;
2054 pthread_mutex_unlock(&ust_exit_mutex
);
2059 * Weak symbol to call when the ust malloc wrapper is not loaded.
2061 __attribute__((weak
))
2062 void lttng_ust_libc_wrapper_malloc_init(void)
2067 * sessiond monitoring thread: monitor presence of global and per-user
2068 * sessiond by polling the application common named pipe.
2070 void __attribute__((constructor
)) lttng_ust_init(void)
2072 struct timespec constructor_timeout
;
2073 sigset_t sig_all_blocked
, orig_parent_mask
;
2074 pthread_attr_t thread_attr
;
2079 if (uatomic_xchg(&initialized
, 1) == 1)
2083 * Fixup interdependency between TLS fixup mutex (which happens
2084 * to be the dynamic linker mutex) and ust_lock, taken within
2087 lttng_ust_fixup_tls();
2089 lttng_ust_loaded
= 1;
2092 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2093 * the unloading of code used by the ust_listener_threads as we can not
2094 * reliably know when they exited. To do that, manually load
2095 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2096 * this library so it never becomes zero, thus never gets unloaded from the
2097 * address space of the process. Since we are already running in the
2098 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2099 * simply increment the refcount and no additionnal work is needed by the
2100 * dynamic loader as the shared library is already loaded in the address
2101 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2102 * unloading of the UST library if its refcount becomes zero (which should
2103 * never happen). Do the return value check but discard the handle at the
2104 * end of the function as it's not needed.
2106 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2108 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2112 * We want precise control over the order in which we construct
2113 * our sub-libraries vs starting to receive commands from
2114 * sessiond (otherwise leading to errors when trying to create
2115 * sessiond before the init functions are completed).
2118 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2119 lttng_ust_tp_init();
2120 lttng_ust_init_fd_tracker();
2121 lttng_ust_clock_init();
2122 lttng_ust_getcpu_init();
2123 lttng_ust_statedump_init();
2124 lttng_ust_ring_buffer_clients_init();
2125 lttng_ust_counter_clients_init();
2126 lttng_perf_counter_init();
2128 * Invoke ust malloc wrapper init before starting other threads.
2130 lttng_ust_libc_wrapper_malloc_init();
2132 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2134 get_allow_blocking();
2136 ret
= sem_init(&constructor_wait
, 0, 0);
2141 ret
= setup_global_apps();
2143 assert(global_apps
.allowed
== 0);
2144 DBG("global apps setup returned %d", ret
);
2147 ret
= setup_local_apps();
2149 assert(local_apps
.allowed
== 0);
2150 DBG("local apps setup returned %d", ret
);
2153 /* A new thread created by pthread_create inherits the signal mask
2154 * from the parent. To avoid any signal being received by the
2155 * listener thread, we block all signals temporarily in the parent,
2156 * while we create the listener thread.
2158 sigfillset(&sig_all_blocked
);
2159 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2161 ERR("pthread_sigmask: %s", strerror(ret
));
2164 ret
= pthread_attr_init(&thread_attr
);
2166 ERR("pthread_attr_init: %s", strerror(ret
));
2168 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2170 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2173 if (global_apps
.allowed
) {
2174 pthread_mutex_lock(&ust_exit_mutex
);
2175 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2176 ust_listener_thread
, &global_apps
);
2178 ERR("pthread_create global: %s", strerror(ret
));
2180 global_apps
.thread_active
= 1;
2181 pthread_mutex_unlock(&ust_exit_mutex
);
2183 handle_register_done(&global_apps
);
2186 if (local_apps
.allowed
) {
2187 pthread_mutex_lock(&ust_exit_mutex
);
2188 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2189 ust_listener_thread
, &local_apps
);
2191 ERR("pthread_create local: %s", strerror(ret
));
2193 local_apps
.thread_active
= 1;
2194 pthread_mutex_unlock(&ust_exit_mutex
);
2196 handle_register_done(&local_apps
);
2198 ret
= pthread_attr_destroy(&thread_attr
);
2200 ERR("pthread_attr_destroy: %s", strerror(ret
));
2203 /* Restore original signal mask in parent */
2204 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2206 ERR("pthread_sigmask: %s", strerror(ret
));
2209 switch (timeout_mode
) {
2210 case 1: /* timeout wait */
2212 ret
= sem_timedwait(&constructor_wait
,
2213 &constructor_timeout
);
2214 } while (ret
< 0 && errno
== EINTR
);
2218 ERR("Timed out waiting for lttng-sessiond");
2221 PERROR("sem_timedwait");
2224 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2229 case -1:/* wait forever */
2231 ret
= sem_wait(&constructor_wait
);
2232 } while (ret
< 0 && errno
== EINTR
);
2239 ERR("Unexpected error \"%s\" returned by sem_wait",
2244 case 0: /* no timeout */
2250 void lttng_ust_cleanup(int exiting
)
2252 cleanup_sock_info(&global_apps
, exiting
);
2253 cleanup_sock_info(&local_apps
, exiting
);
2254 local_apps
.allowed
= 0;
2255 global_apps
.allowed
= 0;
2257 * The teardown in this function all affect data structures
2258 * accessed under the UST lock by the listener thread. This
2259 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2260 * that none of these threads are accessing this data at this
2263 lttng_ust_abi_exit();
2264 lttng_ust_abi_events_exit();
2265 lttng_perf_counter_exit();
2266 lttng_ust_ring_buffer_clients_exit();
2267 lttng_ust_counter_clients_exit();
2268 lttng_ust_statedump_destroy();
2269 lttng_ust_tp_exit();
2271 /* Reinitialize values for fork */
2272 sem_count
= sem_count_initial_value
;
2273 lttng_ust_comm_should_quit
= 0;
2278 void __attribute__((destructor
)) lttng_ust_exit(void)
2283 * Using pthread_cancel here because:
2284 * A) we don't want to hang application teardown.
2285 * B) the thread is not allocating any resource.
2289 * Require the communication thread to quit. Synchronize with
2290 * mutexes to ensure it is not in a mutex critical section when
2291 * pthread_cancel is later called.
2294 lttng_ust_comm_should_quit
= 1;
2297 pthread_mutex_lock(&ust_exit_mutex
);
2298 /* cancel threads */
2299 if (global_apps
.thread_active
) {
2300 ret
= pthread_cancel(global_apps
.ust_listener
);
2302 ERR("Error cancelling global ust listener thread: %s",
2305 global_apps
.thread_active
= 0;
2308 if (local_apps
.thread_active
) {
2309 ret
= pthread_cancel(local_apps
.ust_listener
);
2311 ERR("Error cancelling local ust listener thread: %s",
2314 local_apps
.thread_active
= 0;
2317 pthread_mutex_unlock(&ust_exit_mutex
);
2320 * Do NOT join threads: use of sys_futex makes it impossible to
2321 * join the threads without using async-cancel, but async-cancel
2322 * is delivered by a signal, which could hit the target thread
2323 * anywhere in its code path, including while the ust_lock() is
2324 * held, causing a deadlock for the other thread. Let the OS
2325 * cleanup the threads if there are stalled in a syscall.
2327 lttng_ust_cleanup(1);
2331 void ust_context_ns_reset(void)
2333 lttng_context_pid_ns_reset();
2334 lttng_context_cgroup_ns_reset();
2335 lttng_context_ipc_ns_reset();
2336 lttng_context_mnt_ns_reset();
2337 lttng_context_net_ns_reset();
2338 lttng_context_user_ns_reset();
2339 lttng_context_time_ns_reset();
2340 lttng_context_uts_ns_reset();
2344 void ust_context_vuids_reset(void)
2346 lttng_context_vuid_reset();
2347 lttng_context_veuid_reset();
2348 lttng_context_vsuid_reset();
2352 void ust_context_vgids_reset(void)
2354 lttng_context_vgid_reset();
2355 lttng_context_vegid_reset();
2356 lttng_context_vsgid_reset();
2360 * We exclude the worker threads across fork and clone (except
2361 * CLONE_VM), because these system calls only keep the forking thread
2362 * running in the child. Therefore, we don't want to call fork or clone
2363 * in the middle of an tracepoint or ust tracing state modification.
2364 * Holding this mutex protects these structures across fork and clone.
2366 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2369 * Disable signals. This is to avoid that the child intervenes
2370 * before it is properly setup for tracing. It is safer to
2371 * disable all signals, because then we know we are not breaking
2372 * anything by restoring the original mask.
2377 /* Fixup lttng-ust TLS. */
2378 lttng_ust_fixup_tls();
2380 if (URCU_TLS(lttng_ust_nest_count
))
2382 /* Disable signals */
2383 sigfillset(&all_sigs
);
2384 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2386 PERROR("sigprocmask");
2389 pthread_mutex_lock(&ust_fork_mutex
);
2392 lttng_ust_urcu_before_fork();
2393 lttng_ust_lock_fd_tracker();
2397 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2401 DBG("process %d", getpid());
2402 lttng_perf_unlock();
2403 lttng_ust_unlock_fd_tracker();
2406 pthread_mutex_unlock(&ust_fork_mutex
);
2408 /* Restore signals */
2409 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2411 PERROR("sigprocmask");
2415 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2417 if (URCU_TLS(lttng_ust_nest_count
))
2419 DBG("process %d", getpid());
2420 lttng_ust_urcu_after_fork_parent();
2421 /* Release mutexes and reenable signals */
2422 ust_after_fork_common(restore_sigset
);
2426 * After fork, in the child, we need to cleanup all the leftover state,
2427 * except the worker thread which already magically disappeared thanks
2428 * to the weird Linux fork semantics. After tyding up, we call
2429 * lttng_ust_init() again to start over as a new PID.
2431 * This is meant for forks() that have tracing in the child between the
2432 * fork and following exec call (if there is any).
2434 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2436 if (URCU_TLS(lttng_ust_nest_count
))
2438 lttng_context_vpid_reset();
2439 lttng_context_vtid_reset();
2440 lttng_ust_context_procname_reset();
2441 ust_context_ns_reset();
2442 ust_context_vuids_reset();
2443 ust_context_vgids_reset();
2444 DBG("process %d", getpid());
2445 /* Release urcu mutexes */
2446 lttng_ust_urcu_after_fork_child();
2447 lttng_ust_cleanup(0);
2448 /* Release mutexes and reenable signals */
2449 ust_after_fork_common(restore_sigset
);
2453 void lttng_ust_after_setns(void)
2455 ust_context_ns_reset();
2456 ust_context_vuids_reset();
2457 ust_context_vgids_reset();
2460 void lttng_ust_after_unshare(void)
2462 ust_context_ns_reset();
2463 ust_context_vuids_reset();
2464 ust_context_vgids_reset();
2467 void lttng_ust_after_setuid(void)
2469 ust_context_vuids_reset();
2472 void lttng_ust_after_seteuid(void)
2474 ust_context_vuids_reset();
2477 void lttng_ust_after_setreuid(void)
2479 ust_context_vuids_reset();
2482 void lttng_ust_after_setresuid(void)
2484 ust_context_vuids_reset();
2487 void lttng_ust_after_setgid(void)
2489 ust_context_vgids_reset();
2492 void lttng_ust_after_setegid(void)
2494 ust_context_vgids_reset();
2497 void lttng_ust_after_setregid(void)
2499 ust_context_vgids_reset();
2502 void lttng_ust_after_setresgid(void)
2504 ust_context_vgids_reset();
2507 void lttng_ust_sockinfo_session_enabled(void *owner
)
2509 struct sock_info
*sock_info
= owner
;
2510 sock_info
->statedump_pending
= 1;