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"
58 * Has lttng ust comm constructor been called ?
60 static int initialized
;
63 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
64 * Held when handling a command, also held by fork() to deal with
65 * removal of threads, and by exit path.
67 * The UST lock is the centralized mutex across UST tracing control and
70 * ust_exit_mutex must never nest in ust_mutex.
72 * ust_fork_mutex must never nest in ust_mutex.
74 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
75 * counter lazy initialization called by events within the statedump,
76 * which traces while the ust_mutex is held.
78 * ust_lock nests within the dynamic loader lock (within glibc) because
79 * it is taken within the library constructor.
81 * The ust fd tracker lock nests within the ust_mutex.
83 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
85 /* Allow nesting the ust_mutex within the same thread. */
86 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
89 * ust_exit_mutex protects thread_active variable wrt thread exit. It
90 * cannot be done by ust_mutex because pthread_cancel(), which takes an
91 * internal libc lock, cannot nest within ust_mutex.
93 * It never nests within a ust_mutex.
95 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
98 * ust_fork_mutex protects base address statedump tracing against forks. It
99 * prevents the dynamic loader lock to be taken (by base address statedump
100 * tracing) while a fork is happening, thus preventing deadlock issues with
101 * the dynamic loader lock.
103 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 /* Should the ust comm thread quit ? */
106 static int lttng_ust_comm_should_quit
;
109 * This variable can be tested by applications to check whether
110 * lttng-ust is loaded. They simply have to define their own
111 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
112 * library constructor.
114 int lttng_ust_loaded
__attribute__((weak
));
117 * Return 0 on success, -1 if should quit.
118 * The lock is taken in both cases.
123 sigset_t sig_all_blocked
, orig_mask
;
126 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
128 ERR("pthread_setcancelstate: %s", strerror(ret
));
130 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
131 ERR("pthread_setcancelstate: unexpected oldstate");
133 sigfillset(&sig_all_blocked
);
134 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
136 ERR("pthread_sigmask: %s", strerror(ret
));
138 if (!URCU_TLS(ust_mutex_nest
)++)
139 pthread_mutex_lock(&ust_mutex
);
140 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
142 ERR("pthread_sigmask: %s", strerror(ret
));
144 if (lttng_ust_comm_should_quit
) {
152 * ust_lock_nocheck() can be used in constructors/destructors, because
153 * they are already nested within the dynamic loader lock, and therefore
154 * have exclusive access against execution of liblttng-ust destructor.
157 void ust_lock_nocheck(void)
159 sigset_t sig_all_blocked
, orig_mask
;
162 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
164 ERR("pthread_setcancelstate: %s", strerror(ret
));
166 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
167 ERR("pthread_setcancelstate: unexpected oldstate");
169 sigfillset(&sig_all_blocked
);
170 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
172 ERR("pthread_sigmask: %s", strerror(ret
));
174 if (!URCU_TLS(ust_mutex_nest
)++)
175 pthread_mutex_lock(&ust_mutex
);
176 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
178 ERR("pthread_sigmask: %s", strerror(ret
));
185 void ust_unlock(void)
187 sigset_t sig_all_blocked
, orig_mask
;
190 sigfillset(&sig_all_blocked
);
191 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
193 ERR("pthread_sigmask: %s", strerror(ret
));
195 if (!--URCU_TLS(ust_mutex_nest
))
196 pthread_mutex_unlock(&ust_mutex
);
197 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
199 ERR("pthread_sigmask: %s", strerror(ret
));
201 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
203 ERR("pthread_setcancelstate: %s", strerror(ret
));
205 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
206 ERR("pthread_setcancelstate: unexpected oldstate");
211 * Wait for either of these before continuing to the main
213 * - the register_done message from sessiond daemon
214 * (will let the sessiond daemon enable sessions before main
216 * - sessiond daemon is not reachable.
217 * - timeout (ensuring applications are resilient to session
220 static sem_t constructor_wait
;
222 * Doing this for both the global and local sessiond.
225 sem_count_initial_value
= 4,
228 static int sem_count
= sem_count_initial_value
;
231 * Counting nesting within lttng-ust. Used to ensure that calling fork()
232 * from liblttng-ust does not execute the pre/post fork handlers.
234 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
237 * Info about socket and associated listener thread.
241 pthread_t ust_listener
; /* listener thread */
243 int registration_done
;
248 char sock_path
[PATH_MAX
];
252 char wait_shm_path
[PATH_MAX
];
254 /* Keep track of lazy state dump not performed yet. */
255 int statedump_pending
;
256 int initial_statedump_done
;
257 /* Keep procname for statedump */
258 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
261 /* Socket from app (connect) to session daemon (listen) for communication */
262 struct sock_info global_apps
= {
267 .registration_done
= 0,
271 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
275 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
277 .statedump_pending
= 0,
278 .initial_statedump_done
= 0,
282 /* TODO: allow global_apps_sock_path override */
284 struct sock_info local_apps
= {
288 .registration_done
= 0,
289 .allowed
= 0, /* Check setuid bit first */
295 .statedump_pending
= 0,
296 .initial_statedump_done
= 0,
300 static int wait_poll_fallback
;
302 static const char *cmd_name_mapping
[] = {
303 [ LTTNG_UST_ABI_RELEASE
] = "Release",
304 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
305 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
307 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
308 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
309 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
310 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
312 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
314 /* Session FD commands */
315 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
316 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
317 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
319 /* Channel FD commands */
320 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
321 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
323 /* Event and Channel FD commands */
324 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
325 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
327 /* Event, Channel and Session commands */
328 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
329 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
331 /* Tracepoint list commands */
332 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
333 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
335 /* Event FD commands */
336 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
337 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
339 /* Event notifier group commands */
340 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
342 /* Session and event notifier group commands */
343 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
345 /* Counter commands */
346 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
347 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
350 static const char *str_timeout
;
351 static int got_timeout_env
;
353 extern void lttng_ring_buffer_client_overwrite_init(void);
354 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
355 extern void lttng_ring_buffer_client_discard_init(void);
356 extern void lttng_ring_buffer_client_discard_rt_init(void);
357 extern void lttng_ring_buffer_metadata_client_init(void);
358 extern void lttng_ring_buffer_client_overwrite_exit(void);
359 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
360 extern void lttng_ring_buffer_client_discard_exit(void);
361 extern void lttng_ring_buffer_client_discard_rt_exit(void);
362 extern void lttng_ring_buffer_metadata_client_exit(void);
364 __attribute__((visibility("hidden")))
365 extern void lttng_counter_client_percpu_32_modular_init(void);
367 __attribute__((visibility("hidden")))
368 extern void lttng_counter_client_percpu_32_modular_exit(void);
370 __attribute__((visibility("hidden")))
371 extern void lttng_counter_client_percpu_64_modular_init(void);
373 __attribute__((visibility("hidden")))
374 extern void lttng_counter_client_percpu_64_modular_exit(void);
376 static char *get_map_shm(struct sock_info
*sock_info
);
378 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
381 size_t copied
= 0, to_copy
= len
;
384 ret
= read(fd
, buf
+ copied
, to_copy
);
389 } while ((ret
> 0 && to_copy
> 0)
390 || (ret
< 0 && errno
== EINTR
));
397 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
401 const char *get_lttng_home_dir(void)
405 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
409 return (const char *) lttng_ust_getenv("HOME");
413 * Force a read (imply TLS fixup for dlopen) of TLS variables.
416 void lttng_fixup_nest_count_tls(void)
418 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
422 void lttng_fixup_ust_mutex_nest_tls(void)
424 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
428 * Fixup lttng-ust urcu TLS.
431 void lttng_fixup_lttng_ust_urcu_tls(void)
433 (void) lttng_ust_urcu_read_ongoing();
436 void lttng_ust_fixup_tls(void)
438 lttng_fixup_lttng_ust_urcu_tls();
439 lttng_fixup_ringbuffer_tls();
440 lttng_fixup_vtid_tls();
441 lttng_fixup_nest_count_tls();
442 lttng_fixup_procname_tls();
443 lttng_fixup_ust_mutex_nest_tls();
444 lttng_ust_fixup_perf_counter_tls();
445 lttng_ust_fixup_fd_tracker_tls();
446 lttng_fixup_cgroup_ns_tls();
447 lttng_fixup_ipc_ns_tls();
448 lttng_fixup_net_ns_tls();
449 lttng_fixup_time_ns_tls();
450 lttng_fixup_uts_ns_tls();
453 int lttng_get_notify_socket(void *owner
)
455 struct sock_info
*info
= owner
;
457 return info
->notify_socket
;
461 char* lttng_ust_sockinfo_get_procname(void *owner
)
463 struct sock_info
*info
= owner
;
465 return info
->procname
;
469 void print_cmd(int cmd
, int handle
)
471 const char *cmd_name
= "Unknown";
473 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
474 && cmd_name_mapping
[cmd
]) {
475 cmd_name
= cmd_name_mapping
[cmd
];
477 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
479 lttng_ust_obj_get_name(handle
), handle
);
483 int setup_global_apps(void)
486 assert(!global_apps
.wait_shm_mmap
);
488 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
489 if (!global_apps
.wait_shm_mmap
) {
490 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
491 global_apps
.allowed
= 0;
496 global_apps
.allowed
= 1;
497 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
502 int setup_local_apps(void)
505 const char *home_dir
;
508 assert(!local_apps
.wait_shm_mmap
);
512 * Disallow per-user tracing for setuid binaries.
514 if (uid
!= geteuid()) {
515 assert(local_apps
.allowed
== 0);
519 home_dir
= get_lttng_home_dir();
521 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
522 assert(local_apps
.allowed
== 0);
526 local_apps
.allowed
= 1;
527 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
529 LTTNG_DEFAULT_HOME_RUNDIR
,
530 LTTNG_UST_SOCK_FILENAME
);
531 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
532 LTTNG_UST_WAIT_FILENAME
,
535 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
536 if (!local_apps
.wait_shm_mmap
) {
537 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
538 local_apps
.allowed
= 0;
543 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
549 * Get socket timeout, in ms.
550 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
553 long get_timeout(void)
555 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
557 if (!got_timeout_env
) {
558 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
562 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
563 /* All negative values are considered as "-1". */
564 if (constructor_delay_ms
< -1)
565 constructor_delay_ms
= -1;
566 return constructor_delay_ms
;
569 /* Timeout for notify socket send and recv. */
571 long get_notify_sock_timeout(void)
573 return get_timeout();
576 /* Timeout for connecting to cmd and notify sockets. */
578 long get_connect_sock_timeout(void)
580 return get_timeout();
584 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
587 int get_constructor_timeout(struct timespec
*constructor_timeout
)
589 long constructor_delay_ms
;
592 constructor_delay_ms
= get_timeout();
594 switch (constructor_delay_ms
) {
595 case -1:/* fall-through */
597 return constructor_delay_ms
;
603 * If we are unable to find the current time, don't wait.
605 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
610 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
611 constructor_timeout
->tv_nsec
+=
612 (constructor_delay_ms
% 1000UL) * 1000000UL;
613 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
614 constructor_timeout
->tv_sec
++;
615 constructor_timeout
->tv_nsec
-= 1000000000UL;
617 /* Timeout wait (constructor_delay_ms). */
622 void get_allow_blocking(void)
624 const char *str_allow_blocking
=
625 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
627 if (str_allow_blocking
) {
628 DBG("%s environment variable is set",
629 "LTTNG_UST_ALLOW_BLOCKING");
630 lttng_ust_ringbuffer_set_allow_blocking();
635 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
637 return ustcomm_send_reg_msg(socket
,
640 lttng_alignof(uint8_t) * CHAR_BIT
,
641 lttng_alignof(uint16_t) * CHAR_BIT
,
642 lttng_alignof(uint32_t) * CHAR_BIT
,
643 lttng_alignof(uint64_t) * CHAR_BIT
,
644 lttng_alignof(unsigned long) * CHAR_BIT
);
648 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
652 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
655 DBG("message successfully sent");
658 if (len
== -ECONNRESET
) {
659 DBG("remote end closed connection");
664 DBG("incorrect message size: %zd", len
);
670 void decrement_sem_count(unsigned int count
)
674 assert(uatomic_read(&sem_count
) >= count
);
676 if (uatomic_read(&sem_count
) <= 0) {
680 ret
= uatomic_add_return(&sem_count
, -count
);
682 ret
= sem_post(&constructor_wait
);
688 int handle_register_done(struct sock_info
*sock_info
)
690 if (sock_info
->registration_done
)
692 sock_info
->registration_done
= 1;
694 decrement_sem_count(1);
695 if (!sock_info
->statedump_pending
) {
696 sock_info
->initial_statedump_done
= 1;
697 decrement_sem_count(1);
704 int handle_register_failed(struct sock_info
*sock_info
)
706 if (sock_info
->registration_done
)
708 sock_info
->registration_done
= 1;
709 sock_info
->initial_statedump_done
= 1;
711 decrement_sem_count(2);
717 * Only execute pending statedump after the constructor semaphore has
718 * been posted by the current listener thread. This means statedump will
719 * only be performed after the "registration done" command is received
720 * from this thread's session daemon.
722 * This ensures we don't run into deadlock issues with the dynamic
723 * loader mutex, which is held while the constructor is called and
724 * waiting on the constructor semaphore. All operations requiring this
725 * dynamic loader lock need to be postponed using this mechanism.
727 * In a scenario with two session daemons connected to the application,
728 * it is possible that the first listener thread which receives the
729 * registration done command issues its statedump while the dynamic
730 * loader lock is still held by the application constructor waiting on
731 * the semaphore. It will however be allowed to proceed when the
732 * second session daemon sends the registration done command to the
733 * second listener thread. This situation therefore does not produce
737 void handle_pending_statedump(struct sock_info
*sock_info
)
739 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
740 sock_info
->statedump_pending
= 0;
741 pthread_mutex_lock(&ust_fork_mutex
);
742 lttng_handle_pending_statedump(sock_info
);
743 pthread_mutex_unlock(&ust_fork_mutex
);
745 if (!sock_info
->initial_statedump_done
) {
746 sock_info
->initial_statedump_done
= 1;
747 decrement_sem_count(1);
753 const char *bytecode_type_str(uint32_t cmd
)
756 case LTTNG_UST_ABI_CAPTURE
:
758 case LTTNG_UST_ABI_FILTER
:
766 int handle_bytecode_recv(struct sock_info
*sock_info
,
767 int sock
, struct ustcomm_ust_msg
*lum
)
769 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
770 enum lttng_ust_bytecode_type type
;
771 const struct lttng_ust_abi_objd_ops
*ops
;
772 uint32_t data_size
, data_size_max
, reloc_offset
;
778 case LTTNG_UST_ABI_FILTER
:
779 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
780 data_size
= lum
->u
.filter
.data_size
;
781 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
782 reloc_offset
= lum
->u
.filter
.reloc_offset
;
783 seqnum
= lum
->u
.filter
.seqnum
;
785 case LTTNG_UST_ABI_CAPTURE
:
786 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
787 data_size
= lum
->u
.capture
.data_size
;
788 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
789 reloc_offset
= lum
->u
.capture
.reloc_offset
;
790 seqnum
= lum
->u
.capture
.seqnum
;
796 if (data_size
> data_size_max
) {
797 ERR("Bytecode %s data size is too large: %u bytes",
798 bytecode_type_str(lum
->cmd
), data_size
);
803 if (reloc_offset
> data_size
) {
804 ERR("Bytecode %s reloc offset %u is not within data",
805 bytecode_type_str(lum
->cmd
), reloc_offset
);
810 /* Allocate the structure AND the `data[]` field. */
811 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
817 bytecode
->bc
.len
= data_size
;
818 bytecode
->bc
.reloc_offset
= reloc_offset
;
819 bytecode
->bc
.seqnum
= seqnum
;
820 bytecode
->type
= type
;
822 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
824 case 0: /* orderly shutdown */
828 if (len
== bytecode
->bc
.len
) {
829 DBG("Bytecode %s data received",
830 bytecode_type_str(lum
->cmd
));
832 } else if (len
< 0) {
833 DBG("Receive failed from lttng-sessiond with errno %d",
835 if (len
== -ECONNRESET
) {
836 ERR("%s remote end closed connection",
844 DBG("Incorrect %s bytecode data message size: %zd",
845 bytecode_type_str(lum
->cmd
), len
);
851 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
858 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
859 (unsigned long) &bytecode
,
870 int handle_message(struct sock_info
*sock_info
,
871 int sock
, struct ustcomm_ust_msg
*lum
)
874 const struct lttng_ust_abi_objd_ops
*ops
;
875 struct ustcomm_ust_reply lur
;
876 union lttng_ust_abi_args args
;
877 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
880 memset(&lur
, 0, sizeof(lur
));
883 ret
= -LTTNG_UST_ERR_EXITING
;
887 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
894 case LTTNG_UST_ABI_REGISTER_DONE
:
895 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
896 ret
= handle_register_done(sock_info
);
900 case LTTNG_UST_ABI_RELEASE
:
901 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
904 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
906 case LTTNG_UST_ABI_CAPTURE
:
907 case LTTNG_UST_ABI_FILTER
:
908 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
912 case LTTNG_UST_ABI_EXCLUSION
:
914 /* Receive exclusion names */
915 struct lttng_ust_excluder_node
*node
;
918 count
= lum
->u
.exclusion
.count
;
920 /* There are no names to read */
924 node
= zmalloc(sizeof(*node
) +
925 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
930 node
->excluder
.count
= count
;
931 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
932 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
934 case 0: /* orderly shutdown */
939 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
940 DBG("Exclusion data received");
942 } else if (len
< 0) {
943 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
944 if (len
== -ECONNRESET
) {
945 ERR("%s remote end closed connection", sock_info
->name
);
954 DBG("Incorrect exclusion data message size: %zd", len
);
961 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
962 (unsigned long) &node
,
969 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
971 int event_notifier_notif_fd
, close_ret
;
973 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
974 &event_notifier_notif_fd
);
976 case 0: /* orderly shutdown */
983 DBG("Receive failed from lttng-sessiond with errno %d",
985 if (len
== -ECONNRESET
) {
986 ERR("%s remote end closed connection",
994 DBG("Incorrect event notifier fd message size: %zd",
1000 args
.event_notifier_handle
.event_notifier_notif_fd
=
1001 event_notifier_notif_fd
;
1003 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1004 (unsigned long) &lum
->u
,
1008 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1009 lttng_ust_lock_fd_tracker();
1010 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1011 lttng_ust_unlock_fd_tracker();
1017 case LTTNG_UST_ABI_CHANNEL
:
1022 len
= ustcomm_recv_channel_from_sessiond(sock
,
1023 &chan_data
, lum
->u
.channel
.len
,
1026 case 0: /* orderly shutdown */
1030 if (len
== lum
->u
.channel
.len
) {
1031 DBG("channel data received");
1033 } else if (len
< 0) {
1034 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1035 if (len
== -ECONNRESET
) {
1036 ERR("%s remote end closed connection", sock_info
->name
);
1043 DBG("incorrect channel data message size: %zd", len
);
1048 args
.channel
.chan_data
= chan_data
;
1049 args
.channel
.wakeup_fd
= wakeup_fd
;
1051 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1052 (unsigned long) &lum
->u
,
1056 if (args
.channel
.wakeup_fd
>= 0) {
1059 lttng_ust_lock_fd_tracker();
1060 close_ret
= close(args
.channel
.wakeup_fd
);
1061 lttng_ust_unlock_fd_tracker();
1062 args
.channel
.wakeup_fd
= -1;
1066 free(args
.channel
.chan_data
);
1069 case LTTNG_UST_ABI_STREAM
:
1073 /* Receive shm_fd, wakeup_fd */
1074 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1076 &args
.stream
.shm_fd
,
1077 &args
.stream
.wakeup_fd
);
1083 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1084 (unsigned long) &lum
->u
,
1088 if (args
.stream
.shm_fd
>= 0) {
1089 lttng_ust_lock_fd_tracker();
1090 close_ret
= close(args
.stream
.shm_fd
);
1091 lttng_ust_unlock_fd_tracker();
1092 args
.stream
.shm_fd
= -1;
1096 if (args
.stream
.wakeup_fd
>= 0) {
1097 lttng_ust_lock_fd_tracker();
1098 close_ret
= close(args
.stream
.wakeup_fd
);
1099 lttng_ust_unlock_fd_tracker();
1100 args
.stream
.wakeup_fd
= -1;
1106 case LTTNG_UST_ABI_CONTEXT
:
1107 switch (lum
->u
.context
.ctx
) {
1108 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1111 size_t ctxlen
, recvlen
;
1113 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1114 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1115 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1116 ERR("Application context string length size is too large: %zu bytes",
1121 strcpy(ctxstr
, "$app.");
1122 p
= &ctxstr
[strlen("$app.")];
1123 recvlen
= ctxlen
- strlen("$app.");
1124 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1126 case 0: /* orderly shutdown */
1130 if (len
== recvlen
) {
1131 DBG("app context data received");
1133 } else if (len
< 0) {
1134 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1135 if (len
== -ECONNRESET
) {
1136 ERR("%s remote end closed connection", sock_info
->name
);
1143 DBG("incorrect app context data message size: %zd", len
);
1148 /* Put : between provider and ctxname. */
1149 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1150 args
.app_context
.ctxname
= ctxstr
;
1157 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1158 (unsigned long) &lum
->u
,
1164 case LTTNG_UST_ABI_COUNTER
:
1168 len
= ustcomm_recv_counter_from_sessiond(sock
,
1169 &counter_data
, lum
->u
.counter
.len
);
1171 case 0: /* orderly shutdown */
1175 if (len
== lum
->u
.counter
.len
) {
1176 DBG("counter data received");
1178 } else if (len
< 0) {
1179 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1180 if (len
== -ECONNRESET
) {
1181 ERR("%s remote end closed connection", sock_info
->name
);
1188 DBG("incorrect counter data message size: %zd", len
);
1193 args
.counter
.counter_data
= counter_data
;
1195 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1196 (unsigned long) &lum
->u
,
1200 free(args
.counter
.counter_data
);
1203 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1205 /* Receive shm_fd */
1206 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1207 &args
.counter_shm
.shm_fd
);
1213 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1214 (unsigned long) &lum
->u
,
1218 if (args
.counter_shm
.shm_fd
>= 0) {
1221 lttng_ust_lock_fd_tracker();
1222 close_ret
= close(args
.counter_shm
.shm_fd
);
1223 lttng_ust_unlock_fd_tracker();
1224 args
.counter_shm
.shm_fd
= -1;
1230 case LTTNG_UST_ABI_COUNTER_CPU
:
1232 /* Receive shm_fd */
1233 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1234 &args
.counter_shm
.shm_fd
);
1240 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1241 (unsigned long) &lum
->u
,
1245 if (args
.counter_shm
.shm_fd
>= 0) {
1248 lttng_ust_lock_fd_tracker();
1249 close_ret
= close(args
.counter_shm
.shm_fd
);
1250 lttng_ust_unlock_fd_tracker();
1251 args
.counter_shm
.shm_fd
= -1;
1257 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1259 /* Receive struct lttng_ust_event_notifier */
1260 struct lttng_ust_abi_event_notifier event_notifier
;
1262 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1263 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1267 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1269 case 0: /* orderly shutdown */
1273 if (len
== sizeof(event_notifier
)) {
1274 DBG("event notifier data received");
1276 } else if (len
< 0) {
1277 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1278 if (len
== -ECONNRESET
) {
1279 ERR("%s remote end closed connection", sock_info
->name
);
1286 DBG("incorrect event notifier data message size: %zd", len
);
1292 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1293 (unsigned long) &event_notifier
,
1302 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1303 (unsigned long) &lum
->u
,
1310 lur
.handle
= lum
->handle
;
1314 lur
.ret_code
= LTTNG_UST_OK
;
1317 * Use -LTTNG_UST_ERR as wildcard for UST internal
1318 * error that are not caused by the transport, except if
1319 * we already have a more precise error message to
1322 if (ret
> -LTTNG_UST_ERR
) {
1323 /* Translate code to UST error. */
1326 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1329 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1332 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1335 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1338 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1341 lur
.ret_code
= -LTTNG_UST_ERR
;
1350 case LTTNG_UST_ABI_TRACER_VERSION
:
1351 lur
.u
.version
= lum
->u
.version
;
1353 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1354 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1358 DBG("Return value: %d", lur
.ret_val
);
1363 * Performed delayed statedump operations outside of the UST
1364 * lock. We need to take the dynamic loader lock before we take
1365 * the UST lock internally within handle_pending_statedump().
1367 handle_pending_statedump(sock_info
);
1370 ret
= -LTTNG_UST_ERR_EXITING
;
1374 ret
= send_reply(sock
, &lur
);
1376 DBG("error sending reply");
1381 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1384 if (lur
.ret_code
== LTTNG_UST_OK
) {
1386 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1387 len
= ustcomm_send_unix_sock(sock
,
1388 &args
.field_list
.entry
,
1389 sizeof(args
.field_list
.entry
));
1394 if (len
!= sizeof(args
.field_list
.entry
)) {
1408 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1412 if (sock_info
->root_handle
!= -1) {
1413 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1415 ERR("Error unref root handle");
1417 sock_info
->root_handle
= -1;
1419 sock_info
->registration_done
= 0;
1420 sock_info
->initial_statedump_done
= 0;
1423 * wait_shm_mmap, socket and notify socket are used by listener
1424 * threads outside of the ust lock, so we cannot tear them down
1425 * ourselves, because we cannot join on these threads. Leave
1426 * responsibility of cleaning up these resources to the OS
1432 if (sock_info
->socket
!= -1) {
1433 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1435 ERR("Error closing ust cmd socket");
1437 sock_info
->socket
= -1;
1439 if (sock_info
->notify_socket
!= -1) {
1440 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1442 ERR("Error closing ust notify socket");
1444 sock_info
->notify_socket
= -1;
1446 if (sock_info
->wait_shm_mmap
) {
1449 page_size
= LTTNG_UST_PAGE_SIZE
;
1450 if (page_size
<= 0) {
1454 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1456 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1458 ERR("Error unmapping wait shm");
1461 sock_info
->wait_shm_mmap
= NULL
;
1466 * Using fork to set umask in the child process (not multi-thread safe).
1467 * We deal with the shm_open vs ftruncate race (happening when the
1468 * sessiond owns the shm and does not let everybody modify it, to ensure
1469 * safety against shm_unlink) by simply letting the mmap fail and
1470 * retrying after a few seconds.
1471 * For global shm, everybody has rw access to it until the sessiond
1475 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1477 int wait_shm_fd
, ret
;
1481 * Try to open read-only.
1483 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1484 if (wait_shm_fd
>= 0) {
1487 size_t bytes_read
= 0;
1490 * Try to read the fd. If unable to do so, try opening
1494 len
= read(wait_shm_fd
,
1495 &((char *) &tmp_read
)[bytes_read
],
1496 sizeof(tmp_read
) - bytes_read
);
1500 } while ((len
< 0 && errno
== EINTR
)
1501 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1502 if (bytes_read
!= sizeof(tmp_read
)) {
1503 ret
= close(wait_shm_fd
);
1505 ERR("close wait_shm_fd");
1510 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1512 * Real-only open did not work, and it's not because the
1513 * entry was not present. It's a failure that prohibits
1516 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1522 * If the open failed because the file did not exist, or because
1523 * the file was not truncated yet, try creating it ourself.
1525 URCU_TLS(lttng_ust_nest_count
)++;
1527 URCU_TLS(lttng_ust_nest_count
)--;
1532 * Parent: wait for child to return, in which case the
1533 * shared memory map will have been created.
1535 pid
= wait(&status
);
1536 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1541 * Try to open read-only again after creation.
1543 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1544 if (wait_shm_fd
< 0) {
1546 * Real-only open did not work. It's a failure
1547 * that prohibits using shm.
1549 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1553 } else if (pid
== 0) {
1557 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1558 if (sock_info
->global
)
1559 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1561 * We're alone in a child process, so we can modify the
1562 * process-wide umask.
1564 umask(~create_mode
);
1566 * Try creating shm (or get rw access).
1567 * We don't do an exclusive open, because we allow other
1568 * processes to create+ftruncate it concurrently.
1570 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1571 O_RDWR
| O_CREAT
, create_mode
);
1572 if (wait_shm_fd
>= 0) {
1573 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1575 PERROR("ftruncate");
1576 _exit(EXIT_FAILURE
);
1578 _exit(EXIT_SUCCESS
);
1581 * For local shm, we need to have rw access to accept
1582 * opening it: this means the local sessiond will be
1583 * able to wake us up. For global shm, we open it even
1584 * if rw access is not granted, because the root.root
1585 * sessiond will be able to override all rights and wake
1588 if (!sock_info
->global
&& errno
!= EACCES
) {
1589 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1590 _exit(EXIT_FAILURE
);
1593 * The shm exists, but we cannot open it RW. Report
1596 _exit(EXIT_SUCCESS
);
1601 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1602 struct stat statbuf
;
1605 * Ensure that our user is the owner of the shm file for
1606 * local shm. If we do not own the file, it means our
1607 * sessiond will not have access to wake us up (there is
1608 * probably a rogue process trying to fake our
1609 * sessiond). Fallback to polling method in this case.
1611 ret
= fstat(wait_shm_fd
, &statbuf
);
1616 if (statbuf
.st_uid
!= getuid())
1622 ret
= close(wait_shm_fd
);
1624 PERROR("Error closing fd");
1630 char *get_map_shm(struct sock_info
*sock_info
)
1633 int wait_shm_fd
, ret
;
1634 char *wait_shm_mmap
;
1636 page_size
= sysconf(_SC_PAGE_SIZE
);
1637 if (page_size
<= 0) {
1641 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1645 lttng_ust_lock_fd_tracker();
1646 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1647 if (wait_shm_fd
< 0) {
1648 lttng_ust_unlock_fd_tracker();
1652 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1654 ret
= close(wait_shm_fd
);
1656 PERROR("Error closing fd");
1658 lttng_ust_unlock_fd_tracker();
1663 lttng_ust_unlock_fd_tracker();
1665 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1666 MAP_SHARED
, wait_shm_fd
, 0);
1668 /* close shm fd immediately after taking the mmap reference */
1669 lttng_ust_lock_fd_tracker();
1670 ret
= close(wait_shm_fd
);
1672 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1674 PERROR("Error closing fd");
1676 lttng_ust_unlock_fd_tracker();
1678 if (wait_shm_mmap
== MAP_FAILED
) {
1679 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1682 return wait_shm_mmap
;
1689 void wait_for_sessiond(struct sock_info
*sock_info
)
1691 /* Use ust_lock to check if we should quit. */
1695 if (wait_poll_fallback
) {
1700 assert(sock_info
->wait_shm_mmap
);
1702 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1703 /* Wait for futex wakeup */
1704 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1707 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1708 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1711 /* Value already changed. */
1714 /* Retry if interrupted by signal. */
1715 break; /* Get out of switch. */
1717 wait_poll_fallback
= 1;
1719 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1720 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1721 "Please upgrade your kernel "
1722 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1723 "mainline). LTTng-UST will use polling mode fallback.");
1724 if (ust_err_debug_enabled())
1742 * This thread does not allocate any resource, except within
1743 * handle_message, within mutex protection. This mutex protects against
1745 * The other moment it allocates resources is at socket connection, which
1746 * is also protected by the mutex.
1749 void *ust_listener_thread(void *arg
)
1751 struct sock_info
*sock_info
= arg
;
1752 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1755 lttng_ust_fixup_tls();
1757 * If available, add '-ust' to the end of this thread's
1760 ret
= lttng_ust_setustprocname();
1762 ERR("Unable to set UST process name");
1765 /* Restart trying to connect to the session daemon */
1767 if (prev_connect_failed
) {
1768 /* Wait for sessiond availability with pipe */
1769 wait_for_sessiond(sock_info
);
1773 * Sleep for 5 seconds before retrying after a
1774 * sequence of failure / wait / failure. This
1775 * deals with a killed or broken session daemon.
1781 prev_connect_failed
= 0;
1788 if (sock_info
->socket
!= -1) {
1789 /* FD tracker is updated by ustcomm_close_unix_sock() */
1790 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1792 ERR("Error closing %s ust cmd socket",
1795 sock_info
->socket
= -1;
1797 if (sock_info
->notify_socket
!= -1) {
1798 /* FD tracker is updated by ustcomm_close_unix_sock() */
1799 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1801 ERR("Error closing %s ust notify socket",
1804 sock_info
->notify_socket
= -1;
1809 * Register. We need to perform both connect and sending
1810 * registration message before doing the next connect otherwise
1811 * we may reach unix socket connect queue max limits and block
1812 * on the 2nd connect while the session daemon is awaiting the
1813 * first connect registration message.
1815 /* Connect cmd socket */
1816 lttng_ust_lock_fd_tracker();
1817 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1818 get_connect_sock_timeout());
1820 lttng_ust_unlock_fd_tracker();
1821 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1822 prev_connect_failed
= 1;
1825 * If we cannot find the sessiond daemon, don't delay
1826 * constructor execution.
1828 ret
= handle_register_failed(sock_info
);
1834 ret
= lttng_ust_add_fd_to_tracker(fd
);
1838 PERROR("close on sock_info->socket");
1841 lttng_ust_unlock_fd_tracker();
1846 sock_info
->socket
= ret
;
1847 lttng_ust_unlock_fd_tracker();
1851 * Unlock/relock ust lock because connect is blocking (with
1852 * timeout). Don't delay constructors on the ust lock for too
1860 * Create only one root handle per listener thread for the whole
1861 * process lifetime, so we ensure we get ID which is statically
1862 * assigned to the root handle.
1864 if (sock_info
->root_handle
== -1) {
1865 ret
= lttng_abi_create_root_handle();
1867 ERR("Error creating root handle");
1870 sock_info
->root_handle
= ret
;
1873 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1875 ERR("Error registering to %s ust cmd socket",
1877 prev_connect_failed
= 1;
1879 * If we cannot register to the sessiond daemon, don't
1880 * delay constructor execution.
1882 ret
= handle_register_failed(sock_info
);
1890 * Unlock/relock ust lock because connect is blocking (with
1891 * timeout). Don't delay constructors on the ust lock for too
1898 /* Connect notify socket */
1899 lttng_ust_lock_fd_tracker();
1900 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1901 get_connect_sock_timeout());
1903 lttng_ust_unlock_fd_tracker();
1904 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1905 prev_connect_failed
= 1;
1908 * If we cannot find the sessiond daemon, don't delay
1909 * constructor execution.
1911 ret
= handle_register_failed(sock_info
);
1918 ret
= lttng_ust_add_fd_to_tracker(fd
);
1922 PERROR("close on sock_info->notify_socket");
1925 lttng_ust_unlock_fd_tracker();
1930 sock_info
->notify_socket
= ret
;
1931 lttng_ust_unlock_fd_tracker();
1935 * Unlock/relock ust lock because connect is blocking (with
1936 * timeout). Don't delay constructors on the ust lock for too
1943 timeout
= get_notify_sock_timeout();
1946 * Give at least 10ms to sessiond to reply to
1951 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1954 WARN("Error setting socket receive timeout");
1956 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1959 WARN("Error setting socket send timeout");
1961 } else if (timeout
< -1) {
1962 WARN("Unsupported timeout value %ld", timeout
);
1965 ret
= register_to_sessiond(sock_info
->notify_socket
,
1966 USTCTL_SOCKET_NOTIFY
);
1968 ERR("Error registering to %s ust notify socket",
1970 prev_connect_failed
= 1;
1972 * If we cannot register to the sessiond daemon, don't
1973 * delay constructor execution.
1975 ret
= handle_register_failed(sock_info
);
1980 sock
= sock_info
->socket
;
1986 struct ustcomm_ust_msg lum
;
1988 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1990 case 0: /* orderly shutdown */
1991 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1996 * Either sessiond has shutdown or refused us by closing the socket.
1997 * In either case, we don't want to delay construction execution,
1998 * and we need to wait before retry.
2000 prev_connect_failed
= 1;
2002 * If we cannot register to the sessiond daemon, don't
2003 * delay constructor execution.
2005 ret
= handle_register_failed(sock_info
);
2010 print_cmd(lum
.cmd
, lum
.handle
);
2011 ret
= handle_message(sock_info
, sock
, &lum
);
2013 ERR("Error handling message for %s socket",
2016 * Close socket if protocol error is
2024 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2026 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2028 if (len
== -ECONNRESET
) {
2029 DBG("%s remote end closed connection", sock_info
->name
);
2040 /* Cleanup socket handles before trying to reconnect */
2041 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2043 goto restart
; /* try to reconnect */
2048 pthread_mutex_lock(&ust_exit_mutex
);
2049 sock_info
->thread_active
= 0;
2050 pthread_mutex_unlock(&ust_exit_mutex
);
2055 * Weak symbol to call when the ust malloc wrapper is not loaded.
2057 __attribute__((weak
))
2058 void lttng_ust_libc_wrapper_malloc_init(void)
2063 * sessiond monitoring thread: monitor presence of global and per-user
2064 * sessiond by polling the application common named pipe.
2066 void __attribute__((constructor
)) lttng_ust_init(void)
2068 struct timespec constructor_timeout
;
2069 sigset_t sig_all_blocked
, orig_parent_mask
;
2070 pthread_attr_t thread_attr
;
2075 if (uatomic_xchg(&initialized
, 1) == 1)
2079 * Fixup interdependency between TLS fixup mutex (which happens
2080 * to be the dynamic linker mutex) and ust_lock, taken within
2083 lttng_ust_fixup_tls();
2085 lttng_ust_loaded
= 1;
2088 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2089 * the unloading of code used by the ust_listener_threads as we can not
2090 * reliably know when they exited. To do that, manually load
2091 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2092 * this library so it never becomes zero, thus never gets unloaded from the
2093 * address space of the process. Since we are already running in the
2094 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2095 * simply increment the refcount and no additionnal work is needed by the
2096 * dynamic loader as the shared library is already loaded in the address
2097 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2098 * unloading of the UST library if its refcount becomes zero (which should
2099 * never happen). Do the return value check but discard the handle at the
2100 * end of the function as it's not needed.
2102 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2104 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2108 * We want precise control over the order in which we construct
2109 * our sub-libraries vs starting to receive commands from
2110 * sessiond (otherwise leading to errors when trying to create
2111 * sessiond before the init functions are completed).
2114 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2115 lttng_ust_tp_init();
2116 lttng_ust_init_fd_tracker();
2117 lttng_ust_clock_init();
2118 lttng_ust_getcpu_init();
2119 lttng_ust_statedump_init();
2120 lttng_ring_buffer_metadata_client_init();
2121 lttng_ring_buffer_client_overwrite_init();
2122 lttng_ring_buffer_client_overwrite_rt_init();
2123 lttng_ring_buffer_client_discard_init();
2124 lttng_ring_buffer_client_discard_rt_init();
2125 lttng_counter_client_percpu_32_modular_init();
2126 lttng_counter_client_percpu_64_modular_init();
2127 lttng_perf_counter_init();
2129 * Invoke ust malloc wrapper init before starting other threads.
2131 lttng_ust_libc_wrapper_malloc_init();
2133 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2135 get_allow_blocking();
2137 ret
= sem_init(&constructor_wait
, 0, 0);
2142 ret
= setup_global_apps();
2144 assert(global_apps
.allowed
== 0);
2145 DBG("global apps setup returned %d", ret
);
2148 ret
= setup_local_apps();
2150 assert(local_apps
.allowed
== 0);
2151 DBG("local apps setup returned %d", ret
);
2154 /* A new thread created by pthread_create inherits the signal mask
2155 * from the parent. To avoid any signal being received by the
2156 * listener thread, we block all signals temporarily in the parent,
2157 * while we create the listener thread.
2159 sigfillset(&sig_all_blocked
);
2160 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2162 ERR("pthread_sigmask: %s", strerror(ret
));
2165 ret
= pthread_attr_init(&thread_attr
);
2167 ERR("pthread_attr_init: %s", strerror(ret
));
2169 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2171 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2174 if (global_apps
.allowed
) {
2175 pthread_mutex_lock(&ust_exit_mutex
);
2176 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2177 ust_listener_thread
, &global_apps
);
2179 ERR("pthread_create global: %s", strerror(ret
));
2181 global_apps
.thread_active
= 1;
2182 pthread_mutex_unlock(&ust_exit_mutex
);
2184 handle_register_done(&global_apps
);
2187 if (local_apps
.allowed
) {
2188 pthread_mutex_lock(&ust_exit_mutex
);
2189 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2190 ust_listener_thread
, &local_apps
);
2192 ERR("pthread_create local: %s", strerror(ret
));
2194 local_apps
.thread_active
= 1;
2195 pthread_mutex_unlock(&ust_exit_mutex
);
2197 handle_register_done(&local_apps
);
2199 ret
= pthread_attr_destroy(&thread_attr
);
2201 ERR("pthread_attr_destroy: %s", strerror(ret
));
2204 /* Restore original signal mask in parent */
2205 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2207 ERR("pthread_sigmask: %s", strerror(ret
));
2210 switch (timeout_mode
) {
2211 case 1: /* timeout wait */
2213 ret
= sem_timedwait(&constructor_wait
,
2214 &constructor_timeout
);
2215 } while (ret
< 0 && errno
== EINTR
);
2219 ERR("Timed out waiting for lttng-sessiond");
2222 PERROR("sem_timedwait");
2225 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2230 case -1:/* wait forever */
2232 ret
= sem_wait(&constructor_wait
);
2233 } while (ret
< 0 && errno
== EINTR
);
2240 ERR("Unexpected error \"%s\" returned by sem_wait",
2245 case 0: /* no timeout */
2251 void lttng_ust_cleanup(int exiting
)
2253 cleanup_sock_info(&global_apps
, exiting
);
2254 cleanup_sock_info(&local_apps
, exiting
);
2255 local_apps
.allowed
= 0;
2256 global_apps
.allowed
= 0;
2258 * The teardown in this function all affect data structures
2259 * accessed under the UST lock by the listener thread. This
2260 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2261 * that none of these threads are accessing this data at this
2264 lttng_ust_abi_exit();
2265 lttng_ust_abi_events_exit();
2266 lttng_perf_counter_exit();
2267 lttng_ring_buffer_client_discard_rt_exit();
2268 lttng_ring_buffer_client_discard_exit();
2269 lttng_ring_buffer_client_overwrite_rt_exit();
2270 lttng_ring_buffer_client_overwrite_exit();
2271 lttng_ring_buffer_metadata_client_exit();
2272 lttng_counter_client_percpu_32_modular_exit();
2273 lttng_counter_client_percpu_64_modular_exit();
2274 lttng_ust_statedump_destroy();
2275 lttng_ust_tp_exit();
2277 /* Reinitialize values for fork */
2278 sem_count
= sem_count_initial_value
;
2279 lttng_ust_comm_should_quit
= 0;
2284 void __attribute__((destructor
)) lttng_ust_exit(void)
2289 * Using pthread_cancel here because:
2290 * A) we don't want to hang application teardown.
2291 * B) the thread is not allocating any resource.
2295 * Require the communication thread to quit. Synchronize with
2296 * mutexes to ensure it is not in a mutex critical section when
2297 * pthread_cancel is later called.
2300 lttng_ust_comm_should_quit
= 1;
2303 pthread_mutex_lock(&ust_exit_mutex
);
2304 /* cancel threads */
2305 if (global_apps
.thread_active
) {
2306 ret
= pthread_cancel(global_apps
.ust_listener
);
2308 ERR("Error cancelling global ust listener thread: %s",
2311 global_apps
.thread_active
= 0;
2314 if (local_apps
.thread_active
) {
2315 ret
= pthread_cancel(local_apps
.ust_listener
);
2317 ERR("Error cancelling local ust listener thread: %s",
2320 local_apps
.thread_active
= 0;
2323 pthread_mutex_unlock(&ust_exit_mutex
);
2326 * Do NOT join threads: use of sys_futex makes it impossible to
2327 * join the threads without using async-cancel, but async-cancel
2328 * is delivered by a signal, which could hit the target thread
2329 * anywhere in its code path, including while the ust_lock() is
2330 * held, causing a deadlock for the other thread. Let the OS
2331 * cleanup the threads if there are stalled in a syscall.
2333 lttng_ust_cleanup(1);
2337 void ust_context_ns_reset(void)
2339 lttng_context_pid_ns_reset();
2340 lttng_context_cgroup_ns_reset();
2341 lttng_context_ipc_ns_reset();
2342 lttng_context_mnt_ns_reset();
2343 lttng_context_net_ns_reset();
2344 lttng_context_user_ns_reset();
2345 lttng_context_time_ns_reset();
2346 lttng_context_uts_ns_reset();
2350 void ust_context_vuids_reset(void)
2352 lttng_context_vuid_reset();
2353 lttng_context_veuid_reset();
2354 lttng_context_vsuid_reset();
2358 void ust_context_vgids_reset(void)
2360 lttng_context_vgid_reset();
2361 lttng_context_vegid_reset();
2362 lttng_context_vsgid_reset();
2366 * We exclude the worker threads across fork and clone (except
2367 * CLONE_VM), because these system calls only keep the forking thread
2368 * running in the child. Therefore, we don't want to call fork or clone
2369 * in the middle of an tracepoint or ust tracing state modification.
2370 * Holding this mutex protects these structures across fork and clone.
2372 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2375 * Disable signals. This is to avoid that the child intervenes
2376 * before it is properly setup for tracing. It is safer to
2377 * disable all signals, because then we know we are not breaking
2378 * anything by restoring the original mask.
2383 /* Fixup lttng-ust TLS. */
2384 lttng_ust_fixup_tls();
2386 if (URCU_TLS(lttng_ust_nest_count
))
2388 /* Disable signals */
2389 sigfillset(&all_sigs
);
2390 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2392 PERROR("sigprocmask");
2395 pthread_mutex_lock(&ust_fork_mutex
);
2398 lttng_ust_urcu_before_fork();
2399 lttng_ust_lock_fd_tracker();
2403 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2407 DBG("process %d", getpid());
2408 lttng_perf_unlock();
2409 lttng_ust_unlock_fd_tracker();
2412 pthread_mutex_unlock(&ust_fork_mutex
);
2414 /* Restore signals */
2415 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2417 PERROR("sigprocmask");
2421 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2423 if (URCU_TLS(lttng_ust_nest_count
))
2425 DBG("process %d", getpid());
2426 lttng_ust_urcu_after_fork_parent();
2427 /* Release mutexes and reenable signals */
2428 ust_after_fork_common(restore_sigset
);
2432 * After fork, in the child, we need to cleanup all the leftover state,
2433 * except the worker thread which already magically disappeared thanks
2434 * to the weird Linux fork semantics. After tyding up, we call
2435 * lttng_ust_init() again to start over as a new PID.
2437 * This is meant for forks() that have tracing in the child between the
2438 * fork and following exec call (if there is any).
2440 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2442 if (URCU_TLS(lttng_ust_nest_count
))
2444 lttng_context_vpid_reset();
2445 lttng_context_vtid_reset();
2446 lttng_ust_context_procname_reset();
2447 ust_context_ns_reset();
2448 ust_context_vuids_reset();
2449 ust_context_vgids_reset();
2450 DBG("process %d", getpid());
2451 /* Release urcu mutexes */
2452 lttng_ust_urcu_after_fork_child();
2453 lttng_ust_cleanup(0);
2454 /* Release mutexes and reenable signals */
2455 ust_after_fork_common(restore_sigset
);
2459 void lttng_ust_after_setns(void)
2461 ust_context_ns_reset();
2462 ust_context_vuids_reset();
2463 ust_context_vgids_reset();
2466 void lttng_ust_after_unshare(void)
2468 ust_context_ns_reset();
2469 ust_context_vuids_reset();
2470 ust_context_vgids_reset();
2473 void lttng_ust_after_setuid(void)
2475 ust_context_vuids_reset();
2478 void lttng_ust_after_seteuid(void)
2480 ust_context_vuids_reset();
2483 void lttng_ust_after_setreuid(void)
2485 ust_context_vuids_reset();
2488 void lttng_ust_after_setresuid(void)
2490 ust_context_vuids_reset();
2493 void lttng_ust_after_setgid(void)
2495 ust_context_vgids_reset();
2498 void lttng_ust_after_setegid(void)
2500 ust_context_vgids_reset();
2503 void lttng_ust_after_setregid(void)
2505 ust_context_vgids_reset();
2508 void lttng_ust_after_setresgid(void)
2510 ust_context_vgids_reset();
2513 void lttng_ust_sockinfo_session_enabled(void *owner
)
2515 struct sock_info
*sock_info
= owner
;
2516 sock_info
->statedump_pending
= 1;