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
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <semaphore.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/futex.h>
42 #include <urcu/compiler.h>
44 #include <lttng/ust-events.h>
45 #include <lttng/ust-abi.h>
46 #include <lttng/ust.h>
47 #include <lttng/ust-error.h>
48 #include <lttng/ust-ctl.h>
49 #include <urcu/tls-compat.h>
52 #include <usterr-signal-safe.h>
54 #include "tracepoint-internal.h"
55 #include "lttng-tracer-core.h"
57 #include "../libringbuffer/rb-init.h"
58 #include "lttng-ust-statedump.h"
60 #include "../libringbuffer/getcpu.h"
63 /* Concatenate lttng ust shared library name with its major version number. */
64 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
67 * Has lttng ust comm constructor been called ?
69 static int initialized
;
72 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
73 * Held when handling a command, also held by fork() to deal with
74 * removal of threads, and by exit path.
76 * The UST lock is the centralized mutex across UST tracing control and
79 * ust_exit_mutex must never nest in ust_mutex.
81 * ust_fork_mutex must never nest in ust_mutex.
83 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
84 * counter lazy initialization called by events within the statedump,
85 * which traces while the ust_mutex is held.
87 * ust_lock nests within the dynamic loader lock (within glibc) because
88 * it is taken within the library constructor.
90 * The ust fd tracker lock nests within the ust_mutex.
92 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
94 /* Allow nesting the ust_mutex within the same thread. */
95 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
98 * ust_exit_mutex protects thread_active variable wrt thread exit. It
99 * cannot be done by ust_mutex because pthread_cancel(), which takes an
100 * internal libc lock, cannot nest within ust_mutex.
102 * It never nests within a ust_mutex.
104 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 * ust_fork_mutex protects base address statedump tracing against forks. It
108 * prevents the dynamic loader lock to be taken (by base address statedump
109 * tracing) while a fork is happening, thus preventing deadlock issues with
110 * the dynamic loader lock.
112 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
114 /* Should the ust comm thread quit ? */
115 static int lttng_ust_comm_should_quit
;
118 * This variable can be tested by applications to check whether
119 * lttng-ust is loaded. They simply have to define their own
120 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
121 * library constructor.
123 int lttng_ust_loaded
__attribute__((weak
));
126 * Return 0 on success, -1 if should quit.
127 * The lock is taken in both cases.
132 sigset_t sig_all_blocked
, orig_mask
;
135 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
137 ERR("pthread_setcancelstate: %s", strerror(ret
));
139 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
140 ERR("pthread_setcancelstate: unexpected oldstate");
142 sigfillset(&sig_all_blocked
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (!URCU_TLS(ust_mutex_nest
)++)
148 pthread_mutex_lock(&ust_mutex
);
149 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
151 ERR("pthread_sigmask: %s", strerror(ret
));
153 if (lttng_ust_comm_should_quit
) {
161 * ust_lock_nocheck() can be used in constructors/destructors, because
162 * they are already nested within the dynamic loader lock, and therefore
163 * have exclusive access against execution of liblttng-ust destructor.
166 void ust_lock_nocheck(void)
168 sigset_t sig_all_blocked
, orig_mask
;
171 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
173 ERR("pthread_setcancelstate: %s", strerror(ret
));
175 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
176 ERR("pthread_setcancelstate: unexpected oldstate");
178 sigfillset(&sig_all_blocked
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
183 if (!URCU_TLS(ust_mutex_nest
)++)
184 pthread_mutex_lock(&ust_mutex
);
185 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
187 ERR("pthread_sigmask: %s", strerror(ret
));
194 void ust_unlock(void)
196 sigset_t sig_all_blocked
, orig_mask
;
199 sigfillset(&sig_all_blocked
);
200 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
202 ERR("pthread_sigmask: %s", strerror(ret
));
204 if (!--URCU_TLS(ust_mutex_nest
))
205 pthread_mutex_unlock(&ust_mutex
);
206 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
208 ERR("pthread_sigmask: %s", strerror(ret
));
210 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
212 ERR("pthread_setcancelstate: %s", strerror(ret
));
214 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
215 ERR("pthread_setcancelstate: unexpected oldstate");
220 * Wait for either of these before continuing to the main
222 * - the register_done message from sessiond daemon
223 * (will let the sessiond daemon enable sessions before main
225 * - sessiond daemon is not reachable.
226 * - timeout (ensuring applications are resilient to session
229 static sem_t constructor_wait
;
231 * Doing this for both the global and local sessiond.
234 sem_count_initial_value
= 4,
237 static int sem_count
= sem_count_initial_value
;
240 * Counting nesting within lttng-ust. Used to ensure that calling fork()
241 * from liblttng-ust does not execute the pre/post fork handlers.
243 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
246 * Info about socket and associated listener thread.
250 pthread_t ust_listener
; /* listener thread */
252 int registration_done
;
257 char sock_path
[PATH_MAX
];
261 char wait_shm_path
[PATH_MAX
];
263 /* Keep track of lazy state dump not performed yet. */
264 int statedump_pending
;
265 int initial_statedump_done
;
266 /* Keep procname for statedump */
267 char procname
[LTTNG_UST_PROCNAME_LEN
];
270 /* Socket from app (connect) to session daemon (listen) for communication */
271 struct sock_info global_apps
= {
276 .registration_done
= 0,
280 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
284 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
286 .statedump_pending
= 0,
287 .initial_statedump_done
= 0,
291 /* TODO: allow global_apps_sock_path override */
293 struct sock_info local_apps
= {
297 .registration_done
= 0,
298 .allowed
= 0, /* Check setuid bit first */
304 .statedump_pending
= 0,
305 .initial_statedump_done
= 0,
309 static int wait_poll_fallback
;
311 static const char *cmd_name_mapping
[] = {
312 [ LTTNG_UST_RELEASE
] = "Release",
313 [ LTTNG_UST_SESSION
] = "Create Session",
314 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
316 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
317 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
318 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
319 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
321 /* Session FD commands */
322 [ LTTNG_UST_CHANNEL
] = "Create Channel",
323 [ LTTNG_UST_SESSION_START
] = "Start Session",
324 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
326 /* Channel FD commands */
327 [ LTTNG_UST_STREAM
] = "Create Stream",
328 [ LTTNG_UST_EVENT
] = "Create Event",
330 /* Event and Channel FD commands */
331 [ LTTNG_UST_CONTEXT
] = "Create Context",
332 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
334 /* Event, Channel and Session commands */
335 [ LTTNG_UST_ENABLE
] = "Enable",
336 [ LTTNG_UST_DISABLE
] = "Disable",
338 /* Tracepoint list commands */
339 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
340 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
342 /* Event FD commands */
343 [ LTTNG_UST_FILTER
] = "Create Filter",
344 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
347 static const char *str_timeout
;
348 static int got_timeout_env
;
350 extern void lttng_ring_buffer_client_overwrite_init(void);
351 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
352 extern void lttng_ring_buffer_client_discard_init(void);
353 extern void lttng_ring_buffer_client_discard_rt_init(void);
354 extern void lttng_ring_buffer_metadata_client_init(void);
355 extern void lttng_ring_buffer_client_overwrite_exit(void);
356 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
357 extern void lttng_ring_buffer_client_discard_exit(void);
358 extern void lttng_ring_buffer_client_discard_rt_exit(void);
359 extern void lttng_ring_buffer_metadata_client_exit(void);
361 static char *get_map_shm(struct sock_info
*sock_info
);
363 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
366 size_t copied
= 0, to_copy
= len
;
369 ret
= read(fd
, buf
+ copied
, to_copy
);
374 } while ((ret
> 0 && to_copy
> 0)
375 || (ret
< 0 && errno
== EINTR
));
382 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
386 const char *get_lttng_home_dir(void)
390 val
= (const char *) lttng_getenv("LTTNG_HOME");
394 return (const char *) lttng_getenv("HOME");
398 * Force a read (imply TLS fixup for dlopen) of TLS variables.
401 void lttng_fixup_nest_count_tls(void)
403 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
407 void lttng_fixup_ust_mutex_nest_tls(void)
409 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
416 void lttng_fixup_urcu_bp_tls(void)
422 void lttng_ust_fixup_tls(void)
424 lttng_fixup_urcu_bp_tls();
425 lttng_fixup_ringbuffer_tls();
426 lttng_fixup_vtid_tls();
427 lttng_fixup_nest_count_tls();
428 lttng_fixup_procname_tls();
429 lttng_fixup_ust_mutex_nest_tls();
430 lttng_ust_fixup_perf_counter_tls();
431 lttng_ust_fixup_fd_tracker_tls();
432 lttng_fixup_cgroup_ns_tls();
433 lttng_fixup_ipc_ns_tls();
434 lttng_fixup_net_ns_tls();
435 lttng_fixup_uts_ns_tls();
438 int lttng_get_notify_socket(void *owner
)
440 struct sock_info
*info
= owner
;
442 return info
->notify_socket
;
447 char* lttng_ust_sockinfo_get_procname(void *owner
)
449 struct sock_info
*info
= owner
;
451 return info
->procname
;
455 void print_cmd(int cmd
, int handle
)
457 const char *cmd_name
= "Unknown";
459 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
460 && cmd_name_mapping
[cmd
]) {
461 cmd_name
= cmd_name_mapping
[cmd
];
463 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
465 lttng_ust_obj_get_name(handle
), handle
);
469 int setup_global_apps(void)
472 assert(!global_apps
.wait_shm_mmap
);
474 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
475 if (!global_apps
.wait_shm_mmap
) {
476 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
477 global_apps
.allowed
= 0;
482 global_apps
.allowed
= 1;
483 lttng_ust_getprocname(global_apps
.procname
);
488 int setup_local_apps(void)
491 const char *home_dir
;
494 assert(!local_apps
.wait_shm_mmap
);
498 * Disallow per-user tracing for setuid binaries.
500 if (uid
!= geteuid()) {
501 assert(local_apps
.allowed
== 0);
505 home_dir
= get_lttng_home_dir();
507 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
508 assert(local_apps
.allowed
== 0);
512 local_apps
.allowed
= 1;
513 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
515 LTTNG_DEFAULT_HOME_RUNDIR
,
516 LTTNG_UST_SOCK_FILENAME
);
517 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
518 LTTNG_UST_WAIT_FILENAME
,
521 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
522 if (!local_apps
.wait_shm_mmap
) {
523 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
524 local_apps
.allowed
= 0;
529 lttng_ust_getprocname(local_apps
.procname
);
535 * Get socket timeout, in ms.
536 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
539 long get_timeout(void)
541 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
543 if (!got_timeout_env
) {
544 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
548 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
549 /* All negative values are considered as "-1". */
550 if (constructor_delay_ms
< -1)
551 constructor_delay_ms
= -1;
552 return constructor_delay_ms
;
555 /* Timeout for notify socket send and recv. */
557 long get_notify_sock_timeout(void)
559 return get_timeout();
562 /* Timeout for connecting to cmd and notify sockets. */
564 long get_connect_sock_timeout(void)
566 return get_timeout();
570 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
573 int get_constructor_timeout(struct timespec
*constructor_timeout
)
575 long constructor_delay_ms
;
578 constructor_delay_ms
= get_timeout();
580 switch (constructor_delay_ms
) {
581 case -1:/* fall-through */
583 return constructor_delay_ms
;
589 * If we are unable to find the current time, don't wait.
591 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
596 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
597 constructor_timeout
->tv_nsec
+=
598 (constructor_delay_ms
% 1000UL) * 1000000UL;
599 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
600 constructor_timeout
->tv_sec
++;
601 constructor_timeout
->tv_nsec
-= 1000000000UL;
603 /* Timeout wait (constructor_delay_ms). */
608 void get_allow_blocking(void)
610 const char *str_allow_blocking
=
611 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
613 if (str_allow_blocking
) {
614 DBG("%s environment variable is set",
615 "LTTNG_UST_ALLOW_BLOCKING");
616 lttng_ust_ringbuffer_set_allow_blocking();
621 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
623 return ustcomm_send_reg_msg(socket
,
626 lttng_alignof(uint8_t) * CHAR_BIT
,
627 lttng_alignof(uint16_t) * CHAR_BIT
,
628 lttng_alignof(uint32_t) * CHAR_BIT
,
629 lttng_alignof(uint64_t) * CHAR_BIT
,
630 lttng_alignof(unsigned long) * CHAR_BIT
);
634 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
638 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
641 DBG("message successfully sent");
644 if (len
== -ECONNRESET
) {
645 DBG("remote end closed connection");
650 DBG("incorrect message size: %zd", len
);
656 void decrement_sem_count(unsigned int count
)
660 assert(uatomic_read(&sem_count
) >= count
);
662 if (uatomic_read(&sem_count
) <= 0) {
666 ret
= uatomic_add_return(&sem_count
, -count
);
668 ret
= sem_post(&constructor_wait
);
674 int handle_register_done(struct sock_info
*sock_info
)
676 if (sock_info
->registration_done
)
678 sock_info
->registration_done
= 1;
680 decrement_sem_count(1);
681 if (!sock_info
->statedump_pending
) {
682 sock_info
->initial_statedump_done
= 1;
683 decrement_sem_count(1);
690 int handle_register_failed(struct sock_info
*sock_info
)
692 if (sock_info
->registration_done
)
694 sock_info
->registration_done
= 1;
695 sock_info
->initial_statedump_done
= 1;
697 decrement_sem_count(2);
703 * Only execute pending statedump after the constructor semaphore has
704 * been posted by the current listener thread. This means statedump will
705 * only be performed after the "registration done" command is received
706 * from this thread's session daemon.
708 * This ensures we don't run into deadlock issues with the dynamic
709 * loader mutex, which is held while the constructor is called and
710 * waiting on the constructor semaphore. All operations requiring this
711 * dynamic loader lock need to be postponed using this mechanism.
713 * In a scenario with two session daemons connected to the application,
714 * it is possible that the first listener thread which receives the
715 * registration done command issues its statedump while the dynamic
716 * loader lock is still held by the application constructor waiting on
717 * the semaphore. It will however be allowed to proceed when the
718 * second session daemon sends the registration done command to the
719 * second listener thread. This situation therefore does not produce
723 void handle_pending_statedump(struct sock_info
*sock_info
)
725 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
726 sock_info
->statedump_pending
= 0;
727 pthread_mutex_lock(&ust_fork_mutex
);
728 lttng_handle_pending_statedump(sock_info
);
729 pthread_mutex_unlock(&ust_fork_mutex
);
731 if (!sock_info
->initial_statedump_done
) {
732 sock_info
->initial_statedump_done
= 1;
733 decrement_sem_count(1);
739 int handle_message(struct sock_info
*sock_info
,
740 int sock
, struct ustcomm_ust_msg
*lum
)
743 const struct lttng_ust_objd_ops
*ops
;
744 struct ustcomm_ust_reply lur
;
746 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
749 memset(&lur
, 0, sizeof(lur
));
752 ret
= -LTTNG_UST_ERR_EXITING
;
756 ops
= objd_ops(lum
->handle
);
763 case LTTNG_UST_REGISTER_DONE
:
764 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
765 ret
= handle_register_done(sock_info
);
769 case LTTNG_UST_RELEASE
:
770 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
773 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
775 case LTTNG_UST_FILTER
:
777 /* Receive filter data */
778 struct lttng_ust_filter_bytecode_node
*bytecode
;
780 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
781 ERR("Filter data size is too large: %u bytes",
782 lum
->u
.filter
.data_size
);
787 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
788 ERR("Filter reloc offset %u is not within data",
789 lum
->u
.filter
.reloc_offset
);
794 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
799 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
800 lum
->u
.filter
.data_size
);
802 case 0: /* orderly shutdown */
807 if (len
== lum
->u
.filter
.data_size
) {
808 DBG("filter data received");
810 } else if (len
< 0) {
811 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
812 if (len
== -ECONNRESET
) {
813 ERR("%s remote end closed connection", sock_info
->name
);
822 DBG("incorrect filter data message size: %zd", len
);
828 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
829 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
830 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
832 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
833 (unsigned long) bytecode
,
838 /* don't free bytecode if everything went fine. */
845 case LTTNG_UST_EXCLUSION
:
847 /* Receive exclusion names */
848 struct lttng_ust_excluder_node
*node
;
851 count
= lum
->u
.exclusion
.count
;
853 /* There are no names to read */
857 node
= zmalloc(sizeof(*node
) +
858 count
* LTTNG_UST_SYM_NAME_LEN
);
863 node
->excluder
.count
= count
;
864 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
865 count
* LTTNG_UST_SYM_NAME_LEN
);
867 case 0: /* orderly shutdown */
872 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
873 DBG("Exclusion data received");
875 } else if (len
< 0) {
876 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
877 if (len
== -ECONNRESET
) {
878 ERR("%s remote end closed connection", sock_info
->name
);
887 DBG("Incorrect exclusion data message size: %zd", len
);
894 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
895 (unsigned long) node
,
900 /* Don't free exclusion data if everything went fine. */
907 case LTTNG_UST_CHANNEL
:
912 len
= ustcomm_recv_channel_from_sessiond(sock
,
913 &chan_data
, lum
->u
.channel
.len
,
916 case 0: /* orderly shutdown */
920 if (len
== lum
->u
.channel
.len
) {
921 DBG("channel data received");
923 } else if (len
< 0) {
924 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
925 if (len
== -ECONNRESET
) {
926 ERR("%s remote end closed connection", sock_info
->name
);
933 DBG("incorrect channel data message size: %zd", len
);
938 args
.channel
.chan_data
= chan_data
;
939 args
.channel
.wakeup_fd
= wakeup_fd
;
941 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
942 (unsigned long) &lum
->u
,
946 if (args
.channel
.wakeup_fd
>= 0) {
949 lttng_ust_lock_fd_tracker();
950 close_ret
= close(args
.channel
.wakeup_fd
);
951 lttng_ust_unlock_fd_tracker();
952 args
.channel
.wakeup_fd
= -1;
956 free(args
.channel
.chan_data
);
959 case LTTNG_UST_STREAM
:
963 /* Receive shm_fd, wakeup_fd */
964 ret
= ustcomm_recv_stream_from_sessiond(sock
,
967 &args
.stream
.wakeup_fd
);
973 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
974 (unsigned long) &lum
->u
,
978 if (args
.stream
.shm_fd
>= 0) {
979 lttng_ust_lock_fd_tracker();
980 close_ret
= close(args
.stream
.shm_fd
);
981 lttng_ust_unlock_fd_tracker();
982 args
.stream
.shm_fd
= -1;
986 if (args
.stream
.wakeup_fd
>= 0) {
987 lttng_ust_lock_fd_tracker();
988 close_ret
= close(args
.stream
.wakeup_fd
);
989 lttng_ust_unlock_fd_tracker();
990 args
.stream
.wakeup_fd
= -1;
996 case LTTNG_UST_CONTEXT
:
997 switch (lum
->u
.context
.ctx
) {
998 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
1001 size_t ctxlen
, recvlen
;
1003 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
1004 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
1005 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
1006 ERR("Application context string length size is too large: %zu bytes",
1011 strcpy(ctxstr
, "$app.");
1012 p
= &ctxstr
[strlen("$app.")];
1013 recvlen
= ctxlen
- strlen("$app.");
1014 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1016 case 0: /* orderly shutdown */
1020 if (len
== recvlen
) {
1021 DBG("app context data received");
1023 } else if (len
< 0) {
1024 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1025 if (len
== -ECONNRESET
) {
1026 ERR("%s remote end closed connection", sock_info
->name
);
1033 DBG("incorrect app context data message size: %zd", len
);
1038 /* Put : between provider and ctxname. */
1039 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1040 args
.app_context
.ctxname
= ctxstr
;
1047 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1048 (unsigned long) &lum
->u
,
1056 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1057 (unsigned long) &lum
->u
,
1064 lur
.handle
= lum
->handle
;
1068 lur
.ret_code
= LTTNG_UST_OK
;
1071 * Use -LTTNG_UST_ERR as wildcard for UST internal
1072 * error that are not caused by the transport, except if
1073 * we already have a more precise error message to
1076 if (ret
> -LTTNG_UST_ERR
) {
1077 /* Translate code to UST error. */
1080 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1083 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1086 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1089 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1092 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1095 lur
.ret_code
= -LTTNG_UST_ERR
;
1104 case LTTNG_UST_TRACER_VERSION
:
1105 lur
.u
.version
= lum
->u
.version
;
1107 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1108 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1112 DBG("Return value: %d", lur
.ret_val
);
1117 * Performed delayed statedump operations outside of the UST
1118 * lock. We need to take the dynamic loader lock before we take
1119 * the UST lock internally within handle_pending_statedump().
1121 handle_pending_statedump(sock_info
);
1124 ret
= -LTTNG_UST_ERR_EXITING
;
1128 ret
= send_reply(sock
, &lur
);
1130 DBG("error sending reply");
1135 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1138 if (lur
.ret_code
== LTTNG_UST_OK
) {
1140 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1141 len
= ustcomm_send_unix_sock(sock
,
1142 &args
.field_list
.entry
,
1143 sizeof(args
.field_list
.entry
));
1148 if (len
!= sizeof(args
.field_list
.entry
)) {
1162 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1166 if (sock_info
->root_handle
!= -1) {
1167 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1169 ERR("Error unref root handle");
1171 sock_info
->root_handle
= -1;
1176 * wait_shm_mmap, socket and notify socket are used by listener
1177 * threads outside of the ust lock, so we cannot tear them down
1178 * ourselves, because we cannot join on these threads. Leave
1179 * responsibility of cleaning up these resources to the OS
1185 sock_info
->registration_done
= 0;
1186 sock_info
->initial_statedump_done
= 0;
1188 if (sock_info
->socket
!= -1) {
1189 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1191 ERR("Error closing ust cmd socket");
1193 sock_info
->socket
= -1;
1195 if (sock_info
->notify_socket
!= -1) {
1196 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1198 ERR("Error closing ust notify socket");
1200 sock_info
->notify_socket
= -1;
1202 if (sock_info
->wait_shm_mmap
) {
1205 page_size
= sysconf(_SC_PAGE_SIZE
);
1206 if (page_size
<= 0) {
1210 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1212 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1214 ERR("Error unmapping wait shm");
1217 sock_info
->wait_shm_mmap
= NULL
;
1222 * Using fork to set umask in the child process (not multi-thread safe).
1223 * We deal with the shm_open vs ftruncate race (happening when the
1224 * sessiond owns the shm and does not let everybody modify it, to ensure
1225 * safety against shm_unlink) by simply letting the mmap fail and
1226 * retrying after a few seconds.
1227 * For global shm, everybody has rw access to it until the sessiond
1231 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1233 int wait_shm_fd
, ret
;
1237 * Try to open read-only.
1239 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1240 if (wait_shm_fd
>= 0) {
1243 size_t bytes_read
= 0;
1246 * Try to read the fd. If unable to do so, try opening
1250 len
= read(wait_shm_fd
,
1251 &((char *) &tmp_read
)[bytes_read
],
1252 sizeof(tmp_read
) - bytes_read
);
1256 } while ((len
< 0 && errno
== EINTR
)
1257 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1258 if (bytes_read
!= sizeof(tmp_read
)) {
1259 ret
= close(wait_shm_fd
);
1261 ERR("close wait_shm_fd");
1266 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1268 * Real-only open did not work, and it's not because the
1269 * entry was not present. It's a failure that prohibits
1272 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1278 * If the open failed because the file did not exist, or because
1279 * the file was not truncated yet, try creating it ourself.
1281 URCU_TLS(lttng_ust_nest_count
)++;
1283 URCU_TLS(lttng_ust_nest_count
)--;
1288 * Parent: wait for child to return, in which case the
1289 * shared memory map will have been created.
1291 pid
= wait(&status
);
1292 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1297 * Try to open read-only again after creation.
1299 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1300 if (wait_shm_fd
< 0) {
1302 * Real-only open did not work. It's a failure
1303 * that prohibits using shm.
1305 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1309 } else if (pid
== 0) {
1313 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1314 if (sock_info
->global
)
1315 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1317 * We're alone in a child process, so we can modify the
1318 * process-wide umask.
1320 umask(~create_mode
);
1322 * Try creating shm (or get rw access).
1323 * We don't do an exclusive open, because we allow other
1324 * processes to create+ftruncate it concurrently.
1326 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1327 O_RDWR
| O_CREAT
, create_mode
);
1328 if (wait_shm_fd
>= 0) {
1329 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1331 PERROR("ftruncate");
1332 _exit(EXIT_FAILURE
);
1334 _exit(EXIT_SUCCESS
);
1337 * For local shm, we need to have rw access to accept
1338 * opening it: this means the local sessiond will be
1339 * able to wake us up. For global shm, we open it even
1340 * if rw access is not granted, because the root.root
1341 * sessiond will be able to override all rights and wake
1344 if (!sock_info
->global
&& errno
!= EACCES
) {
1345 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1346 _exit(EXIT_FAILURE
);
1349 * The shm exists, but we cannot open it RW. Report
1352 _exit(EXIT_SUCCESS
);
1357 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1358 struct stat statbuf
;
1361 * Ensure that our user is the owner of the shm file for
1362 * local shm. If we do not own the file, it means our
1363 * sessiond will not have access to wake us up (there is
1364 * probably a rogue process trying to fake our
1365 * sessiond). Fallback to polling method in this case.
1367 ret
= fstat(wait_shm_fd
, &statbuf
);
1372 if (statbuf
.st_uid
!= getuid())
1378 ret
= close(wait_shm_fd
);
1380 PERROR("Error closing fd");
1386 char *get_map_shm(struct sock_info
*sock_info
)
1389 int wait_shm_fd
, ret
;
1390 char *wait_shm_mmap
;
1392 page_size
= sysconf(_SC_PAGE_SIZE
);
1393 if (page_size
<= 0) {
1397 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1401 lttng_ust_lock_fd_tracker();
1402 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1403 if (wait_shm_fd
< 0) {
1404 lttng_ust_unlock_fd_tracker();
1408 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1410 ret
= close(wait_shm_fd
);
1412 PERROR("Error closing fd");
1414 lttng_ust_unlock_fd_tracker();
1419 lttng_ust_unlock_fd_tracker();
1421 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1422 MAP_SHARED
, wait_shm_fd
, 0);
1424 /* close shm fd immediately after taking the mmap reference */
1425 lttng_ust_lock_fd_tracker();
1426 ret
= close(wait_shm_fd
);
1428 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1430 PERROR("Error closing fd");
1432 lttng_ust_unlock_fd_tracker();
1434 if (wait_shm_mmap
== MAP_FAILED
) {
1435 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1438 return wait_shm_mmap
;
1445 void wait_for_sessiond(struct sock_info
*sock_info
)
1447 /* Use ust_lock to check if we should quit. */
1451 if (wait_poll_fallback
) {
1456 assert(sock_info
->wait_shm_mmap
);
1458 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1459 /* Wait for futex wakeup */
1460 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1463 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1464 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1467 /* Value already changed. */
1470 /* Retry if interrupted by signal. */
1471 break; /* Get out of switch. */
1473 wait_poll_fallback
= 1;
1475 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1476 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1477 "Please upgrade your kernel "
1478 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1479 "mainline). LTTng-UST will use polling mode fallback.");
1498 * This thread does not allocate any resource, except within
1499 * handle_message, within mutex protection. This mutex protects against
1501 * The other moment it allocates resources is at socket connection, which
1502 * is also protected by the mutex.
1505 void *ust_listener_thread(void *arg
)
1507 struct sock_info
*sock_info
= arg
;
1508 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1511 lttng_ust_fixup_tls();
1513 * If available, add '-ust' to the end of this thread's
1516 ret
= lttng_ust_setustprocname();
1518 ERR("Unable to set UST process name");
1521 /* Restart trying to connect to the session daemon */
1523 if (prev_connect_failed
) {
1524 /* Wait for sessiond availability with pipe */
1525 wait_for_sessiond(sock_info
);
1529 * Sleep for 5 seconds before retrying after a
1530 * sequence of failure / wait / failure. This
1531 * deals with a killed or broken session daemon.
1537 prev_connect_failed
= 0;
1544 if (sock_info
->socket
!= -1) {
1545 /* FD tracker is updated by ustcomm_close_unix_sock() */
1546 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1548 ERR("Error closing %s ust cmd socket",
1551 sock_info
->socket
= -1;
1553 if (sock_info
->notify_socket
!= -1) {
1554 /* FD tracker is updated by ustcomm_close_unix_sock() */
1555 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1557 ERR("Error closing %s ust notify socket",
1560 sock_info
->notify_socket
= -1;
1565 * Register. We need to perform both connect and sending
1566 * registration message before doing the next connect otherwise
1567 * we may reach unix socket connect queue max limits and block
1568 * on the 2nd connect while the session daemon is awaiting the
1569 * first connect registration message.
1571 /* Connect cmd socket */
1572 lttng_ust_lock_fd_tracker();
1573 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1574 get_connect_sock_timeout());
1576 lttng_ust_unlock_fd_tracker();
1577 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1578 prev_connect_failed
= 1;
1581 * If we cannot find the sessiond daemon, don't delay
1582 * constructor execution.
1584 ret
= handle_register_failed(sock_info
);
1590 ret
= lttng_ust_add_fd_to_tracker(fd
);
1594 PERROR("close on sock_info->socket");
1597 lttng_ust_unlock_fd_tracker();
1602 sock_info
->socket
= ret
;
1603 lttng_ust_unlock_fd_tracker();
1607 * Unlock/relock ust lock because connect is blocking (with
1608 * timeout). Don't delay constructors on the ust lock for too
1616 * Create only one root handle per listener thread for the whole
1617 * process lifetime, so we ensure we get ID which is statically
1618 * assigned to the root handle.
1620 if (sock_info
->root_handle
== -1) {
1621 ret
= lttng_abi_create_root_handle();
1623 ERR("Error creating root handle");
1626 sock_info
->root_handle
= ret
;
1629 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1631 ERR("Error registering to %s ust cmd socket",
1633 prev_connect_failed
= 1;
1635 * If we cannot register to the sessiond daemon, don't
1636 * delay constructor execution.
1638 ret
= handle_register_failed(sock_info
);
1646 * Unlock/relock ust lock because connect is blocking (with
1647 * timeout). Don't delay constructors on the ust lock for too
1654 /* Connect notify socket */
1655 lttng_ust_lock_fd_tracker();
1656 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1657 get_connect_sock_timeout());
1659 lttng_ust_unlock_fd_tracker();
1660 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1661 prev_connect_failed
= 1;
1664 * If we cannot find the sessiond daemon, don't delay
1665 * constructor execution.
1667 ret
= handle_register_failed(sock_info
);
1674 ret
= lttng_ust_add_fd_to_tracker(fd
);
1678 PERROR("close on sock_info->notify_socket");
1681 lttng_ust_unlock_fd_tracker();
1686 sock_info
->notify_socket
= ret
;
1687 lttng_ust_unlock_fd_tracker();
1691 * Unlock/relock ust lock because connect is blocking (with
1692 * timeout). Don't delay constructors on the ust lock for too
1699 timeout
= get_notify_sock_timeout();
1702 * Give at least 10ms to sessiond to reply to
1707 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1710 WARN("Error setting socket receive timeout");
1712 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1715 WARN("Error setting socket send timeout");
1717 } else if (timeout
< -1) {
1718 WARN("Unsupported timeout value %ld", timeout
);
1721 ret
= register_to_sessiond(sock_info
->notify_socket
,
1722 USTCTL_SOCKET_NOTIFY
);
1724 ERR("Error registering to %s ust notify socket",
1726 prev_connect_failed
= 1;
1728 * If we cannot register to the sessiond daemon, don't
1729 * delay constructor execution.
1731 ret
= handle_register_failed(sock_info
);
1736 sock
= sock_info
->socket
;
1742 struct ustcomm_ust_msg lum
;
1744 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1746 case 0: /* orderly shutdown */
1747 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1752 * Either sessiond has shutdown or refused us by closing the socket.
1753 * In either case, we don't want to delay construction execution,
1754 * and we need to wait before retry.
1756 prev_connect_failed
= 1;
1758 * If we cannot register to the sessiond daemon, don't
1759 * delay constructor execution.
1761 ret
= handle_register_failed(sock_info
);
1766 print_cmd(lum
.cmd
, lum
.handle
);
1767 ret
= handle_message(sock_info
, sock
, &lum
);
1769 ERR("Error handling message for %s socket",
1772 * Close socket if protocol error is
1780 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1782 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1784 if (len
== -ECONNRESET
) {
1785 DBG("%s remote end closed connection", sock_info
->name
);
1796 /* Cleanup socket handles before trying to reconnect */
1797 lttng_ust_objd_table_owner_cleanup(sock_info
);
1799 goto restart
; /* try to reconnect */
1804 pthread_mutex_lock(&ust_exit_mutex
);
1805 sock_info
->thread_active
= 0;
1806 pthread_mutex_unlock(&ust_exit_mutex
);
1811 * Weak symbol to call when the ust malloc wrapper is not loaded.
1813 __attribute__((weak
))
1814 void lttng_ust_malloc_wrapper_init(void)
1819 * sessiond monitoring thread: monitor presence of global and per-user
1820 * sessiond by polling the application common named pipe.
1822 void __attribute__((constructor
)) lttng_ust_init(void)
1824 struct timespec constructor_timeout
;
1825 sigset_t sig_all_blocked
, orig_parent_mask
;
1826 pthread_attr_t thread_attr
;
1831 if (uatomic_xchg(&initialized
, 1) == 1)
1835 * Fixup interdependency between TLS fixup mutex (which happens
1836 * to be the dynamic linker mutex) and ust_lock, taken within
1839 lttng_ust_fixup_tls();
1841 lttng_ust_loaded
= 1;
1844 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1845 * the unloading of code used by the ust_listener_threads as we can not
1846 * reliably know when they exited. To do that, manually load
1847 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1848 * this library so it never becomes zero, thus never gets unloaded from the
1849 * address space of the process. Since we are already running in the
1850 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1851 * simply increment the refcount and no additionnal work is needed by the
1852 * dynamic loader as the shared library is already loaded in the address
1853 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1854 * unloading of the UST library if its refcount becomes zero (which should
1855 * never happen). Do the return value check but discard the handle at the
1856 * end of the function as it's not needed.
1858 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1860 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1864 * We want precise control over the order in which we construct
1865 * our sub-libraries vs starting to receive commands from
1866 * sessiond (otherwise leading to errors when trying to create
1867 * sessiond before the init functions are completed).
1870 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1872 lttng_ust_init_fd_tracker();
1873 lttng_ust_clock_init();
1874 lttng_ust_getcpu_init();
1875 lttng_ust_statedump_init();
1876 lttng_ring_buffer_metadata_client_init();
1877 lttng_ring_buffer_client_overwrite_init();
1878 lttng_ring_buffer_client_overwrite_rt_init();
1879 lttng_ring_buffer_client_discard_init();
1880 lttng_ring_buffer_client_discard_rt_init();
1881 lttng_perf_counter_init();
1883 * Invoke ust malloc wrapper init before starting other threads.
1885 lttng_ust_malloc_wrapper_init();
1887 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1889 get_allow_blocking();
1891 ret
= sem_init(&constructor_wait
, 0, 0);
1896 ret
= setup_global_apps();
1898 assert(global_apps
.allowed
== 0);
1899 DBG("global apps setup returned %d", ret
);
1902 ret
= setup_local_apps();
1904 assert(local_apps
.allowed
== 0);
1905 DBG("local apps setup returned %d", ret
);
1908 /* A new thread created by pthread_create inherits the signal mask
1909 * from the parent. To avoid any signal being received by the
1910 * listener thread, we block all signals temporarily in the parent,
1911 * while we create the listener thread.
1913 sigfillset(&sig_all_blocked
);
1914 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1916 ERR("pthread_sigmask: %s", strerror(ret
));
1919 ret
= pthread_attr_init(&thread_attr
);
1921 ERR("pthread_attr_init: %s", strerror(ret
));
1923 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1925 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1928 if (global_apps
.allowed
) {
1929 pthread_mutex_lock(&ust_exit_mutex
);
1930 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1931 ust_listener_thread
, &global_apps
);
1933 ERR("pthread_create global: %s", strerror(ret
));
1935 global_apps
.thread_active
= 1;
1936 pthread_mutex_unlock(&ust_exit_mutex
);
1938 handle_register_done(&global_apps
);
1941 if (local_apps
.allowed
) {
1942 pthread_mutex_lock(&ust_exit_mutex
);
1943 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1944 ust_listener_thread
, &local_apps
);
1946 ERR("pthread_create local: %s", strerror(ret
));
1948 local_apps
.thread_active
= 1;
1949 pthread_mutex_unlock(&ust_exit_mutex
);
1951 handle_register_done(&local_apps
);
1953 ret
= pthread_attr_destroy(&thread_attr
);
1955 ERR("pthread_attr_destroy: %s", strerror(ret
));
1958 /* Restore original signal mask in parent */
1959 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1961 ERR("pthread_sigmask: %s", strerror(ret
));
1964 switch (timeout_mode
) {
1965 case 1: /* timeout wait */
1967 ret
= sem_timedwait(&constructor_wait
,
1968 &constructor_timeout
);
1969 } while (ret
< 0 && errno
== EINTR
);
1973 ERR("Timed out waiting for lttng-sessiond");
1976 PERROR("sem_timedwait");
1979 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1984 case -1:/* wait forever */
1986 ret
= sem_wait(&constructor_wait
);
1987 } while (ret
< 0 && errno
== EINTR
);
1994 ERR("Unexpected error \"%s\" returned by sem_wait",
1999 case 0: /* no timeout */
2005 void lttng_ust_cleanup(int exiting
)
2007 cleanup_sock_info(&global_apps
, exiting
);
2008 cleanup_sock_info(&local_apps
, exiting
);
2009 local_apps
.allowed
= 0;
2010 global_apps
.allowed
= 0;
2012 * The teardown in this function all affect data structures
2013 * accessed under the UST lock by the listener thread. This
2014 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2015 * that none of these threads are accessing this data at this
2018 lttng_ust_abi_exit();
2019 lttng_ust_events_exit();
2020 lttng_perf_counter_exit();
2021 lttng_ring_buffer_client_discard_rt_exit();
2022 lttng_ring_buffer_client_discard_exit();
2023 lttng_ring_buffer_client_overwrite_rt_exit();
2024 lttng_ring_buffer_client_overwrite_exit();
2025 lttng_ring_buffer_metadata_client_exit();
2026 lttng_ust_statedump_destroy();
2029 /* Reinitialize values for fork */
2030 sem_count
= sem_count_initial_value
;
2031 lttng_ust_comm_should_quit
= 0;
2036 void __attribute__((destructor
)) lttng_ust_exit(void)
2041 * Using pthread_cancel here because:
2042 * A) we don't want to hang application teardown.
2043 * B) the thread is not allocating any resource.
2047 * Require the communication thread to quit. Synchronize with
2048 * mutexes to ensure it is not in a mutex critical section when
2049 * pthread_cancel is later called.
2052 lttng_ust_comm_should_quit
= 1;
2055 pthread_mutex_lock(&ust_exit_mutex
);
2056 /* cancel threads */
2057 if (global_apps
.thread_active
) {
2058 ret
= pthread_cancel(global_apps
.ust_listener
);
2060 ERR("Error cancelling global ust listener thread: %s",
2063 global_apps
.thread_active
= 0;
2066 if (local_apps
.thread_active
) {
2067 ret
= pthread_cancel(local_apps
.ust_listener
);
2069 ERR("Error cancelling local ust listener thread: %s",
2072 local_apps
.thread_active
= 0;
2075 pthread_mutex_unlock(&ust_exit_mutex
);
2078 * Do NOT join threads: use of sys_futex makes it impossible to
2079 * join the threads without using async-cancel, but async-cancel
2080 * is delivered by a signal, which could hit the target thread
2081 * anywhere in its code path, including while the ust_lock() is
2082 * held, causing a deadlock for the other thread. Let the OS
2083 * cleanup the threads if there are stalled in a syscall.
2085 lttng_ust_cleanup(1);
2089 void ust_context_ns_reset(void)
2091 lttng_context_pid_ns_reset();
2092 lttng_context_cgroup_ns_reset();
2093 lttng_context_ipc_ns_reset();
2094 lttng_context_mnt_ns_reset();
2095 lttng_context_net_ns_reset();
2096 lttng_context_user_ns_reset();
2097 lttng_context_uts_ns_reset();
2101 void ust_context_vuids_reset(void)
2103 lttng_context_vuid_reset();
2104 lttng_context_veuid_reset();
2105 lttng_context_vsuid_reset();
2109 void ust_context_vgids_reset(void)
2111 lttng_context_vgid_reset();
2112 lttng_context_vegid_reset();
2113 lttng_context_vsgid_reset();
2117 * We exclude the worker threads across fork and clone (except
2118 * CLONE_VM), because these system calls only keep the forking thread
2119 * running in the child. Therefore, we don't want to call fork or clone
2120 * in the middle of an tracepoint or ust tracing state modification.
2121 * Holding this mutex protects these structures across fork and clone.
2123 void ust_before_fork(sigset_t
*save_sigset
)
2126 * Disable signals. This is to avoid that the child intervenes
2127 * before it is properly setup for tracing. It is safer to
2128 * disable all signals, because then we know we are not breaking
2129 * anything by restoring the original mask.
2134 /* Fixup lttng-ust TLS. */
2135 lttng_ust_fixup_tls();
2137 if (URCU_TLS(lttng_ust_nest_count
))
2139 /* Disable signals */
2140 sigfillset(&all_sigs
);
2141 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2143 PERROR("sigprocmask");
2146 pthread_mutex_lock(&ust_fork_mutex
);
2149 urcu_bp_before_fork();
2150 lttng_ust_lock_fd_tracker();
2154 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2158 DBG("process %d", getpid());
2159 lttng_perf_unlock();
2160 lttng_ust_unlock_fd_tracker();
2163 pthread_mutex_unlock(&ust_fork_mutex
);
2165 /* Restore signals */
2166 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2168 PERROR("sigprocmask");
2172 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2174 if (URCU_TLS(lttng_ust_nest_count
))
2176 DBG("process %d", getpid());
2177 urcu_bp_after_fork_parent();
2178 /* Release mutexes and reenable signals */
2179 ust_after_fork_common(restore_sigset
);
2183 * After fork, in the child, we need to cleanup all the leftover state,
2184 * except the worker thread which already magically disappeared thanks
2185 * to the weird Linux fork semantics. After tyding up, we call
2186 * lttng_ust_init() again to start over as a new PID.
2188 * This is meant for forks() that have tracing in the child between the
2189 * fork and following exec call (if there is any).
2191 void ust_after_fork_child(sigset_t
*restore_sigset
)
2193 if (URCU_TLS(lttng_ust_nest_count
))
2195 lttng_context_vpid_reset();
2196 lttng_context_vtid_reset();
2197 lttng_context_procname_reset();
2198 ust_context_ns_reset();
2199 ust_context_vuids_reset();
2200 ust_context_vgids_reset();
2201 DBG("process %d", getpid());
2202 /* Release urcu mutexes */
2203 urcu_bp_after_fork_child();
2204 lttng_ust_cleanup(0);
2205 /* Release mutexes and reenable signals */
2206 ust_after_fork_common(restore_sigset
);
2210 void ust_after_setns(void)
2212 ust_context_ns_reset();
2213 ust_context_vuids_reset();
2214 ust_context_vgids_reset();
2217 void ust_after_unshare(void)
2219 ust_context_ns_reset();
2220 ust_context_vuids_reset();
2221 ust_context_vgids_reset();
2224 void ust_after_setuid(void)
2226 ust_context_vuids_reset();
2229 void ust_after_seteuid(void)
2231 ust_context_vuids_reset();
2234 void ust_after_setreuid(void)
2236 ust_context_vuids_reset();
2239 void ust_after_setresuid(void)
2241 ust_context_vuids_reset();
2244 void ust_after_setgid(void)
2246 ust_context_vgids_reset();
2249 void ust_after_setegid(void)
2251 ust_context_vgids_reset();
2254 void ust_after_setregid(void)
2256 ust_context_vgids_reset();
2259 void ust_after_setresgid(void)
2261 ust_context_vgids_reset();
2264 void lttng_ust_sockinfo_session_enabled(void *owner
)
2266 struct sock_info
*sock_info
= owner
;
2267 sock_info
->statedump_pending
= 1;