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"
65 /* Concatenate lttng ust shared library name with its major version number. */
66 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
69 * Has lttng ust comm constructor been called ?
71 static int initialized
;
74 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
75 * Held when handling a command, also held by fork() to deal with
76 * removal of threads, and by exit path.
78 * The UST lock is the centralized mutex across UST tracing control and
81 * ust_exit_mutex must never nest in ust_mutex.
83 * ust_fork_mutex must never nest in ust_mutex.
85 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
86 * counter lazy initialization called by events within the statedump,
87 * which traces while the ust_mutex is held.
89 * ust_lock nests within the dynamic loader lock (within glibc) because
90 * it is taken within the library constructor.
92 * The ust fd tracker lock nests within the ust_mutex.
94 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
96 /* Allow nesting the ust_mutex within the same thread. */
97 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
100 * ust_exit_mutex protects thread_active variable wrt thread exit. It
101 * cannot be done by ust_mutex because pthread_cancel(), which takes an
102 * internal libc lock, cannot nest within ust_mutex.
104 * It never nests within a ust_mutex.
106 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 * ust_fork_mutex protects base address statedump tracing against forks. It
110 * prevents the dynamic loader lock to be taken (by base address statedump
111 * tracing) while a fork is happening, thus preventing deadlock issues with
112 * the dynamic loader lock.
114 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
116 /* Should the ust comm thread quit ? */
117 static int lttng_ust_comm_should_quit
;
120 * This variable can be tested by applications to check whether
121 * lttng-ust is loaded. They simply have to define their own
122 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
123 * library constructor.
125 int lttng_ust_loaded
__attribute__((weak
));
128 * Return 0 on success, -1 if should quit.
129 * The lock is taken in both cases.
134 sigset_t sig_all_blocked
, orig_mask
;
137 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
139 ERR("pthread_setcancelstate: %s", strerror(ret
));
141 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
142 ERR("pthread_setcancelstate: unexpected oldstate");
144 sigfillset(&sig_all_blocked
);
145 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
147 ERR("pthread_sigmask: %s", strerror(ret
));
149 if (!URCU_TLS(ust_mutex_nest
)++)
150 pthread_mutex_lock(&ust_mutex
);
151 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
153 ERR("pthread_sigmask: %s", strerror(ret
));
155 if (lttng_ust_comm_should_quit
) {
163 * ust_lock_nocheck() can be used in constructors/destructors, because
164 * they are already nested within the dynamic loader lock, and therefore
165 * have exclusive access against execution of liblttng-ust destructor.
168 void ust_lock_nocheck(void)
170 sigset_t sig_all_blocked
, orig_mask
;
173 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
175 ERR("pthread_setcancelstate: %s", strerror(ret
));
177 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
178 ERR("pthread_setcancelstate: unexpected oldstate");
180 sigfillset(&sig_all_blocked
);
181 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
183 ERR("pthread_sigmask: %s", strerror(ret
));
185 if (!URCU_TLS(ust_mutex_nest
)++)
186 pthread_mutex_lock(&ust_mutex
);
187 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
189 ERR("pthread_sigmask: %s", strerror(ret
));
196 void ust_unlock(void)
198 sigset_t sig_all_blocked
, orig_mask
;
201 sigfillset(&sig_all_blocked
);
202 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
204 ERR("pthread_sigmask: %s", strerror(ret
));
206 if (!--URCU_TLS(ust_mutex_nest
))
207 pthread_mutex_unlock(&ust_mutex
);
208 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
210 ERR("pthread_sigmask: %s", strerror(ret
));
212 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
214 ERR("pthread_setcancelstate: %s", strerror(ret
));
216 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
217 ERR("pthread_setcancelstate: unexpected oldstate");
222 * Wait for either of these before continuing to the main
224 * - the register_done message from sessiond daemon
225 * (will let the sessiond daemon enable sessions before main
227 * - sessiond daemon is not reachable.
228 * - timeout (ensuring applications are resilient to session
231 static sem_t constructor_wait
;
233 * Doing this for both the global and local sessiond.
236 sem_count_initial_value
= 4,
239 static int sem_count
= sem_count_initial_value
;
242 * Counting nesting within lttng-ust. Used to ensure that calling fork()
243 * from liblttng-ust does not execute the pre/post fork handlers.
245 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
248 * Info about socket and associated listener thread.
252 pthread_t ust_listener
; /* listener thread */
254 int registration_done
;
259 char sock_path
[PATH_MAX
];
263 char wait_shm_path
[PATH_MAX
];
265 /* Keep track of lazy state dump not performed yet. */
266 int statedump_pending
;
267 int initial_statedump_done
;
268 /* Keep procname for statedump */
269 char procname
[LTTNG_UST_ABI_PROCNAME_LEN
];
272 /* Socket from app (connect) to session daemon (listen) for communication */
273 struct sock_info global_apps
= {
278 .registration_done
= 0,
282 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
286 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
288 .statedump_pending
= 0,
289 .initial_statedump_done
= 0,
293 /* TODO: allow global_apps_sock_path override */
295 struct sock_info local_apps
= {
299 .registration_done
= 0,
300 .allowed
= 0, /* Check setuid bit first */
306 .statedump_pending
= 0,
307 .initial_statedump_done
= 0,
311 static int wait_poll_fallback
;
313 static const char *cmd_name_mapping
[] = {
314 [ LTTNG_UST_RELEASE
] = "Release",
315 [ LTTNG_UST_SESSION
] = "Create Session",
316 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
318 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
319 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
320 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
321 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
323 /* Session FD commands */
324 [ LTTNG_UST_CHANNEL
] = "Create Channel",
325 [ LTTNG_UST_SESSION_START
] = "Start Session",
326 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
328 /* Channel FD commands */
329 [ LTTNG_UST_STREAM
] = "Create Stream",
330 [ LTTNG_UST_EVENT
] = "Create Event",
332 /* Event and Channel FD commands */
333 [ LTTNG_UST_CONTEXT
] = "Create Context",
334 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
336 /* Event, Channel and Session commands */
337 [ LTTNG_UST_ENABLE
] = "Enable",
338 [ LTTNG_UST_DISABLE
] = "Disable",
340 /* Tracepoint list commands */
341 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
342 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
344 /* Event FD commands */
345 [ LTTNG_UST_FILTER
] = "Create Filter",
346 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
349 static const char *str_timeout
;
350 static int got_timeout_env
;
352 extern void lttng_ring_buffer_client_overwrite_init(void);
353 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
354 extern void lttng_ring_buffer_client_discard_init(void);
355 extern void lttng_ring_buffer_client_discard_rt_init(void);
356 extern void lttng_ring_buffer_metadata_client_init(void);
357 extern void lttng_ring_buffer_client_overwrite_exit(void);
358 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
359 extern void lttng_ring_buffer_client_discard_exit(void);
360 extern void lttng_ring_buffer_client_discard_rt_exit(void);
361 extern void lttng_ring_buffer_metadata_client_exit(void);
363 static char *get_map_shm(struct sock_info
*sock_info
);
365 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
368 size_t copied
= 0, to_copy
= len
;
371 ret
= read(fd
, buf
+ copied
, to_copy
);
376 } while ((ret
> 0 && to_copy
> 0)
377 || (ret
< 0 && errno
== EINTR
));
384 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
388 const char *get_lttng_home_dir(void)
392 val
= (const char *) lttng_getenv("LTTNG_HOME");
396 return (const char *) lttng_getenv("HOME");
400 * Force a read (imply TLS fixup for dlopen) of TLS variables.
403 void lttng_fixup_nest_count_tls(void)
405 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
409 void lttng_fixup_ust_mutex_nest_tls(void)
411 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
418 void lttng_fixup_urcu_bp_tls(void)
424 void lttng_ust_fixup_tls(void)
426 lttng_fixup_urcu_bp_tls();
427 lttng_fixup_ringbuffer_tls();
428 lttng_fixup_vtid_tls();
429 lttng_fixup_nest_count_tls();
430 lttng_fixup_procname_tls();
431 lttng_fixup_ust_mutex_nest_tls();
432 lttng_ust_fixup_perf_counter_tls();
433 lttng_ust_fixup_fd_tracker_tls();
434 lttng_fixup_cgroup_ns_tls();
435 lttng_fixup_ipc_ns_tls();
436 lttng_fixup_net_ns_tls();
437 lttng_fixup_time_ns_tls();
438 lttng_fixup_uts_ns_tls();
441 int lttng_get_notify_socket(void *owner
)
443 struct sock_info
*info
= owner
;
445 return info
->notify_socket
;
450 char* lttng_ust_sockinfo_get_procname(void *owner
)
452 struct sock_info
*info
= owner
;
454 return info
->procname
;
458 void print_cmd(int cmd
, int handle
)
460 const char *cmd_name
= "Unknown";
462 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
463 && cmd_name_mapping
[cmd
]) {
464 cmd_name
= cmd_name_mapping
[cmd
];
466 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
468 lttng_ust_obj_get_name(handle
), handle
);
472 int setup_global_apps(void)
475 assert(!global_apps
.wait_shm_mmap
);
477 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
478 if (!global_apps
.wait_shm_mmap
) {
479 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
480 global_apps
.allowed
= 0;
485 global_apps
.allowed
= 1;
486 lttng_pthread_getname_np(global_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
491 int setup_local_apps(void)
494 const char *home_dir
;
497 assert(!local_apps
.wait_shm_mmap
);
501 * Disallow per-user tracing for setuid binaries.
503 if (uid
!= geteuid()) {
504 assert(local_apps
.allowed
== 0);
508 home_dir
= get_lttng_home_dir();
510 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
511 assert(local_apps
.allowed
== 0);
515 local_apps
.allowed
= 1;
516 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
518 LTTNG_DEFAULT_HOME_RUNDIR
,
519 LTTNG_UST_SOCK_FILENAME
);
520 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
521 LTTNG_UST_WAIT_FILENAME
,
524 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
525 if (!local_apps
.wait_shm_mmap
) {
526 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
527 local_apps
.allowed
= 0;
532 lttng_pthread_getname_np(local_apps
.procname
, LTTNG_UST_ABI_PROCNAME_LEN
);
538 * Get socket timeout, in ms.
539 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
542 long get_timeout(void)
544 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
546 if (!got_timeout_env
) {
547 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
551 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
552 /* All negative values are considered as "-1". */
553 if (constructor_delay_ms
< -1)
554 constructor_delay_ms
= -1;
555 return constructor_delay_ms
;
558 /* Timeout for notify socket send and recv. */
560 long get_notify_sock_timeout(void)
562 return get_timeout();
565 /* Timeout for connecting to cmd and notify sockets. */
567 long get_connect_sock_timeout(void)
569 return get_timeout();
573 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
576 int get_constructor_timeout(struct timespec
*constructor_timeout
)
578 long constructor_delay_ms
;
581 constructor_delay_ms
= get_timeout();
583 switch (constructor_delay_ms
) {
584 case -1:/* fall-through */
586 return constructor_delay_ms
;
592 * If we are unable to find the current time, don't wait.
594 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
599 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
600 constructor_timeout
->tv_nsec
+=
601 (constructor_delay_ms
% 1000UL) * 1000000UL;
602 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
603 constructor_timeout
->tv_sec
++;
604 constructor_timeout
->tv_nsec
-= 1000000000UL;
606 /* Timeout wait (constructor_delay_ms). */
611 void get_allow_blocking(void)
613 const char *str_allow_blocking
=
614 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
616 if (str_allow_blocking
) {
617 DBG("%s environment variable is set",
618 "LTTNG_UST_ALLOW_BLOCKING");
619 lttng_ust_ringbuffer_set_allow_blocking();
624 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
626 return ustcomm_send_reg_msg(socket
,
629 lttng_alignof(uint8_t) * CHAR_BIT
,
630 lttng_alignof(uint16_t) * CHAR_BIT
,
631 lttng_alignof(uint32_t) * CHAR_BIT
,
632 lttng_alignof(uint64_t) * CHAR_BIT
,
633 lttng_alignof(unsigned long) * CHAR_BIT
);
637 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
641 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
644 DBG("message successfully sent");
647 if (len
== -ECONNRESET
) {
648 DBG("remote end closed connection");
653 DBG("incorrect message size: %zd", len
);
659 void decrement_sem_count(unsigned int count
)
663 assert(uatomic_read(&sem_count
) >= count
);
665 if (uatomic_read(&sem_count
) <= 0) {
669 ret
= uatomic_add_return(&sem_count
, -count
);
671 ret
= sem_post(&constructor_wait
);
677 int handle_register_done(struct sock_info
*sock_info
)
679 if (sock_info
->registration_done
)
681 sock_info
->registration_done
= 1;
683 decrement_sem_count(1);
684 if (!sock_info
->statedump_pending
) {
685 sock_info
->initial_statedump_done
= 1;
686 decrement_sem_count(1);
693 int handle_register_failed(struct sock_info
*sock_info
)
695 if (sock_info
->registration_done
)
697 sock_info
->registration_done
= 1;
698 sock_info
->initial_statedump_done
= 1;
700 decrement_sem_count(2);
706 * Only execute pending statedump after the constructor semaphore has
707 * been posted by the current listener thread. This means statedump will
708 * only be performed after the "registration done" command is received
709 * from this thread's session daemon.
711 * This ensures we don't run into deadlock issues with the dynamic
712 * loader mutex, which is held while the constructor is called and
713 * waiting on the constructor semaphore. All operations requiring this
714 * dynamic loader lock need to be postponed using this mechanism.
716 * In a scenario with two session daemons connected to the application,
717 * it is possible that the first listener thread which receives the
718 * registration done command issues its statedump while the dynamic
719 * loader lock is still held by the application constructor waiting on
720 * the semaphore. It will however be allowed to proceed when the
721 * second session daemon sends the registration done command to the
722 * second listener thread. This situation therefore does not produce
726 void handle_pending_statedump(struct sock_info
*sock_info
)
728 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
729 sock_info
->statedump_pending
= 0;
730 pthread_mutex_lock(&ust_fork_mutex
);
731 lttng_handle_pending_statedump(sock_info
);
732 pthread_mutex_unlock(&ust_fork_mutex
);
734 if (!sock_info
->initial_statedump_done
) {
735 sock_info
->initial_statedump_done
= 1;
736 decrement_sem_count(1);
742 int handle_message(struct sock_info
*sock_info
,
743 int sock
, struct ustcomm_ust_msg
*lum
)
746 const struct lttng_ust_objd_ops
*ops
;
747 struct ustcomm_ust_reply lur
;
749 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
752 memset(&lur
, 0, sizeof(lur
));
755 ret
= -LTTNG_UST_ERR_EXITING
;
759 ops
= objd_ops(lum
->handle
);
766 case LTTNG_UST_REGISTER_DONE
:
767 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
768 ret
= handle_register_done(sock_info
);
772 case LTTNG_UST_RELEASE
:
773 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
776 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
778 case LTTNG_UST_FILTER
:
780 /* Receive filter data */
781 struct lttng_ust_filter_bytecode_node
*bytecode
;
783 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
784 ERR("Filter data size is too large: %u bytes",
785 lum
->u
.filter
.data_size
);
790 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
791 ERR("Filter reloc offset %u is not within data",
792 lum
->u
.filter
.reloc_offset
);
797 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
802 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
803 lum
->u
.filter
.data_size
);
805 case 0: /* orderly shutdown */
810 if (len
== lum
->u
.filter
.data_size
) {
811 DBG("filter data received");
813 } else if (len
< 0) {
814 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
815 if (len
== -ECONNRESET
) {
816 ERR("%s remote end closed connection", sock_info
->name
);
825 DBG("incorrect filter data message size: %zd", len
);
831 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
832 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
833 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
835 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
836 (unsigned long) bytecode
,
841 /* don't free bytecode if everything went fine. */
848 case LTTNG_UST_EXCLUSION
:
850 /* Receive exclusion names */
851 struct lttng_ust_excluder_node
*node
;
854 count
= lum
->u
.exclusion
.count
;
856 /* There are no names to read */
860 node
= zmalloc(sizeof(*node
) +
861 count
* LTTNG_UST_SYM_NAME_LEN
);
866 node
->excluder
.count
= count
;
867 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
868 count
* LTTNG_UST_SYM_NAME_LEN
);
870 case 0: /* orderly shutdown */
875 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
876 DBG("Exclusion data received");
878 } else if (len
< 0) {
879 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
880 if (len
== -ECONNRESET
) {
881 ERR("%s remote end closed connection", sock_info
->name
);
890 DBG("Incorrect exclusion data message size: %zd", len
);
897 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
898 (unsigned long) node
,
903 /* Don't free exclusion data if everything went fine. */
910 case LTTNG_UST_CHANNEL
:
915 len
= ustcomm_recv_channel_from_sessiond(sock
,
916 &chan_data
, lum
->u
.channel
.len
,
919 case 0: /* orderly shutdown */
923 if (len
== lum
->u
.channel
.len
) {
924 DBG("channel data received");
926 } else if (len
< 0) {
927 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
928 if (len
== -ECONNRESET
) {
929 ERR("%s remote end closed connection", sock_info
->name
);
936 DBG("incorrect channel data message size: %zd", len
);
941 args
.channel
.chan_data
= chan_data
;
942 args
.channel
.wakeup_fd
= wakeup_fd
;
944 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
945 (unsigned long) &lum
->u
,
951 case LTTNG_UST_STREAM
:
953 /* Receive shm_fd, wakeup_fd */
954 ret
= ustcomm_recv_stream_from_sessiond(sock
,
957 &args
.stream
.wakeup_fd
);
963 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
964 (unsigned long) &lum
->u
,
970 case LTTNG_UST_CONTEXT
:
971 switch (lum
->u
.context
.ctx
) {
972 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
975 size_t ctxlen
, recvlen
;
977 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
978 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
979 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
980 ERR("Application context string length size is too large: %zu bytes",
985 strcpy(ctxstr
, "$app.");
986 p
= &ctxstr
[strlen("$app.")];
987 recvlen
= ctxlen
- strlen("$app.");
988 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
990 case 0: /* orderly shutdown */
994 if (len
== recvlen
) {
995 DBG("app context data received");
997 } else if (len
< 0) {
998 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
999 if (len
== -ECONNRESET
) {
1000 ERR("%s remote end closed connection", sock_info
->name
);
1007 DBG("incorrect app context data message size: %zd", len
);
1012 /* Put : between provider and ctxname. */
1013 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1014 args
.app_context
.ctxname
= ctxstr
;
1021 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1022 (unsigned long) &lum
->u
,
1030 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1031 (unsigned long) &lum
->u
,
1038 lur
.handle
= lum
->handle
;
1042 lur
.ret_code
= LTTNG_UST_OK
;
1045 * Use -LTTNG_UST_ERR as wildcard for UST internal
1046 * error that are not caused by the transport, except if
1047 * we already have a more precise error message to
1050 if (ret
> -LTTNG_UST_ERR
) {
1051 /* Translate code to UST error. */
1054 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1057 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1060 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1063 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1066 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1069 lur
.ret_code
= -LTTNG_UST_ERR
;
1078 case LTTNG_UST_TRACER_VERSION
:
1079 lur
.u
.version
= lum
->u
.version
;
1081 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1082 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1086 DBG("Return value: %d", lur
.ret_val
);
1091 * Performed delayed statedump operations outside of the UST
1092 * lock. We need to take the dynamic loader lock before we take
1093 * the UST lock internally within handle_pending_statedump().
1095 handle_pending_statedump(sock_info
);
1098 ret
= -LTTNG_UST_ERR_EXITING
;
1102 ret
= send_reply(sock
, &lur
);
1104 DBG("error sending reply");
1109 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1112 if (lur
.ret_code
== LTTNG_UST_OK
) {
1114 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1115 len
= ustcomm_send_unix_sock(sock
,
1116 &args
.field_list
.entry
,
1117 sizeof(args
.field_list
.entry
));
1122 if (len
!= sizeof(args
.field_list
.entry
)) {
1136 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1140 if (sock_info
->root_handle
!= -1) {
1141 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1143 ERR("Error unref root handle");
1145 sock_info
->root_handle
= -1;
1147 sock_info
->registration_done
= 0;
1148 sock_info
->initial_statedump_done
= 0;
1151 * wait_shm_mmap, socket and notify socket are used by listener
1152 * threads outside of the ust lock, so we cannot tear them down
1153 * ourselves, because we cannot join on these threads. Leave
1154 * responsibility of cleaning up these resources to the OS
1160 if (sock_info
->socket
!= -1) {
1161 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1163 ERR("Error closing ust cmd socket");
1165 sock_info
->socket
= -1;
1167 if (sock_info
->notify_socket
!= -1) {
1168 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1170 ERR("Error closing ust notify socket");
1172 sock_info
->notify_socket
= -1;
1174 if (sock_info
->wait_shm_mmap
) {
1177 page_size
= LTTNG_UST_PAGE_SIZE
;
1178 if (page_size
<= 0) {
1182 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1184 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1186 ERR("Error unmapping wait shm");
1189 sock_info
->wait_shm_mmap
= NULL
;
1194 * Using fork to set umask in the child process (not multi-thread safe).
1195 * We deal with the shm_open vs ftruncate race (happening when the
1196 * sessiond owns the shm and does not let everybody modify it, to ensure
1197 * safety against shm_unlink) by simply letting the mmap fail and
1198 * retrying after a few seconds.
1199 * For global shm, everybody has rw access to it until the sessiond
1203 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1205 int wait_shm_fd
, ret
;
1209 * Try to open read-only.
1211 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1212 if (wait_shm_fd
>= 0) {
1215 size_t bytes_read
= 0;
1218 * Try to read the fd. If unable to do so, try opening
1222 len
= read(wait_shm_fd
,
1223 &((char *) &tmp_read
)[bytes_read
],
1224 sizeof(tmp_read
) - bytes_read
);
1228 } while ((len
< 0 && errno
== EINTR
)
1229 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1230 if (bytes_read
!= sizeof(tmp_read
)) {
1231 ret
= close(wait_shm_fd
);
1233 ERR("close wait_shm_fd");
1238 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1240 * Real-only open did not work, and it's not because the
1241 * entry was not present. It's a failure that prohibits
1244 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1250 * If the open failed because the file did not exist, or because
1251 * the file was not truncated yet, try creating it ourself.
1253 URCU_TLS(lttng_ust_nest_count
)++;
1255 URCU_TLS(lttng_ust_nest_count
)--;
1260 * Parent: wait for child to return, in which case the
1261 * shared memory map will have been created.
1263 pid
= wait(&status
);
1264 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1269 * Try to open read-only again after creation.
1271 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1272 if (wait_shm_fd
< 0) {
1274 * Real-only open did not work. It's a failure
1275 * that prohibits using shm.
1277 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1281 } else if (pid
== 0) {
1285 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1286 if (sock_info
->global
)
1287 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1289 * We're alone in a child process, so we can modify the
1290 * process-wide umask.
1292 umask(~create_mode
);
1294 * Try creating shm (or get rw access).
1295 * We don't do an exclusive open, because we allow other
1296 * processes to create+ftruncate it concurrently.
1298 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1299 O_RDWR
| O_CREAT
, create_mode
);
1300 if (wait_shm_fd
>= 0) {
1301 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1303 PERROR("ftruncate");
1304 _exit(EXIT_FAILURE
);
1306 _exit(EXIT_SUCCESS
);
1309 * For local shm, we need to have rw access to accept
1310 * opening it: this means the local sessiond will be
1311 * able to wake us up. For global shm, we open it even
1312 * if rw access is not granted, because the root.root
1313 * sessiond will be able to override all rights and wake
1316 if (!sock_info
->global
&& errno
!= EACCES
) {
1317 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1318 _exit(EXIT_FAILURE
);
1321 * The shm exists, but we cannot open it RW. Report
1324 _exit(EXIT_SUCCESS
);
1329 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1330 struct stat statbuf
;
1333 * Ensure that our user is the owner of the shm file for
1334 * local shm. If we do not own the file, it means our
1335 * sessiond will not have access to wake us up (there is
1336 * probably a rogue process trying to fake our
1337 * sessiond). Fallback to polling method in this case.
1339 ret
= fstat(wait_shm_fd
, &statbuf
);
1344 if (statbuf
.st_uid
!= getuid())
1350 ret
= close(wait_shm_fd
);
1352 PERROR("Error closing fd");
1358 char *get_map_shm(struct sock_info
*sock_info
)
1361 int wait_shm_fd
, ret
;
1362 char *wait_shm_mmap
;
1364 page_size
= sysconf(_SC_PAGE_SIZE
);
1365 if (page_size
<= 0) {
1369 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1373 lttng_ust_lock_fd_tracker();
1374 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1375 if (wait_shm_fd
< 0) {
1376 lttng_ust_unlock_fd_tracker();
1380 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1382 ret
= close(wait_shm_fd
);
1384 PERROR("Error closing fd");
1386 lttng_ust_unlock_fd_tracker();
1391 lttng_ust_unlock_fd_tracker();
1393 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1394 MAP_SHARED
, wait_shm_fd
, 0);
1396 /* close shm fd immediately after taking the mmap reference */
1397 lttng_ust_lock_fd_tracker();
1398 ret
= close(wait_shm_fd
);
1400 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1402 PERROR("Error closing fd");
1404 lttng_ust_unlock_fd_tracker();
1406 if (wait_shm_mmap
== MAP_FAILED
) {
1407 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1410 return wait_shm_mmap
;
1417 void wait_for_sessiond(struct sock_info
*sock_info
)
1419 /* Use ust_lock to check if we should quit. */
1423 if (wait_poll_fallback
) {
1428 assert(sock_info
->wait_shm_mmap
);
1430 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1431 /* Wait for futex wakeup */
1432 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1435 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1436 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1439 /* Value already changed. */
1442 /* Retry if interrupted by signal. */
1443 break; /* Get out of switch. */
1445 wait_poll_fallback
= 1;
1447 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1448 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1449 "Please upgrade your kernel "
1450 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1451 "mainline). LTTng-UST will use polling mode fallback.");
1470 * This thread does not allocate any resource, except within
1471 * handle_message, within mutex protection. This mutex protects against
1473 * The other moment it allocates resources is at socket connection, which
1474 * is also protected by the mutex.
1477 void *ust_listener_thread(void *arg
)
1479 struct sock_info
*sock_info
= arg
;
1480 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1483 lttng_ust_fixup_tls();
1485 * If available, add '-ust' to the end of this thread's
1488 ret
= lttng_ust_setustprocname();
1490 ERR("Unable to set UST process name");
1493 /* Restart trying to connect to the session daemon */
1495 if (prev_connect_failed
) {
1496 /* Wait for sessiond availability with pipe */
1497 wait_for_sessiond(sock_info
);
1501 * Sleep for 5 seconds before retrying after a
1502 * sequence of failure / wait / failure. This
1503 * deals with a killed or broken session daemon.
1509 prev_connect_failed
= 0;
1516 if (sock_info
->socket
!= -1) {
1517 /* FD tracker is updated by ustcomm_close_unix_sock() */
1518 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1520 ERR("Error closing %s ust cmd socket",
1523 sock_info
->socket
= -1;
1525 if (sock_info
->notify_socket
!= -1) {
1526 /* FD tracker is updated by ustcomm_close_unix_sock() */
1527 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1529 ERR("Error closing %s ust notify socket",
1532 sock_info
->notify_socket
= -1;
1537 * Register. We need to perform both connect and sending
1538 * registration message before doing the next connect otherwise
1539 * we may reach unix socket connect queue max limits and block
1540 * on the 2nd connect while the session daemon is awaiting the
1541 * first connect registration message.
1543 /* Connect cmd socket */
1544 lttng_ust_lock_fd_tracker();
1545 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1546 get_connect_sock_timeout());
1548 lttng_ust_unlock_fd_tracker();
1549 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1550 prev_connect_failed
= 1;
1553 * If we cannot find the sessiond daemon, don't delay
1554 * constructor execution.
1556 ret
= handle_register_failed(sock_info
);
1562 ret
= lttng_ust_add_fd_to_tracker(fd
);
1566 PERROR("close on sock_info->socket");
1569 lttng_ust_unlock_fd_tracker();
1574 sock_info
->socket
= ret
;
1575 lttng_ust_unlock_fd_tracker();
1579 * Unlock/relock ust lock because connect is blocking (with
1580 * timeout). Don't delay constructors on the ust lock for too
1588 * Create only one root handle per listener thread for the whole
1589 * process lifetime, so we ensure we get ID which is statically
1590 * assigned to the root handle.
1592 if (sock_info
->root_handle
== -1) {
1593 ret
= lttng_abi_create_root_handle();
1595 ERR("Error creating root handle");
1598 sock_info
->root_handle
= ret
;
1601 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1603 ERR("Error registering to %s ust cmd socket",
1605 prev_connect_failed
= 1;
1607 * If we cannot register to the sessiond daemon, don't
1608 * delay constructor execution.
1610 ret
= handle_register_failed(sock_info
);
1618 * Unlock/relock ust lock because connect is blocking (with
1619 * timeout). Don't delay constructors on the ust lock for too
1626 /* Connect notify socket */
1627 lttng_ust_lock_fd_tracker();
1628 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1629 get_connect_sock_timeout());
1631 lttng_ust_unlock_fd_tracker();
1632 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1633 prev_connect_failed
= 1;
1636 * If we cannot find the sessiond daemon, don't delay
1637 * constructor execution.
1639 ret
= handle_register_failed(sock_info
);
1646 ret
= lttng_ust_add_fd_to_tracker(fd
);
1650 PERROR("close on sock_info->notify_socket");
1653 lttng_ust_unlock_fd_tracker();
1658 sock_info
->notify_socket
= ret
;
1659 lttng_ust_unlock_fd_tracker();
1663 * Unlock/relock ust lock because connect is blocking (with
1664 * timeout). Don't delay constructors on the ust lock for too
1671 timeout
= get_notify_sock_timeout();
1674 * Give at least 10ms to sessiond to reply to
1679 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1682 WARN("Error setting socket receive timeout");
1684 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1687 WARN("Error setting socket send timeout");
1689 } else if (timeout
< -1) {
1690 WARN("Unsupported timeout value %ld", timeout
);
1693 ret
= register_to_sessiond(sock_info
->notify_socket
,
1694 USTCTL_SOCKET_NOTIFY
);
1696 ERR("Error registering to %s ust notify socket",
1698 prev_connect_failed
= 1;
1700 * If we cannot register to the sessiond daemon, don't
1701 * delay constructor execution.
1703 ret
= handle_register_failed(sock_info
);
1708 sock
= sock_info
->socket
;
1714 struct ustcomm_ust_msg lum
;
1716 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1718 case 0: /* orderly shutdown */
1719 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1724 * Either sessiond has shutdown or refused us by closing the socket.
1725 * In either case, we don't want to delay construction execution,
1726 * and we need to wait before retry.
1728 prev_connect_failed
= 1;
1730 * If we cannot register to the sessiond daemon, don't
1731 * delay constructor execution.
1733 ret
= handle_register_failed(sock_info
);
1738 print_cmd(lum
.cmd
, lum
.handle
);
1739 ret
= handle_message(sock_info
, sock
, &lum
);
1741 ERR("Error handling message for %s socket",
1744 * Close socket if protocol error is
1752 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1754 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1756 if (len
== -ECONNRESET
) {
1757 DBG("%s remote end closed connection", sock_info
->name
);
1768 /* Cleanup socket handles before trying to reconnect */
1769 lttng_ust_objd_table_owner_cleanup(sock_info
);
1771 goto restart
; /* try to reconnect */
1776 pthread_mutex_lock(&ust_exit_mutex
);
1777 sock_info
->thread_active
= 0;
1778 pthread_mutex_unlock(&ust_exit_mutex
);
1783 * Weak symbol to call when the ust malloc wrapper is not loaded.
1785 __attribute__((weak
))
1786 void lttng_ust_malloc_wrapper_init(void)
1791 * sessiond monitoring thread: monitor presence of global and per-user
1792 * sessiond by polling the application common named pipe.
1794 void __attribute__((constructor
)) lttng_ust_init(void)
1796 struct timespec constructor_timeout
;
1797 sigset_t sig_all_blocked
, orig_parent_mask
;
1798 pthread_attr_t thread_attr
;
1803 if (uatomic_xchg(&initialized
, 1) == 1)
1807 * Fixup interdependency between TLS fixup mutex (which happens
1808 * to be the dynamic linker mutex) and ust_lock, taken within
1811 lttng_ust_fixup_tls();
1813 lttng_ust_loaded
= 1;
1816 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1817 * the unloading of code used by the ust_listener_threads as we can not
1818 * reliably know when they exited. To do that, manually load
1819 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1820 * this library so it never becomes zero, thus never gets unloaded from the
1821 * address space of the process. Since we are already running in the
1822 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1823 * simply increment the refcount and no additionnal work is needed by the
1824 * dynamic loader as the shared library is already loaded in the address
1825 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1826 * unloading of the UST library if its refcount becomes zero (which should
1827 * never happen). Do the return value check but discard the handle at the
1828 * end of the function as it's not needed.
1830 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1832 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1836 * We want precise control over the order in which we construct
1837 * our sub-libraries vs starting to receive commands from
1838 * sessiond (otherwise leading to errors when trying to create
1839 * sessiond before the init functions are completed).
1842 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1844 lttng_ust_init_fd_tracker();
1845 lttng_ust_clock_init();
1846 lttng_ust_getcpu_init();
1847 lttng_ust_statedump_init();
1848 lttng_ring_buffer_metadata_client_init();
1849 lttng_ring_buffer_client_overwrite_init();
1850 lttng_ring_buffer_client_overwrite_rt_init();
1851 lttng_ring_buffer_client_discard_init();
1852 lttng_ring_buffer_client_discard_rt_init();
1853 lttng_perf_counter_init();
1855 * Invoke ust malloc wrapper init before starting other threads.
1857 lttng_ust_malloc_wrapper_init();
1859 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1861 get_allow_blocking();
1863 ret
= sem_init(&constructor_wait
, 0, 0);
1868 ret
= setup_global_apps();
1870 assert(global_apps
.allowed
== 0);
1871 DBG("global apps setup returned %d", ret
);
1874 ret
= setup_local_apps();
1876 assert(local_apps
.allowed
== 0);
1877 DBG("local apps setup returned %d", ret
);
1880 /* A new thread created by pthread_create inherits the signal mask
1881 * from the parent. To avoid any signal being received by the
1882 * listener thread, we block all signals temporarily in the parent,
1883 * while we create the listener thread.
1885 sigfillset(&sig_all_blocked
);
1886 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1888 ERR("pthread_sigmask: %s", strerror(ret
));
1891 ret
= pthread_attr_init(&thread_attr
);
1893 ERR("pthread_attr_init: %s", strerror(ret
));
1895 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1897 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1900 if (global_apps
.allowed
) {
1901 pthread_mutex_lock(&ust_exit_mutex
);
1902 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1903 ust_listener_thread
, &global_apps
);
1905 ERR("pthread_create global: %s", strerror(ret
));
1907 global_apps
.thread_active
= 1;
1908 pthread_mutex_unlock(&ust_exit_mutex
);
1910 handle_register_done(&global_apps
);
1913 if (local_apps
.allowed
) {
1914 pthread_mutex_lock(&ust_exit_mutex
);
1915 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1916 ust_listener_thread
, &local_apps
);
1918 ERR("pthread_create local: %s", strerror(ret
));
1920 local_apps
.thread_active
= 1;
1921 pthread_mutex_unlock(&ust_exit_mutex
);
1923 handle_register_done(&local_apps
);
1925 ret
= pthread_attr_destroy(&thread_attr
);
1927 ERR("pthread_attr_destroy: %s", strerror(ret
));
1930 /* Restore original signal mask in parent */
1931 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1933 ERR("pthread_sigmask: %s", strerror(ret
));
1936 switch (timeout_mode
) {
1937 case 1: /* timeout wait */
1939 ret
= sem_timedwait(&constructor_wait
,
1940 &constructor_timeout
);
1941 } while (ret
< 0 && errno
== EINTR
);
1945 ERR("Timed out waiting for lttng-sessiond");
1948 PERROR("sem_timedwait");
1951 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1956 case -1:/* wait forever */
1958 ret
= sem_wait(&constructor_wait
);
1959 } while (ret
< 0 && errno
== EINTR
);
1966 ERR("Unexpected error \"%s\" returned by sem_wait",
1971 case 0: /* no timeout */
1977 void lttng_ust_cleanup(int exiting
)
1979 cleanup_sock_info(&global_apps
, exiting
);
1980 cleanup_sock_info(&local_apps
, exiting
);
1981 local_apps
.allowed
= 0;
1982 global_apps
.allowed
= 0;
1984 * The teardown in this function all affect data structures
1985 * accessed under the UST lock by the listener thread. This
1986 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1987 * that none of these threads are accessing this data at this
1990 lttng_ust_abi_exit();
1991 lttng_ust_events_exit();
1992 lttng_perf_counter_exit();
1993 lttng_ring_buffer_client_discard_rt_exit();
1994 lttng_ring_buffer_client_discard_exit();
1995 lttng_ring_buffer_client_overwrite_rt_exit();
1996 lttng_ring_buffer_client_overwrite_exit();
1997 lttng_ring_buffer_metadata_client_exit();
1998 lttng_ust_statedump_destroy();
2001 /* Reinitialize values for fork */
2002 sem_count
= sem_count_initial_value
;
2003 lttng_ust_comm_should_quit
= 0;
2008 void __attribute__((destructor
)) lttng_ust_exit(void)
2013 * Using pthread_cancel here because:
2014 * A) we don't want to hang application teardown.
2015 * B) the thread is not allocating any resource.
2019 * Require the communication thread to quit. Synchronize with
2020 * mutexes to ensure it is not in a mutex critical section when
2021 * pthread_cancel is later called.
2024 lttng_ust_comm_should_quit
= 1;
2027 pthread_mutex_lock(&ust_exit_mutex
);
2028 /* cancel threads */
2029 if (global_apps
.thread_active
) {
2030 ret
= pthread_cancel(global_apps
.ust_listener
);
2032 ERR("Error cancelling global ust listener thread: %s",
2035 global_apps
.thread_active
= 0;
2038 if (local_apps
.thread_active
) {
2039 ret
= pthread_cancel(local_apps
.ust_listener
);
2041 ERR("Error cancelling local ust listener thread: %s",
2044 local_apps
.thread_active
= 0;
2047 pthread_mutex_unlock(&ust_exit_mutex
);
2050 * Do NOT join threads: use of sys_futex makes it impossible to
2051 * join the threads without using async-cancel, but async-cancel
2052 * is delivered by a signal, which could hit the target thread
2053 * anywhere in its code path, including while the ust_lock() is
2054 * held, causing a deadlock for the other thread. Let the OS
2055 * cleanup the threads if there are stalled in a syscall.
2057 lttng_ust_cleanup(1);
2061 void ust_context_ns_reset(void)
2063 lttng_context_pid_ns_reset();
2064 lttng_context_cgroup_ns_reset();
2065 lttng_context_ipc_ns_reset();
2066 lttng_context_mnt_ns_reset();
2067 lttng_context_net_ns_reset();
2068 lttng_context_user_ns_reset();
2069 lttng_context_time_ns_reset();
2070 lttng_context_uts_ns_reset();
2074 void ust_context_vuids_reset(void)
2076 lttng_context_vuid_reset();
2077 lttng_context_veuid_reset();
2078 lttng_context_vsuid_reset();
2082 void ust_context_vgids_reset(void)
2084 lttng_context_vgid_reset();
2085 lttng_context_vegid_reset();
2086 lttng_context_vsgid_reset();
2090 * We exclude the worker threads across fork and clone (except
2091 * CLONE_VM), because these system calls only keep the forking thread
2092 * running in the child. Therefore, we don't want to call fork or clone
2093 * in the middle of an tracepoint or ust tracing state modification.
2094 * Holding this mutex protects these structures across fork and clone.
2096 void ust_before_fork(sigset_t
*save_sigset
)
2099 * Disable signals. This is to avoid that the child intervenes
2100 * before it is properly setup for tracing. It is safer to
2101 * disable all signals, because then we know we are not breaking
2102 * anything by restoring the original mask.
2107 /* Fixup lttng-ust TLS. */
2108 lttng_ust_fixup_tls();
2110 if (URCU_TLS(lttng_ust_nest_count
))
2112 /* Disable signals */
2113 sigfillset(&all_sigs
);
2114 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2116 PERROR("sigprocmask");
2119 pthread_mutex_lock(&ust_fork_mutex
);
2122 urcu_bp_before_fork();
2123 lttng_ust_lock_fd_tracker();
2127 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2131 DBG("process %d", getpid());
2132 lttng_perf_unlock();
2133 lttng_ust_unlock_fd_tracker();
2136 pthread_mutex_unlock(&ust_fork_mutex
);
2138 /* Restore signals */
2139 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2141 PERROR("sigprocmask");
2145 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2147 if (URCU_TLS(lttng_ust_nest_count
))
2149 DBG("process %d", getpid());
2150 urcu_bp_after_fork_parent();
2151 /* Release mutexes and reenable signals */
2152 ust_after_fork_common(restore_sigset
);
2156 * After fork, in the child, we need to cleanup all the leftover state,
2157 * except the worker thread which already magically disappeared thanks
2158 * to the weird Linux fork semantics. After tyding up, we call
2159 * lttng_ust_init() again to start over as a new PID.
2161 * This is meant for forks() that have tracing in the child between the
2162 * fork and following exec call (if there is any).
2164 void ust_after_fork_child(sigset_t
*restore_sigset
)
2166 if (URCU_TLS(lttng_ust_nest_count
))
2168 lttng_context_vpid_reset();
2169 lttng_context_vtid_reset();
2170 lttng_context_procname_reset();
2171 ust_context_ns_reset();
2172 ust_context_vuids_reset();
2173 ust_context_vgids_reset();
2174 DBG("process %d", getpid());
2175 /* Release urcu mutexes */
2176 urcu_bp_after_fork_child();
2177 lttng_ust_cleanup(0);
2178 /* Release mutexes and reenable signals */
2179 ust_after_fork_common(restore_sigset
);
2183 void ust_after_setns(void)
2185 ust_context_ns_reset();
2186 ust_context_vuids_reset();
2187 ust_context_vgids_reset();
2190 void ust_after_unshare(void)
2192 ust_context_ns_reset();
2193 ust_context_vuids_reset();
2194 ust_context_vgids_reset();
2197 void ust_after_setuid(void)
2199 ust_context_vuids_reset();
2202 void ust_after_seteuid(void)
2204 ust_context_vuids_reset();
2207 void ust_after_setreuid(void)
2209 ust_context_vuids_reset();
2212 void ust_after_setresuid(void)
2214 ust_context_vuids_reset();
2217 void ust_after_setgid(void)
2219 ust_context_vgids_reset();
2222 void ust_after_setegid(void)
2224 ust_context_vgids_reset();
2227 void ust_after_setregid(void)
2229 ust_context_vgids_reset();
2232 void ust_after_setresgid(void)
2234 ust_context_vgids_reset();
2237 void lttng_ust_sockinfo_session_enabled(void *owner
)
2239 struct sock_info
*sock_info
= owner
;
2240 sock_info
->statedump_pending
= 1;