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>
28 #include <urcu/compiler.h>
29 #include <lttng/urcu/urcu-ust.h>
31 #include <lttng/ust-utils.h>
32 #include <lttng/ust-events.h>
33 #include <lttng/ust-abi.h>
34 #include <lttng/ust-fork.h>
35 #include <lttng/ust-error.h>
36 #include <lttng/ust-ctl.h>
37 #include <lttng/ust-libc-wrapper.h>
38 #include <lttng/ust-thread.h>
39 #include <lttng/ust-tracer.h>
40 #include <lttng/ust-common.h>
41 #include <urcu/tls-compat.h>
42 #include "common/compat/futex.h"
43 #include "common/ustcomm.h"
44 #include "common/ust-fd.h"
45 #include "common/logging.h"
46 #include "common/macros.h"
47 #include "common/tracepoint.h"
48 #include "lttng-tracer-core.h"
49 #include "common/compat/pthread.h"
50 #include "common/procname.h"
51 #include "common/ringbuffer/rb-init.h"
52 #include "lttng-ust-statedump.h"
54 #include "lib/lttng-ust/getcpu.h"
55 #include "common/getenv.h"
56 #include "lib/lttng-ust/events.h"
57 #include "context-internal.h"
58 #include "common/align.h"
59 #include "lttng-counter-client.h"
60 #include "lttng-rb-clients.h"
63 * Has lttng ust comm constructor been called ?
65 static int initialized
;
68 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
69 * Held when handling a command, also held by fork() to deal with
70 * removal of threads, and by exit path.
72 * The UST lock is the centralized mutex across UST tracing control and
75 * ust_exit_mutex must never nest in ust_mutex.
77 * ust_fork_mutex must never nest in ust_mutex.
79 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
80 * counter lazy initialization called by events within the statedump,
81 * which traces while the ust_mutex is held.
83 * ust_lock nests within the dynamic loader lock (within glibc) because
84 * it is taken within the library constructor.
86 * The ust fd tracker lock nests within the ust_mutex.
88 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
90 /* Allow nesting the ust_mutex within the same thread. */
91 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
94 * ust_exit_mutex protects thread_active variable wrt thread exit. It
95 * cannot be done by ust_mutex because pthread_cancel(), which takes an
96 * internal libc lock, cannot nest within ust_mutex.
98 * It never nests within a ust_mutex.
100 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
103 * ust_fork_mutex protects base address statedump tracing against forks. It
104 * prevents the dynamic loader lock to be taken (by base address statedump
105 * tracing) while a fork is happening, thus preventing deadlock issues with
106 * the dynamic loader lock.
108 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
110 /* Should the ust comm thread quit ? */
111 static int lttng_ust_comm_should_quit
;
114 * This variable can be tested by applications to check whether
115 * lttng-ust is loaded. They simply have to define their own
116 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
117 * library constructor.
119 int lttng_ust_loaded
__attribute__((weak
));
122 * Return 0 on success, -1 if should quit.
123 * The lock is taken in both cases.
128 sigset_t sig_all_blocked
, orig_mask
;
131 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
133 ERR("pthread_setcancelstate: %s", strerror(ret
));
135 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
136 ERR("pthread_setcancelstate: unexpected oldstate");
138 sigfillset(&sig_all_blocked
);
139 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
141 ERR("pthread_sigmask: %s", strerror(ret
));
143 if (!URCU_TLS(ust_mutex_nest
)++)
144 pthread_mutex_lock(&ust_mutex
);
145 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
147 ERR("pthread_sigmask: %s", strerror(ret
));
149 if (lttng_ust_comm_should_quit
) {
157 * ust_lock_nocheck() can be used in constructors/destructors, because
158 * they are already nested within the dynamic loader lock, and therefore
159 * have exclusive access against execution of liblttng-ust destructor.
162 void ust_lock_nocheck(void)
164 sigset_t sig_all_blocked
, orig_mask
;
167 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
169 ERR("pthread_setcancelstate: %s", strerror(ret
));
171 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
172 ERR("pthread_setcancelstate: unexpected oldstate");
174 sigfillset(&sig_all_blocked
);
175 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
177 ERR("pthread_sigmask: %s", strerror(ret
));
179 if (!URCU_TLS(ust_mutex_nest
)++)
180 pthread_mutex_lock(&ust_mutex
);
181 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
183 ERR("pthread_sigmask: %s", strerror(ret
));
190 void ust_unlock(void)
192 sigset_t sig_all_blocked
, orig_mask
;
195 sigfillset(&sig_all_blocked
);
196 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
198 ERR("pthread_sigmask: %s", strerror(ret
));
200 if (!--URCU_TLS(ust_mutex_nest
))
201 pthread_mutex_unlock(&ust_mutex
);
202 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
204 ERR("pthread_sigmask: %s", strerror(ret
));
206 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
208 ERR("pthread_setcancelstate: %s", strerror(ret
));
210 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
211 ERR("pthread_setcancelstate: unexpected oldstate");
216 * Wait for either of these before continuing to the main
218 * - the register_done message from sessiond daemon
219 * (will let the sessiond daemon enable sessions before main
221 * - sessiond daemon is not reachable.
222 * - timeout (ensuring applications are resilient to session
225 static sem_t constructor_wait
;
227 * Doing this for both the global and local sessiond.
230 sem_count_initial_value
= 4,
233 static int sem_count
= sem_count_initial_value
;
236 * Counting nesting within lttng-ust. Used to ensure that calling fork()
237 * from liblttng-ust does not execute the pre/post fork handlers.
239 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
242 * Info about socket and associated listener thread.
246 pthread_t ust_listener
; /* listener thread */
248 int registration_done
;
253 char sock_path
[PATH_MAX
];
257 char wait_shm_path
[PATH_MAX
];
259 /* Keep track of lazy state dump not performed yet. */
260 int statedump_pending
;
261 int initial_statedump_done
;
262 /* Keep procname for statedump */
263 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
266 /* Socket from app (connect) to session daemon (listen) for communication */
267 struct sock_info global_apps
= {
272 .registration_done
= 0,
276 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
280 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
282 .statedump_pending
= 0,
283 .initial_statedump_done
= 0,
287 /* TODO: allow global_apps_sock_path override */
289 struct sock_info local_apps
= {
293 .registration_done
= 0,
294 .allowed
= 0, /* Check setuid bit first */
300 .statedump_pending
= 0,
301 .initial_statedump_done
= 0,
305 static int wait_poll_fallback
;
307 static const char *cmd_name_mapping
[] = {
308 [ LTTNG_UST_ABI_RELEASE
] = "Release",
309 [ LTTNG_UST_ABI_SESSION
] = "Create Session",
310 [ LTTNG_UST_ABI_TRACER_VERSION
] = "Get Tracer Version",
312 [ LTTNG_UST_ABI_TRACEPOINT_LIST
] = "Create Tracepoint List",
313 [ LTTNG_UST_ABI_WAIT_QUIESCENT
] = "Wait for Quiescent State",
314 [ LTTNG_UST_ABI_REGISTER_DONE
] = "Registration Done",
315 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
317 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
319 /* Session FD commands */
320 [ LTTNG_UST_ABI_CHANNEL
] = "Create Channel",
321 [ LTTNG_UST_ABI_SESSION_START
] = "Start Session",
322 [ LTTNG_UST_ABI_SESSION_STOP
] = "Stop Session",
324 /* Channel FD commands */
325 [ LTTNG_UST_ABI_STREAM
] = "Create Stream",
326 [ LTTNG_UST_ABI_EVENT
] = "Create Event",
328 /* Event and Channel FD commands */
329 [ LTTNG_UST_ABI_CONTEXT
] = "Create Context",
330 [ LTTNG_UST_ABI_FLUSH_BUFFER
] = "Flush Buffer",
332 /* Event, Channel and Session commands */
333 [ LTTNG_UST_ABI_ENABLE
] = "Enable",
334 [ LTTNG_UST_ABI_DISABLE
] = "Disable",
336 /* Tracepoint list commands */
337 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
338 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
340 /* Event FD commands */
341 [ LTTNG_UST_ABI_FILTER
] = "Create Filter",
342 [ LTTNG_UST_ABI_EXCLUSION
] = "Add exclusions to event",
344 /* Event notifier group commands */
345 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
347 /* Session and event notifier group commands */
348 [ LTTNG_UST_ABI_COUNTER
] = "Create Counter",
350 /* Counter commands */
351 [ LTTNG_UST_ABI_COUNTER_GLOBAL
] = "Create Counter Global",
352 [ LTTNG_UST_ABI_COUNTER_CPU
] = "Create Counter CPU",
355 static const char *str_timeout
;
356 static int got_timeout_env
;
358 static char *get_map_shm(struct sock_info
*sock_info
);
361 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
365 const char *get_lttng_home_dir(void)
369 val
= (const char *) lttng_ust_getenv("LTTNG_HOME");
373 return (const char *) lttng_ust_getenv("HOME");
377 * Force a read (imply TLS fixup for dlopen) of TLS variables.
380 void lttng_fixup_nest_count_tls(void)
382 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
386 void lttng_fixup_ust_mutex_nest_tls(void)
388 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
392 * Fixup lttng-ust urcu TLS.
395 void lttng_fixup_lttng_ust_urcu_tls(void)
397 (void) lttng_ust_urcu_read_ongoing();
400 void lttng_ust_fixup_tls(void)
402 lttng_fixup_lttng_ust_urcu_tls();
403 lttng_fixup_ringbuffer_tls();
404 lttng_fixup_vtid_tls();
405 lttng_fixup_nest_count_tls();
406 lttng_fixup_procname_tls();
407 lttng_fixup_ust_mutex_nest_tls();
408 lttng_ust_fixup_perf_counter_tls();
409 lttng_ust_fixup_fd_tracker_tls();
410 lttng_fixup_cgroup_ns_tls();
411 lttng_fixup_ipc_ns_tls();
412 lttng_fixup_net_ns_tls();
413 lttng_fixup_time_ns_tls();
414 lttng_fixup_uts_ns_tls();
415 lttng_ust_fixup_ring_buffer_client_discard_tls();
416 lttng_ust_fixup_ring_buffer_client_discard_rt_tls();
417 lttng_ust_fixup_ring_buffer_client_overwrite_tls();
418 lttng_ust_fixup_ring_buffer_client_overwrite_rt_tls();
422 * LTTng-UST uses Global Dynamic model TLS variables rather than IE
423 * model because many versions of glibc don't preallocate a pool large
424 * enough for TLS variables IE model defined in other shared libraries,
425 * and causes issues when using LTTng-UST for Java tracing.
427 * Because of this use of Global Dynamic TLS variables, users wishing to
428 * trace from signal handlers need to explicitly trigger the lazy
429 * allocation of those variables for each thread before using them.
430 * This can be triggered by calling lttng_ust_init_thread().
432 void lttng_ust_init_thread(void)
435 * Because those TLS variables are global dynamic, we need to
436 * ensure those are initialized before a signal handler nesting over
437 * this thread attempts to use them.
439 lttng_ust_fixup_tls();
442 int lttng_get_notify_socket(void *owner
)
444 struct sock_info
*info
= owner
;
446 return info
->notify_socket
;
450 char* lttng_ust_sockinfo_get_procname(void *owner
)
452 struct sock_info
*info
= owner
;
454 return info
->procname
;
458 void print_cmd(int cmd
, int handle
)
460 const char *cmd_name
= "Unknown";
462 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
463 && cmd_name_mapping
[cmd
]) {
464 cmd_name
= cmd_name_mapping
[cmd
];
466 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
468 lttng_ust_obj_get_name(handle
), handle
);
472 int setup_global_apps(void)
475 assert(!global_apps
.wait_shm_mmap
);
477 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
478 if (!global_apps
.wait_shm_mmap
) {
479 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
480 global_apps
.allowed
= 0;
485 global_apps
.allowed
= 1;
486 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
491 int setup_local_apps(void)
494 const char *home_dir
;
497 assert(!local_apps
.wait_shm_mmap
);
501 * Disallow per-user tracing for setuid binaries.
503 if (uid
!= geteuid()) {
504 assert(local_apps
.allowed
== 0);
508 home_dir
= get_lttng_home_dir();
510 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
511 assert(local_apps
.allowed
== 0);
515 local_apps
.allowed
= 1;
516 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
518 LTTNG_DEFAULT_HOME_RUNDIR
,
519 LTTNG_UST_SOCK_FILENAME
);
520 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
521 LTTNG_UST_WAIT_FILENAME
,
524 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
525 if (!local_apps
.wait_shm_mmap
) {
526 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
527 local_apps
.allowed
= 0;
532 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
538 * Get socket timeout, in ms.
539 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
542 long get_timeout(void)
544 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
546 if (!got_timeout_env
) {
547 str_timeout
= lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
551 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
552 /* All negative values are considered as "-1". */
553 if (constructor_delay_ms
< -1)
554 constructor_delay_ms
= -1;
555 return constructor_delay_ms
;
558 /* Timeout for notify socket send and recv. */
560 long get_notify_sock_timeout(void)
562 return get_timeout();
565 /* Timeout for connecting to cmd and notify sockets. */
567 long get_connect_sock_timeout(void)
569 return get_timeout();
573 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
576 int get_constructor_timeout(struct timespec
*constructor_timeout
)
578 long constructor_delay_ms
;
581 constructor_delay_ms
= get_timeout();
583 switch (constructor_delay_ms
) {
584 case -1:/* fall-through */
586 return constructor_delay_ms
;
592 * If we are unable to find the current time, don't wait.
594 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
599 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
600 constructor_timeout
->tv_nsec
+=
601 (constructor_delay_ms
% 1000UL) * 1000000UL;
602 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
603 constructor_timeout
->tv_sec
++;
604 constructor_timeout
->tv_nsec
-= 1000000000UL;
606 /* Timeout wait (constructor_delay_ms). */
611 void get_allow_blocking(void)
613 const char *str_allow_blocking
=
614 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
616 if (str_allow_blocking
) {
617 DBG("%s environment variable is set",
618 "LTTNG_UST_ALLOW_BLOCKING");
619 lttng_ust_ringbuffer_set_allow_blocking();
624 int register_to_sessiond(int socket
, enum lttng_ust_ctl_socket_type type
)
626 return ustcomm_send_reg_msg(socket
,
629 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT
,
630 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT
,
631 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT
,
632 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT
,
633 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT
);
637 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
641 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
644 DBG("message successfully sent");
647 if (len
== -ECONNRESET
) {
648 DBG("remote end closed connection");
653 DBG("incorrect message size: %zd", len
);
659 void decrement_sem_count(unsigned int count
)
663 assert(uatomic_read(&sem_count
) >= count
);
665 if (uatomic_read(&sem_count
) <= 0) {
669 ret
= uatomic_add_return(&sem_count
, -count
);
671 ret
= sem_post(&constructor_wait
);
677 int handle_register_done(struct sock_info
*sock_info
)
679 if (sock_info
->registration_done
)
681 sock_info
->registration_done
= 1;
683 decrement_sem_count(1);
684 if (!sock_info
->statedump_pending
) {
685 sock_info
->initial_statedump_done
= 1;
686 decrement_sem_count(1);
693 int handle_register_failed(struct sock_info
*sock_info
)
695 if (sock_info
->registration_done
)
697 sock_info
->registration_done
= 1;
698 sock_info
->initial_statedump_done
= 1;
700 decrement_sem_count(2);
706 * Only execute pending statedump after the constructor semaphore has
707 * been posted by the current listener thread. This means statedump will
708 * only be performed after the "registration done" command is received
709 * from this thread's session daemon.
711 * This ensures we don't run into deadlock issues with the dynamic
712 * loader mutex, which is held while the constructor is called and
713 * waiting on the constructor semaphore. All operations requiring this
714 * dynamic loader lock need to be postponed using this mechanism.
716 * In a scenario with two session daemons connected to the application,
717 * it is possible that the first listener thread which receives the
718 * registration done command issues its statedump while the dynamic
719 * loader lock is still held by the application constructor waiting on
720 * the semaphore. It will however be allowed to proceed when the
721 * second session daemon sends the registration done command to the
722 * second listener thread. This situation therefore does not produce
726 void handle_pending_statedump(struct sock_info
*sock_info
)
728 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
729 sock_info
->statedump_pending
= 0;
730 pthread_mutex_lock(&ust_fork_mutex
);
731 lttng_handle_pending_statedump(sock_info
);
732 pthread_mutex_unlock(&ust_fork_mutex
);
734 if (!sock_info
->initial_statedump_done
) {
735 sock_info
->initial_statedump_done
= 1;
736 decrement_sem_count(1);
742 const char *bytecode_type_str(uint32_t cmd
)
745 case LTTNG_UST_ABI_CAPTURE
:
747 case LTTNG_UST_ABI_FILTER
:
755 int handle_bytecode_recv(struct sock_info
*sock_info
,
756 int sock
, struct ustcomm_ust_msg
*lum
)
758 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
759 enum lttng_ust_bytecode_type type
;
760 const struct lttng_ust_abi_objd_ops
*ops
;
761 uint32_t data_size
, data_size_max
, reloc_offset
;
767 case LTTNG_UST_ABI_FILTER
:
768 type
= LTTNG_UST_BYTECODE_TYPE_FILTER
;
769 data_size
= lum
->u
.filter
.data_size
;
770 data_size_max
= LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN
;
771 reloc_offset
= lum
->u
.filter
.reloc_offset
;
772 seqnum
= lum
->u
.filter
.seqnum
;
774 case LTTNG_UST_ABI_CAPTURE
:
775 type
= LTTNG_UST_BYTECODE_TYPE_CAPTURE
;
776 data_size
= lum
->u
.capture
.data_size
;
777 data_size_max
= LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN
;
778 reloc_offset
= lum
->u
.capture
.reloc_offset
;
779 seqnum
= lum
->u
.capture
.seqnum
;
785 if (data_size
> data_size_max
) {
786 ERR("Bytecode %s data size is too large: %u bytes",
787 bytecode_type_str(lum
->cmd
), data_size
);
792 if (reloc_offset
> data_size
) {
793 ERR("Bytecode %s reloc offset %u is not within data",
794 bytecode_type_str(lum
->cmd
), reloc_offset
);
799 /* Allocate the structure AND the `data[]` field. */
800 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
806 bytecode
->bc
.len
= data_size
;
807 bytecode
->bc
.reloc_offset
= reloc_offset
;
808 bytecode
->bc
.seqnum
= seqnum
;
809 bytecode
->type
= type
;
811 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
813 case 0: /* orderly shutdown */
817 if (len
== bytecode
->bc
.len
) {
818 DBG("Bytecode %s data received",
819 bytecode_type_str(lum
->cmd
));
821 } else if (len
< 0) {
822 DBG("Receive failed from lttng-sessiond with errno %d",
824 if (len
== -ECONNRESET
) {
825 ERR("%s remote end closed connection",
833 DBG("Incorrect %s bytecode data message size: %zd",
834 bytecode_type_str(lum
->cmd
), len
);
840 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
847 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
848 (unsigned long) &bytecode
,
859 int handle_message(struct sock_info
*sock_info
,
860 int sock
, struct ustcomm_ust_msg
*lum
)
863 const struct lttng_ust_abi_objd_ops
*ops
;
864 struct ustcomm_ust_reply lur
;
865 union lttng_ust_abi_args args
;
866 char ctxstr
[LTTNG_UST_ABI_SYM_NAME_LEN
]; /* App context string. */
869 memset(&lur
, 0, sizeof(lur
));
872 ret
= -LTTNG_UST_ERR_EXITING
;
876 ops
= lttng_ust_abi_objd_ops(lum
->handle
);
883 case LTTNG_UST_ABI_REGISTER_DONE
:
884 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
885 ret
= handle_register_done(sock_info
);
889 case LTTNG_UST_ABI_RELEASE
:
890 if (lum
->handle
== LTTNG_UST_ABI_ROOT_HANDLE
)
893 ret
= lttng_ust_abi_objd_unref(lum
->handle
, 1);
895 case LTTNG_UST_ABI_CAPTURE
:
896 case LTTNG_UST_ABI_FILTER
:
897 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
901 case LTTNG_UST_ABI_EXCLUSION
:
903 /* Receive exclusion names */
904 struct lttng_ust_excluder_node
*node
;
907 count
= lum
->u
.exclusion
.count
;
909 /* There are no names to read */
913 node
= zmalloc(sizeof(*node
) +
914 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
919 node
->excluder
.count
= count
;
920 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
921 count
* LTTNG_UST_ABI_SYM_NAME_LEN
);
923 case 0: /* orderly shutdown */
928 if (len
== count
* LTTNG_UST_ABI_SYM_NAME_LEN
) {
929 DBG("Exclusion data received");
931 } else if (len
< 0) {
932 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
933 if (len
== -ECONNRESET
) {
934 ERR("%s remote end closed connection", sock_info
->name
);
943 DBG("Incorrect exclusion data message size: %zd", len
);
950 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
951 (unsigned long) &node
,
958 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE
:
960 int event_notifier_notif_fd
, close_ret
;
962 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
963 &event_notifier_notif_fd
);
965 case 0: /* orderly shutdown */
972 DBG("Receive failed from lttng-sessiond with errno %d",
974 if (len
== -ECONNRESET
) {
975 ERR("%s remote end closed connection",
983 DBG("Incorrect event notifier fd message size: %zd",
989 args
.event_notifier_handle
.event_notifier_notif_fd
=
990 event_notifier_notif_fd
;
992 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
993 (unsigned long) &lum
->u
,
997 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
998 lttng_ust_lock_fd_tracker();
999 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1000 lttng_ust_unlock_fd_tracker();
1006 case LTTNG_UST_ABI_CHANNEL
:
1011 len
= ustcomm_recv_channel_from_sessiond(sock
,
1012 &chan_data
, lum
->u
.channel
.len
,
1015 case 0: /* orderly shutdown */
1019 if (len
== lum
->u
.channel
.len
) {
1020 DBG("channel data received");
1022 } else if (len
< 0) {
1023 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1024 if (len
== -ECONNRESET
) {
1025 ERR("%s remote end closed connection", sock_info
->name
);
1032 DBG("incorrect channel data message size: %zd", len
);
1037 args
.channel
.chan_data
= chan_data
;
1038 args
.channel
.wakeup_fd
= wakeup_fd
;
1040 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1041 (unsigned long) &lum
->u
,
1045 if (args
.channel
.wakeup_fd
>= 0) {
1048 lttng_ust_lock_fd_tracker();
1049 close_ret
= close(args
.channel
.wakeup_fd
);
1050 lttng_ust_unlock_fd_tracker();
1051 args
.channel
.wakeup_fd
= -1;
1055 free(args
.channel
.chan_data
);
1058 case LTTNG_UST_ABI_STREAM
:
1062 /* Receive shm_fd, wakeup_fd */
1063 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1065 &args
.stream
.shm_fd
,
1066 &args
.stream
.wakeup_fd
);
1072 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1073 (unsigned long) &lum
->u
,
1077 if (args
.stream
.shm_fd
>= 0) {
1078 lttng_ust_lock_fd_tracker();
1079 close_ret
= close(args
.stream
.shm_fd
);
1080 lttng_ust_unlock_fd_tracker();
1081 args
.stream
.shm_fd
= -1;
1085 if (args
.stream
.wakeup_fd
>= 0) {
1086 lttng_ust_lock_fd_tracker();
1087 close_ret
= close(args
.stream
.wakeup_fd
);
1088 lttng_ust_unlock_fd_tracker();
1089 args
.stream
.wakeup_fd
= -1;
1095 case LTTNG_UST_ABI_CONTEXT
:
1096 switch (lum
->u
.context
.ctx
) {
1097 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT
:
1100 size_t ctxlen
, recvlen
;
1102 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1103 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1104 if (ctxlen
>= LTTNG_UST_ABI_SYM_NAME_LEN
) {
1105 ERR("Application context string length size is too large: %zu bytes",
1110 strcpy(ctxstr
, "$app.");
1111 p
= &ctxstr
[strlen("$app.")];
1112 recvlen
= ctxlen
- strlen("$app.");
1113 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1115 case 0: /* orderly shutdown */
1119 if (len
== recvlen
) {
1120 DBG("app context data received");
1122 } else if (len
< 0) {
1123 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1124 if (len
== -ECONNRESET
) {
1125 ERR("%s remote end closed connection", sock_info
->name
);
1132 DBG("incorrect app context data message size: %zd", len
);
1137 /* Put : between provider and ctxname. */
1138 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1139 args
.app_context
.ctxname
= ctxstr
;
1146 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1147 (unsigned long) &lum
->u
,
1153 case LTTNG_UST_ABI_COUNTER
:
1157 len
= ustcomm_recv_counter_from_sessiond(sock
,
1158 &counter_data
, lum
->u
.counter
.len
);
1160 case 0: /* orderly shutdown */
1164 if (len
== lum
->u
.counter
.len
) {
1165 DBG("counter data received");
1167 } else if (len
< 0) {
1168 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1169 if (len
== -ECONNRESET
) {
1170 ERR("%s remote end closed connection", sock_info
->name
);
1177 DBG("incorrect counter data message size: %zd", len
);
1182 args
.counter
.counter_data
= counter_data
;
1184 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1185 (unsigned long) &lum
->u
,
1189 free(args
.counter
.counter_data
);
1192 case LTTNG_UST_ABI_COUNTER_GLOBAL
:
1194 /* Receive shm_fd */
1195 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1196 &args
.counter_shm
.shm_fd
);
1202 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1203 (unsigned long) &lum
->u
,
1207 if (args
.counter_shm
.shm_fd
>= 0) {
1210 lttng_ust_lock_fd_tracker();
1211 close_ret
= close(args
.counter_shm
.shm_fd
);
1212 lttng_ust_unlock_fd_tracker();
1213 args
.counter_shm
.shm_fd
= -1;
1219 case LTTNG_UST_ABI_COUNTER_CPU
:
1221 /* Receive shm_fd */
1222 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1223 &args
.counter_shm
.shm_fd
);
1229 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1230 (unsigned long) &lum
->u
,
1234 if (args
.counter_shm
.shm_fd
>= 0) {
1237 lttng_ust_lock_fd_tracker();
1238 close_ret
= close(args
.counter_shm
.shm_fd
);
1239 lttng_ust_unlock_fd_tracker();
1240 args
.counter_shm
.shm_fd
= -1;
1246 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE
:
1248 /* Receive struct lttng_ust_event_notifier */
1249 struct lttng_ust_abi_event_notifier event_notifier
;
1251 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1252 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1256 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1258 case 0: /* orderly shutdown */
1262 if (len
== sizeof(event_notifier
)) {
1263 DBG("event notifier data received");
1265 } else if (len
< 0) {
1266 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1267 if (len
== -ECONNRESET
) {
1268 ERR("%s remote end closed connection", sock_info
->name
);
1275 DBG("incorrect event notifier data message size: %zd", len
);
1281 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1282 (unsigned long) &event_notifier
,
1291 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1292 (unsigned long) &lum
->u
,
1299 lur
.handle
= lum
->handle
;
1303 lur
.ret_code
= LTTNG_UST_OK
;
1306 * Use -LTTNG_UST_ERR as wildcard for UST internal
1307 * error that are not caused by the transport, except if
1308 * we already have a more precise error message to
1311 if (ret
> -LTTNG_UST_ERR
) {
1312 /* Translate code to UST error. */
1315 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1318 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1321 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1324 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1327 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1330 lur
.ret_code
= -LTTNG_UST_ERR
;
1339 case LTTNG_UST_ABI_TRACER_VERSION
:
1340 lur
.u
.version
= lum
->u
.version
;
1342 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET
:
1343 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1347 DBG("Return value: %d", lur
.ret_val
);
1352 * Performed delayed statedump operations outside of the UST
1353 * lock. We need to take the dynamic loader lock before we take
1354 * the UST lock internally within handle_pending_statedump().
1356 handle_pending_statedump(sock_info
);
1359 ret
= -LTTNG_UST_ERR_EXITING
;
1363 ret
= send_reply(sock
, &lur
);
1365 DBG("error sending reply");
1370 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1373 if (lur
.ret_code
== LTTNG_UST_OK
) {
1375 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET
:
1376 len
= ustcomm_send_unix_sock(sock
,
1377 &args
.field_list
.entry
,
1378 sizeof(args
.field_list
.entry
));
1383 if (len
!= sizeof(args
.field_list
.entry
)) {
1397 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1401 if (sock_info
->root_handle
!= -1) {
1402 ret
= lttng_ust_abi_objd_unref(sock_info
->root_handle
, 1);
1404 ERR("Error unref root handle");
1406 sock_info
->root_handle
= -1;
1408 sock_info
->registration_done
= 0;
1409 sock_info
->initial_statedump_done
= 0;
1412 * wait_shm_mmap, socket and notify socket are used by listener
1413 * threads outside of the ust lock, so we cannot tear them down
1414 * ourselves, because we cannot join on these threads. Leave
1415 * responsibility of cleaning up these resources to the OS
1421 if (sock_info
->socket
!= -1) {
1422 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1424 ERR("Error closing ust cmd socket");
1426 sock_info
->socket
= -1;
1428 if (sock_info
->notify_socket
!= -1) {
1429 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1431 ERR("Error closing ust notify socket");
1433 sock_info
->notify_socket
= -1;
1435 if (sock_info
->wait_shm_mmap
) {
1438 page_size
= LTTNG_UST_PAGE_SIZE
;
1439 if (page_size
<= 0) {
1443 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1445 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1447 ERR("Error unmapping wait shm");
1450 sock_info
->wait_shm_mmap
= NULL
;
1455 * Using fork to set umask in the child process (not multi-thread safe).
1456 * We deal with the shm_open vs ftruncate race (happening when the
1457 * sessiond owns the shm and does not let everybody modify it, to ensure
1458 * safety against shm_unlink) by simply letting the mmap fail and
1459 * retrying after a few seconds.
1460 * For global shm, everybody has rw access to it until the sessiond
1464 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1466 int wait_shm_fd
, ret
;
1470 * Try to open read-only.
1472 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1473 if (wait_shm_fd
>= 0) {
1476 size_t bytes_read
= 0;
1479 * Try to read the fd. If unable to do so, try opening
1483 len
= read(wait_shm_fd
,
1484 &((char *) &tmp_read
)[bytes_read
],
1485 sizeof(tmp_read
) - bytes_read
);
1489 } while ((len
< 0 && errno
== EINTR
)
1490 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1491 if (bytes_read
!= sizeof(tmp_read
)) {
1492 ret
= close(wait_shm_fd
);
1494 ERR("close wait_shm_fd");
1499 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1501 * Real-only open did not work, and it's not because the
1502 * entry was not present. It's a failure that prohibits
1505 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1511 * If the open failed because the file did not exist, or because
1512 * the file was not truncated yet, try creating it ourself.
1514 URCU_TLS(lttng_ust_nest_count
)++;
1516 URCU_TLS(lttng_ust_nest_count
)--;
1521 * Parent: wait for child to return, in which case the
1522 * shared memory map will have been created.
1524 pid
= wait(&status
);
1525 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1530 * Try to open read-only again after creation.
1532 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1533 if (wait_shm_fd
< 0) {
1535 * Real-only open did not work. It's a failure
1536 * that prohibits using shm.
1538 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1542 } else if (pid
== 0) {
1546 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1547 if (sock_info
->global
)
1548 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1550 * We're alone in a child process, so we can modify the
1551 * process-wide umask.
1553 umask(~create_mode
);
1555 * Try creating shm (or get rw access).
1556 * We don't do an exclusive open, because we allow other
1557 * processes to create+ftruncate it concurrently.
1559 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1560 O_RDWR
| O_CREAT
, create_mode
);
1561 if (wait_shm_fd
>= 0) {
1562 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1564 PERROR("ftruncate");
1565 _exit(EXIT_FAILURE
);
1567 _exit(EXIT_SUCCESS
);
1570 * For local shm, we need to have rw access to accept
1571 * opening it: this means the local sessiond will be
1572 * able to wake us up. For global shm, we open it even
1573 * if rw access is not granted, because the root.root
1574 * sessiond will be able to override all rights and wake
1577 if (!sock_info
->global
&& errno
!= EACCES
) {
1578 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1579 _exit(EXIT_FAILURE
);
1582 * The shm exists, but we cannot open it RW. Report
1585 _exit(EXIT_SUCCESS
);
1590 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1591 struct stat statbuf
;
1594 * Ensure that our user is the owner of the shm file for
1595 * local shm. If we do not own the file, it means our
1596 * sessiond will not have access to wake us up (there is
1597 * probably a rogue process trying to fake our
1598 * sessiond). Fallback to polling method in this case.
1600 ret
= fstat(wait_shm_fd
, &statbuf
);
1605 if (statbuf
.st_uid
!= getuid())
1611 ret
= close(wait_shm_fd
);
1613 PERROR("Error closing fd");
1619 char *get_map_shm(struct sock_info
*sock_info
)
1622 int wait_shm_fd
, ret
;
1623 char *wait_shm_mmap
;
1625 page_size
= sysconf(_SC_PAGE_SIZE
);
1626 if (page_size
<= 0) {
1630 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1634 lttng_ust_lock_fd_tracker();
1635 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1636 if (wait_shm_fd
< 0) {
1637 lttng_ust_unlock_fd_tracker();
1641 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1643 ret
= close(wait_shm_fd
);
1645 PERROR("Error closing fd");
1647 lttng_ust_unlock_fd_tracker();
1652 lttng_ust_unlock_fd_tracker();
1654 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1655 MAP_SHARED
, wait_shm_fd
, 0);
1657 /* close shm fd immediately after taking the mmap reference */
1658 lttng_ust_lock_fd_tracker();
1659 ret
= close(wait_shm_fd
);
1661 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1663 PERROR("Error closing fd");
1665 lttng_ust_unlock_fd_tracker();
1667 if (wait_shm_mmap
== MAP_FAILED
) {
1668 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1671 return wait_shm_mmap
;
1678 void wait_for_sessiond(struct sock_info
*sock_info
)
1680 /* Use ust_lock to check if we should quit. */
1684 if (wait_poll_fallback
) {
1689 assert(sock_info
->wait_shm_mmap
);
1691 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1692 /* Wait for futex wakeup */
1693 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1696 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1697 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1700 /* Value already changed. */
1703 /* Retry if interrupted by signal. */
1704 break; /* Get out of switch. */
1706 wait_poll_fallback
= 1;
1708 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1709 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1710 "Please upgrade your kernel "
1711 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1712 "mainline). LTTng-UST will use polling mode fallback.");
1713 if (lttng_ust_logging_debug_enabled())
1731 * This thread does not allocate any resource, except within
1732 * handle_message, within mutex protection. This mutex protects against
1734 * The other moment it allocates resources is at socket connection, which
1735 * is also protected by the mutex.
1738 void *ust_listener_thread(void *arg
)
1740 struct sock_info
*sock_info
= arg
;
1741 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1744 lttng_ust_fixup_tls();
1746 * If available, add '-ust' to the end of this thread's
1749 ret
= lttng_ust_setustprocname();
1751 ERR("Unable to set UST process name");
1754 /* Restart trying to connect to the session daemon */
1756 if (prev_connect_failed
) {
1757 /* Wait for sessiond availability with pipe */
1758 wait_for_sessiond(sock_info
);
1762 * Sleep for 5 seconds before retrying after a
1763 * sequence of failure / wait / failure. This
1764 * deals with a killed or broken session daemon.
1770 prev_connect_failed
= 0;
1777 if (sock_info
->socket
!= -1) {
1778 /* FD tracker is updated by ustcomm_close_unix_sock() */
1779 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1781 ERR("Error closing %s ust cmd socket",
1784 sock_info
->socket
= -1;
1786 if (sock_info
->notify_socket
!= -1) {
1787 /* FD tracker is updated by ustcomm_close_unix_sock() */
1788 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1790 ERR("Error closing %s ust notify socket",
1793 sock_info
->notify_socket
= -1;
1798 * Register. We need to perform both connect and sending
1799 * registration message before doing the next connect otherwise
1800 * we may reach unix socket connect queue max limits and block
1801 * on the 2nd connect while the session daemon is awaiting the
1802 * first connect registration message.
1804 /* Connect cmd socket */
1805 lttng_ust_lock_fd_tracker();
1806 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1807 get_connect_sock_timeout());
1809 lttng_ust_unlock_fd_tracker();
1810 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1811 prev_connect_failed
= 1;
1814 * If we cannot find the sessiond daemon, don't delay
1815 * constructor execution.
1817 ret
= handle_register_failed(sock_info
);
1823 ret
= lttng_ust_add_fd_to_tracker(fd
);
1827 PERROR("close on sock_info->socket");
1830 lttng_ust_unlock_fd_tracker();
1835 sock_info
->socket
= ret
;
1836 lttng_ust_unlock_fd_tracker();
1840 * Unlock/relock ust lock because connect is blocking (with
1841 * timeout). Don't delay constructors on the ust lock for too
1849 * Create only one root handle per listener thread for the whole
1850 * process lifetime, so we ensure we get ID which is statically
1851 * assigned to the root handle.
1853 if (sock_info
->root_handle
== -1) {
1854 ret
= lttng_abi_create_root_handle();
1856 ERR("Error creating root handle");
1859 sock_info
->root_handle
= ret
;
1862 ret
= register_to_sessiond(sock_info
->socket
, LTTNG_UST_CTL_SOCKET_CMD
);
1864 ERR("Error registering to %s ust cmd socket",
1866 prev_connect_failed
= 1;
1868 * If we cannot register to the sessiond daemon, don't
1869 * delay constructor execution.
1871 ret
= handle_register_failed(sock_info
);
1879 * Unlock/relock ust lock because connect is blocking (with
1880 * timeout). Don't delay constructors on the ust lock for too
1887 /* Connect notify socket */
1888 lttng_ust_lock_fd_tracker();
1889 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1890 get_connect_sock_timeout());
1892 lttng_ust_unlock_fd_tracker();
1893 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1894 prev_connect_failed
= 1;
1897 * If we cannot find the sessiond daemon, don't delay
1898 * constructor execution.
1900 ret
= handle_register_failed(sock_info
);
1907 ret
= lttng_ust_add_fd_to_tracker(fd
);
1911 PERROR("close on sock_info->notify_socket");
1914 lttng_ust_unlock_fd_tracker();
1919 sock_info
->notify_socket
= ret
;
1920 lttng_ust_unlock_fd_tracker();
1924 * Unlock/relock ust lock because connect is blocking (with
1925 * timeout). Don't delay constructors on the ust lock for too
1932 timeout
= get_notify_sock_timeout();
1935 * Give at least 10ms to sessiond to reply to
1940 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1943 WARN("Error setting socket receive timeout");
1945 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1948 WARN("Error setting socket send timeout");
1950 } else if (timeout
< -1) {
1951 WARN("Unsupported timeout value %ld", timeout
);
1954 ret
= register_to_sessiond(sock_info
->notify_socket
,
1955 LTTNG_UST_CTL_SOCKET_NOTIFY
);
1957 ERR("Error registering to %s ust notify socket",
1959 prev_connect_failed
= 1;
1961 * If we cannot register to the sessiond daemon, don't
1962 * delay constructor execution.
1964 ret
= handle_register_failed(sock_info
);
1969 sock
= sock_info
->socket
;
1975 struct ustcomm_ust_msg lum
;
1977 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1979 case 0: /* orderly shutdown */
1980 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1985 * Either sessiond has shutdown or refused us by closing the socket.
1986 * In either case, we don't want to delay construction execution,
1987 * and we need to wait before retry.
1989 prev_connect_failed
= 1;
1991 * If we cannot register to the sessiond daemon, don't
1992 * delay constructor execution.
1994 ret
= handle_register_failed(sock_info
);
1999 print_cmd(lum
.cmd
, lum
.handle
);
2000 ret
= handle_message(sock_info
, sock
, &lum
);
2002 ERR("Error handling message for %s socket",
2005 * Close socket if protocol error is
2013 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2015 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2017 if (len
== -ECONNRESET
) {
2018 DBG("%s remote end closed connection", sock_info
->name
);
2029 /* Cleanup socket handles before trying to reconnect */
2030 lttng_ust_abi_objd_table_owner_cleanup(sock_info
);
2032 goto restart
; /* try to reconnect */
2037 pthread_mutex_lock(&ust_exit_mutex
);
2038 sock_info
->thread_active
= 0;
2039 pthread_mutex_unlock(&ust_exit_mutex
);
2044 * Weak symbol to call when the ust malloc wrapper is not loaded.
2046 __attribute__((weak
))
2047 void lttng_ust_libc_wrapper_malloc_ctor(void)
2052 * sessiond monitoring thread: monitor presence of global and per-user
2053 * sessiond by polling the application common named pipe.
2056 void lttng_ust_ctor(void)
2057 __attribute__((constructor
));
2059 void lttng_ust_ctor(void)
2061 struct timespec constructor_timeout
;
2062 sigset_t sig_all_blocked
, orig_parent_mask
;
2063 pthread_attr_t thread_attr
;
2068 if (uatomic_xchg(&initialized
, 1) == 1)
2072 * Fixup interdependency between TLS fixup mutex (which happens
2073 * to be the dynamic linker mutex) and ust_lock, taken within
2076 lttng_ust_fixup_tls();
2078 lttng_ust_loaded
= 1;
2081 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2082 * the unloading of code used by the ust_listener_threads as we can not
2083 * reliably know when they exited. To do that, manually load
2084 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2085 * this library so it never becomes zero, thus never gets unloaded from the
2086 * address space of the process. Since we are already running in the
2087 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2088 * simply increment the refcount and no additionnal work is needed by the
2089 * dynamic loader as the shared library is already loaded in the address
2090 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2091 * unloading of the UST library if its refcount becomes zero (which should
2092 * never happen). Do the return value check but discard the handle at the
2093 * end of the function as it's not needed.
2095 handle
= dlopen(LTTNG_UST_LIB_SONAME
, RTLD_LAZY
| RTLD_NODELETE
);
2097 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME
);
2101 * We want precise control over the order in which we construct
2102 * our sub-libraries vs starting to receive commands from
2103 * sessiond (otherwise leading to errors when trying to create
2104 * sessiond before the init functions are completed).
2108 * Both the logging and getenv lazy-initialization uses getenv()
2109 * internally and thus needs to be explicitly initialized in
2110 * liblttng-ust before we start any threads as an unsuspecting normally
2111 * single threaded application using liblttng-ust could be using
2112 * setenv() which is not thread-safe.
2114 lttng_ust_logging_init();
2115 lttng_ust_getenv_init();
2117 /* Call the liblttng-ust-common constructor. */
2118 lttng_ust_common_ctor();
2120 lttng_ust_tp_init();
2121 lttng_ust_clock_init();
2122 lttng_ust_getcpu_plugin_init();
2123 lttng_ust_statedump_init();
2124 lttng_ust_ring_buffer_clients_init();
2125 lttng_ust_counter_clients_init();
2126 lttng_perf_counter_init();
2128 * Invoke ust malloc wrapper init before starting other threads.
2130 lttng_ust_libc_wrapper_malloc_ctor();
2132 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2134 get_allow_blocking();
2136 ret
= sem_init(&constructor_wait
, 0, 0);
2141 ret
= setup_global_apps();
2143 assert(global_apps
.allowed
== 0);
2144 DBG("global apps setup returned %d", ret
);
2147 ret
= setup_local_apps();
2149 assert(local_apps
.allowed
== 0);
2150 DBG("local apps setup returned %d", ret
);
2153 /* A new thread created by pthread_create inherits the signal mask
2154 * from the parent. To avoid any signal being received by the
2155 * listener thread, we block all signals temporarily in the parent,
2156 * while we create the listener thread.
2158 sigfillset(&sig_all_blocked
);
2159 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2161 ERR("pthread_sigmask: %s", strerror(ret
));
2164 ret
= pthread_attr_init(&thread_attr
);
2166 ERR("pthread_attr_init: %s", strerror(ret
));
2168 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2170 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2173 if (global_apps
.allowed
) {
2174 pthread_mutex_lock(&ust_exit_mutex
);
2175 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2176 ust_listener_thread
, &global_apps
);
2178 ERR("pthread_create global: %s", strerror(ret
));
2180 global_apps
.thread_active
= 1;
2181 pthread_mutex_unlock(&ust_exit_mutex
);
2183 handle_register_done(&global_apps
);
2186 if (local_apps
.allowed
) {
2187 pthread_mutex_lock(&ust_exit_mutex
);
2188 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2189 ust_listener_thread
, &local_apps
);
2191 ERR("pthread_create local: %s", strerror(ret
));
2193 local_apps
.thread_active
= 1;
2194 pthread_mutex_unlock(&ust_exit_mutex
);
2196 handle_register_done(&local_apps
);
2198 ret
= pthread_attr_destroy(&thread_attr
);
2200 ERR("pthread_attr_destroy: %s", strerror(ret
));
2203 /* Restore original signal mask in parent */
2204 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2206 ERR("pthread_sigmask: %s", strerror(ret
));
2209 switch (timeout_mode
) {
2210 case 1: /* timeout wait */
2212 ret
= sem_timedwait(&constructor_wait
,
2213 &constructor_timeout
);
2214 } while (ret
< 0 && errno
== EINTR
);
2218 ERR("Timed out waiting for lttng-sessiond");
2221 PERROR("sem_timedwait");
2224 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2229 case -1:/* wait forever */
2231 ret
= sem_wait(&constructor_wait
);
2232 } while (ret
< 0 && errno
== EINTR
);
2239 ERR("Unexpected error \"%s\" returned by sem_wait",
2244 case 0: /* no timeout */
2250 void lttng_ust_cleanup(int exiting
)
2252 cleanup_sock_info(&global_apps
, exiting
);
2253 cleanup_sock_info(&local_apps
, exiting
);
2254 local_apps
.allowed
= 0;
2255 global_apps
.allowed
= 0;
2257 * The teardown in this function all affect data structures
2258 * accessed under the UST lock by the listener thread. This
2259 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2260 * that none of these threads are accessing this data at this
2263 lttng_ust_abi_exit();
2264 lttng_ust_abi_events_exit();
2265 lttng_perf_counter_exit();
2266 lttng_ust_ring_buffer_clients_exit();
2267 lttng_ust_counter_clients_exit();
2268 lttng_ust_statedump_destroy();
2269 lttng_ust_tp_exit();
2271 /* Reinitialize values for fork */
2272 sem_count
= sem_count_initial_value
;
2273 lttng_ust_comm_should_quit
= 0;
2279 void lttng_ust_exit(void)
2280 __attribute__((destructor
));
2282 void lttng_ust_exit(void)
2287 * Using pthread_cancel here because:
2288 * A) we don't want to hang application teardown.
2289 * B) the thread is not allocating any resource.
2293 * Require the communication thread to quit. Synchronize with
2294 * mutexes to ensure it is not in a mutex critical section when
2295 * pthread_cancel is later called.
2298 lttng_ust_comm_should_quit
= 1;
2301 pthread_mutex_lock(&ust_exit_mutex
);
2302 /* cancel threads */
2303 if (global_apps
.thread_active
) {
2304 ret
= pthread_cancel(global_apps
.ust_listener
);
2306 ERR("Error cancelling global ust listener thread: %s",
2309 global_apps
.thread_active
= 0;
2312 if (local_apps
.thread_active
) {
2313 ret
= pthread_cancel(local_apps
.ust_listener
);
2315 ERR("Error cancelling local ust listener thread: %s",
2318 local_apps
.thread_active
= 0;
2321 pthread_mutex_unlock(&ust_exit_mutex
);
2324 * Do NOT join threads: use of sys_futex makes it impossible to
2325 * join the threads without using async-cancel, but async-cancel
2326 * is delivered by a signal, which could hit the target thread
2327 * anywhere in its code path, including while the ust_lock() is
2328 * held, causing a deadlock for the other thread. Let the OS
2329 * cleanup the threads if there are stalled in a syscall.
2331 lttng_ust_cleanup(1);
2335 void ust_context_ns_reset(void)
2337 lttng_context_pid_ns_reset();
2338 lttng_context_cgroup_ns_reset();
2339 lttng_context_ipc_ns_reset();
2340 lttng_context_mnt_ns_reset();
2341 lttng_context_net_ns_reset();
2342 lttng_context_user_ns_reset();
2343 lttng_context_time_ns_reset();
2344 lttng_context_uts_ns_reset();
2348 void ust_context_vuids_reset(void)
2350 lttng_context_vuid_reset();
2351 lttng_context_veuid_reset();
2352 lttng_context_vsuid_reset();
2356 void ust_context_vgids_reset(void)
2358 lttng_context_vgid_reset();
2359 lttng_context_vegid_reset();
2360 lttng_context_vsgid_reset();
2364 * We exclude the worker threads across fork and clone (except
2365 * CLONE_VM), because these system calls only keep the forking thread
2366 * running in the child. Therefore, we don't want to call fork or clone
2367 * in the middle of an tracepoint or ust tracing state modification.
2368 * Holding this mutex protects these structures across fork and clone.
2370 void lttng_ust_before_fork(sigset_t
*save_sigset
)
2373 * Disable signals. This is to avoid that the child intervenes
2374 * before it is properly setup for tracing. It is safer to
2375 * disable all signals, because then we know we are not breaking
2376 * anything by restoring the original mask.
2381 /* Fixup lttng-ust TLS. */
2382 lttng_ust_fixup_tls();
2384 if (URCU_TLS(lttng_ust_nest_count
))
2386 /* Disable signals */
2387 sigfillset(&all_sigs
);
2388 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2390 PERROR("sigprocmask");
2393 pthread_mutex_lock(&ust_fork_mutex
);
2396 lttng_ust_urcu_before_fork();
2397 lttng_ust_lock_fd_tracker();
2401 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2405 DBG("process %d", getpid());
2406 lttng_perf_unlock();
2407 lttng_ust_unlock_fd_tracker();
2410 pthread_mutex_unlock(&ust_fork_mutex
);
2412 /* Restore signals */
2413 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2415 PERROR("sigprocmask");
2419 void lttng_ust_after_fork_parent(sigset_t
*restore_sigset
)
2421 if (URCU_TLS(lttng_ust_nest_count
))
2423 DBG("process %d", getpid());
2424 lttng_ust_urcu_after_fork_parent();
2425 /* Release mutexes and reenable signals */
2426 ust_after_fork_common(restore_sigset
);
2430 * After fork, in the child, we need to cleanup all the leftover state,
2431 * except the worker thread which already magically disappeared thanks
2432 * to the weird Linux fork semantics. After tyding up, we call
2433 * lttng_ust_ctor() again to start over as a new PID.
2435 * This is meant for forks() that have tracing in the child between the
2436 * fork and following exec call (if there is any).
2438 void lttng_ust_after_fork_child(sigset_t
*restore_sigset
)
2440 if (URCU_TLS(lttng_ust_nest_count
))
2442 lttng_context_vpid_reset();
2443 lttng_context_vtid_reset();
2444 lttng_ust_context_procname_reset();
2445 ust_context_ns_reset();
2446 ust_context_vuids_reset();
2447 ust_context_vgids_reset();
2448 DBG("process %d", getpid());
2449 /* Release urcu mutexes */
2450 lttng_ust_urcu_after_fork_child();
2451 lttng_ust_cleanup(0);
2452 /* Release mutexes and reenable signals */
2453 ust_after_fork_common(restore_sigset
);
2457 void lttng_ust_after_setns(void)
2459 ust_context_ns_reset();
2460 ust_context_vuids_reset();
2461 ust_context_vgids_reset();
2464 void lttng_ust_after_unshare(void)
2466 ust_context_ns_reset();
2467 ust_context_vuids_reset();
2468 ust_context_vgids_reset();
2471 void lttng_ust_after_setuid(void)
2473 ust_context_vuids_reset();
2476 void lttng_ust_after_seteuid(void)
2478 ust_context_vuids_reset();
2481 void lttng_ust_after_setreuid(void)
2483 ust_context_vuids_reset();
2486 void lttng_ust_after_setresuid(void)
2488 ust_context_vuids_reset();
2491 void lttng_ust_after_setgid(void)
2493 ust_context_vgids_reset();
2496 void lttng_ust_after_setegid(void)
2498 ust_context_vgids_reset();
2501 void lttng_ust_after_setregid(void)
2503 ust_context_vgids_reset();
2506 void lttng_ust_after_setresgid(void)
2508 ust_context_vgids_reset();
2511 void lttng_ust_sockinfo_session_enabled(void *owner
)
2513 struct sock_info
*sock_info
= owner
;
2514 sock_info
->statedump_pending
= 1;