4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <sys/types.h>
26 #include <sys/socket.h>
29 #include <sys/types.h>
36 #include <semaphore.h>
41 #include <urcu/uatomic.h>
43 #include <urcu/compiler.h>
44 #include <lttng/urcu/urcu-ust.h>
46 #include <lttng/align.h>
47 #include <lttng/ust-events.h>
48 #include <lttng/ust-abi.h>
49 #include <lttng/ust.h>
50 #include <lttng/ust-error.h>
51 #include <lttng/ust-ctl.h>
52 #include <urcu/tls-compat.h>
55 #include <usterr-signal-safe.h>
57 #include "tracepoint-internal.h"
58 #include "lttng-tracer-core.h"
60 #include "../libringbuffer/rb-init.h"
61 #include "lttng-ust-statedump.h"
63 #include "../libringbuffer/getcpu.h"
65 #include "ust-events-internal.h"
66 #include "context-internal.h"
68 /* Concatenate lttng ust shared library name with its major version number. */
69 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
72 * Has lttng ust comm constructor been called ?
74 static int initialized
;
77 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
78 * Held when handling a command, also held by fork() to deal with
79 * removal of threads, and by exit path.
81 * The UST lock is the centralized mutex across UST tracing control and
84 * ust_exit_mutex must never nest in ust_mutex.
86 * ust_fork_mutex must never nest in ust_mutex.
88 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
89 * counter lazy initialization called by events within the statedump,
90 * which traces while the ust_mutex is held.
92 * ust_lock nests within the dynamic loader lock (within glibc) because
93 * it is taken within the library constructor.
95 * The ust fd tracker lock nests within the ust_mutex.
97 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
99 /* Allow nesting the ust_mutex within the same thread. */
100 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
103 * ust_exit_mutex protects thread_active variable wrt thread exit. It
104 * cannot be done by ust_mutex because pthread_cancel(), which takes an
105 * internal libc lock, cannot nest within ust_mutex.
107 * It never nests within a ust_mutex.
109 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 * ust_fork_mutex protects base address statedump tracing against forks. It
113 * prevents the dynamic loader lock to be taken (by base address statedump
114 * tracing) while a fork is happening, thus preventing deadlock issues with
115 * the dynamic loader lock.
117 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
119 /* Should the ust comm thread quit ? */
120 static int lttng_ust_comm_should_quit
;
123 * This variable can be tested by applications to check whether
124 * lttng-ust is loaded. They simply have to define their own
125 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
126 * library constructor.
128 int lttng_ust_loaded
__attribute__((weak
));
131 * Return 0 on success, -1 if should quit.
132 * The lock is taken in both cases.
137 sigset_t sig_all_blocked
, orig_mask
;
140 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
142 ERR("pthread_setcancelstate: %s", strerror(ret
));
144 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
145 ERR("pthread_setcancelstate: unexpected oldstate");
147 sigfillset(&sig_all_blocked
);
148 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
150 ERR("pthread_sigmask: %s", strerror(ret
));
152 if (!URCU_TLS(ust_mutex_nest
)++)
153 pthread_mutex_lock(&ust_mutex
);
154 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
156 ERR("pthread_sigmask: %s", strerror(ret
));
158 if (lttng_ust_comm_should_quit
) {
166 * ust_lock_nocheck() can be used in constructors/destructors, because
167 * they are already nested within the dynamic loader lock, and therefore
168 * have exclusive access against execution of liblttng-ust destructor.
171 void ust_lock_nocheck(void)
173 sigset_t sig_all_blocked
, orig_mask
;
176 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
178 ERR("pthread_setcancelstate: %s", strerror(ret
));
180 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
181 ERR("pthread_setcancelstate: unexpected oldstate");
183 sigfillset(&sig_all_blocked
);
184 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
186 ERR("pthread_sigmask: %s", strerror(ret
));
188 if (!URCU_TLS(ust_mutex_nest
)++)
189 pthread_mutex_lock(&ust_mutex
);
190 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
192 ERR("pthread_sigmask: %s", strerror(ret
));
199 void ust_unlock(void)
201 sigset_t sig_all_blocked
, orig_mask
;
204 sigfillset(&sig_all_blocked
);
205 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
207 ERR("pthread_sigmask: %s", strerror(ret
));
209 if (!--URCU_TLS(ust_mutex_nest
))
210 pthread_mutex_unlock(&ust_mutex
);
211 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
213 ERR("pthread_sigmask: %s", strerror(ret
));
215 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
217 ERR("pthread_setcancelstate: %s", strerror(ret
));
219 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
220 ERR("pthread_setcancelstate: unexpected oldstate");
225 * Wait for either of these before continuing to the main
227 * - the register_done message from sessiond daemon
228 * (will let the sessiond daemon enable sessions before main
230 * - sessiond daemon is not reachable.
231 * - timeout (ensuring applications are resilient to session
234 static sem_t constructor_wait
;
236 * Doing this for both the global and local sessiond.
239 sem_count_initial_value
= 4,
242 static int sem_count
= sem_count_initial_value
;
245 * Counting nesting within lttng-ust. Used to ensure that calling fork()
246 * from liblttng-ust does not execute the pre/post fork handlers.
248 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
251 * Info about socket and associated listener thread.
255 pthread_t ust_listener
; /* listener thread */
257 int registration_done
;
262 char sock_path
[PATH_MAX
];
266 char wait_shm_path
[PATH_MAX
];
268 /* Keep track of lazy state dump not performed yet. */
269 int statedump_pending
;
270 int initial_statedump_done
;
271 /* Keep procname for statedump */
272 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
275 /* Socket from app (connect) to session daemon (listen) for communication */
276 struct sock_info global_apps
= {
281 .registration_done
= 0,
285 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
289 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
291 .statedump_pending
= 0,
292 .initial_statedump_done
= 0,
296 /* TODO: allow global_apps_sock_path override */
298 struct sock_info local_apps
= {
302 .registration_done
= 0,
303 .allowed
= 0, /* Check setuid bit first */
309 .statedump_pending
= 0,
310 .initial_statedump_done
= 0,
314 static int wait_poll_fallback
;
316 static const char *cmd_name_mapping
[] = {
317 [ LTTNG_UST_RELEASE
] = "Release",
318 [ LTTNG_UST_SESSION
] = "Create Session",
319 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
321 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
322 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
323 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
324 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
326 [ LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
328 /* Session FD commands */
329 [ LTTNG_UST_CHANNEL
] = "Create Channel",
330 [ LTTNG_UST_SESSION_START
] = "Start Session",
331 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
333 /* Channel FD commands */
334 [ LTTNG_UST_STREAM
] = "Create Stream",
335 [ LTTNG_UST_EVENT
] = "Create Event",
337 /* Event and Channel FD commands */
338 [ LTTNG_UST_CONTEXT
] = "Create Context",
339 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
341 /* Event, Channel and Session commands */
342 [ LTTNG_UST_ENABLE
] = "Enable",
343 [ LTTNG_UST_DISABLE
] = "Disable",
345 /* Tracepoint list commands */
346 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
347 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
349 /* Event FD commands */
350 [ LTTNG_UST_FILTER
] = "Create Filter",
351 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
353 /* Event notifier group commands */
354 [ LTTNG_UST_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
356 /* Session and event notifier group commands */
357 [ LTTNG_UST_COUNTER
] = "Create Counter",
359 /* Counter commands */
360 [ LTTNG_UST_COUNTER_GLOBAL
] = "Create Counter Global",
361 [ LTTNG_UST_COUNTER_CPU
] = "Create Counter CPU",
364 static const char *str_timeout
;
365 static int got_timeout_env
;
367 extern void lttng_ring_buffer_client_overwrite_init(void);
368 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
369 extern void lttng_ring_buffer_client_discard_init(void);
370 extern void lttng_ring_buffer_client_discard_rt_init(void);
371 extern void lttng_ring_buffer_metadata_client_init(void);
372 extern void lttng_ring_buffer_client_overwrite_exit(void);
373 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
374 extern void lttng_ring_buffer_client_discard_exit(void);
375 extern void lttng_ring_buffer_client_discard_rt_exit(void);
376 extern void lttng_ring_buffer_metadata_client_exit(void);
378 extern void lttng_counter_client_percpu_32_modular_init(void);
380 extern void lttng_counter_client_percpu_32_modular_exit(void);
382 extern void lttng_counter_client_percpu_64_modular_init(void);
384 extern void lttng_counter_client_percpu_64_modular_exit(void);
386 static char *get_map_shm(struct sock_info
*sock_info
);
388 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
391 size_t copied
= 0, to_copy
= len
;
394 ret
= read(fd
, buf
+ copied
, to_copy
);
399 } while ((ret
> 0 && to_copy
> 0)
400 || (ret
< 0 && errno
== EINTR
));
407 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
411 const char *get_lttng_home_dir(void)
415 val
= (const char *) lttng_getenv("LTTNG_HOME");
419 return (const char *) lttng_getenv("HOME");
423 * Force a read (imply TLS fixup for dlopen) of TLS variables.
426 void lttng_fixup_nest_count_tls(void)
428 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
432 void lttng_fixup_ust_mutex_nest_tls(void)
434 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
438 * Fixup lttng-ust urcu TLS.
441 void lttng_fixup_lttng_ust_urcu_tls(void)
443 (void) lttng_ust_urcu_read_ongoing();
446 void lttng_ust_fixup_tls(void)
448 lttng_fixup_lttng_ust_urcu_tls();
449 lttng_fixup_ringbuffer_tls();
450 lttng_fixup_vtid_tls();
451 lttng_fixup_nest_count_tls();
452 lttng_fixup_procname_tls();
453 lttng_fixup_ust_mutex_nest_tls();
454 lttng_ust_fixup_perf_counter_tls();
455 lttng_ust_fixup_fd_tracker_tls();
456 lttng_fixup_cgroup_ns_tls();
457 lttng_fixup_ipc_ns_tls();
458 lttng_fixup_net_ns_tls();
459 lttng_fixup_time_ns_tls();
460 lttng_fixup_uts_ns_tls();
463 int lttng_get_notify_socket(void *owner
)
465 struct sock_info
*info
= owner
;
467 return info
->notify_socket
;
472 char* lttng_ust_sockinfo_get_procname(void *owner
)
474 struct sock_info
*info
= owner
;
476 return info
->procname
;
480 void print_cmd(int cmd
, int handle
)
482 const char *cmd_name
= "Unknown";
484 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
485 && cmd_name_mapping
[cmd
]) {
486 cmd_name
= cmd_name_mapping
[cmd
];
488 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
490 lttng_ust_obj_get_name(handle
), handle
);
494 int setup_global_apps(void)
497 assert(!global_apps
.wait_shm_mmap
);
499 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
500 if (!global_apps
.wait_shm_mmap
) {
501 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
502 global_apps
.allowed
= 0;
507 global_apps
.allowed
= 1;
508 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
513 int setup_local_apps(void)
516 const char *home_dir
;
519 assert(!local_apps
.wait_shm_mmap
);
523 * Disallow per-user tracing for setuid binaries.
525 if (uid
!= geteuid()) {
526 assert(local_apps
.allowed
== 0);
530 home_dir
= get_lttng_home_dir();
532 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
533 assert(local_apps
.allowed
== 0);
537 local_apps
.allowed
= 1;
538 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
540 LTTNG_DEFAULT_HOME_RUNDIR
,
541 LTTNG_UST_SOCK_FILENAME
);
542 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
543 LTTNG_UST_WAIT_FILENAME
,
546 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
547 if (!local_apps
.wait_shm_mmap
) {
548 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
549 local_apps
.allowed
= 0;
554 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
560 * Get socket timeout, in ms.
561 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
564 long get_timeout(void)
566 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
568 if (!got_timeout_env
) {
569 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
573 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
574 /* All negative values are considered as "-1". */
575 if (constructor_delay_ms
< -1)
576 constructor_delay_ms
= -1;
577 return constructor_delay_ms
;
580 /* Timeout for notify socket send and recv. */
582 long get_notify_sock_timeout(void)
584 return get_timeout();
587 /* Timeout for connecting to cmd and notify sockets. */
589 long get_connect_sock_timeout(void)
591 return get_timeout();
595 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
598 int get_constructor_timeout(struct timespec
*constructor_timeout
)
600 long constructor_delay_ms
;
603 constructor_delay_ms
= get_timeout();
605 switch (constructor_delay_ms
) {
606 case -1:/* fall-through */
608 return constructor_delay_ms
;
614 * If we are unable to find the current time, don't wait.
616 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
621 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
622 constructor_timeout
->tv_nsec
+=
623 (constructor_delay_ms
% 1000UL) * 1000000UL;
624 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
625 constructor_timeout
->tv_sec
++;
626 constructor_timeout
->tv_nsec
-= 1000000000UL;
628 /* Timeout wait (constructor_delay_ms). */
633 void get_allow_blocking(void)
635 const char *str_allow_blocking
=
636 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
638 if (str_allow_blocking
) {
639 DBG("%s environment variable is set",
640 "LTTNG_UST_ALLOW_BLOCKING");
641 lttng_ust_ringbuffer_set_allow_blocking();
646 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
648 return ustcomm_send_reg_msg(socket
,
651 lttng_alignof(uint8_t) * CHAR_BIT
,
652 lttng_alignof(uint16_t) * CHAR_BIT
,
653 lttng_alignof(uint32_t) * CHAR_BIT
,
654 lttng_alignof(uint64_t) * CHAR_BIT
,
655 lttng_alignof(unsigned long) * CHAR_BIT
);
659 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
663 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
666 DBG("message successfully sent");
669 if (len
== -ECONNRESET
) {
670 DBG("remote end closed connection");
675 DBG("incorrect message size: %zd", len
);
681 void decrement_sem_count(unsigned int count
)
685 assert(uatomic_read(&sem_count
) >= count
);
687 if (uatomic_read(&sem_count
) <= 0) {
691 ret
= uatomic_add_return(&sem_count
, -count
);
693 ret
= sem_post(&constructor_wait
);
699 int handle_register_done(struct sock_info
*sock_info
)
701 if (sock_info
->registration_done
)
703 sock_info
->registration_done
= 1;
705 decrement_sem_count(1);
706 if (!sock_info
->statedump_pending
) {
707 sock_info
->initial_statedump_done
= 1;
708 decrement_sem_count(1);
715 int handle_register_failed(struct sock_info
*sock_info
)
717 if (sock_info
->registration_done
)
719 sock_info
->registration_done
= 1;
720 sock_info
->initial_statedump_done
= 1;
722 decrement_sem_count(2);
728 * Only execute pending statedump after the constructor semaphore has
729 * been posted by the current listener thread. This means statedump will
730 * only be performed after the "registration done" command is received
731 * from this thread's session daemon.
733 * This ensures we don't run into deadlock issues with the dynamic
734 * loader mutex, which is held while the constructor is called and
735 * waiting on the constructor semaphore. All operations requiring this
736 * dynamic loader lock need to be postponed using this mechanism.
738 * In a scenario with two session daemons connected to the application,
739 * it is possible that the first listener thread which receives the
740 * registration done command issues its statedump while the dynamic
741 * loader lock is still held by the application constructor waiting on
742 * the semaphore. It will however be allowed to proceed when the
743 * second session daemon sends the registration done command to the
744 * second listener thread. This situation therefore does not produce
748 void handle_pending_statedump(struct sock_info
*sock_info
)
750 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
751 sock_info
->statedump_pending
= 0;
752 pthread_mutex_lock(&ust_fork_mutex
);
753 lttng_handle_pending_statedump(sock_info
);
754 pthread_mutex_unlock(&ust_fork_mutex
);
756 if (!sock_info
->initial_statedump_done
) {
757 sock_info
->initial_statedump_done
= 1;
758 decrement_sem_count(1);
764 const char *bytecode_type_str(uint32_t cmd
)
767 case LTTNG_UST_CAPTURE
:
769 case LTTNG_UST_FILTER
:
777 int handle_bytecode_recv(struct sock_info
*sock_info
,
778 int sock
, struct ustcomm_ust_msg
*lum
)
780 struct lttng_ust_bytecode_node
*bytecode
= NULL
;
781 enum lttng_ust_bytecode_node_type type
;
782 const struct lttng_ust_objd_ops
*ops
;
783 uint32_t data_size
, data_size_max
, reloc_offset
;
789 case LTTNG_UST_FILTER
:
790 type
= LTTNG_UST_BYTECODE_NODE_TYPE_FILTER
;
791 data_size
= lum
->u
.filter
.data_size
;
792 data_size_max
= FILTER_BYTECODE_MAX_LEN
;
793 reloc_offset
= lum
->u
.filter
.reloc_offset
;
794 seqnum
= lum
->u
.filter
.seqnum
;
796 case LTTNG_UST_CAPTURE
:
797 type
= LTTNG_UST_BYTECODE_NODE_TYPE_CAPTURE
;
798 data_size
= lum
->u
.capture
.data_size
;
799 data_size_max
= CAPTURE_BYTECODE_MAX_LEN
;
800 reloc_offset
= lum
->u
.capture
.reloc_offset
;
801 seqnum
= lum
->u
.capture
.seqnum
;
807 if (data_size
> data_size_max
) {
808 ERR("Bytecode %s data size is too large: %u bytes",
809 bytecode_type_str(lum
->cmd
), data_size
);
814 if (reloc_offset
> data_size
) {
815 ERR("Bytecode %s reloc offset %u is not within data",
816 bytecode_type_str(lum
->cmd
), reloc_offset
);
821 /* Allocate the structure AND the `data[]` field. */
822 bytecode
= zmalloc(sizeof(*bytecode
) + data_size
);
828 bytecode
->bc
.len
= data_size
;
829 bytecode
->bc
.reloc_offset
= reloc_offset
;
830 bytecode
->bc
.seqnum
= seqnum
;
831 bytecode
->type
= type
;
833 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
, bytecode
->bc
.len
);
835 case 0: /* orderly shutdown */
839 if (len
== bytecode
->bc
.len
) {
840 DBG("Bytecode %s data received",
841 bytecode_type_str(lum
->cmd
));
843 } else if (len
< 0) {
844 DBG("Receive failed from lttng-sessiond with errno %d",
846 if (len
== -ECONNRESET
) {
847 ERR("%s remote end closed connection",
855 DBG("Incorrect %s bytecode data message size: %zd",
856 bytecode_type_str(lum
->cmd
), len
);
862 ops
= objd_ops(lum
->handle
);
869 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
870 (unsigned long) &bytecode
,
881 int handle_message(struct sock_info
*sock_info
,
882 int sock
, struct ustcomm_ust_msg
*lum
)
885 const struct lttng_ust_objd_ops
*ops
;
886 struct ustcomm_ust_reply lur
;
888 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
891 memset(&lur
, 0, sizeof(lur
));
894 ret
= -LTTNG_UST_ERR_EXITING
;
898 ops
= objd_ops(lum
->handle
);
905 case LTTNG_UST_REGISTER_DONE
:
906 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
907 ret
= handle_register_done(sock_info
);
911 case LTTNG_UST_RELEASE
:
912 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
915 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
917 case LTTNG_UST_CAPTURE
:
918 case LTTNG_UST_FILTER
:
919 ret
= handle_bytecode_recv(sock_info
, sock
, lum
);
923 case LTTNG_UST_EXCLUSION
:
925 /* Receive exclusion names */
926 struct lttng_ust_excluder_node
*node
;
929 count
= lum
->u
.exclusion
.count
;
931 /* There are no names to read */
935 node
= zmalloc(sizeof(*node
) +
936 count
* LTTNG_UST_SYM_NAME_LEN
);
941 node
->excluder
.count
= count
;
942 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
943 count
* LTTNG_UST_SYM_NAME_LEN
);
945 case 0: /* orderly shutdown */
950 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
951 DBG("Exclusion data received");
953 } else if (len
< 0) {
954 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
955 if (len
== -ECONNRESET
) {
956 ERR("%s remote end closed connection", sock_info
->name
);
965 DBG("Incorrect exclusion data message size: %zd", len
);
972 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
973 (unsigned long) &node
,
980 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
:
982 int event_notifier_notif_fd
, close_ret
;
984 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
985 &event_notifier_notif_fd
);
987 case 0: /* orderly shutdown */
994 DBG("Receive failed from lttng-sessiond with errno %d",
996 if (len
== -ECONNRESET
) {
997 ERR("%s remote end closed connection",
1005 DBG("Incorrect event notifier fd message size: %zd",
1011 args
.event_notifier_handle
.event_notifier_notif_fd
=
1012 event_notifier_notif_fd
;
1014 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1015 (unsigned long) &lum
->u
,
1019 if (args
.event_notifier_handle
.event_notifier_notif_fd
>= 0) {
1020 lttng_ust_lock_fd_tracker();
1021 close_ret
= close(args
.event_notifier_handle
.event_notifier_notif_fd
);
1022 lttng_ust_unlock_fd_tracker();
1028 case LTTNG_UST_CHANNEL
:
1033 len
= ustcomm_recv_channel_from_sessiond(sock
,
1034 &chan_data
, lum
->u
.channel
.len
,
1037 case 0: /* orderly shutdown */
1041 if (len
== lum
->u
.channel
.len
) {
1042 DBG("channel data received");
1044 } else if (len
< 0) {
1045 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1046 if (len
== -ECONNRESET
) {
1047 ERR("%s remote end closed connection", sock_info
->name
);
1054 DBG("incorrect channel data message size: %zd", len
);
1059 args
.channel
.chan_data
= chan_data
;
1060 args
.channel
.wakeup_fd
= wakeup_fd
;
1062 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1063 (unsigned long) &lum
->u
,
1067 if (args
.channel
.wakeup_fd
>= 0) {
1070 lttng_ust_lock_fd_tracker();
1071 close_ret
= close(args
.channel
.wakeup_fd
);
1072 lttng_ust_unlock_fd_tracker();
1073 args
.channel
.wakeup_fd
= -1;
1077 free(args
.channel
.chan_data
);
1080 case LTTNG_UST_STREAM
:
1084 /* Receive shm_fd, wakeup_fd */
1085 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1087 &args
.stream
.shm_fd
,
1088 &args
.stream
.wakeup_fd
);
1094 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1095 (unsigned long) &lum
->u
,
1099 if (args
.stream
.shm_fd
>= 0) {
1100 lttng_ust_lock_fd_tracker();
1101 close_ret
= close(args
.stream
.shm_fd
);
1102 lttng_ust_unlock_fd_tracker();
1103 args
.stream
.shm_fd
= -1;
1107 if (args
.stream
.wakeup_fd
>= 0) {
1108 lttng_ust_lock_fd_tracker();
1109 close_ret
= close(args
.stream
.wakeup_fd
);
1110 lttng_ust_unlock_fd_tracker();
1111 args
.stream
.wakeup_fd
= -1;
1117 case LTTNG_UST_CONTEXT
:
1118 switch (lum
->u
.context
.ctx
) {
1119 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1122 size_t ctxlen
, recvlen
;
1124 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1125 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1126 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
1127 ERR("Application context string length size is too large: %zu bytes",
1132 strcpy(ctxstr
, "$app.");
1133 p
= &ctxstr
[strlen("$app.")];
1134 recvlen
= ctxlen
- strlen("$app.");
1135 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1137 case 0: /* orderly shutdown */
1141 if (len
== recvlen
) {
1142 DBG("app context data received");
1144 } else if (len
< 0) {
1145 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1146 if (len
== -ECONNRESET
) {
1147 ERR("%s remote end closed connection", sock_info
->name
);
1154 DBG("incorrect app context data message size: %zd", len
);
1159 /* Put : between provider and ctxname. */
1160 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1161 args
.app_context
.ctxname
= ctxstr
;
1168 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1169 (unsigned long) &lum
->u
,
1175 case LTTNG_UST_COUNTER
:
1179 len
= ustcomm_recv_counter_from_sessiond(sock
,
1180 &counter_data
, lum
->u
.counter
.len
);
1182 case 0: /* orderly shutdown */
1186 if (len
== lum
->u
.counter
.len
) {
1187 DBG("counter data received");
1189 } else if (len
< 0) {
1190 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1191 if (len
== -ECONNRESET
) {
1192 ERR("%s remote end closed connection", sock_info
->name
);
1199 DBG("incorrect counter data message size: %zd", len
);
1204 args
.counter
.counter_data
= counter_data
;
1206 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1207 (unsigned long) &lum
->u
,
1211 free(args
.counter
.counter_data
);
1214 case LTTNG_UST_COUNTER_GLOBAL
:
1216 /* Receive shm_fd */
1217 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1218 &args
.counter_shm
.shm_fd
);
1224 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1225 (unsigned long) &lum
->u
,
1229 if (args
.counter_shm
.shm_fd
>= 0) {
1232 lttng_ust_lock_fd_tracker();
1233 close_ret
= close(args
.counter_shm
.shm_fd
);
1234 lttng_ust_unlock_fd_tracker();
1235 args
.counter_shm
.shm_fd
= -1;
1241 case LTTNG_UST_COUNTER_CPU
:
1243 /* Receive shm_fd */
1244 ret
= ustcomm_recv_counter_shm_from_sessiond(sock
,
1245 &args
.counter_shm
.shm_fd
);
1251 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1252 (unsigned long) &lum
->u
,
1256 if (args
.counter_shm
.shm_fd
>= 0) {
1259 lttng_ust_lock_fd_tracker();
1260 close_ret
= close(args
.counter_shm
.shm_fd
);
1261 lttng_ust_unlock_fd_tracker();
1262 args
.counter_shm
.shm_fd
= -1;
1268 case LTTNG_UST_EVENT_NOTIFIER_CREATE
:
1270 /* Receive struct lttng_ust_event_notifier */
1271 struct lttng_ust_event_notifier event_notifier
;
1273 if (sizeof(event_notifier
) != lum
->u
.event_notifier
.len
) {
1274 DBG("incorrect event notifier data message size: %u", lum
->u
.event_notifier
.len
);
1278 len
= ustcomm_recv_unix_sock(sock
, &event_notifier
, sizeof(event_notifier
));
1280 case 0: /* orderly shutdown */
1284 if (len
== sizeof(event_notifier
)) {
1285 DBG("event notifier data received");
1287 } else if (len
< 0) {
1288 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1289 if (len
== -ECONNRESET
) {
1290 ERR("%s remote end closed connection", sock_info
->name
);
1297 DBG("incorrect event notifier data message size: %zd", len
);
1303 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1304 (unsigned long) &event_notifier
,
1313 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1314 (unsigned long) &lum
->u
,
1321 lur
.handle
= lum
->handle
;
1325 lur
.ret_code
= LTTNG_UST_OK
;
1328 * Use -LTTNG_UST_ERR as wildcard for UST internal
1329 * error that are not caused by the transport, except if
1330 * we already have a more precise error message to
1333 if (ret
> -LTTNG_UST_ERR
) {
1334 /* Translate code to UST error. */
1337 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1340 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1343 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1346 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1349 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1352 lur
.ret_code
= -LTTNG_UST_ERR
;
1361 case LTTNG_UST_TRACER_VERSION
:
1362 lur
.u
.version
= lum
->u
.version
;
1364 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1365 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1369 DBG("Return value: %d", lur
.ret_val
);
1374 * Performed delayed statedump operations outside of the UST
1375 * lock. We need to take the dynamic loader lock before we take
1376 * the UST lock internally within handle_pending_statedump().
1378 handle_pending_statedump(sock_info
);
1381 ret
= -LTTNG_UST_ERR_EXITING
;
1385 ret
= send_reply(sock
, &lur
);
1387 DBG("error sending reply");
1392 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1395 if (lur
.ret_code
== LTTNG_UST_OK
) {
1397 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1398 len
= ustcomm_send_unix_sock(sock
,
1399 &args
.field_list
.entry
,
1400 sizeof(args
.field_list
.entry
));
1405 if (len
!= sizeof(args
.field_list
.entry
)) {
1419 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1423 if (sock_info
->root_handle
!= -1) {
1424 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1426 ERR("Error unref root handle");
1428 sock_info
->root_handle
= -1;
1430 sock_info
->registration_done
= 0;
1431 sock_info
->initial_statedump_done
= 0;
1434 * wait_shm_mmap, socket and notify socket are used by listener
1435 * threads outside of the ust lock, so we cannot tear them down
1436 * ourselves, because we cannot join on these threads. Leave
1437 * responsibility of cleaning up these resources to the OS
1443 if (sock_info
->socket
!= -1) {
1444 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1446 ERR("Error closing ust cmd socket");
1448 sock_info
->socket
= -1;
1450 if (sock_info
->notify_socket
!= -1) {
1451 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1453 ERR("Error closing ust notify socket");
1455 sock_info
->notify_socket
= -1;
1457 if (sock_info
->wait_shm_mmap
) {
1460 page_size
= LTTNG_UST_PAGE_SIZE
;
1461 if (page_size
<= 0) {
1465 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1467 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1469 ERR("Error unmapping wait shm");
1472 sock_info
->wait_shm_mmap
= NULL
;
1477 * Using fork to set umask in the child process (not multi-thread safe).
1478 * We deal with the shm_open vs ftruncate race (happening when the
1479 * sessiond owns the shm and does not let everybody modify it, to ensure
1480 * safety against shm_unlink) by simply letting the mmap fail and
1481 * retrying after a few seconds.
1482 * For global shm, everybody has rw access to it until the sessiond
1486 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1488 int wait_shm_fd
, ret
;
1492 * Try to open read-only.
1494 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1495 if (wait_shm_fd
>= 0) {
1498 size_t bytes_read
= 0;
1501 * Try to read the fd. If unable to do so, try opening
1505 len
= read(wait_shm_fd
,
1506 &((char *) &tmp_read
)[bytes_read
],
1507 sizeof(tmp_read
) - bytes_read
);
1511 } while ((len
< 0 && errno
== EINTR
)
1512 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1513 if (bytes_read
!= sizeof(tmp_read
)) {
1514 ret
= close(wait_shm_fd
);
1516 ERR("close wait_shm_fd");
1521 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1523 * Real-only open did not work, and it's not because the
1524 * entry was not present. It's a failure that prohibits
1527 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1533 * If the open failed because the file did not exist, or because
1534 * the file was not truncated yet, try creating it ourself.
1536 URCU_TLS(lttng_ust_nest_count
)++;
1538 URCU_TLS(lttng_ust_nest_count
)--;
1543 * Parent: wait for child to return, in which case the
1544 * shared memory map will have been created.
1546 pid
= wait(&status
);
1547 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1552 * Try to open read-only again after creation.
1554 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1555 if (wait_shm_fd
< 0) {
1557 * Real-only open did not work. It's a failure
1558 * that prohibits using shm.
1560 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1564 } else if (pid
== 0) {
1568 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1569 if (sock_info
->global
)
1570 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1572 * We're alone in a child process, so we can modify the
1573 * process-wide umask.
1575 umask(~create_mode
);
1577 * Try creating shm (or get rw access).
1578 * We don't do an exclusive open, because we allow other
1579 * processes to create+ftruncate it concurrently.
1581 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1582 O_RDWR
| O_CREAT
, create_mode
);
1583 if (wait_shm_fd
>= 0) {
1584 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1586 PERROR("ftruncate");
1587 _exit(EXIT_FAILURE
);
1589 _exit(EXIT_SUCCESS
);
1592 * For local shm, we need to have rw access to accept
1593 * opening it: this means the local sessiond will be
1594 * able to wake us up. For global shm, we open it even
1595 * if rw access is not granted, because the root.root
1596 * sessiond will be able to override all rights and wake
1599 if (!sock_info
->global
&& errno
!= EACCES
) {
1600 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1601 _exit(EXIT_FAILURE
);
1604 * The shm exists, but we cannot open it RW. Report
1607 _exit(EXIT_SUCCESS
);
1612 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1613 struct stat statbuf
;
1616 * Ensure that our user is the owner of the shm file for
1617 * local shm. If we do not own the file, it means our
1618 * sessiond will not have access to wake us up (there is
1619 * probably a rogue process trying to fake our
1620 * sessiond). Fallback to polling method in this case.
1622 ret
= fstat(wait_shm_fd
, &statbuf
);
1627 if (statbuf
.st_uid
!= getuid())
1633 ret
= close(wait_shm_fd
);
1635 PERROR("Error closing fd");
1641 char *get_map_shm(struct sock_info
*sock_info
)
1644 int wait_shm_fd
, ret
;
1645 char *wait_shm_mmap
;
1647 page_size
= sysconf(_SC_PAGE_SIZE
);
1648 if (page_size
<= 0) {
1652 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1656 lttng_ust_lock_fd_tracker();
1657 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1658 if (wait_shm_fd
< 0) {
1659 lttng_ust_unlock_fd_tracker();
1663 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1665 ret
= close(wait_shm_fd
);
1667 PERROR("Error closing fd");
1669 lttng_ust_unlock_fd_tracker();
1674 lttng_ust_unlock_fd_tracker();
1676 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1677 MAP_SHARED
, wait_shm_fd
, 0);
1679 /* close shm fd immediately after taking the mmap reference */
1680 lttng_ust_lock_fd_tracker();
1681 ret
= close(wait_shm_fd
);
1683 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1685 PERROR("Error closing fd");
1687 lttng_ust_unlock_fd_tracker();
1689 if (wait_shm_mmap
== MAP_FAILED
) {
1690 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1693 return wait_shm_mmap
;
1700 void wait_for_sessiond(struct sock_info
*sock_info
)
1702 /* Use ust_lock to check if we should quit. */
1706 if (wait_poll_fallback
) {
1711 assert(sock_info
->wait_shm_mmap
);
1713 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1714 /* Wait for futex wakeup */
1715 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1718 while (lttng_ust_futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1719 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1722 /* Value already changed. */
1725 /* Retry if interrupted by signal. */
1726 break; /* Get out of switch. */
1728 wait_poll_fallback
= 1;
1730 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1731 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1732 "Please upgrade your kernel "
1733 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1734 "mainline). LTTng-UST will use polling mode fallback.");
1753 * This thread does not allocate any resource, except within
1754 * handle_message, within mutex protection. This mutex protects against
1756 * The other moment it allocates resources is at socket connection, which
1757 * is also protected by the mutex.
1760 void *ust_listener_thread(void *arg
)
1762 struct sock_info
*sock_info
= arg
;
1763 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1766 lttng_ust_fixup_tls();
1768 * If available, add '-ust' to the end of this thread's
1771 ret
= lttng_ust_setustprocname();
1773 ERR("Unable to set UST process name");
1776 /* Restart trying to connect to the session daemon */
1778 if (prev_connect_failed
) {
1779 /* Wait for sessiond availability with pipe */
1780 wait_for_sessiond(sock_info
);
1784 * Sleep for 5 seconds before retrying after a
1785 * sequence of failure / wait / failure. This
1786 * deals with a killed or broken session daemon.
1792 prev_connect_failed
= 0;
1799 if (sock_info
->socket
!= -1) {
1800 /* FD tracker is updated by ustcomm_close_unix_sock() */
1801 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1803 ERR("Error closing %s ust cmd socket",
1806 sock_info
->socket
= -1;
1808 if (sock_info
->notify_socket
!= -1) {
1809 /* FD tracker is updated by ustcomm_close_unix_sock() */
1810 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1812 ERR("Error closing %s ust notify socket",
1815 sock_info
->notify_socket
= -1;
1820 * Register. We need to perform both connect and sending
1821 * registration message before doing the next connect otherwise
1822 * we may reach unix socket connect queue max limits and block
1823 * on the 2nd connect while the session daemon is awaiting the
1824 * first connect registration message.
1826 /* Connect cmd socket */
1827 lttng_ust_lock_fd_tracker();
1828 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1829 get_connect_sock_timeout());
1831 lttng_ust_unlock_fd_tracker();
1832 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1833 prev_connect_failed
= 1;
1836 * If we cannot find the sessiond daemon, don't delay
1837 * constructor execution.
1839 ret
= handle_register_failed(sock_info
);
1845 ret
= lttng_ust_add_fd_to_tracker(fd
);
1849 PERROR("close on sock_info->socket");
1852 lttng_ust_unlock_fd_tracker();
1857 sock_info
->socket
= ret
;
1858 lttng_ust_unlock_fd_tracker();
1862 * Unlock/relock ust lock because connect is blocking (with
1863 * timeout). Don't delay constructors on the ust lock for too
1871 * Create only one root handle per listener thread for the whole
1872 * process lifetime, so we ensure we get ID which is statically
1873 * assigned to the root handle.
1875 if (sock_info
->root_handle
== -1) {
1876 ret
= lttng_abi_create_root_handle();
1878 ERR("Error creating root handle");
1881 sock_info
->root_handle
= ret
;
1884 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1886 ERR("Error registering to %s ust cmd socket",
1888 prev_connect_failed
= 1;
1890 * If we cannot register to the sessiond daemon, don't
1891 * delay constructor execution.
1893 ret
= handle_register_failed(sock_info
);
1901 * Unlock/relock ust lock because connect is blocking (with
1902 * timeout). Don't delay constructors on the ust lock for too
1909 /* Connect notify socket */
1910 lttng_ust_lock_fd_tracker();
1911 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1912 get_connect_sock_timeout());
1914 lttng_ust_unlock_fd_tracker();
1915 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1916 prev_connect_failed
= 1;
1919 * If we cannot find the sessiond daemon, don't delay
1920 * constructor execution.
1922 ret
= handle_register_failed(sock_info
);
1929 ret
= lttng_ust_add_fd_to_tracker(fd
);
1933 PERROR("close on sock_info->notify_socket");
1936 lttng_ust_unlock_fd_tracker();
1941 sock_info
->notify_socket
= ret
;
1942 lttng_ust_unlock_fd_tracker();
1946 * Unlock/relock ust lock because connect is blocking (with
1947 * timeout). Don't delay constructors on the ust lock for too
1954 timeout
= get_notify_sock_timeout();
1957 * Give at least 10ms to sessiond to reply to
1962 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1965 WARN("Error setting socket receive timeout");
1967 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1970 WARN("Error setting socket send timeout");
1972 } else if (timeout
< -1) {
1973 WARN("Unsupported timeout value %ld", timeout
);
1976 ret
= register_to_sessiond(sock_info
->notify_socket
,
1977 USTCTL_SOCKET_NOTIFY
);
1979 ERR("Error registering to %s ust notify socket",
1981 prev_connect_failed
= 1;
1983 * If we cannot register to the sessiond daemon, don't
1984 * delay constructor execution.
1986 ret
= handle_register_failed(sock_info
);
1991 sock
= sock_info
->socket
;
1997 struct ustcomm_ust_msg lum
;
1999 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
2001 case 0: /* orderly shutdown */
2002 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
2007 * Either sessiond has shutdown or refused us by closing the socket.
2008 * In either case, we don't want to delay construction execution,
2009 * and we need to wait before retry.
2011 prev_connect_failed
= 1;
2013 * If we cannot register to the sessiond daemon, don't
2014 * delay constructor execution.
2016 ret
= handle_register_failed(sock_info
);
2021 print_cmd(lum
.cmd
, lum
.handle
);
2022 ret
= handle_message(sock_info
, sock
, &lum
);
2024 ERR("Error handling message for %s socket",
2027 * Close socket if protocol error is
2035 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
2037 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
2039 if (len
== -ECONNRESET
) {
2040 DBG("%s remote end closed connection", sock_info
->name
);
2051 /* Cleanup socket handles before trying to reconnect */
2052 lttng_ust_objd_table_owner_cleanup(sock_info
);
2054 goto restart
; /* try to reconnect */
2059 pthread_mutex_lock(&ust_exit_mutex
);
2060 sock_info
->thread_active
= 0;
2061 pthread_mutex_unlock(&ust_exit_mutex
);
2066 * Weak symbol to call when the ust malloc wrapper is not loaded.
2068 __attribute__((weak
))
2069 void lttng_ust_malloc_wrapper_init(void)
2074 * sessiond monitoring thread: monitor presence of global and per-user
2075 * sessiond by polling the application common named pipe.
2077 void __attribute__((constructor
)) lttng_ust_init(void)
2079 struct timespec constructor_timeout
;
2080 sigset_t sig_all_blocked
, orig_parent_mask
;
2081 pthread_attr_t thread_attr
;
2086 if (uatomic_xchg(&initialized
, 1) == 1)
2090 * Fixup interdependency between TLS fixup mutex (which happens
2091 * to be the dynamic linker mutex) and ust_lock, taken within
2094 lttng_ust_fixup_tls();
2096 lttng_ust_loaded
= 1;
2099 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2100 * the unloading of code used by the ust_listener_threads as we can not
2101 * reliably know when they exited. To do that, manually load
2102 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2103 * this library so it never becomes zero, thus never gets unloaded from the
2104 * address space of the process. Since we are already running in the
2105 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
2106 * simply increment the refcount and no additionnal work is needed by the
2107 * dynamic loader as the shared library is already loaded in the address
2108 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2109 * unloading of the UST library if its refcount becomes zero (which should
2110 * never happen). Do the return value check but discard the handle at the
2111 * end of the function as it's not needed.
2113 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
2115 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
2119 * We want precise control over the order in which we construct
2120 * our sub-libraries vs starting to receive commands from
2121 * sessiond (otherwise leading to errors when trying to create
2122 * sessiond before the init functions are completed).
2125 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
2127 lttng_ust_init_fd_tracker();
2128 lttng_ust_clock_init();
2129 lttng_ust_getcpu_init();
2130 lttng_ust_statedump_init();
2131 lttng_ring_buffer_metadata_client_init();
2132 lttng_ring_buffer_client_overwrite_init();
2133 lttng_ring_buffer_client_overwrite_rt_init();
2134 lttng_ring_buffer_client_discard_init();
2135 lttng_ring_buffer_client_discard_rt_init();
2136 lttng_counter_client_percpu_32_modular_init();
2137 lttng_counter_client_percpu_64_modular_init();
2138 lttng_perf_counter_init();
2140 * Invoke ust malloc wrapper init before starting other threads.
2142 lttng_ust_malloc_wrapper_init();
2144 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
2146 get_allow_blocking();
2148 ret
= sem_init(&constructor_wait
, 0, 0);
2153 ret
= setup_global_apps();
2155 assert(global_apps
.allowed
== 0);
2156 DBG("global apps setup returned %d", ret
);
2159 ret
= setup_local_apps();
2161 assert(local_apps
.allowed
== 0);
2162 DBG("local apps setup returned %d", ret
);
2165 /* A new thread created by pthread_create inherits the signal mask
2166 * from the parent. To avoid any signal being received by the
2167 * listener thread, we block all signals temporarily in the parent,
2168 * while we create the listener thread.
2170 sigfillset(&sig_all_blocked
);
2171 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
2173 ERR("pthread_sigmask: %s", strerror(ret
));
2176 ret
= pthread_attr_init(&thread_attr
);
2178 ERR("pthread_attr_init: %s", strerror(ret
));
2180 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
2182 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
2185 if (global_apps
.allowed
) {
2186 pthread_mutex_lock(&ust_exit_mutex
);
2187 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
2188 ust_listener_thread
, &global_apps
);
2190 ERR("pthread_create global: %s", strerror(ret
));
2192 global_apps
.thread_active
= 1;
2193 pthread_mutex_unlock(&ust_exit_mutex
);
2195 handle_register_done(&global_apps
);
2198 if (local_apps
.allowed
) {
2199 pthread_mutex_lock(&ust_exit_mutex
);
2200 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
2201 ust_listener_thread
, &local_apps
);
2203 ERR("pthread_create local: %s", strerror(ret
));
2205 local_apps
.thread_active
= 1;
2206 pthread_mutex_unlock(&ust_exit_mutex
);
2208 handle_register_done(&local_apps
);
2210 ret
= pthread_attr_destroy(&thread_attr
);
2212 ERR("pthread_attr_destroy: %s", strerror(ret
));
2215 /* Restore original signal mask in parent */
2216 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
2218 ERR("pthread_sigmask: %s", strerror(ret
));
2221 switch (timeout_mode
) {
2222 case 1: /* timeout wait */
2224 ret
= sem_timedwait(&constructor_wait
,
2225 &constructor_timeout
);
2226 } while (ret
< 0 && errno
== EINTR
);
2230 ERR("Timed out waiting for lttng-sessiond");
2233 PERROR("sem_timedwait");
2236 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2241 case -1:/* wait forever */
2243 ret
= sem_wait(&constructor_wait
);
2244 } while (ret
< 0 && errno
== EINTR
);
2251 ERR("Unexpected error \"%s\" returned by sem_wait",
2256 case 0: /* no timeout */
2262 void lttng_ust_cleanup(int exiting
)
2264 cleanup_sock_info(&global_apps
, exiting
);
2265 cleanup_sock_info(&local_apps
, exiting
);
2266 local_apps
.allowed
= 0;
2267 global_apps
.allowed
= 0;
2269 * The teardown in this function all affect data structures
2270 * accessed under the UST lock by the listener thread. This
2271 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2272 * that none of these threads are accessing this data at this
2275 lttng_ust_abi_exit();
2276 lttng_ust_events_exit();
2277 lttng_perf_counter_exit();
2278 lttng_ring_buffer_client_discard_rt_exit();
2279 lttng_ring_buffer_client_discard_exit();
2280 lttng_ring_buffer_client_overwrite_rt_exit();
2281 lttng_ring_buffer_client_overwrite_exit();
2282 lttng_ring_buffer_metadata_client_exit();
2283 lttng_counter_client_percpu_32_modular_exit();
2284 lttng_counter_client_percpu_64_modular_exit();
2285 lttng_ust_statedump_destroy();
2288 /* Reinitialize values for fork */
2289 sem_count
= sem_count_initial_value
;
2290 lttng_ust_comm_should_quit
= 0;
2295 void __attribute__((destructor
)) lttng_ust_exit(void)
2300 * Using pthread_cancel here because:
2301 * A) we don't want to hang application teardown.
2302 * B) the thread is not allocating any resource.
2306 * Require the communication thread to quit. Synchronize with
2307 * mutexes to ensure it is not in a mutex critical section when
2308 * pthread_cancel is later called.
2311 lttng_ust_comm_should_quit
= 1;
2314 pthread_mutex_lock(&ust_exit_mutex
);
2315 /* cancel threads */
2316 if (global_apps
.thread_active
) {
2317 ret
= pthread_cancel(global_apps
.ust_listener
);
2319 ERR("Error cancelling global ust listener thread: %s",
2322 global_apps
.thread_active
= 0;
2325 if (local_apps
.thread_active
) {
2326 ret
= pthread_cancel(local_apps
.ust_listener
);
2328 ERR("Error cancelling local ust listener thread: %s",
2331 local_apps
.thread_active
= 0;
2334 pthread_mutex_unlock(&ust_exit_mutex
);
2337 * Do NOT join threads: use of sys_futex makes it impossible to
2338 * join the threads without using async-cancel, but async-cancel
2339 * is delivered by a signal, which could hit the target thread
2340 * anywhere in its code path, including while the ust_lock() is
2341 * held, causing a deadlock for the other thread. Let the OS
2342 * cleanup the threads if there are stalled in a syscall.
2344 lttng_ust_cleanup(1);
2348 void ust_context_ns_reset(void)
2350 lttng_context_pid_ns_reset();
2351 lttng_context_cgroup_ns_reset();
2352 lttng_context_ipc_ns_reset();
2353 lttng_context_mnt_ns_reset();
2354 lttng_context_net_ns_reset();
2355 lttng_context_user_ns_reset();
2356 lttng_context_time_ns_reset();
2357 lttng_context_uts_ns_reset();
2361 void ust_context_vuids_reset(void)
2363 lttng_context_vuid_reset();
2364 lttng_context_veuid_reset();
2365 lttng_context_vsuid_reset();
2369 void ust_context_vgids_reset(void)
2371 lttng_context_vgid_reset();
2372 lttng_context_vegid_reset();
2373 lttng_context_vsgid_reset();
2377 * We exclude the worker threads across fork and clone (except
2378 * CLONE_VM), because these system calls only keep the forking thread
2379 * running in the child. Therefore, we don't want to call fork or clone
2380 * in the middle of an tracepoint or ust tracing state modification.
2381 * Holding this mutex protects these structures across fork and clone.
2383 void ust_before_fork(sigset_t
*save_sigset
)
2386 * Disable signals. This is to avoid that the child intervenes
2387 * before it is properly setup for tracing. It is safer to
2388 * disable all signals, because then we know we are not breaking
2389 * anything by restoring the original mask.
2394 /* Fixup lttng-ust TLS. */
2395 lttng_ust_fixup_tls();
2397 if (URCU_TLS(lttng_ust_nest_count
))
2399 /* Disable signals */
2400 sigfillset(&all_sigs
);
2401 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2403 PERROR("sigprocmask");
2406 pthread_mutex_lock(&ust_fork_mutex
);
2409 lttng_ust_urcu_before_fork();
2410 lttng_ust_lock_fd_tracker();
2414 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2418 DBG("process %d", getpid());
2419 lttng_perf_unlock();
2420 lttng_ust_unlock_fd_tracker();
2423 pthread_mutex_unlock(&ust_fork_mutex
);
2425 /* Restore signals */
2426 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2428 PERROR("sigprocmask");
2432 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2434 if (URCU_TLS(lttng_ust_nest_count
))
2436 DBG("process %d", getpid());
2437 lttng_ust_urcu_after_fork_parent();
2438 /* Release mutexes and reenable signals */
2439 ust_after_fork_common(restore_sigset
);
2443 * After fork, in the child, we need to cleanup all the leftover state,
2444 * except the worker thread which already magically disappeared thanks
2445 * to the weird Linux fork semantics. After tyding up, we call
2446 * lttng_ust_init() again to start over as a new PID.
2448 * This is meant for forks() that have tracing in the child between the
2449 * fork and following exec call (if there is any).
2451 void ust_after_fork_child(sigset_t
*restore_sigset
)
2453 if (URCU_TLS(lttng_ust_nest_count
))
2455 lttng_context_vpid_reset();
2456 lttng_context_vtid_reset();
2457 lttng_context_procname_reset();
2458 ust_context_ns_reset();
2459 ust_context_vuids_reset();
2460 ust_context_vgids_reset();
2461 DBG("process %d", getpid());
2462 /* Release urcu mutexes */
2463 lttng_ust_urcu_after_fork_child();
2464 lttng_ust_cleanup(0);
2465 /* Release mutexes and reenable signals */
2466 ust_after_fork_common(restore_sigset
);
2470 void ust_after_setns(void)
2472 ust_context_ns_reset();
2473 ust_context_vuids_reset();
2474 ust_context_vgids_reset();
2477 void ust_after_unshare(void)
2479 ust_context_ns_reset();
2480 ust_context_vuids_reset();
2481 ust_context_vgids_reset();
2484 void ust_after_setuid(void)
2486 ust_context_vuids_reset();
2489 void ust_after_seteuid(void)
2491 ust_context_vuids_reset();
2494 void ust_after_setreuid(void)
2496 ust_context_vuids_reset();
2499 void ust_after_setresuid(void)
2501 ust_context_vuids_reset();
2504 void ust_after_setgid(void)
2506 ust_context_vgids_reset();
2509 void ust_after_setegid(void)
2511 ust_context_vgids_reset();
2514 void ust_after_setregid(void)
2516 ust_context_vgids_reset();
2519 void ust_after_setresgid(void)
2521 ust_context_vgids_reset();
2524 void lttng_ust_sockinfo_session_enabled(void *owner
)
2526 struct sock_info
*sock_info
= owner
;
2527 sock_info
->statedump_pending
= 1;