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.h>
36 #include <lttng/ust-error.h>
37 #include <lttng/ust-ctl.h>
38 #include <lttng/ust-libc-wrapper.h>
39 #include <urcu/tls-compat.h>
42 #include <usterr-signal-safe.h>
43 #include <ust-helper.h>
44 #include "tracepoint-internal.h"
45 #include "lttng-tracer-core.h"
47 #include "../libringbuffer/rb-init.h"
48 #include "lttng-ust-statedump.h"
50 #include "../libringbuffer/getcpu.h"
52 #include "ust-events-internal.h"
53 #include "context-internal.h"
54 #include "ust-compat.h"
56 /* Concatenate lttng ust shared library name with its major version number. */
57 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." lttng_ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
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 extern void lttng_ring_buffer_client_overwrite_init(void);
356 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
357 extern void lttng_ring_buffer_client_discard_init(void);
358 extern void lttng_ring_buffer_client_discard_rt_init(void);
359 extern void lttng_ring_buffer_metadata_client_init(void);
360 extern void lttng_ring_buffer_client_overwrite_exit(void);
361 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
362 extern void lttng_ring_buffer_client_discard_exit(void);
363 extern void lttng_ring_buffer_client_discard_rt_exit(void);
364 extern void lttng_ring_buffer_metadata_client_exit(void);
366 __attribute__((visibility("hidden")))
367 extern void lttng_counter_client_percpu_32_modular_init(void);
369 __attribute__((visibility("hidden")))
370 extern void lttng_counter_client_percpu_32_modular_exit(void);
372 __attribute__((visibility("hidden")))
373 extern void lttng_counter_client_percpu_64_modular_init(void);
375 __attribute__((visibility("hidden")))
376 extern void lttng_counter_client_percpu_64_modular_exit(void);
378 static char *get_map_shm(struct sock_info
*sock_info
);
380 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
383 size_t copied
= 0, to_copy
= len
;
386 ret
= read(fd
, buf
+ copied
, to_copy
);
391 } while ((ret
> 0 && to_copy
> 0)
392 || (ret
< 0 && errno
== EINTR
));
399 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
403 const char *get_lttng_home_dir(void)
407 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
411 return (const char *) lttng_ust_getenv("HOME");
415 * Force a read (imply TLS fixup for dlopen) of TLS variables.
418 void lttng_fixup_nest_count_tls(void)
420 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
424 void lttng_fixup_ust_mutex_nest_tls(void)
426 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
430 * Fixup lttng-ust urcu TLS.
433 void lttng_fixup_lttng_ust_urcu_tls(void)
435 (void) lttng_ust_urcu_read_ongoing();
438 void lttng_ust_fixup_tls(void)
440 lttng_fixup_lttng_ust_urcu_tls();
441 lttng_fixup_ringbuffer_tls();
442 lttng_fixup_vtid_tls();
443 lttng_fixup_nest_count_tls();
444 lttng_fixup_procname_tls();
445 lttng_fixup_ust_mutex_nest_tls();
446 lttng_ust_fixup_perf_counter_tls();
447 lttng_ust_fixup_fd_tracker_tls();
448 lttng_fixup_cgroup_ns_tls();
449 lttng_fixup_ipc_ns_tls();
450 lttng_fixup_net_ns_tls();
451 lttng_fixup_time_ns_tls();
452 lttng_fixup_uts_ns_tls();
455 int lttng_get_notify_socket(void *owner
)
457 struct sock_info
*info
= owner
;
459 return info
->notify_socket
;
463 char* lttng_ust_sockinfo_get_procname(void *owner
)
465 struct sock_info
*info
= owner
;
467 return info
->procname
;
471 void print_cmd(int cmd
, int handle
)
473 const char *cmd_name
= "Unknown";
475 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
476 && cmd_name_mapping
[cmd
]) {
477 cmd_name
= cmd_name_mapping
[cmd
];
479 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
481 lttng_ust_obj_get_name(handle
), handle
);
485 int setup_global_apps(void)
488 assert(!global_apps
.wait_shm_mmap
);
490 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
491 if (!global_apps
.wait_shm_mmap
) {
492 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
493 global_apps
.allowed
= 0;
498 global_apps
.allowed
= 1;
499 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
504 int setup_local_apps(void)
507 const char *home_dir
;
510 assert(!local_apps
.wait_shm_mmap
);
514 * Disallow per-user tracing for setuid binaries.
516 if (uid
!= geteuid()) {
517 assert(local_apps
.allowed
== 0);
521 home_dir
= get_lttng_home_dir();
523 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
524 assert(local_apps
.allowed
== 0);
528 local_apps
.allowed
= 1;
529 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
531 LTTNG_DEFAULT_HOME_RUNDIR
,
532 LTTNG_UST_SOCK_FILENAME
);
533 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
534 LTTNG_UST_WAIT_FILENAME
,
537 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
538 if (!local_apps
.wait_shm_mmap
) {
539 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
540 local_apps
.allowed
= 0;
545 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
551 * Get socket timeout, in ms.
552 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
555 long get_timeout(void)
557 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
559 if (!got_timeout_env
) {
560 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
564 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
565 /* All negative values are considered as "-1". */
566 if (constructor_delay_ms
< -1)
567 constructor_delay_ms
= -1;
568 return constructor_delay_ms
;
571 /* Timeout for notify socket send and recv. */
573 long get_notify_sock_timeout(void)
575 return get_timeout();
578 /* Timeout for connecting to cmd and notify sockets. */
580 long get_connect_sock_timeout(void)
582 return get_timeout();
586 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
589 int get_constructor_timeout(struct timespec
*constructor_timeout
)
591 long constructor_delay_ms
;
594 constructor_delay_ms
= get_timeout();
596 switch (constructor_delay_ms
) {
597 case -1:/* fall-through */
599 return constructor_delay_ms
;
605 * If we are unable to find the current time, don't wait.
607 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
612 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
613 constructor_timeout
->tv_nsec
+=
614 (constructor_delay_ms
% 1000UL) * 1000000UL;
615 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
616 constructor_timeout
->tv_sec
++;
617 constructor_timeout
->tv_nsec
-= 1000000000UL;
619 /* Timeout wait (constructor_delay_ms). */
624 void get_allow_blocking(void)
626 const char *str_allow_blocking
=
627 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
629 if (str_allow_blocking
) {
630 DBG("%s environment variable is set",
631 "LTTNG_UST_ALLOW_BLOCKING");
632 lttng_ust_ringbuffer_set_allow_blocking();
637 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
639 return ustcomm_send_reg_msg(socket
,
642 lttng_alignof(uint8_t) * CHAR_BIT
,
643 lttng_alignof(uint16_t) * CHAR_BIT
,
644 lttng_alignof(uint32_t) * CHAR_BIT
,
645 lttng_alignof(uint64_t) * CHAR_BIT
,
646 lttng_alignof(unsigned long) * CHAR_BIT
);
650 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
654 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
657 DBG("message successfully sent");
660 if (len
== -ECONNRESET
) {
661 DBG("remote end closed connection");
666 DBG("incorrect message size: %zd", len
);
672 void decrement_sem_count(unsigned int count
)
676 assert(uatomic_read(&sem_count
) >= count
);
678 if (uatomic_read(&sem_count
) <= 0) {
682 ret
= uatomic_add_return(&sem_count
, -count
);
684 ret
= sem_post(&constructor_wait
);
690 int handle_register_done(struct sock_info
*sock_info
)
692 if (sock_info
->registration_done
)
694 sock_info
->registration_done
= 1;
696 decrement_sem_count(1);
697 if (!sock_info
->statedump_pending
) {
698 sock_info
->initial_statedump_done
= 1;
699 decrement_sem_count(1);
706 int handle_register_failed(struct sock_info
*sock_info
)
708 if (sock_info
->registration_done
)
710 sock_info
->registration_done
= 1;
711 sock_info
->initial_statedump_done
= 1;
713 decrement_sem_count(2);
719 * Only execute pending statedump after the constructor semaphore has
720 * been posted by the current listener thread. This means statedump will
721 * only be performed after the "registration done" command is received
722 * from this thread's session daemon.
724 * This ensures we don't run into deadlock issues with the dynamic
725 * loader mutex, which is held while the constructor is called and
726 * waiting on the constructor semaphore. All operations requiring this
727 * dynamic loader lock need to be postponed using this mechanism.
729 * In a scenario with two session daemons connected to the application,
730 * it is possible that the first listener thread which receives the
731 * registration done command issues its statedump while the dynamic
732 * loader lock is still held by the application constructor waiting on
733 * the semaphore. It will however be allowed to proceed when the
734 * second session daemon sends the registration done command to the
735 * second listener thread. This situation therefore does not produce
739 void handle_pending_statedump(struct sock_info
*sock_info
)
741 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
742 sock_info
->statedump_pending
= 0;
743 pthread_mutex_lock(&ust_fork_mutex
);
744 lttng_handle_pending_statedump(sock_info
);
745 pthread_mutex_unlock(&ust_fork_mutex
);
747 if (!sock_info
->initial_statedump_done
) {
748 sock_info
->initial_statedump_done
= 1;
749 decrement_sem_count(1);
755 const char *bytecode_type_str(uint32_t cmd
)
758 case LTTNG_UST_ABI_CAPTURE
:
760 case LTTNG_UST_ABI_FILTER
:
768 int handle_bytecode_recv(struct sock_info
*sock_info
,
769 int sock
, struct ustcomm_ust_msg
*lum
)
771 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
772 enum lttng_ust_bytecode_type type
;
773 const struct lttng_ust_abi_objd_ops
*ops
;
774 uint32_t data_size
, data_size_max
, reloc_offset
;
780 case LTTNG_UST_ABI_FILTER
:
781 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
782 data_size
= lum
->u
.filter
.data_size
;
783 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
784 reloc_offset
= lum
->u
.filter
.reloc_offset
;
785 seqnum
= lum
->u
.filter
.seqnum
;
787 case LTTNG_UST_ABI_CAPTURE
:
788 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
789 data_size
= lum
->u
.capture
.data_size
;
790 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
791 reloc_offset
= lum
->u
.capture
.reloc_offset
;
792 seqnum
= lum
->u
.capture
.seqnum
;
798 if (data_size
> data_size_max
) {
799 ERR("Bytecode %s data size is too large: %u bytes",
800 bytecode_type_str(lum
->cmd
), data_size
);
805 if (reloc_offset
> data_size
) {
806 ERR("Bytecode %s reloc offset %u is not within data",
807 bytecode_type_str(lum
->cmd
), reloc_offset
);
812 /* Allocate the structure AND the `data[]` field. */
813 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
819 bytecode
->bc
.len
= data_size
;
820 bytecode
->bc
.reloc_offset
= reloc_offset
;
821 bytecode
->bc
.seqnum
= seqnum
;
822 bytecode
->type
= type
;
824 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
826 case 0: /* orderly shutdown */
830 if (len
== bytecode
->bc
.len
) {
831 DBG("Bytecode %s data received",
832 bytecode_type_str(lum
->cmd
));
834 } else if (len
< 0) {
835 DBG("Receive failed from lttng-sessiond with errno %d",
837 if (len
== -ECONNRESET
) {
838 ERR("%s remote end closed connection",
846 DBG("Incorrect %s bytecode data message size: %zd",
847 bytecode_type_str(lum
->cmd
), len
);
853 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
860 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
861 (unsigned long) &bytecode
,
872 int handle_message(struct sock_info
*sock_info
,
873 int sock
, struct ustcomm_ust_msg
*lum
)
876 const struct lttng_ust_abi_objd_ops
*ops
;
877 struct ustcomm_ust_reply lur
;
878 union lttng_ust_abi_args args
;
879 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
882 memset(&lur
, 0, sizeof(lur
));
885 ret
= -LTTNG_UST_ERR_EXITING
;
889 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
896 case LTTNG_UST_ABI_REGISTER_DONE
:
897 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
898 ret
= handle_register_done(sock_info
);
902 case LTTNG_UST_ABI_RELEASE
:
903 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
906 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
908 case LTTNG_UST_ABI_CAPTURE
:
909 case LTTNG_UST_ABI_FILTER
:
910 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
914 case LTTNG_UST_ABI_EXCLUSION
:
916 /* Receive exclusion names */
917 struct lttng_ust_excluder_node
*node
;
920 count
= lum
->u
.exclusion
.count
;
922 /* There are no names to read */
926 node
= zmalloc(sizeof(*node
) +
927 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
932 node
->excluder
.count
= count
;
933 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
934 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
936 case 0: /* orderly shutdown */
941 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
942 DBG("Exclusion data received");
944 } else if (len
< 0) {
945 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
946 if (len
== -ECONNRESET
) {
947 ERR("%s remote end closed connection", sock_info
->name
);
956 DBG("Incorrect exclusion data message size: %zd", len
);
963 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
964 (unsigned long) &node
,
971 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
973 int event_notifier_notif_fd
, close_ret
;
975 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
976 &event_notifier_notif_fd
);
978 case 0: /* orderly shutdown */
985 DBG("Receive failed from lttng-sessiond with errno %d",
987 if (len
== -ECONNRESET
) {
988 ERR("%s remote end closed connection",
996 DBG("Incorrect event notifier fd message size: %zd",
1002 args
.event_notifier_handle
.event_notifier_notif_fd
=
1003 event_notifier_notif_fd
;
1005 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1006 (unsigned long) &lum
->u
,
1010 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1011 lttng_ust_lock_fd_tracker();
1012 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1013 lttng_ust_unlock_fd_tracker();
1019 case LTTNG_UST_ABI_CHANNEL
:
1024 len
= ustcomm_recv_channel_from_sessiond(sock
,
1025 &chan_data
, lum
->u
.channel
.len
,
1028 case 0: /* orderly shutdown */
1032 if (len
== lum
->u
.channel
.len
) {
1033 DBG("channel data received");
1035 } else if (len
< 0) {
1036 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1037 if (len
== -ECONNRESET
) {
1038 ERR("%s remote end closed connection", sock_info
->name
);
1045 DBG("incorrect channel data message size: %zd", len
);
1050 args
.channel
.chan_data
= chan_data
;
1051 args
.channel
.wakeup_fd
= wakeup_fd
;
1053 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1054 (unsigned long) &lum
->u
,
1058 if (args
.channel
.wakeup_fd
>= 0) {
1061 lttng_ust_lock_fd_tracker();
1062 close_ret
= close(args
.channel
.wakeup_fd
);
1063 lttng_ust_unlock_fd_tracker();
1064 args
.channel
.wakeup_fd
= -1;
1068 free(args
.channel
.chan_data
);
1071 case LTTNG_UST_ABI_STREAM
:
1075 /* Receive shm_fd, wakeup_fd */
1076 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1078 &args
.stream
.shm_fd
,
1079 &args
.stream
.wakeup_fd
);
1085 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1086 (unsigned long) &lum
->u
,
1090 if (args
.stream
.shm_fd
>= 0) {
1091 lttng_ust_lock_fd_tracker();
1092 close_ret
= close(args
.stream
.shm_fd
);
1093 lttng_ust_unlock_fd_tracker();
1094 args
.stream
.shm_fd
= -1;
1098 if (args
.stream
.wakeup_fd
>= 0) {
1099 lttng_ust_lock_fd_tracker();
1100 close_ret
= close(args
.stream
.wakeup_fd
);
1101 lttng_ust_unlock_fd_tracker();
1102 args
.stream
.wakeup_fd
= -1;
1108 case LTTNG_UST_ABI_CONTEXT
:
1109 switch (lum
->u
.context
.ctx
) {
1110 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1113 size_t ctxlen
, recvlen
;
1115 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1116 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1117 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1118 ERR("Application context string length size is too large: %zu bytes",
1123 strcpy(ctxstr
, "$app.");
1124 p
= &ctxstr
[strlen("$app.")];
1125 recvlen
= ctxlen
- strlen("$app.");
1126 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1128 case 0: /* orderly shutdown */
1132 if (len
== recvlen
) {
1133 DBG("app context data received");
1135 } else if (len
< 0) {
1136 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1137 if (len
== -ECONNRESET
) {
1138 ERR("%s remote end closed connection", sock_info
->name
);
1145 DBG("incorrect app context data message size: %zd", len
);
1150 /* Put : between provider and ctxname. */
1151 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1152 args
.app_context
.ctxname
= ctxstr
;
1159 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1160 (unsigned long) &lum
->u
,
1166 case LTTNG_UST_ABI_COUNTER
:
1170 len
= ustcomm_recv_counter_from_sessiond(sock
,
1171 &counter_data
, lum
->u
.counter
.len
);
1173 case 0: /* orderly shutdown */
1177 if (len
== lum
->u
.counter
.len
) {
1178 DBG("counter data received");
1180 } else if (len
< 0) {
1181 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1182 if (len
== -ECONNRESET
) {
1183 ERR("%s remote end closed connection", sock_info
->name
);
1190 DBG("incorrect counter data message size: %zd", len
);
1195 args
.counter
.counter_data
= counter_data
;
1197 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1198 (unsigned long) &lum
->u
,
1202 free(args
.counter
.counter_data
);
1205 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1207 /* Receive shm_fd */
1208 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1209 &args
.counter_shm
.shm_fd
);
1215 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1216 (unsigned long) &lum
->u
,
1220 if (args
.counter_shm
.shm_fd
>= 0) {
1223 lttng_ust_lock_fd_tracker();
1224 close_ret
= close(args
.counter_shm
.shm_fd
);
1225 lttng_ust_unlock_fd_tracker();
1226 args
.counter_shm
.shm_fd
= -1;
1232 case LTTNG_UST_ABI_COUNTER_CPU
:
1234 /* Receive shm_fd */
1235 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1236 &args
.counter_shm
.shm_fd
);
1242 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1243 (unsigned long) &lum
->u
,
1247 if (args
.counter_shm
.shm_fd
>= 0) {
1250 lttng_ust_lock_fd_tracker();
1251 close_ret
= close(args
.counter_shm
.shm_fd
);
1252 lttng_ust_unlock_fd_tracker();
1253 args
.counter_shm
.shm_fd
= -1;
1259 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1261 /* Receive struct lttng_ust_event_notifier */
1262 struct lttng_ust_abi_event_notifier event_notifier
;
1264 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1265 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1269 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1271 case 0: /* orderly shutdown */
1275 if (len
== sizeof(event_notifier
)) {
1276 DBG("event notifier data received");
1278 } else if (len
< 0) {
1279 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1280 if (len
== -ECONNRESET
) {
1281 ERR("%s remote end closed connection", sock_info
->name
);
1288 DBG("incorrect event notifier data message size: %zd", len
);
1294 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1295 (unsigned long) &event_notifier
,
1304 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1305 (unsigned long) &lum
->u
,
1312 lur
.handle
= lum
->handle
;
1316 lur
.ret_code
= LTTNG_UST_OK
;
1319 * Use -LTTNG_UST_ERR as wildcard for UST internal
1320 * error that are not caused by the transport, except if
1321 * we already have a more precise error message to
1324 if (ret
> -LTTNG_UST_ERR
) {
1325 /* Translate code to UST error. */
1328 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1331 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1334 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1337 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1340 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1343 lur
.ret_code
= -LTTNG_UST_ERR
;
1352 case LTTNG_UST_ABI_TRACER_VERSION
:
1353 lur
.u
.version
= lum
->u
.version
;
1355 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1356 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1360 DBG("Return value: %d", lur
.ret_val
);
1365 * Performed delayed statedump operations outside of the UST
1366 * lock. We need to take the dynamic loader lock before we take
1367 * the UST lock internally within handle_pending_statedump().
1369 handle_pending_statedump(sock_info
);
1372 ret
= -LTTNG_UST_ERR_EXITING
;
1376 ret
= send_reply(sock
, &lur
);
1378 DBG("error sending reply");
1383 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1386 if (lur
.ret_code
== LTTNG_UST_OK
) {
1388 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1389 len
= ustcomm_send_unix_sock(sock
,
1390 &args
.field_list
.entry
,
1391 sizeof(args
.field_list
.entry
));
1396 if (len
!= sizeof(args
.field_list
.entry
)) {
1410 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1414 if (sock_info
->root_handle
!= -1) {
1415 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1417 ERR("Error unref root handle");
1419 sock_info
->root_handle
= -1;
1421 sock_info
->registration_done
= 0;
1422 sock_info
->initial_statedump_done
= 0;
1425 * wait_shm_mmap, socket and notify socket are used by listener
1426 * threads outside of the ust lock, so we cannot tear them down
1427 * ourselves, because we cannot join on these threads. Leave
1428 * responsibility of cleaning up these resources to the OS
1434 if (sock_info
->socket
!= -1) {
1435 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1437 ERR("Error closing ust cmd socket");
1439 sock_info
->socket
= -1;
1441 if (sock_info
->notify_socket
!= -1) {
1442 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1444 ERR("Error closing ust notify socket");
1446 sock_info
->notify_socket
= -1;
1448 if (sock_info
->wait_shm_mmap
) {
1451 page_size
= LTTNG_UST_PAGE_SIZE
;
1452 if (page_size
<= 0) {
1456 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1458 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1460 ERR("Error unmapping wait shm");
1463 sock_info
->wait_shm_mmap
= NULL
;
1468 * Using fork to set umask in the child process (not multi-thread safe).
1469 * We deal with the shm_open vs ftruncate race (happening when the
1470 * sessiond owns the shm and does not let everybody modify it, to ensure
1471 * safety against shm_unlink) by simply letting the mmap fail and
1472 * retrying after a few seconds.
1473 * For global shm, everybody has rw access to it until the sessiond
1477 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1479 int wait_shm_fd
, ret
;
1483 * Try to open read-only.
1485 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1486 if (wait_shm_fd
>= 0) {
1489 size_t bytes_read
= 0;
1492 * Try to read the fd. If unable to do so, try opening
1496 len
= read(wait_shm_fd
,
1497 &((char *) &tmp_read
)[bytes_read
],
1498 sizeof(tmp_read
) - bytes_read
);
1502 } while ((len
< 0 && errno
== EINTR
)
1503 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1504 if (bytes_read
!= sizeof(tmp_read
)) {
1505 ret
= close(wait_shm_fd
);
1507 ERR("close wait_shm_fd");
1512 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1514 * Real-only open did not work, and it's not because the
1515 * entry was not present. It's a failure that prohibits
1518 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1524 * If the open failed because the file did not exist, or because
1525 * the file was not truncated yet, try creating it ourself.
1527 URCU_TLS(lttng_ust_nest_count
)++;
1529 URCU_TLS(lttng_ust_nest_count
)--;
1534 * Parent: wait for child to return, in which case the
1535 * shared memory map will have been created.
1537 pid
= wait(&status
);
1538 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1543 * Try to open read-only again after creation.
1545 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1546 if (wait_shm_fd
< 0) {
1548 * Real-only open did not work. It's a failure
1549 * that prohibits using shm.
1551 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1555 } else if (pid
== 0) {
1559 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1560 if (sock_info
->global
)
1561 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1563 * We're alone in a child process, so we can modify the
1564 * process-wide umask.
1566 umask(~create_mode
);
1568 * Try creating shm (or get rw access).
1569 * We don't do an exclusive open, because we allow other
1570 * processes to create+ftruncate it concurrently.
1572 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1573 O_RDWR
| O_CREAT
, create_mode
);
1574 if (wait_shm_fd
>= 0) {
1575 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1577 PERROR("ftruncate");
1578 _exit(EXIT_FAILURE
);
1580 _exit(EXIT_SUCCESS
);
1583 * For local shm, we need to have rw access to accept
1584 * opening it: this means the local sessiond will be
1585 * able to wake us up. For global shm, we open it even
1586 * if rw access is not granted, because the root.root
1587 * sessiond will be able to override all rights and wake
1590 if (!sock_info
->global
&& errno
!= EACCES
) {
1591 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1592 _exit(EXIT_FAILURE
);
1595 * The shm exists, but we cannot open it RW. Report
1598 _exit(EXIT_SUCCESS
);
1603 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1604 struct stat statbuf
;
1607 * Ensure that our user is the owner of the shm file for
1608 * local shm. If we do not own the file, it means our
1609 * sessiond will not have access to wake us up (there is
1610 * probably a rogue process trying to fake our
1611 * sessiond). Fallback to polling method in this case.
1613 ret
= fstat(wait_shm_fd
, &statbuf
);
1618 if (statbuf
.st_uid
!= getuid())
1624 ret
= close(wait_shm_fd
);
1626 PERROR("Error closing fd");
1632 char *get_map_shm(struct sock_info
*sock_info
)
1635 int wait_shm_fd
, ret
;
1636 char *wait_shm_mmap
;
1638 page_size
= sysconf(_SC_PAGE_SIZE
);
1639 if (page_size
<= 0) {
1643 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1647 lttng_ust_lock_fd_tracker();
1648 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1649 if (wait_shm_fd
< 0) {
1650 lttng_ust_unlock_fd_tracker();
1654 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1656 ret
= close(wait_shm_fd
);
1658 PERROR("Error closing fd");
1660 lttng_ust_unlock_fd_tracker();
1665 lttng_ust_unlock_fd_tracker();
1667 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1668 MAP_SHARED
, wait_shm_fd
, 0);
1670 /* close shm fd immediately after taking the mmap reference */
1671 lttng_ust_lock_fd_tracker();
1672 ret
= close(wait_shm_fd
);
1674 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1676 PERROR("Error closing fd");
1678 lttng_ust_unlock_fd_tracker();
1680 if (wait_shm_mmap
== MAP_FAILED
) {
1681 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1684 return wait_shm_mmap
;
1691 void wait_for_sessiond(struct sock_info
*sock_info
)
1693 /* Use ust_lock to check if we should quit. */
1697 if (wait_poll_fallback
) {
1702 assert(sock_info
->wait_shm_mmap
);
1704 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1705 /* Wait for futex wakeup */
1706 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1709 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1710 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1713 /* Value already changed. */
1716 /* Retry if interrupted by signal. */
1717 break; /* Get out of switch. */
1719 wait_poll_fallback
= 1;
1721 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1722 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1723 "Please upgrade your kernel "
1724 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1725 "mainline). LTTng-UST will use polling mode fallback.");
1726 if (ust_err_debug_enabled())
1744 * This thread does not allocate any resource, except within
1745 * handle_message, within mutex protection. This mutex protects against
1747 * The other moment it allocates resources is at socket connection, which
1748 * is also protected by the mutex.
1751 void *ust_listener_thread(void *arg
)
1753 struct sock_info
*sock_info
= arg
;
1754 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1757 lttng_ust_fixup_tls();
1759 * If available, add '-ust' to the end of this thread's
1762 ret
= lttng_ust_setustprocname();
1764 ERR("Unable to set UST process name");
1767 /* Restart trying to connect to the session daemon */
1769 if (prev_connect_failed
) {
1770 /* Wait for sessiond availability with pipe */
1771 wait_for_sessiond(sock_info
);
1775 * Sleep for 5 seconds before retrying after a
1776 * sequence of failure / wait / failure. This
1777 * deals with a killed or broken session daemon.
1783 prev_connect_failed
= 0;
1790 if (sock_info
->socket
!= -1) {
1791 /* FD tracker is updated by ustcomm_close_unix_sock() */
1792 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1794 ERR("Error closing %s ust cmd socket",
1797 sock_info
->socket
= -1;
1799 if (sock_info
->notify_socket
!= -1) {
1800 /* FD tracker is updated by ustcomm_close_unix_sock() */
1801 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1803 ERR("Error closing %s ust notify socket",
1806 sock_info
->notify_socket
= -1;
1811 * Register. We need to perform both connect and sending
1812 * registration message before doing the next connect otherwise
1813 * we may reach unix socket connect queue max limits and block
1814 * on the 2nd connect while the session daemon is awaiting the
1815 * first connect registration message.
1817 /* Connect cmd socket */
1818 lttng_ust_lock_fd_tracker();
1819 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1820 get_connect_sock_timeout());
1822 lttng_ust_unlock_fd_tracker();
1823 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1824 prev_connect_failed
= 1;
1827 * If we cannot find the sessiond daemon, don't delay
1828 * constructor execution.
1830 ret
= handle_register_failed(sock_info
);
1836 ret
= lttng_ust_add_fd_to_tracker(fd
);
1840 PERROR("close on sock_info->socket");
1843 lttng_ust_unlock_fd_tracker();
1848 sock_info
->socket
= ret
;
1849 lttng_ust_unlock_fd_tracker();
1853 * Unlock/relock ust lock because connect is blocking (with
1854 * timeout). Don't delay constructors on the ust lock for too
1862 * Create only one root handle per listener thread for the whole
1863 * process lifetime, so we ensure we get ID which is statically
1864 * assigned to the root handle.
1866 if (sock_info
->root_handle
== -1) {
1867 ret
= lttng_abi_create_root_handle();
1869 ERR("Error creating root handle");
1872 sock_info
->root_handle
= ret
;
1875 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1877 ERR("Error registering to %s ust cmd socket",
1879 prev_connect_failed
= 1;
1881 * If we cannot register to the sessiond daemon, don't
1882 * delay constructor execution.
1884 ret
= handle_register_failed(sock_info
);
1892 * Unlock/relock ust lock because connect is blocking (with
1893 * timeout). Don't delay constructors on the ust lock for too
1900 /* Connect notify socket */
1901 lttng_ust_lock_fd_tracker();
1902 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1903 get_connect_sock_timeout());
1905 lttng_ust_unlock_fd_tracker();
1906 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1907 prev_connect_failed
= 1;
1910 * If we cannot find the sessiond daemon, don't delay
1911 * constructor execution.
1913 ret
= handle_register_failed(sock_info
);
1920 ret
= lttng_ust_add_fd_to_tracker(fd
);
1924 PERROR("close on sock_info->notify_socket");
1927 lttng_ust_unlock_fd_tracker();
1932 sock_info
->notify_socket
= ret
;
1933 lttng_ust_unlock_fd_tracker();
1937 * Unlock/relock ust lock because connect is blocking (with
1938 * timeout). Don't delay constructors on the ust lock for too
1945 timeout
= get_notify_sock_timeout();
1948 * Give at least 10ms to sessiond to reply to
1953 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1956 WARN("Error setting socket receive timeout");
1958 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1961 WARN("Error setting socket send timeout");
1963 } else if (timeout
< -1) {
1964 WARN("Unsupported timeout value %ld", timeout
);
1967 ret
= register_to_sessiond(sock_info
->notify_socket
,
1968 USTCTL_SOCKET_NOTIFY
);
1970 ERR("Error registering to %s ust notify socket",
1972 prev_connect_failed
= 1;
1974 * If we cannot register to the sessiond daemon, don't
1975 * delay constructor execution.
1977 ret
= handle_register_failed(sock_info
);
1982 sock
= sock_info
->socket
;
1988 struct ustcomm_ust_msg lum
;
1990 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1992 case 0: /* orderly shutdown */
1993 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1998 * Either sessiond has shutdown or refused us by closing the socket.
1999 * In either case, we don't want to delay construction execution,
2000 * and we need to wait before retry.
2002 prev_connect_failed
= 1;
2004 * If we cannot register to the sessiond daemon, don't
2005 * delay constructor execution.
2007 ret
= handle_register_failed(sock_info
);
2012 print_cmd(lum
.cmd
, lum
.handle
);
2013 ret
= handle_message(sock_info
, sock
, &lum
);
2015 ERR("Error handling message for %s socket",
2018 * Close socket if protocol error is
2026 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2028 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2030 if (len
== -ECONNRESET
) {
2031 DBG("%s remote end closed connection", sock_info
->name
);
2042 /* Cleanup socket handles before trying to reconnect */
2043 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2045 goto restart
; /* try to reconnect */
2050 pthread_mutex_lock(&ust_exit_mutex
);
2051 sock_info
->thread_active
= 0;
2052 pthread_mutex_unlock(&ust_exit_mutex
);
2057 * Weak symbol to call when the ust malloc wrapper is not loaded.
2059 __attribute__((weak
))
2060 void lttng_ust_libc_wrapper_malloc_init(void)
2065 * sessiond monitoring thread: monitor presence of global and per-user
2066 * sessiond by polling the application common named pipe.
2068 void __attribute__((constructor
)) lttng_ust_init(void)
2070 struct timespec constructor_timeout
;
2071 sigset_t sig_all_blocked
, orig_parent_mask
;
2072 pthread_attr_t thread_attr
;
2077 if (uatomic_xchg(&initialized
, 1) == 1)
2081 * Fixup interdependency between TLS fixup mutex (which happens
2082 * to be the dynamic linker mutex) and ust_lock, taken within
2085 lttng_ust_fixup_tls();
2087 lttng_ust_loaded
= 1;
2090 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2091 * the unloading of code used by the ust_listener_threads as we can not
2092 * reliably know when they exited. To do that, manually load
2093 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2094 * this library so it never becomes zero, thus never gets unloaded from the
2095 * address space of the process. Since we are already running in the
2096 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
2097 * simply increment the refcount and no additionnal work is needed by the
2098 * dynamic loader as the shared library is already loaded in the address
2099 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2100 * unloading of the UST library if its refcount becomes zero (which should
2101 * never happen). Do the return value check but discard the handle at the
2102 * end of the function as it's not needed.
2104 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
2106 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
2110 * We want precise control over the order in which we construct
2111 * our sub-libraries vs starting to receive commands from
2112 * sessiond (otherwise leading to errors when trying to create
2113 * sessiond before the init functions are completed).
2116 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2117 lttng_ust_tp_init();
2118 lttng_ust_init_fd_tracker();
2119 lttng_ust_clock_init();
2120 lttng_ust_getcpu_init();
2121 lttng_ust_statedump_init();
2122 lttng_ring_buffer_metadata_client_init();
2123 lttng_ring_buffer_client_overwrite_init();
2124 lttng_ring_buffer_client_overwrite_rt_init();
2125 lttng_ring_buffer_client_discard_init();
2126 lttng_ring_buffer_client_discard_rt_init();
2127 lttng_counter_client_percpu_32_modular_init();
2128 lttng_counter_client_percpu_64_modular_init();
2129 lttng_perf_counter_init();
2131 * Invoke ust malloc wrapper init before starting other threads.
2133 lttng_ust_libc_wrapper_malloc_init();
2135 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2137 get_allow_blocking();
2139 ret
= sem_init(&constructor_wait
, 0, 0);
2144 ret
= setup_global_apps();
2146 assert(global_apps
.allowed
== 0);
2147 DBG("global apps setup returned %d", ret
);
2150 ret
= setup_local_apps();
2152 assert(local_apps
.allowed
== 0);
2153 DBG("local apps setup returned %d", ret
);
2156 /* A new thread created by pthread_create inherits the signal mask
2157 * from the parent. To avoid any signal being received by the
2158 * listener thread, we block all signals temporarily in the parent,
2159 * while we create the listener thread.
2161 sigfillset(&sig_all_blocked
);
2162 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2164 ERR("pthread_sigmask: %s", strerror(ret
));
2167 ret
= pthread_attr_init(&thread_attr
);
2169 ERR("pthread_attr_init: %s", strerror(ret
));
2171 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2173 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2176 if (global_apps
.allowed
) {
2177 pthread_mutex_lock(&ust_exit_mutex
);
2178 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2179 ust_listener_thread
, &global_apps
);
2181 ERR("pthread_create global: %s", strerror(ret
));
2183 global_apps
.thread_active
= 1;
2184 pthread_mutex_unlock(&ust_exit_mutex
);
2186 handle_register_done(&global_apps
);
2189 if (local_apps
.allowed
) {
2190 pthread_mutex_lock(&ust_exit_mutex
);
2191 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2192 ust_listener_thread
, &local_apps
);
2194 ERR("pthread_create local: %s", strerror(ret
));
2196 local_apps
.thread_active
= 1;
2197 pthread_mutex_unlock(&ust_exit_mutex
);
2199 handle_register_done(&local_apps
);
2201 ret
= pthread_attr_destroy(&thread_attr
);
2203 ERR("pthread_attr_destroy: %s", strerror(ret
));
2206 /* Restore original signal mask in parent */
2207 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2209 ERR("pthread_sigmask: %s", strerror(ret
));
2212 switch (timeout_mode
) {
2213 case 1: /* timeout wait */
2215 ret
= sem_timedwait(&constructor_wait
,
2216 &constructor_timeout
);
2217 } while (ret
< 0 && errno
== EINTR
);
2221 ERR("Timed out waiting for lttng-sessiond");
2224 PERROR("sem_timedwait");
2227 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2232 case -1:/* wait forever */
2234 ret
= sem_wait(&constructor_wait
);
2235 } while (ret
< 0 && errno
== EINTR
);
2242 ERR("Unexpected error \"%s\" returned by sem_wait",
2247 case 0: /* no timeout */
2253 void lttng_ust_cleanup(int exiting
)
2255 cleanup_sock_info(&global_apps
, exiting
);
2256 cleanup_sock_info(&local_apps
, exiting
);
2257 local_apps
.allowed
= 0;
2258 global_apps
.allowed
= 0;
2260 * The teardown in this function all affect data structures
2261 * accessed under the UST lock by the listener thread. This
2262 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2263 * that none of these threads are accessing this data at this
2266 lttng_ust_abi_exit();
2267 lttng_ust_abi_events_exit();
2268 lttng_perf_counter_exit();
2269 lttng_ring_buffer_client_discard_rt_exit();
2270 lttng_ring_buffer_client_discard_exit();
2271 lttng_ring_buffer_client_overwrite_rt_exit();
2272 lttng_ring_buffer_client_overwrite_exit();
2273 lttng_ring_buffer_metadata_client_exit();
2274 lttng_counter_client_percpu_32_modular_exit();
2275 lttng_counter_client_percpu_64_modular_exit();
2276 lttng_ust_statedump_destroy();
2277 lttng_ust_tp_exit();
2279 /* Reinitialize values for fork */
2280 sem_count
= sem_count_initial_value
;
2281 lttng_ust_comm_should_quit
= 0;
2286 void __attribute__((destructor
)) lttng_ust_exit(void)
2291 * Using pthread_cancel here because:
2292 * A) we don't want to hang application teardown.
2293 * B) the thread is not allocating any resource.
2297 * Require the communication thread to quit. Synchronize with
2298 * mutexes to ensure it is not in a mutex critical section when
2299 * pthread_cancel is later called.
2302 lttng_ust_comm_should_quit
= 1;
2305 pthread_mutex_lock(&ust_exit_mutex
);
2306 /* cancel threads */
2307 if (global_apps
.thread_active
) {
2308 ret
= pthread_cancel(global_apps
.ust_listener
);
2310 ERR("Error cancelling global ust listener thread: %s",
2313 global_apps
.thread_active
= 0;
2316 if (local_apps
.thread_active
) {
2317 ret
= pthread_cancel(local_apps
.ust_listener
);
2319 ERR("Error cancelling local ust listener thread: %s",
2322 local_apps
.thread_active
= 0;
2325 pthread_mutex_unlock(&ust_exit_mutex
);
2328 * Do NOT join threads: use of sys_futex makes it impossible to
2329 * join the threads without using async-cancel, but async-cancel
2330 * is delivered by a signal, which could hit the target thread
2331 * anywhere in its code path, including while the ust_lock() is
2332 * held, causing a deadlock for the other thread. Let the OS
2333 * cleanup the threads if there are stalled in a syscall.
2335 lttng_ust_cleanup(1);
2339 void ust_context_ns_reset(void)
2341 lttng_context_pid_ns_reset();
2342 lttng_context_cgroup_ns_reset();
2343 lttng_context_ipc_ns_reset();
2344 lttng_context_mnt_ns_reset();
2345 lttng_context_net_ns_reset();
2346 lttng_context_user_ns_reset();
2347 lttng_context_time_ns_reset();
2348 lttng_context_uts_ns_reset();
2352 void ust_context_vuids_reset(void)
2354 lttng_context_vuid_reset();
2355 lttng_context_veuid_reset();
2356 lttng_context_vsuid_reset();
2360 void ust_context_vgids_reset(void)
2362 lttng_context_vgid_reset();
2363 lttng_context_vegid_reset();
2364 lttng_context_vsgid_reset();
2368 * We exclude the worker threads across fork and clone (except
2369 * CLONE_VM), because these system calls only keep the forking thread
2370 * running in the child. Therefore, we don't want to call fork or clone
2371 * in the middle of an tracepoint or ust tracing state modification.
2372 * Holding this mutex protects these structures across fork and clone.
2374 void ust_before_fork(sigset_t
*save_sigset
)
2377 * Disable signals. This is to avoid that the child intervenes
2378 * before it is properly setup for tracing. It is safer to
2379 * disable all signals, because then we know we are not breaking
2380 * anything by restoring the original mask.
2385 /* Fixup lttng-ust TLS. */
2386 lttng_ust_fixup_tls();
2388 if (URCU_TLS(lttng_ust_nest_count
))
2390 /* Disable signals */
2391 sigfillset(&all_sigs
);
2392 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2394 PERROR("sigprocmask");
2397 pthread_mutex_lock(&ust_fork_mutex
);
2400 lttng_ust_urcu_before_fork();
2401 lttng_ust_lock_fd_tracker();
2405 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2409 DBG("process %d", getpid());
2410 lttng_perf_unlock();
2411 lttng_ust_unlock_fd_tracker();
2414 pthread_mutex_unlock(&ust_fork_mutex
);
2416 /* Restore signals */
2417 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2419 PERROR("sigprocmask");
2423 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2425 if (URCU_TLS(lttng_ust_nest_count
))
2427 DBG("process %d", getpid());
2428 lttng_ust_urcu_after_fork_parent();
2429 /* Release mutexes and reenable signals */
2430 ust_after_fork_common(restore_sigset
);
2434 * After fork, in the child, we need to cleanup all the leftover state,
2435 * except the worker thread which already magically disappeared thanks
2436 * to the weird Linux fork semantics. After tyding up, we call
2437 * lttng_ust_init() again to start over as a new PID.
2439 * This is meant for forks() that have tracing in the child between the
2440 * fork and following exec call (if there is any).
2442 void ust_after_fork_child(sigset_t
*restore_sigset
)
2444 if (URCU_TLS(lttng_ust_nest_count
))
2446 lttng_context_vpid_reset();
2447 lttng_context_vtid_reset();
2448 lttng_ust_context_procname_reset();
2449 ust_context_ns_reset();
2450 ust_context_vuids_reset();
2451 ust_context_vgids_reset();
2452 DBG("process %d", getpid());
2453 /* Release urcu mutexes */
2454 lttng_ust_urcu_after_fork_child();
2455 lttng_ust_cleanup(0);
2456 /* Release mutexes and reenable signals */
2457 ust_after_fork_common(restore_sigset
);
2461 void ust_after_setns(void)
2463 ust_context_ns_reset();
2464 ust_context_vuids_reset();
2465 ust_context_vgids_reset();
2468 void ust_after_unshare(void)
2470 ust_context_ns_reset();
2471 ust_context_vuids_reset();
2472 ust_context_vgids_reset();
2475 void ust_after_setuid(void)
2477 ust_context_vuids_reset();
2480 void ust_after_seteuid(void)
2482 ust_context_vuids_reset();
2485 void ust_after_setreuid(void)
2487 ust_context_vuids_reset();
2490 void ust_after_setresuid(void)
2492 ust_context_vuids_reset();
2495 void ust_after_setgid(void)
2497 ust_context_vgids_reset();
2500 void ust_after_setegid(void)
2502 ust_context_vgids_reset();
2505 void ust_after_setregid(void)
2507 ust_context_vgids_reset();
2510 void ust_after_setresgid(void)
2512 ust_context_vgids_reset();
2515 void lttng_ust_sockinfo_session_enabled(void *owner
)
2517 struct sock_info
*sock_info
= owner
;
2518 sock_info
->statedump_pending
= 1;