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>
42 #include <urcu/futex.h>
43 #include <urcu/compiler.h>
45 #include <lttng/align.h>
46 #include <lttng/ust-events.h>
47 #include <lttng/ust-abi.h>
48 #include <lttng/ust.h>
49 #include <lttng/ust-error.h>
50 #include <lttng/ust-ctl.h>
51 #include <urcu/tls-compat.h>
54 #include <usterr-signal-safe.h>
56 #include "tracepoint-internal.h"
57 #include "lttng-tracer-core.h"
59 #include "../libringbuffer/rb-init.h"
60 #include "lttng-ust-statedump.h"
62 #include "../libringbuffer/getcpu.h"
64 #include "ust-events-internal.h"
66 /* Concatenate lttng ust shared library name with its major version number. */
67 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
70 * Has lttng ust comm constructor been called ?
72 static int initialized
;
75 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
76 * Held when handling a command, also held by fork() to deal with
77 * removal of threads, and by exit path.
79 * The UST lock is the centralized mutex across UST tracing control and
82 * ust_exit_mutex must never nest in ust_mutex.
84 * ust_fork_mutex must never nest in ust_mutex.
86 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
87 * counter lazy initialization called by events within the statedump,
88 * which traces while the ust_mutex is held.
90 * ust_lock nests within the dynamic loader lock (within glibc) because
91 * it is taken within the library constructor.
93 * The ust fd tracker lock nests within the ust_mutex.
95 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
97 /* Allow nesting the ust_mutex within the same thread. */
98 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
101 * ust_exit_mutex protects thread_active variable wrt thread exit. It
102 * cannot be done by ust_mutex because pthread_cancel(), which takes an
103 * internal libc lock, cannot nest within ust_mutex.
105 * It never nests within a ust_mutex.
107 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
110 * ust_fork_mutex protects base address statedump tracing against forks. It
111 * prevents the dynamic loader lock to be taken (by base address statedump
112 * tracing) while a fork is happening, thus preventing deadlock issues with
113 * the dynamic loader lock.
115 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
117 /* Should the ust comm thread quit ? */
118 static int lttng_ust_comm_should_quit
;
121 * This variable can be tested by applications to check whether
122 * lttng-ust is loaded. They simply have to define their own
123 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
124 * library constructor.
126 int lttng_ust_loaded
__attribute__((weak
));
129 * Return 0 on success, -1 if should quit.
130 * The lock is taken in both cases.
135 sigset_t sig_all_blocked
, orig_mask
;
138 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
140 ERR("pthread_setcancelstate: %s", strerror(ret
));
142 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
143 ERR("pthread_setcancelstate: unexpected oldstate");
145 sigfillset(&sig_all_blocked
);
146 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
148 ERR("pthread_sigmask: %s", strerror(ret
));
150 if (!URCU_TLS(ust_mutex_nest
)++)
151 pthread_mutex_lock(&ust_mutex
);
152 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
154 ERR("pthread_sigmask: %s", strerror(ret
));
156 if (lttng_ust_comm_should_quit
) {
164 * ust_lock_nocheck() can be used in constructors/destructors, because
165 * they are already nested within the dynamic loader lock, and therefore
166 * have exclusive access against execution of liblttng-ust destructor.
169 void ust_lock_nocheck(void)
171 sigset_t sig_all_blocked
, orig_mask
;
174 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
176 ERR("pthread_setcancelstate: %s", strerror(ret
));
178 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
179 ERR("pthread_setcancelstate: unexpected oldstate");
181 sigfillset(&sig_all_blocked
);
182 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
184 ERR("pthread_sigmask: %s", strerror(ret
));
186 if (!URCU_TLS(ust_mutex_nest
)++)
187 pthread_mutex_lock(&ust_mutex
);
188 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
190 ERR("pthread_sigmask: %s", strerror(ret
));
197 void ust_unlock(void)
199 sigset_t sig_all_blocked
, orig_mask
;
202 sigfillset(&sig_all_blocked
);
203 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
205 ERR("pthread_sigmask: %s", strerror(ret
));
207 if (!--URCU_TLS(ust_mutex_nest
))
208 pthread_mutex_unlock(&ust_mutex
);
209 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
211 ERR("pthread_sigmask: %s", strerror(ret
));
213 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
215 ERR("pthread_setcancelstate: %s", strerror(ret
));
217 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
218 ERR("pthread_setcancelstate: unexpected oldstate");
223 * Wait for either of these before continuing to the main
225 * - the register_done message from sessiond daemon
226 * (will let the sessiond daemon enable sessions before main
228 * - sessiond daemon is not reachable.
229 * - timeout (ensuring applications are resilient to session
232 static sem_t constructor_wait
;
234 * Doing this for both the global and local sessiond.
237 sem_count_initial_value
= 4,
240 static int sem_count
= sem_count_initial_value
;
243 * Counting nesting within lttng-ust. Used to ensure that calling fork()
244 * from liblttng-ust does not execute the pre/post fork handlers.
246 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
249 * Info about socket and associated listener thread.
253 pthread_t ust_listener
; /* listener thread */
255 int registration_done
;
260 char sock_path
[PATH_MAX
];
264 char wait_shm_path
[PATH_MAX
];
266 /* Keep track of lazy state dump not performed yet. */
267 int statedump_pending
;
268 int initial_statedump_done
;
269 /* Keep procname for statedump */
270 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
273 /* Socket from app (connect) to session daemon (listen) for communication */
274 struct sock_info global_apps
= {
279 .registration_done
= 0,
283 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
287 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
289 .statedump_pending
= 0,
290 .initial_statedump_done
= 0,
294 /* TODO: allow global_apps_sock_path override */
296 struct sock_info local_apps
= {
300 .registration_done
= 0,
301 .allowed
= 0, /* Check setuid bit first */
307 .statedump_pending
= 0,
308 .initial_statedump_done
= 0,
312 static int wait_poll_fallback
;
314 static const char *cmd_name_mapping
[] = {
315 [ LTTNG_UST_RELEASE
] = "Release",
316 [ LTTNG_UST_SESSION
] = "Create Session",
317 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
319 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
320 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
321 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
322 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
324 [ LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
] = "Create event notifier group",
326 /* Session FD commands */
327 [ LTTNG_UST_CHANNEL
] = "Create Channel",
328 [ LTTNG_UST_SESSION_START
] = "Start Session",
329 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
331 /* Channel FD commands */
332 [ LTTNG_UST_STREAM
] = "Create Stream",
333 [ LTTNG_UST_EVENT
] = "Create Event",
335 /* Event and Channel FD commands */
336 [ LTTNG_UST_CONTEXT
] = "Create Context",
337 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
339 /* Event, Channel and Session commands */
340 [ LTTNG_UST_ENABLE
] = "Enable",
341 [ LTTNG_UST_DISABLE
] = "Disable",
343 /* Tracepoint list commands */
344 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
345 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
347 /* Event FD commands */
348 [ LTTNG_UST_FILTER
] = "Create Filter",
349 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
351 /* Event notifier group commands */
352 [ LTTNG_UST_EVENT_NOTIFIER_CREATE
] = "Create event notifier",
355 static const char *str_timeout
;
356 static int got_timeout_env
;
358 extern void lttng_ring_buffer_client_overwrite_init(void);
359 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
360 extern void lttng_ring_buffer_client_discard_init(void);
361 extern void lttng_ring_buffer_client_discard_rt_init(void);
362 extern void lttng_ring_buffer_metadata_client_init(void);
363 extern void lttng_ring_buffer_client_overwrite_exit(void);
364 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
365 extern void lttng_ring_buffer_client_discard_exit(void);
366 extern void lttng_ring_buffer_client_discard_rt_exit(void);
367 extern void lttng_ring_buffer_metadata_client_exit(void);
369 static char *get_map_shm(struct sock_info
*sock_info
);
371 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
374 size_t copied
= 0, to_copy
= len
;
377 ret
= read(fd
, buf
+ copied
, to_copy
);
382 } while ((ret
> 0 && to_copy
> 0)
383 || (ret
< 0 && errno
== EINTR
));
390 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
394 const char *get_lttng_home_dir(void)
398 val
= (const char *) lttng_getenv("LTTNG_HOME");
402 return (const char *) lttng_getenv("HOME");
406 * Force a read (imply TLS fixup for dlopen) of TLS variables.
409 void lttng_fixup_nest_count_tls(void)
411 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
415 void lttng_fixup_ust_mutex_nest_tls(void)
417 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
424 void lttng_fixup_urcu_bp_tls(void)
430 void lttng_ust_fixup_tls(void)
432 lttng_fixup_urcu_bp_tls();
433 lttng_fixup_ringbuffer_tls();
434 lttng_fixup_vtid_tls();
435 lttng_fixup_nest_count_tls();
436 lttng_fixup_procname_tls();
437 lttng_fixup_ust_mutex_nest_tls();
438 lttng_ust_fixup_perf_counter_tls();
439 lttng_ust_fixup_fd_tracker_tls();
440 lttng_fixup_cgroup_ns_tls();
441 lttng_fixup_ipc_ns_tls();
442 lttng_fixup_net_ns_tls();
443 lttng_fixup_time_ns_tls();
444 lttng_fixup_uts_ns_tls();
447 int lttng_get_notify_socket(void *owner
)
449 struct sock_info
*info
= owner
;
451 return info
->notify_socket
;
456 char* lttng_ust_sockinfo_get_procname(void *owner
)
458 struct sock_info
*info
= owner
;
460 return info
->procname
;
464 void print_cmd(int cmd
, int handle
)
466 const char *cmd_name
= "Unknown";
468 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
469 && cmd_name_mapping
[cmd
]) {
470 cmd_name
= cmd_name_mapping
[cmd
];
472 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
474 lttng_ust_obj_get_name(handle
), handle
);
478 int setup_global_apps(void)
481 assert(!global_apps
.wait_shm_mmap
);
483 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
484 if (!global_apps
.wait_shm_mmap
) {
485 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
486 global_apps
.allowed
= 0;
491 global_apps
.allowed
= 1;
492 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
497 int setup_local_apps(void)
500 const char *home_dir
;
503 assert(!local_apps
.wait_shm_mmap
);
507 * Disallow per-user tracing for setuid binaries.
509 if (uid
!= geteuid()) {
510 assert(local_apps
.allowed
== 0);
514 home_dir
= get_lttng_home_dir();
516 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
517 assert(local_apps
.allowed
== 0);
521 local_apps
.allowed
= 1;
522 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
524 LTTNG_DEFAULT_HOME_RUNDIR
,
525 LTTNG_UST_SOCK_FILENAME
);
526 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
527 LTTNG_UST_WAIT_FILENAME
,
530 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
531 if (!local_apps
.wait_shm_mmap
) {
532 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
533 local_apps
.allowed
= 0;
538 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
544 * Get socket timeout, in ms.
545 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
548 long get_timeout(void)
550 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
552 if (!got_timeout_env
) {
553 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
557 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
558 /* All negative values are considered as "-1". */
559 if (constructor_delay_ms
< -1)
560 constructor_delay_ms
= -1;
561 return constructor_delay_ms
;
564 /* Timeout for notify socket send and recv. */
566 long get_notify_sock_timeout(void)
568 return get_timeout();
571 /* Timeout for connecting to cmd and notify sockets. */
573 long get_connect_sock_timeout(void)
575 return get_timeout();
579 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
582 int get_constructor_timeout(struct timespec
*constructor_timeout
)
584 long constructor_delay_ms
;
587 constructor_delay_ms
= get_timeout();
589 switch (constructor_delay_ms
) {
590 case -1:/* fall-through */
592 return constructor_delay_ms
;
598 * If we are unable to find the current time, don't wait.
600 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
605 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
606 constructor_timeout
->tv_nsec
+=
607 (constructor_delay_ms
% 1000UL) * 1000000UL;
608 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
609 constructor_timeout
->tv_sec
++;
610 constructor_timeout
->tv_nsec
-= 1000000000UL;
612 /* Timeout wait (constructor_delay_ms). */
617 void get_allow_blocking(void)
619 const char *str_allow_blocking
=
620 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
622 if (str_allow_blocking
) {
623 DBG("%s environment variable is set",
624 "LTTNG_UST_ALLOW_BLOCKING");
625 lttng_ust_ringbuffer_set_allow_blocking();
630 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
632 return ustcomm_send_reg_msg(socket
,
635 lttng_alignof(uint8_t) * CHAR_BIT
,
636 lttng_alignof(uint16_t) * CHAR_BIT
,
637 lttng_alignof(uint32_t) * CHAR_BIT
,
638 lttng_alignof(uint64_t) * CHAR_BIT
,
639 lttng_alignof(unsigned long) * CHAR_BIT
);
643 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
647 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
650 DBG("message successfully sent");
653 if (len
== -ECONNRESET
) {
654 DBG("remote end closed connection");
659 DBG("incorrect message size: %zd", len
);
665 void decrement_sem_count(unsigned int count
)
669 assert(uatomic_read(&sem_count
) >= count
);
671 if (uatomic_read(&sem_count
) <= 0) {
675 ret
= uatomic_add_return(&sem_count
, -count
);
677 ret
= sem_post(&constructor_wait
);
683 int handle_register_done(struct sock_info
*sock_info
)
685 if (sock_info
->registration_done
)
687 sock_info
->registration_done
= 1;
689 decrement_sem_count(1);
690 if (!sock_info
->statedump_pending
) {
691 sock_info
->initial_statedump_done
= 1;
692 decrement_sem_count(1);
699 int handle_register_failed(struct sock_info
*sock_info
)
701 if (sock_info
->registration_done
)
703 sock_info
->registration_done
= 1;
704 sock_info
->initial_statedump_done
= 1;
706 decrement_sem_count(2);
712 * Only execute pending statedump after the constructor semaphore has
713 * been posted by the current listener thread. This means statedump will
714 * only be performed after the "registration done" command is received
715 * from this thread's session daemon.
717 * This ensures we don't run into deadlock issues with the dynamic
718 * loader mutex, which is held while the constructor is called and
719 * waiting on the constructor semaphore. All operations requiring this
720 * dynamic loader lock need to be postponed using this mechanism.
722 * In a scenario with two session daemons connected to the application,
723 * it is possible that the first listener thread which receives the
724 * registration done command issues its statedump while the dynamic
725 * loader lock is still held by the application constructor waiting on
726 * the semaphore. It will however be allowed to proceed when the
727 * second session daemon sends the registration done command to the
728 * second listener thread. This situation therefore does not produce
732 void handle_pending_statedump(struct sock_info
*sock_info
)
734 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
735 sock_info
->statedump_pending
= 0;
736 pthread_mutex_lock(&ust_fork_mutex
);
737 lttng_handle_pending_statedump(sock_info
);
738 pthread_mutex_unlock(&ust_fork_mutex
);
740 if (!sock_info
->initial_statedump_done
) {
741 sock_info
->initial_statedump_done
= 1;
742 decrement_sem_count(1);
748 int handle_message(struct sock_info
*sock_info
,
749 int sock
, struct ustcomm_ust_msg
*lum
)
752 const struct lttng_ust_objd_ops
*ops
;
753 struct ustcomm_ust_reply lur
;
755 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
758 memset(&lur
, 0, sizeof(lur
));
761 ret
= -LTTNG_UST_ERR_EXITING
;
765 ops
= objd_ops(lum
->handle
);
772 case LTTNG_UST_REGISTER_DONE
:
773 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
774 ret
= handle_register_done(sock_info
);
778 case LTTNG_UST_RELEASE
:
779 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
782 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
784 case LTTNG_UST_FILTER
:
786 /* Receive filter data */
787 struct lttng_ust_bytecode_node
*bytecode
;
789 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
790 ERR("Filter data size is too large: %u bytes",
791 lum
->u
.filter
.data_size
);
796 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
797 ERR("Filter reloc offset %u is not within data",
798 lum
->u
.filter
.reloc_offset
);
803 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
809 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
810 lum
->u
.filter
.data_size
);
812 case 0: /* orderly shutdown */
817 if (len
== lum
->u
.filter
.data_size
) {
818 DBG("filter data received");
820 } else if (len
< 0) {
821 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
822 if (len
== -ECONNRESET
) {
823 ERR("%s remote end closed connection", sock_info
->name
);
832 DBG("incorrect filter data message size: %zd", len
);
838 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
839 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
840 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
842 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
843 (unsigned long) bytecode
,
848 /* don't free bytecode if everything went fine. */
855 case LTTNG_UST_EXCLUSION
:
857 /* Receive exclusion names */
858 struct lttng_ust_excluder_node
*node
;
861 count
= lum
->u
.exclusion
.count
;
863 /* There are no names to read */
867 node
= zmalloc(sizeof(*node
) +
868 count
* LTTNG_UST_SYM_NAME_LEN
);
873 node
->excluder
.count
= count
;
874 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
875 count
* LTTNG_UST_SYM_NAME_LEN
);
877 case 0: /* orderly shutdown */
882 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
883 DBG("Exclusion data received");
885 } else if (len
< 0) {
886 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
887 if (len
== -ECONNRESET
) {
888 ERR("%s remote end closed connection", sock_info
->name
);
897 DBG("Incorrect exclusion data message size: %zd", len
);
904 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
905 (unsigned long) node
,
910 /* Don't free exclusion data if everything went fine. */
917 case LTTNG_UST_EVENT_NOTIFIER_GROUP_CREATE
:
919 int event_notifier_notif_fd
;
921 len
= ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock
,
922 &event_notifier_notif_fd
);
924 case 0: /* orderly shutdown */
931 DBG("Receive failed from lttng-sessiond with errno %d",
933 if (len
== -ECONNRESET
) {
934 ERR("%s remote end closed connection",
942 DBG("Incorrect event notifier fd message size: %zd",
948 args
.event_notifier_handle
.event_notifier_notif_fd
=
949 event_notifier_notif_fd
;
951 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
952 (unsigned long) &lum
->u
,
958 case LTTNG_UST_CHANNEL
:
963 len
= ustcomm_recv_channel_from_sessiond(sock
,
964 &chan_data
, lum
->u
.channel
.len
,
967 case 0: /* orderly shutdown */
971 if (len
== lum
->u
.channel
.len
) {
972 DBG("channel data received");
974 } else if (len
< 0) {
975 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
976 if (len
== -ECONNRESET
) {
977 ERR("%s remote end closed connection", sock_info
->name
);
984 DBG("incorrect channel data message size: %zd", len
);
989 args
.channel
.chan_data
= chan_data
;
990 args
.channel
.wakeup_fd
= wakeup_fd
;
992 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
993 (unsigned long) &lum
->u
,
999 case LTTNG_UST_STREAM
:
1001 /* Receive shm_fd, wakeup_fd */
1002 ret
= ustcomm_recv_stream_from_sessiond(sock
,
1004 &args
.stream
.shm_fd
,
1005 &args
.stream
.wakeup_fd
);
1011 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1012 (unsigned long) &lum
->u
,
1018 case LTTNG_UST_CONTEXT
:
1019 switch (lum
->u
.context
.ctx
) {
1020 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1023 size_t ctxlen
, recvlen
;
1025 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1026 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1027 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
1028 ERR("Application context string length size is too large: %zu bytes",
1033 strcpy(ctxstr
, "$app.");
1034 p
= &ctxstr
[strlen("$app.")];
1035 recvlen
= ctxlen
- strlen("$app.");
1036 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1038 case 0: /* orderly shutdown */
1042 if (len
== recvlen
) {
1043 DBG("app context data received");
1045 } else if (len
< 0) {
1046 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1047 if (len
== -ECONNRESET
) {
1048 ERR("%s remote end closed connection", sock_info
->name
);
1055 DBG("incorrect app context data message size: %zd", len
);
1060 /* Put : between provider and ctxname. */
1061 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1062 args
.app_context
.ctxname
= ctxstr
;
1069 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1070 (unsigned long) &lum
->u
,
1078 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1079 (unsigned long) &lum
->u
,
1086 lur
.handle
= lum
->handle
;
1090 lur
.ret_code
= LTTNG_UST_OK
;
1093 * Use -LTTNG_UST_ERR as wildcard for UST internal
1094 * error that are not caused by the transport, except if
1095 * we already have a more precise error message to
1098 if (ret
> -LTTNG_UST_ERR
) {
1099 /* Translate code to UST error. */
1102 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1105 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1108 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1111 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1114 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1117 lur
.ret_code
= -LTTNG_UST_ERR
;
1126 case LTTNG_UST_TRACER_VERSION
:
1127 lur
.u
.version
= lum
->u
.version
;
1129 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1130 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1134 DBG("Return value: %d", lur
.ret_val
);
1139 * Performed delayed statedump operations outside of the UST
1140 * lock. We need to take the dynamic loader lock before we take
1141 * the UST lock internally within handle_pending_statedump().
1143 handle_pending_statedump(sock_info
);
1146 ret
= -LTTNG_UST_ERR_EXITING
;
1150 ret
= send_reply(sock
, &lur
);
1152 DBG("error sending reply");
1157 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1160 if (lur
.ret_code
== LTTNG_UST_OK
) {
1162 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1163 len
= ustcomm_send_unix_sock(sock
,
1164 &args
.field_list
.entry
,
1165 sizeof(args
.field_list
.entry
));
1170 if (len
!= sizeof(args
.field_list
.entry
)) {
1184 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1188 if (sock_info
->root_handle
!= -1) {
1189 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1191 ERR("Error unref root handle");
1193 sock_info
->root_handle
= -1;
1195 sock_info
->registration_done
= 0;
1196 sock_info
->initial_statedump_done
= 0;
1199 * wait_shm_mmap, socket and notify socket are used by listener
1200 * threads outside of the ust lock, so we cannot tear them down
1201 * ourselves, because we cannot join on these threads. Leave
1202 * responsibility of cleaning up these resources to the OS
1208 if (sock_info
->socket
!= -1) {
1209 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1211 ERR("Error closing ust cmd socket");
1213 sock_info
->socket
= -1;
1215 if (sock_info
->notify_socket
!= -1) {
1216 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1218 ERR("Error closing ust notify socket");
1220 sock_info
->notify_socket
= -1;
1222 if (sock_info
->wait_shm_mmap
) {
1225 page_size
= LTTNG_UST_PAGE_SIZE
;
1226 if (page_size
<= 0) {
1230 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1232 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1234 ERR("Error unmapping wait shm");
1237 sock_info
->wait_shm_mmap
= NULL
;
1242 * Using fork to set umask in the child process (not multi-thread safe).
1243 * We deal with the shm_open vs ftruncate race (happening when the
1244 * sessiond owns the shm and does not let everybody modify it, to ensure
1245 * safety against shm_unlink) by simply letting the mmap fail and
1246 * retrying after a few seconds.
1247 * For global shm, everybody has rw access to it until the sessiond
1251 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1253 int wait_shm_fd
, ret
;
1257 * Try to open read-only.
1259 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1260 if (wait_shm_fd
>= 0) {
1263 size_t bytes_read
= 0;
1266 * Try to read the fd. If unable to do so, try opening
1270 len
= read(wait_shm_fd
,
1271 &((char *) &tmp_read
)[bytes_read
],
1272 sizeof(tmp_read
) - bytes_read
);
1276 } while ((len
< 0 && errno
== EINTR
)
1277 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1278 if (bytes_read
!= sizeof(tmp_read
)) {
1279 ret
= close(wait_shm_fd
);
1281 ERR("close wait_shm_fd");
1286 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1288 * Real-only open did not work, and it's not because the
1289 * entry was not present. It's a failure that prohibits
1292 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1298 * If the open failed because the file did not exist, or because
1299 * the file was not truncated yet, try creating it ourself.
1301 URCU_TLS(lttng_ust_nest_count
)++;
1303 URCU_TLS(lttng_ust_nest_count
)--;
1308 * Parent: wait for child to return, in which case the
1309 * shared memory map will have been created.
1311 pid
= wait(&status
);
1312 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1317 * Try to open read-only again after creation.
1319 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1320 if (wait_shm_fd
< 0) {
1322 * Real-only open did not work. It's a failure
1323 * that prohibits using shm.
1325 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1329 } else if (pid
== 0) {
1333 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1334 if (sock_info
->global
)
1335 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1337 * We're alone in a child process, so we can modify the
1338 * process-wide umask.
1340 umask(~create_mode
);
1342 * Try creating shm (or get rw access).
1343 * We don't do an exclusive open, because we allow other
1344 * processes to create+ftruncate it concurrently.
1346 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1347 O_RDWR
| O_CREAT
, create_mode
);
1348 if (wait_shm_fd
>= 0) {
1349 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1351 PERROR("ftruncate");
1352 _exit(EXIT_FAILURE
);
1354 _exit(EXIT_SUCCESS
);
1357 * For local shm, we need to have rw access to accept
1358 * opening it: this means the local sessiond will be
1359 * able to wake us up. For global shm, we open it even
1360 * if rw access is not granted, because the root.root
1361 * sessiond will be able to override all rights and wake
1364 if (!sock_info
->global
&& errno
!= EACCES
) {
1365 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1366 _exit(EXIT_FAILURE
);
1369 * The shm exists, but we cannot open it RW. Report
1372 _exit(EXIT_SUCCESS
);
1377 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1378 struct stat statbuf
;
1381 * Ensure that our user is the owner of the shm file for
1382 * local shm. If we do not own the file, it means our
1383 * sessiond will not have access to wake us up (there is
1384 * probably a rogue process trying to fake our
1385 * sessiond). Fallback to polling method in this case.
1387 ret
= fstat(wait_shm_fd
, &statbuf
);
1392 if (statbuf
.st_uid
!= getuid())
1398 ret
= close(wait_shm_fd
);
1400 PERROR("Error closing fd");
1406 char *get_map_shm(struct sock_info
*sock_info
)
1409 int wait_shm_fd
, ret
;
1410 char *wait_shm_mmap
;
1412 page_size
= sysconf(_SC_PAGE_SIZE
);
1413 if (page_size
<= 0) {
1417 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1421 lttng_ust_lock_fd_tracker();
1422 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1423 if (wait_shm_fd
< 0) {
1424 lttng_ust_unlock_fd_tracker();
1428 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1430 ret
= close(wait_shm_fd
);
1432 PERROR("Error closing fd");
1434 lttng_ust_unlock_fd_tracker();
1439 lttng_ust_unlock_fd_tracker();
1441 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1442 MAP_SHARED
, wait_shm_fd
, 0);
1444 /* close shm fd immediately after taking the mmap reference */
1445 lttng_ust_lock_fd_tracker();
1446 ret
= close(wait_shm_fd
);
1448 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1450 PERROR("Error closing fd");
1452 lttng_ust_unlock_fd_tracker();
1454 if (wait_shm_mmap
== MAP_FAILED
) {
1455 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1458 return wait_shm_mmap
;
1465 void wait_for_sessiond(struct sock_info
*sock_info
)
1467 /* Use ust_lock to check if we should quit. */
1471 if (wait_poll_fallback
) {
1476 assert(sock_info
->wait_shm_mmap
);
1478 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1479 /* Wait for futex wakeup */
1480 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1483 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1484 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1487 /* Value already changed. */
1490 /* Retry if interrupted by signal. */
1491 break; /* Get out of switch. */
1493 wait_poll_fallback
= 1;
1495 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1496 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1497 "Please upgrade your kernel "
1498 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1499 "mainline). LTTng-UST will use polling mode fallback.");
1518 * This thread does not allocate any resource, except within
1519 * handle_message, within mutex protection. This mutex protects against
1521 * The other moment it allocates resources is at socket connection, which
1522 * is also protected by the mutex.
1525 void *ust_listener_thread(void *arg
)
1527 struct sock_info
*sock_info
= arg
;
1528 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1531 lttng_ust_fixup_tls();
1533 * If available, add '-ust' to the end of this thread's
1536 ret
= lttng_ust_setustprocname();
1538 ERR("Unable to set UST process name");
1541 /* Restart trying to connect to the session daemon */
1543 if (prev_connect_failed
) {
1544 /* Wait for sessiond availability with pipe */
1545 wait_for_sessiond(sock_info
);
1549 * Sleep for 5 seconds before retrying after a
1550 * sequence of failure / wait / failure. This
1551 * deals with a killed or broken session daemon.
1557 prev_connect_failed
= 0;
1564 if (sock_info
->socket
!= -1) {
1565 /* FD tracker is updated by ustcomm_close_unix_sock() */
1566 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1568 ERR("Error closing %s ust cmd socket",
1571 sock_info
->socket
= -1;
1573 if (sock_info
->notify_socket
!= -1) {
1574 /* FD tracker is updated by ustcomm_close_unix_sock() */
1575 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1577 ERR("Error closing %s ust notify socket",
1580 sock_info
->notify_socket
= -1;
1585 * Register. We need to perform both connect and sending
1586 * registration message before doing the next connect otherwise
1587 * we may reach unix socket connect queue max limits and block
1588 * on the 2nd connect while the session daemon is awaiting the
1589 * first connect registration message.
1591 /* Connect cmd socket */
1592 lttng_ust_lock_fd_tracker();
1593 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1594 get_connect_sock_timeout());
1596 lttng_ust_unlock_fd_tracker();
1597 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1598 prev_connect_failed
= 1;
1601 * If we cannot find the sessiond daemon, don't delay
1602 * constructor execution.
1604 ret
= handle_register_failed(sock_info
);
1610 ret
= lttng_ust_add_fd_to_tracker(fd
);
1614 PERROR("close on sock_info->socket");
1617 lttng_ust_unlock_fd_tracker();
1622 sock_info
->socket
= ret
;
1623 lttng_ust_unlock_fd_tracker();
1627 * Unlock/relock ust lock because connect is blocking (with
1628 * timeout). Don't delay constructors on the ust lock for too
1636 * Create only one root handle per listener thread for the whole
1637 * process lifetime, so we ensure we get ID which is statically
1638 * assigned to the root handle.
1640 if (sock_info
->root_handle
== -1) {
1641 ret
= lttng_abi_create_root_handle();
1643 ERR("Error creating root handle");
1646 sock_info
->root_handle
= ret
;
1649 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1651 ERR("Error registering to %s ust cmd socket",
1653 prev_connect_failed
= 1;
1655 * If we cannot register to the sessiond daemon, don't
1656 * delay constructor execution.
1658 ret
= handle_register_failed(sock_info
);
1666 * Unlock/relock ust lock because connect is blocking (with
1667 * timeout). Don't delay constructors on the ust lock for too
1674 /* Connect notify socket */
1675 lttng_ust_lock_fd_tracker();
1676 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1677 get_connect_sock_timeout());
1679 lttng_ust_unlock_fd_tracker();
1680 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1681 prev_connect_failed
= 1;
1684 * If we cannot find the sessiond daemon, don't delay
1685 * constructor execution.
1687 ret
= handle_register_failed(sock_info
);
1694 ret
= lttng_ust_add_fd_to_tracker(fd
);
1698 PERROR("close on sock_info->notify_socket");
1701 lttng_ust_unlock_fd_tracker();
1706 sock_info
->notify_socket
= ret
;
1707 lttng_ust_unlock_fd_tracker();
1711 * Unlock/relock ust lock because connect is blocking (with
1712 * timeout). Don't delay constructors on the ust lock for too
1719 timeout
= get_notify_sock_timeout();
1722 * Give at least 10ms to sessiond to reply to
1727 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1730 WARN("Error setting socket receive timeout");
1732 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1735 WARN("Error setting socket send timeout");
1737 } else if (timeout
< -1) {
1738 WARN("Unsupported timeout value %ld", timeout
);
1741 ret
= register_to_sessiond(sock_info
->notify_socket
,
1742 USTCTL_SOCKET_NOTIFY
);
1744 ERR("Error registering to %s ust notify socket",
1746 prev_connect_failed
= 1;
1748 * If we cannot register to the sessiond daemon, don't
1749 * delay constructor execution.
1751 ret
= handle_register_failed(sock_info
);
1756 sock
= sock_info
->socket
;
1762 struct ustcomm_ust_msg lum
;
1764 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1766 case 0: /* orderly shutdown */
1767 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1772 * Either sessiond has shutdown or refused us by closing the socket.
1773 * In either case, we don't want to delay construction execution,
1774 * and we need to wait before retry.
1776 prev_connect_failed
= 1;
1778 * If we cannot register to the sessiond daemon, don't
1779 * delay constructor execution.
1781 ret
= handle_register_failed(sock_info
);
1786 print_cmd(lum
.cmd
, lum
.handle
);
1787 ret
= handle_message(sock_info
, sock
, &lum
);
1789 ERR("Error handling message for %s socket",
1792 * Close socket if protocol error is
1800 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1802 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1804 if (len
== -ECONNRESET
) {
1805 DBG("%s remote end closed connection", sock_info
->name
);
1816 /* Cleanup socket handles before trying to reconnect */
1817 lttng_ust_objd_table_owner_cleanup(sock_info
);
1819 goto restart
; /* try to reconnect */
1824 pthread_mutex_lock(&ust_exit_mutex
);
1825 sock_info
->thread_active
= 0;
1826 pthread_mutex_unlock(&ust_exit_mutex
);
1831 * Weak symbol to call when the ust malloc wrapper is not loaded.
1833 __attribute__((weak
))
1834 void lttng_ust_malloc_wrapper_init(void)
1839 * sessiond monitoring thread: monitor presence of global and per-user
1840 * sessiond by polling the application common named pipe.
1842 void __attribute__((constructor
)) lttng_ust_init(void)
1844 struct timespec constructor_timeout
;
1845 sigset_t sig_all_blocked
, orig_parent_mask
;
1846 pthread_attr_t thread_attr
;
1851 if (uatomic_xchg(&initialized
, 1) == 1)
1855 * Fixup interdependency between TLS fixup mutex (which happens
1856 * to be the dynamic linker mutex) and ust_lock, taken within
1859 lttng_ust_fixup_tls();
1861 lttng_ust_loaded
= 1;
1864 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1865 * the unloading of code used by the ust_listener_threads as we can not
1866 * reliably know when they exited. To do that, manually load
1867 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1868 * this library so it never becomes zero, thus never gets unloaded from the
1869 * address space of the process. Since we are already running in the
1870 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1871 * simply increment the refcount and no additionnal work is needed by the
1872 * dynamic loader as the shared library is already loaded in the address
1873 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1874 * unloading of the UST library if its refcount becomes zero (which should
1875 * never happen). Do the return value check but discard the handle at the
1876 * end of the function as it's not needed.
1878 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1880 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1884 * We want precise control over the order in which we construct
1885 * our sub-libraries vs starting to receive commands from
1886 * sessiond (otherwise leading to errors when trying to create
1887 * sessiond before the init functions are completed).
1890 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1892 lttng_ust_init_fd_tracker();
1893 lttng_ust_clock_init();
1894 lttng_ust_getcpu_init();
1895 lttng_ust_statedump_init();
1896 lttng_ring_buffer_metadata_client_init();
1897 lttng_ring_buffer_client_overwrite_init();
1898 lttng_ring_buffer_client_overwrite_rt_init();
1899 lttng_ring_buffer_client_discard_init();
1900 lttng_ring_buffer_client_discard_rt_init();
1901 lttng_perf_counter_init();
1903 * Invoke ust malloc wrapper init before starting other threads.
1905 lttng_ust_malloc_wrapper_init();
1907 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1909 get_allow_blocking();
1911 ret
= sem_init(&constructor_wait
, 0, 0);
1916 ret
= setup_global_apps();
1918 assert(global_apps
.allowed
== 0);
1919 DBG("global apps setup returned %d", ret
);
1922 ret
= setup_local_apps();
1924 assert(local_apps
.allowed
== 0);
1925 DBG("local apps setup returned %d", ret
);
1928 /* A new thread created by pthread_create inherits the signal mask
1929 * from the parent. To avoid any signal being received by the
1930 * listener thread, we block all signals temporarily in the parent,
1931 * while we create the listener thread.
1933 sigfillset(&sig_all_blocked
);
1934 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1936 ERR("pthread_sigmask: %s", strerror(ret
));
1939 ret
= pthread_attr_init(&thread_attr
);
1941 ERR("pthread_attr_init: %s", strerror(ret
));
1943 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1945 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1948 if (global_apps
.allowed
) {
1949 pthread_mutex_lock(&ust_exit_mutex
);
1950 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1951 ust_listener_thread
, &global_apps
);
1953 ERR("pthread_create global: %s", strerror(ret
));
1955 global_apps
.thread_active
= 1;
1956 pthread_mutex_unlock(&ust_exit_mutex
);
1958 handle_register_done(&global_apps
);
1961 if (local_apps
.allowed
) {
1962 pthread_mutex_lock(&ust_exit_mutex
);
1963 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1964 ust_listener_thread
, &local_apps
);
1966 ERR("pthread_create local: %s", strerror(ret
));
1968 local_apps
.thread_active
= 1;
1969 pthread_mutex_unlock(&ust_exit_mutex
);
1971 handle_register_done(&local_apps
);
1973 ret
= pthread_attr_destroy(&thread_attr
);
1975 ERR("pthread_attr_destroy: %s", strerror(ret
));
1978 /* Restore original signal mask in parent */
1979 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1981 ERR("pthread_sigmask: %s", strerror(ret
));
1984 switch (timeout_mode
) {
1985 case 1: /* timeout wait */
1987 ret
= sem_timedwait(&constructor_wait
,
1988 &constructor_timeout
);
1989 } while (ret
< 0 && errno
== EINTR
);
1993 ERR("Timed out waiting for lttng-sessiond");
1996 PERROR("sem_timedwait");
1999 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2004 case -1:/* wait forever */
2006 ret
= sem_wait(&constructor_wait
);
2007 } while (ret
< 0 && errno
== EINTR
);
2014 ERR("Unexpected error \"%s\" returned by sem_wait",
2019 case 0: /* no timeout */
2025 void lttng_ust_cleanup(int exiting
)
2027 cleanup_sock_info(&global_apps
, exiting
);
2028 cleanup_sock_info(&local_apps
, exiting
);
2029 local_apps
.allowed
= 0;
2030 global_apps
.allowed
= 0;
2032 * The teardown in this function all affect data structures
2033 * accessed under the UST lock by the listener thread. This
2034 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2035 * that none of these threads are accessing this data at this
2038 lttng_ust_abi_exit();
2039 lttng_ust_events_exit();
2040 lttng_perf_counter_exit();
2041 lttng_ring_buffer_client_discard_rt_exit();
2042 lttng_ring_buffer_client_discard_exit();
2043 lttng_ring_buffer_client_overwrite_rt_exit();
2044 lttng_ring_buffer_client_overwrite_exit();
2045 lttng_ring_buffer_metadata_client_exit();
2046 lttng_ust_statedump_destroy();
2049 /* Reinitialize values for fork */
2050 sem_count
= sem_count_initial_value
;
2051 lttng_ust_comm_should_quit
= 0;
2056 void __attribute__((destructor
)) lttng_ust_exit(void)
2061 * Using pthread_cancel here because:
2062 * A) we don't want to hang application teardown.
2063 * B) the thread is not allocating any resource.
2067 * Require the communication thread to quit. Synchronize with
2068 * mutexes to ensure it is not in a mutex critical section when
2069 * pthread_cancel is later called.
2072 lttng_ust_comm_should_quit
= 1;
2075 pthread_mutex_lock(&ust_exit_mutex
);
2076 /* cancel threads */
2077 if (global_apps
.thread_active
) {
2078 ret
= pthread_cancel(global_apps
.ust_listener
);
2080 ERR("Error cancelling global ust listener thread: %s",
2083 global_apps
.thread_active
= 0;
2086 if (local_apps
.thread_active
) {
2087 ret
= pthread_cancel(local_apps
.ust_listener
);
2089 ERR("Error cancelling local ust listener thread: %s",
2092 local_apps
.thread_active
= 0;
2095 pthread_mutex_unlock(&ust_exit_mutex
);
2098 * Do NOT join threads: use of sys_futex makes it impossible to
2099 * join the threads without using async-cancel, but async-cancel
2100 * is delivered by a signal, which could hit the target thread
2101 * anywhere in its code path, including while the ust_lock() is
2102 * held, causing a deadlock for the other thread. Let the OS
2103 * cleanup the threads if there are stalled in a syscall.
2105 lttng_ust_cleanup(1);
2109 void ust_context_ns_reset(void)
2111 lttng_context_pid_ns_reset();
2112 lttng_context_cgroup_ns_reset();
2113 lttng_context_ipc_ns_reset();
2114 lttng_context_mnt_ns_reset();
2115 lttng_context_net_ns_reset();
2116 lttng_context_user_ns_reset();
2117 lttng_context_time_ns_reset();
2118 lttng_context_uts_ns_reset();
2122 void ust_context_vuids_reset(void)
2124 lttng_context_vuid_reset();
2125 lttng_context_veuid_reset();
2126 lttng_context_vsuid_reset();
2130 void ust_context_vgids_reset(void)
2132 lttng_context_vgid_reset();
2133 lttng_context_vegid_reset();
2134 lttng_context_vsgid_reset();
2138 * We exclude the worker threads across fork and clone (except
2139 * CLONE_VM), because these system calls only keep the forking thread
2140 * running in the child. Therefore, we don't want to call fork or clone
2141 * in the middle of an tracepoint or ust tracing state modification.
2142 * Holding this mutex protects these structures across fork and clone.
2144 void ust_before_fork(sigset_t
*save_sigset
)
2147 * Disable signals. This is to avoid that the child intervenes
2148 * before it is properly setup for tracing. It is safer to
2149 * disable all signals, because then we know we are not breaking
2150 * anything by restoring the original mask.
2155 /* Fixup lttng-ust TLS. */
2156 lttng_ust_fixup_tls();
2158 if (URCU_TLS(lttng_ust_nest_count
))
2160 /* Disable signals */
2161 sigfillset(&all_sigs
);
2162 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2164 PERROR("sigprocmask");
2167 pthread_mutex_lock(&ust_fork_mutex
);
2170 urcu_bp_before_fork();
2171 lttng_ust_lock_fd_tracker();
2175 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2179 DBG("process %d", getpid());
2180 lttng_perf_unlock();
2181 lttng_ust_unlock_fd_tracker();
2184 pthread_mutex_unlock(&ust_fork_mutex
);
2186 /* Restore signals */
2187 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2189 PERROR("sigprocmask");
2193 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2195 if (URCU_TLS(lttng_ust_nest_count
))
2197 DBG("process %d", getpid());
2198 urcu_bp_after_fork_parent();
2199 /* Release mutexes and reenable signals */
2200 ust_after_fork_common(restore_sigset
);
2204 * After fork, in the child, we need to cleanup all the leftover state,
2205 * except the worker thread which already magically disappeared thanks
2206 * to the weird Linux fork semantics. After tyding up, we call
2207 * lttng_ust_init() again to start over as a new PID.
2209 * This is meant for forks() that have tracing in the child between the
2210 * fork and following exec call (if there is any).
2212 void ust_after_fork_child(sigset_t
*restore_sigset
)
2214 if (URCU_TLS(lttng_ust_nest_count
))
2216 lttng_context_vpid_reset();
2217 lttng_context_vtid_reset();
2218 lttng_context_procname_reset();
2219 ust_context_ns_reset();
2220 ust_context_vuids_reset();
2221 ust_context_vgids_reset();
2222 DBG("process %d", getpid());
2223 /* Release urcu mutexes */
2224 urcu_bp_after_fork_child();
2225 lttng_ust_cleanup(0);
2226 /* Release mutexes and reenable signals */
2227 ust_after_fork_common(restore_sigset
);
2231 void ust_after_setns(void)
2233 ust_context_ns_reset();
2234 ust_context_vuids_reset();
2235 ust_context_vgids_reset();
2238 void ust_after_unshare(void)
2240 ust_context_ns_reset();
2241 ust_context_vuids_reset();
2242 ust_context_vgids_reset();
2245 void ust_after_setuid(void)
2247 ust_context_vuids_reset();
2250 void ust_after_seteuid(void)
2252 ust_context_vuids_reset();
2255 void ust_after_setreuid(void)
2257 ust_context_vuids_reset();
2260 void ust_after_setresuid(void)
2262 ust_context_vuids_reset();
2265 void ust_after_setgid(void)
2267 ust_context_vgids_reset();
2270 void ust_after_setegid(void)
2272 ust_context_vgids_reset();
2275 void ust_after_setregid(void)
2277 ust_context_vgids_reset();
2280 void ust_after_setresgid(void)
2282 ust_context_vgids_reset();
2285 void lttng_ust_sockinfo_session_enabled(void *owner
)
2287 struct sock_info
*sock_info
= owner
;
2288 sock_info
->statedump_pending
= 1;