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/ust-events.h>
46 #include <lttng/ust-abi.h>
47 #include <lttng/ust.h>
48 #include <lttng/ust-error.h>
49 #include <lttng/ust-ctl.h>
50 #include <urcu/tls-compat.h>
53 #include <usterr-signal-safe.h>
55 #include "tracepoint-internal.h"
56 #include "lttng-tracer-core.h"
58 #include "../libringbuffer/rb-init.h"
59 #include "lttng-ust-statedump.h"
61 #include "../libringbuffer/getcpu.h"
64 /* Concatenate lttng ust shared library name with its major version number. */
65 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
68 * Has lttng ust comm constructor been called ?
70 static int initialized
;
73 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
74 * Held when handling a command, also held by fork() to deal with
75 * removal of threads, and by exit path.
77 * The UST lock is the centralized mutex across UST tracing control and
80 * ust_exit_mutex must never nest in ust_mutex.
82 * ust_fork_mutex must never nest in ust_mutex.
84 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
85 * counter lazy initialization called by events within the statedump,
86 * which traces while the ust_mutex is held.
88 * ust_lock nests within the dynamic loader lock (within glibc) because
89 * it is taken within the library constructor.
91 * The ust fd tracker lock nests within the ust_mutex.
93 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
95 /* Allow nesting the ust_mutex within the same thread. */
96 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
99 * ust_exit_mutex protects thread_active variable wrt thread exit. It
100 * cannot be done by ust_mutex because pthread_cancel(), which takes an
101 * internal libc lock, cannot nest within ust_mutex.
103 * It never nests within a ust_mutex.
105 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 * ust_fork_mutex protects base address statedump tracing against forks. It
109 * prevents the dynamic loader lock to be taken (by base address statedump
110 * tracing) while a fork is happening, thus preventing deadlock issues with
111 * the dynamic loader lock.
113 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
115 /* Should the ust comm thread quit ? */
116 static int lttng_ust_comm_should_quit
;
119 * This variable can be tested by applications to check whether
120 * lttng-ust is loaded. They simply have to define their own
121 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
122 * library constructor.
124 int lttng_ust_loaded
__attribute__((weak
));
127 * Return 0 on success, -1 if should quit.
128 * The lock is taken in both cases.
133 sigset_t sig_all_blocked
, orig_mask
;
136 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
138 ERR("pthread_setcancelstate: %s", strerror(ret
));
140 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
141 ERR("pthread_setcancelstate: unexpected oldstate");
143 sigfillset(&sig_all_blocked
);
144 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
146 ERR("pthread_sigmask: %s", strerror(ret
));
148 if (!URCU_TLS(ust_mutex_nest
)++)
149 pthread_mutex_lock(&ust_mutex
);
150 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
152 ERR("pthread_sigmask: %s", strerror(ret
));
154 if (lttng_ust_comm_should_quit
) {
162 * ust_lock_nocheck() can be used in constructors/destructors, because
163 * they are already nested within the dynamic loader lock, and therefore
164 * have exclusive access against execution of liblttng-ust destructor.
167 void ust_lock_nocheck(void)
169 sigset_t sig_all_blocked
, orig_mask
;
172 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
174 ERR("pthread_setcancelstate: %s", strerror(ret
));
176 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
177 ERR("pthread_setcancelstate: unexpected oldstate");
179 sigfillset(&sig_all_blocked
);
180 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
182 ERR("pthread_sigmask: %s", strerror(ret
));
184 if (!URCU_TLS(ust_mutex_nest
)++)
185 pthread_mutex_lock(&ust_mutex
);
186 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
188 ERR("pthread_sigmask: %s", strerror(ret
));
195 void ust_unlock(void)
197 sigset_t sig_all_blocked
, orig_mask
;
200 sigfillset(&sig_all_blocked
);
201 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
203 ERR("pthread_sigmask: %s", strerror(ret
));
205 if (!--URCU_TLS(ust_mutex_nest
))
206 pthread_mutex_unlock(&ust_mutex
);
207 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
209 ERR("pthread_sigmask: %s", strerror(ret
));
211 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
213 ERR("pthread_setcancelstate: %s", strerror(ret
));
215 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
216 ERR("pthread_setcancelstate: unexpected oldstate");
221 * Wait for either of these before continuing to the main
223 * - the register_done message from sessiond daemon
224 * (will let the sessiond daemon enable sessions before main
226 * - sessiond daemon is not reachable.
227 * - timeout (ensuring applications are resilient to session
230 static sem_t constructor_wait
;
232 * Doing this for both the global and local sessiond.
235 sem_count_initial_value
= 4,
238 static int sem_count
= sem_count_initial_value
;
241 * Counting nesting within lttng-ust. Used to ensure that calling fork()
242 * from liblttng-ust does not execute the pre/post fork handlers.
244 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
247 * Info about socket and associated listener thread.
251 pthread_t ust_listener
; /* listener thread */
253 int registration_done
;
258 char sock_path
[PATH_MAX
];
262 char wait_shm_path
[PATH_MAX
];
264 /* Keep track of lazy state dump not performed yet. */
265 int statedump_pending
;
266 int initial_statedump_done
;
267 /* Keep procname for statedump */
268 char procname
[LTTNG_UST_PROCNAME_LEN
];
271 /* Socket from app (connect) to session daemon (listen) for communication */
272 struct sock_info global_apps
= {
277 .registration_done
= 0,
281 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
285 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
287 .statedump_pending
= 0,
288 .initial_statedump_done
= 0,
292 /* TODO: allow global_apps_sock_path override */
294 struct sock_info local_apps
= {
298 .registration_done
= 0,
299 .allowed
= 0, /* Check setuid bit first */
305 .statedump_pending
= 0,
306 .initial_statedump_done
= 0,
310 static int wait_poll_fallback
;
312 static const char *cmd_name_mapping
[] = {
313 [ LTTNG_UST_RELEASE
] = "Release",
314 [ LTTNG_UST_SESSION
] = "Create Session",
315 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
317 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
318 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
319 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
320 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
322 /* Session FD commands */
323 [ LTTNG_UST_CHANNEL
] = "Create Channel",
324 [ LTTNG_UST_SESSION_START
] = "Start Session",
325 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
327 /* Channel FD commands */
328 [ LTTNG_UST_STREAM
] = "Create Stream",
329 [ LTTNG_UST_EVENT
] = "Create Event",
331 /* Event and Channel FD commands */
332 [ LTTNG_UST_CONTEXT
] = "Create Context",
333 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
335 /* Event, Channel and Session commands */
336 [ LTTNG_UST_ENABLE
] = "Enable",
337 [ LTTNG_UST_DISABLE
] = "Disable",
339 /* Tracepoint list commands */
340 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
341 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
343 /* Event FD commands */
344 [ LTTNG_UST_FILTER
] = "Create Filter",
345 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
348 static const char *str_timeout
;
349 static int got_timeout_env
;
351 extern void lttng_ring_buffer_client_overwrite_init(void);
352 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
353 extern void lttng_ring_buffer_client_discard_init(void);
354 extern void lttng_ring_buffer_client_discard_rt_init(void);
355 extern void lttng_ring_buffer_metadata_client_init(void);
356 extern void lttng_ring_buffer_client_overwrite_exit(void);
357 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
358 extern void lttng_ring_buffer_client_discard_exit(void);
359 extern void lttng_ring_buffer_client_discard_rt_exit(void);
360 extern void lttng_ring_buffer_metadata_client_exit(void);
362 static char *get_map_shm(struct sock_info
*sock_info
);
364 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
367 size_t copied
= 0, to_copy
= len
;
370 ret
= read(fd
, buf
+ copied
, to_copy
);
375 } while ((ret
> 0 && to_copy
> 0)
376 || (ret
< 0 && errno
== EINTR
));
383 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
387 const char *get_lttng_home_dir(void)
391 val
= (const char *) lttng_getenv("LTTNG_HOME");
395 return (const char *) lttng_getenv("HOME");
399 * Force a read (imply TLS fixup for dlopen) of TLS variables.
402 void lttng_fixup_nest_count_tls(void)
404 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
408 void lttng_fixup_ust_mutex_nest_tls(void)
410 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
417 void lttng_fixup_urcu_bp_tls(void)
423 void lttng_ust_fixup_tls(void)
425 lttng_fixup_urcu_bp_tls();
426 lttng_fixup_ringbuffer_tls();
427 lttng_fixup_vtid_tls();
428 lttng_fixup_nest_count_tls();
429 lttng_fixup_procname_tls();
430 lttng_fixup_ust_mutex_nest_tls();
431 lttng_ust_fixup_perf_counter_tls();
432 lttng_ust_fixup_fd_tracker_tls();
433 lttng_fixup_cgroup_ns_tls();
434 lttng_fixup_ipc_ns_tls();
435 lttng_fixup_net_ns_tls();
436 lttng_fixup_uts_ns_tls();
439 int lttng_get_notify_socket(void *owner
)
441 struct sock_info
*info
= owner
;
443 return info
->notify_socket
;
448 char* lttng_ust_sockinfo_get_procname(void *owner
)
450 struct sock_info
*info
= owner
;
452 return info
->procname
;
456 void print_cmd(int cmd
, int handle
)
458 const char *cmd_name
= "Unknown";
460 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
461 && cmd_name_mapping
[cmd
]) {
462 cmd_name
= cmd_name_mapping
[cmd
];
464 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
466 lttng_ust_obj_get_name(handle
), handle
);
470 int setup_global_apps(void)
473 assert(!global_apps
.wait_shm_mmap
);
475 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
476 if (!global_apps
.wait_shm_mmap
) {
477 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
478 global_apps
.allowed
= 0;
483 global_apps
.allowed
= 1;
484 lttng_ust_getprocname(global_apps
.procname
);
489 int setup_local_apps(void)
492 const char *home_dir
;
495 assert(!local_apps
.wait_shm_mmap
);
499 * Disallow per-user tracing for setuid binaries.
501 if (uid
!= geteuid()) {
502 assert(local_apps
.allowed
== 0);
506 home_dir
= get_lttng_home_dir();
508 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
509 assert(local_apps
.allowed
== 0);
513 local_apps
.allowed
= 1;
514 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
516 LTTNG_DEFAULT_HOME_RUNDIR
,
517 LTTNG_UST_SOCK_FILENAME
);
518 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
519 LTTNG_UST_WAIT_FILENAME
,
522 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
523 if (!local_apps
.wait_shm_mmap
) {
524 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
525 local_apps
.allowed
= 0;
530 lttng_ust_getprocname(local_apps
.procname
);
536 * Get socket timeout, in ms.
537 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
540 long get_timeout(void)
542 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
544 if (!got_timeout_env
) {
545 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
549 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
550 /* All negative values are considered as "-1". */
551 if (constructor_delay_ms
< -1)
552 constructor_delay_ms
= -1;
553 return constructor_delay_ms
;
556 /* Timeout for notify socket send and recv. */
558 long get_notify_sock_timeout(void)
560 return get_timeout();
563 /* Timeout for connecting to cmd and notify sockets. */
565 long get_connect_sock_timeout(void)
567 return get_timeout();
571 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
574 int get_constructor_timeout(struct timespec
*constructor_timeout
)
576 long constructor_delay_ms
;
579 constructor_delay_ms
= get_timeout();
581 switch (constructor_delay_ms
) {
582 case -1:/* fall-through */
584 return constructor_delay_ms
;
590 * If we are unable to find the current time, don't wait.
592 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
597 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
598 constructor_timeout
->tv_nsec
+=
599 (constructor_delay_ms
% 1000UL) * 1000000UL;
600 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
601 constructor_timeout
->tv_sec
++;
602 constructor_timeout
->tv_nsec
-= 1000000000UL;
604 /* Timeout wait (constructor_delay_ms). */
609 void get_allow_blocking(void)
611 const char *str_allow_blocking
=
612 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
614 if (str_allow_blocking
) {
615 DBG("%s environment variable is set",
616 "LTTNG_UST_ALLOW_BLOCKING");
617 lttng_ust_ringbuffer_set_allow_blocking();
622 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
624 return ustcomm_send_reg_msg(socket
,
627 lttng_alignof(uint8_t) * CHAR_BIT
,
628 lttng_alignof(uint16_t) * CHAR_BIT
,
629 lttng_alignof(uint32_t) * CHAR_BIT
,
630 lttng_alignof(uint64_t) * CHAR_BIT
,
631 lttng_alignof(unsigned long) * CHAR_BIT
);
635 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
639 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
642 DBG("message successfully sent");
645 if (len
== -ECONNRESET
) {
646 DBG("remote end closed connection");
651 DBG("incorrect message size: %zd", len
);
657 void decrement_sem_count(unsigned int count
)
661 assert(uatomic_read(&sem_count
) >= count
);
663 if (uatomic_read(&sem_count
) <= 0) {
667 ret
= uatomic_add_return(&sem_count
, -count
);
669 ret
= sem_post(&constructor_wait
);
675 int handle_register_done(struct sock_info
*sock_info
)
677 if (sock_info
->registration_done
)
679 sock_info
->registration_done
= 1;
681 decrement_sem_count(1);
682 if (!sock_info
->statedump_pending
) {
683 sock_info
->initial_statedump_done
= 1;
684 decrement_sem_count(1);
691 int handle_register_failed(struct sock_info
*sock_info
)
693 if (sock_info
->registration_done
)
695 sock_info
->registration_done
= 1;
696 sock_info
->initial_statedump_done
= 1;
698 decrement_sem_count(2);
704 * Only execute pending statedump after the constructor semaphore has
705 * been posted by the current listener thread. This means statedump will
706 * only be performed after the "registration done" command is received
707 * from this thread's session daemon.
709 * This ensures we don't run into deadlock issues with the dynamic
710 * loader mutex, which is held while the constructor is called and
711 * waiting on the constructor semaphore. All operations requiring this
712 * dynamic loader lock need to be postponed using this mechanism.
714 * In a scenario with two session daemons connected to the application,
715 * it is possible that the first listener thread which receives the
716 * registration done command issues its statedump while the dynamic
717 * loader lock is still held by the application constructor waiting on
718 * the semaphore. It will however be allowed to proceed when the
719 * second session daemon sends the registration done command to the
720 * second listener thread. This situation therefore does not produce
724 void handle_pending_statedump(struct sock_info
*sock_info
)
726 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
727 sock_info
->statedump_pending
= 0;
728 pthread_mutex_lock(&ust_fork_mutex
);
729 lttng_handle_pending_statedump(sock_info
);
730 pthread_mutex_unlock(&ust_fork_mutex
);
732 if (!sock_info
->initial_statedump_done
) {
733 sock_info
->initial_statedump_done
= 1;
734 decrement_sem_count(1);
740 int handle_message(struct sock_info
*sock_info
,
741 int sock
, struct ustcomm_ust_msg
*lum
)
744 const struct lttng_ust_objd_ops
*ops
;
745 struct ustcomm_ust_reply lur
;
747 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
750 memset(&lur
, 0, sizeof(lur
));
753 ret
= -LTTNG_UST_ERR_EXITING
;
757 ops
= objd_ops(lum
->handle
);
764 case LTTNG_UST_REGISTER_DONE
:
765 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
766 ret
= handle_register_done(sock_info
);
770 case LTTNG_UST_RELEASE
:
771 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
774 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
776 case LTTNG_UST_FILTER
:
778 /* Receive filter data */
779 struct lttng_ust_filter_bytecode_node
*bytecode
;
781 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
782 ERR("Filter data size is too large: %u bytes",
783 lum
->u
.filter
.data_size
);
788 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
789 ERR("Filter reloc offset %u is not within data",
790 lum
->u
.filter
.reloc_offset
);
795 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
800 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
801 lum
->u
.filter
.data_size
);
803 case 0: /* orderly shutdown */
808 if (len
== lum
->u
.filter
.data_size
) {
809 DBG("filter data received");
811 } else if (len
< 0) {
812 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
813 if (len
== -ECONNRESET
) {
814 ERR("%s remote end closed connection", sock_info
->name
);
823 DBG("incorrect filter data message size: %zd", len
);
829 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
830 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
831 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
833 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
834 (unsigned long) bytecode
,
839 /* don't free bytecode if everything went fine. */
846 case LTTNG_UST_EXCLUSION
:
848 /* Receive exclusion names */
849 struct lttng_ust_excluder_node
*node
;
852 count
= lum
->u
.exclusion
.count
;
854 /* There are no names to read */
858 node
= zmalloc(sizeof(*node
) +
859 count
* LTTNG_UST_SYM_NAME_LEN
);
864 node
->excluder
.count
= count
;
865 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
866 count
* LTTNG_UST_SYM_NAME_LEN
);
868 case 0: /* orderly shutdown */
873 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
874 DBG("Exclusion data received");
876 } else if (len
< 0) {
877 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
878 if (len
== -ECONNRESET
) {
879 ERR("%s remote end closed connection", sock_info
->name
);
888 DBG("Incorrect exclusion data message size: %zd", len
);
895 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
896 (unsigned long) node
,
901 /* Don't free exclusion data if everything went fine. */
908 case LTTNG_UST_CHANNEL
:
913 len
= ustcomm_recv_channel_from_sessiond(sock
,
914 &chan_data
, lum
->u
.channel
.len
,
917 case 0: /* orderly shutdown */
921 if (len
== lum
->u
.channel
.len
) {
922 DBG("channel data received");
924 } else if (len
< 0) {
925 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
926 if (len
== -ECONNRESET
) {
927 ERR("%s remote end closed connection", sock_info
->name
);
934 DBG("incorrect channel data message size: %zd", len
);
939 args
.channel
.chan_data
= chan_data
;
940 args
.channel
.wakeup_fd
= wakeup_fd
;
942 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
943 (unsigned long) &lum
->u
,
949 case LTTNG_UST_STREAM
:
951 /* Receive shm_fd, wakeup_fd */
952 ret
= ustcomm_recv_stream_from_sessiond(sock
,
955 &args
.stream
.wakeup_fd
);
961 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
962 (unsigned long) &lum
->u
,
968 case LTTNG_UST_CONTEXT
:
969 switch (lum
->u
.context
.ctx
) {
970 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
973 size_t ctxlen
, recvlen
;
975 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
976 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
977 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
978 ERR("Application context string length size is too large: %zu bytes",
983 strcpy(ctxstr
, "$app.");
984 p
= &ctxstr
[strlen("$app.")];
985 recvlen
= ctxlen
- strlen("$app.");
986 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
988 case 0: /* orderly shutdown */
992 if (len
== recvlen
) {
993 DBG("app context data received");
995 } else if (len
< 0) {
996 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
997 if (len
== -ECONNRESET
) {
998 ERR("%s remote end closed connection", sock_info
->name
);
1005 DBG("incorrect app context data message size: %zd", len
);
1010 /* Put : between provider and ctxname. */
1011 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1012 args
.app_context
.ctxname
= ctxstr
;
1019 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1020 (unsigned long) &lum
->u
,
1028 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1029 (unsigned long) &lum
->u
,
1036 lur
.handle
= lum
->handle
;
1040 lur
.ret_code
= LTTNG_UST_OK
;
1043 * Use -LTTNG_UST_ERR as wildcard for UST internal
1044 * error that are not caused by the transport, except if
1045 * we already have a more precise error message to
1048 if (ret
> -LTTNG_UST_ERR
) {
1049 /* Translate code to UST error. */
1052 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1055 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1058 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1061 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1064 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1067 lur
.ret_code
= -LTTNG_UST_ERR
;
1076 case LTTNG_UST_TRACER_VERSION
:
1077 lur
.u
.version
= lum
->u
.version
;
1079 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1080 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1084 DBG("Return value: %d", lur
.ret_val
);
1089 * Performed delayed statedump operations outside of the UST
1090 * lock. We need to take the dynamic loader lock before we take
1091 * the UST lock internally within handle_pending_statedump().
1093 handle_pending_statedump(sock_info
);
1096 ret
= -LTTNG_UST_ERR_EXITING
;
1100 ret
= send_reply(sock
, &lur
);
1102 DBG("error sending reply");
1107 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1110 if (lur
.ret_code
== LTTNG_UST_OK
) {
1112 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1113 len
= ustcomm_send_unix_sock(sock
,
1114 &args
.field_list
.entry
,
1115 sizeof(args
.field_list
.entry
));
1120 if (len
!= sizeof(args
.field_list
.entry
)) {
1134 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1138 if (sock_info
->root_handle
!= -1) {
1139 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1141 ERR("Error unref root handle");
1143 sock_info
->root_handle
= -1;
1145 sock_info
->registration_done
= 0;
1146 sock_info
->initial_statedump_done
= 0;
1149 * wait_shm_mmap, socket and notify socket are used by listener
1150 * threads outside of the ust lock, so we cannot tear them down
1151 * ourselves, because we cannot join on these threads. Leave
1152 * responsibility of cleaning up these resources to the OS
1158 if (sock_info
->socket
!= -1) {
1159 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1161 ERR("Error closing ust cmd socket");
1163 sock_info
->socket
= -1;
1165 if (sock_info
->notify_socket
!= -1) {
1166 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1168 ERR("Error closing ust notify socket");
1170 sock_info
->notify_socket
= -1;
1172 if (sock_info
->wait_shm_mmap
) {
1175 page_size
= sysconf(_SC_PAGE_SIZE
);
1176 if (page_size
<= 0) {
1180 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1182 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1184 ERR("Error unmapping wait shm");
1187 sock_info
->wait_shm_mmap
= NULL
;
1192 * Using fork to set umask in the child process (not multi-thread safe).
1193 * We deal with the shm_open vs ftruncate race (happening when the
1194 * sessiond owns the shm and does not let everybody modify it, to ensure
1195 * safety against shm_unlink) by simply letting the mmap fail and
1196 * retrying after a few seconds.
1197 * For global shm, everybody has rw access to it until the sessiond
1201 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1203 int wait_shm_fd
, ret
;
1207 * Try to open read-only.
1209 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1210 if (wait_shm_fd
>= 0) {
1213 size_t bytes_read
= 0;
1216 * Try to read the fd. If unable to do so, try opening
1220 len
= read(wait_shm_fd
,
1221 &((char *) &tmp_read
)[bytes_read
],
1222 sizeof(tmp_read
) - bytes_read
);
1226 } while ((len
< 0 && errno
== EINTR
)
1227 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1228 if (bytes_read
!= sizeof(tmp_read
)) {
1229 ret
= close(wait_shm_fd
);
1231 ERR("close wait_shm_fd");
1236 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1238 * Real-only open did not work, and it's not because the
1239 * entry was not present. It's a failure that prohibits
1242 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1248 * If the open failed because the file did not exist, or because
1249 * the file was not truncated yet, try creating it ourself.
1251 URCU_TLS(lttng_ust_nest_count
)++;
1253 URCU_TLS(lttng_ust_nest_count
)--;
1258 * Parent: wait for child to return, in which case the
1259 * shared memory map will have been created.
1261 pid
= wait(&status
);
1262 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1267 * Try to open read-only again after creation.
1269 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1270 if (wait_shm_fd
< 0) {
1272 * Real-only open did not work. It's a failure
1273 * that prohibits using shm.
1275 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1279 } else if (pid
== 0) {
1283 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1284 if (sock_info
->global
)
1285 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1287 * We're alone in a child process, so we can modify the
1288 * process-wide umask.
1290 umask(~create_mode
);
1292 * Try creating shm (or get rw access).
1293 * We don't do an exclusive open, because we allow other
1294 * processes to create+ftruncate it concurrently.
1296 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1297 O_RDWR
| O_CREAT
, create_mode
);
1298 if (wait_shm_fd
>= 0) {
1299 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1301 PERROR("ftruncate");
1302 _exit(EXIT_FAILURE
);
1304 _exit(EXIT_SUCCESS
);
1307 * For local shm, we need to have rw access to accept
1308 * opening it: this means the local sessiond will be
1309 * able to wake us up. For global shm, we open it even
1310 * if rw access is not granted, because the root.root
1311 * sessiond will be able to override all rights and wake
1314 if (!sock_info
->global
&& errno
!= EACCES
) {
1315 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1316 _exit(EXIT_FAILURE
);
1319 * The shm exists, but we cannot open it RW. Report
1322 _exit(EXIT_SUCCESS
);
1327 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1328 struct stat statbuf
;
1331 * Ensure that our user is the owner of the shm file for
1332 * local shm. If we do not own the file, it means our
1333 * sessiond will not have access to wake us up (there is
1334 * probably a rogue process trying to fake our
1335 * sessiond). Fallback to polling method in this case.
1337 ret
= fstat(wait_shm_fd
, &statbuf
);
1342 if (statbuf
.st_uid
!= getuid())
1348 ret
= close(wait_shm_fd
);
1350 PERROR("Error closing fd");
1356 char *get_map_shm(struct sock_info
*sock_info
)
1359 int wait_shm_fd
, ret
;
1360 char *wait_shm_mmap
;
1362 page_size
= sysconf(_SC_PAGE_SIZE
);
1363 if (page_size
<= 0) {
1367 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1371 lttng_ust_lock_fd_tracker();
1372 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1373 if (wait_shm_fd
< 0) {
1374 lttng_ust_unlock_fd_tracker();
1378 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1380 ret
= close(wait_shm_fd
);
1382 PERROR("Error closing fd");
1384 lttng_ust_unlock_fd_tracker();
1389 lttng_ust_unlock_fd_tracker();
1391 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1392 MAP_SHARED
, wait_shm_fd
, 0);
1394 /* close shm fd immediately after taking the mmap reference */
1395 lttng_ust_lock_fd_tracker();
1396 ret
= close(wait_shm_fd
);
1398 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1400 PERROR("Error closing fd");
1402 lttng_ust_unlock_fd_tracker();
1404 if (wait_shm_mmap
== MAP_FAILED
) {
1405 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1408 return wait_shm_mmap
;
1415 void wait_for_sessiond(struct sock_info
*sock_info
)
1417 /* Use ust_lock to check if we should quit. */
1421 if (wait_poll_fallback
) {
1426 assert(sock_info
->wait_shm_mmap
);
1428 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1429 /* Wait for futex wakeup */
1430 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1433 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1434 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1437 /* Value already changed. */
1440 /* Retry if interrupted by signal. */
1441 break; /* Get out of switch. */
1443 wait_poll_fallback
= 1;
1445 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1446 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1447 "Please upgrade your kernel "
1448 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1449 "mainline). LTTng-UST will use polling mode fallback.");
1468 * This thread does not allocate any resource, except within
1469 * handle_message, within mutex protection. This mutex protects against
1471 * The other moment it allocates resources is at socket connection, which
1472 * is also protected by the mutex.
1475 void *ust_listener_thread(void *arg
)
1477 struct sock_info
*sock_info
= arg
;
1478 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1481 lttng_ust_fixup_tls();
1483 * If available, add '-ust' to the end of this thread's
1486 ret
= lttng_ust_setustprocname();
1488 ERR("Unable to set UST process name");
1491 /* Restart trying to connect to the session daemon */
1493 if (prev_connect_failed
) {
1494 /* Wait for sessiond availability with pipe */
1495 wait_for_sessiond(sock_info
);
1499 * Sleep for 5 seconds before retrying after a
1500 * sequence of failure / wait / failure. This
1501 * deals with a killed or broken session daemon.
1507 prev_connect_failed
= 0;
1514 if (sock_info
->socket
!= -1) {
1515 /* FD tracker is updated by ustcomm_close_unix_sock() */
1516 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1518 ERR("Error closing %s ust cmd socket",
1521 sock_info
->socket
= -1;
1523 if (sock_info
->notify_socket
!= -1) {
1524 /* FD tracker is updated by ustcomm_close_unix_sock() */
1525 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1527 ERR("Error closing %s ust notify socket",
1530 sock_info
->notify_socket
= -1;
1535 * Register. We need to perform both connect and sending
1536 * registration message before doing the next connect otherwise
1537 * we may reach unix socket connect queue max limits and block
1538 * on the 2nd connect while the session daemon is awaiting the
1539 * first connect registration message.
1541 /* Connect cmd socket */
1542 lttng_ust_lock_fd_tracker();
1543 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1544 get_connect_sock_timeout());
1546 lttng_ust_unlock_fd_tracker();
1547 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1548 prev_connect_failed
= 1;
1551 * If we cannot find the sessiond daemon, don't delay
1552 * constructor execution.
1554 ret
= handle_register_failed(sock_info
);
1560 ret
= lttng_ust_add_fd_to_tracker(fd
);
1564 PERROR("close on sock_info->socket");
1567 lttng_ust_unlock_fd_tracker();
1572 sock_info
->socket
= ret
;
1573 lttng_ust_unlock_fd_tracker();
1577 * Unlock/relock ust lock because connect is blocking (with
1578 * timeout). Don't delay constructors on the ust lock for too
1586 * Create only one root handle per listener thread for the whole
1587 * process lifetime, so we ensure we get ID which is statically
1588 * assigned to the root handle.
1590 if (sock_info
->root_handle
== -1) {
1591 ret
= lttng_abi_create_root_handle();
1593 ERR("Error creating root handle");
1596 sock_info
->root_handle
= ret
;
1599 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1601 ERR("Error registering to %s ust cmd socket",
1603 prev_connect_failed
= 1;
1605 * If we cannot register to the sessiond daemon, don't
1606 * delay constructor execution.
1608 ret
= handle_register_failed(sock_info
);
1616 * Unlock/relock ust lock because connect is blocking (with
1617 * timeout). Don't delay constructors on the ust lock for too
1624 /* Connect notify socket */
1625 lttng_ust_lock_fd_tracker();
1626 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1627 get_connect_sock_timeout());
1629 lttng_ust_unlock_fd_tracker();
1630 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1631 prev_connect_failed
= 1;
1634 * If we cannot find the sessiond daemon, don't delay
1635 * constructor execution.
1637 ret
= handle_register_failed(sock_info
);
1644 ret
= lttng_ust_add_fd_to_tracker(fd
);
1648 PERROR("close on sock_info->notify_socket");
1651 lttng_ust_unlock_fd_tracker();
1656 sock_info
->notify_socket
= ret
;
1657 lttng_ust_unlock_fd_tracker();
1661 * Unlock/relock ust lock because connect is blocking (with
1662 * timeout). Don't delay constructors on the ust lock for too
1669 timeout
= get_notify_sock_timeout();
1672 * Give at least 10ms to sessiond to reply to
1677 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1680 WARN("Error setting socket receive timeout");
1682 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1685 WARN("Error setting socket send timeout");
1687 } else if (timeout
< -1) {
1688 WARN("Unsupported timeout value %ld", timeout
);
1691 ret
= register_to_sessiond(sock_info
->notify_socket
,
1692 USTCTL_SOCKET_NOTIFY
);
1694 ERR("Error registering to %s ust notify socket",
1696 prev_connect_failed
= 1;
1698 * If we cannot register to the sessiond daemon, don't
1699 * delay constructor execution.
1701 ret
= handle_register_failed(sock_info
);
1706 sock
= sock_info
->socket
;
1712 struct ustcomm_ust_msg lum
;
1714 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1716 case 0: /* orderly shutdown */
1717 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1722 * Either sessiond has shutdown or refused us by closing the socket.
1723 * In either case, we don't want to delay construction execution,
1724 * and we need to wait before retry.
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 print_cmd(lum
.cmd
, lum
.handle
);
1737 ret
= handle_message(sock_info
, sock
, &lum
);
1739 ERR("Error handling message for %s socket",
1742 * Close socket if protocol error is
1750 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1752 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1754 if (len
== -ECONNRESET
) {
1755 DBG("%s remote end closed connection", sock_info
->name
);
1766 /* Cleanup socket handles before trying to reconnect */
1767 lttng_ust_objd_table_owner_cleanup(sock_info
);
1769 goto restart
; /* try to reconnect */
1774 pthread_mutex_lock(&ust_exit_mutex
);
1775 sock_info
->thread_active
= 0;
1776 pthread_mutex_unlock(&ust_exit_mutex
);
1781 * Weak symbol to call when the ust malloc wrapper is not loaded.
1783 __attribute__((weak
))
1784 void lttng_ust_malloc_wrapper_init(void)
1789 * sessiond monitoring thread: monitor presence of global and per-user
1790 * sessiond by polling the application common named pipe.
1792 void __attribute__((constructor
)) lttng_ust_init(void)
1794 struct timespec constructor_timeout
;
1795 sigset_t sig_all_blocked
, orig_parent_mask
;
1796 pthread_attr_t thread_attr
;
1801 if (uatomic_xchg(&initialized
, 1) == 1)
1805 * Fixup interdependency between TLS fixup mutex (which happens
1806 * to be the dynamic linker mutex) and ust_lock, taken within
1809 lttng_ust_fixup_tls();
1811 lttng_ust_loaded
= 1;
1814 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1815 * the unloading of code used by the ust_listener_threads as we can not
1816 * reliably know when they exited. To do that, manually load
1817 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1818 * this library so it never becomes zero, thus never gets unloaded from the
1819 * address space of the process. Since we are already running in the
1820 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1821 * simply increment the refcount and no additionnal work is needed by the
1822 * dynamic loader as the shared library is already loaded in the address
1823 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1824 * unloading of the UST library if its refcount becomes zero (which should
1825 * never happen). Do the return value check but discard the handle at the
1826 * end of the function as it's not needed.
1828 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1830 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1834 * We want precise control over the order in which we construct
1835 * our sub-libraries vs starting to receive commands from
1836 * sessiond (otherwise leading to errors when trying to create
1837 * sessiond before the init functions are completed).
1840 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1842 lttng_ust_init_fd_tracker();
1843 lttng_ust_clock_init();
1844 lttng_ust_getcpu_init();
1845 lttng_ust_statedump_init();
1846 lttng_ring_buffer_metadata_client_init();
1847 lttng_ring_buffer_client_overwrite_init();
1848 lttng_ring_buffer_client_overwrite_rt_init();
1849 lttng_ring_buffer_client_discard_init();
1850 lttng_ring_buffer_client_discard_rt_init();
1851 lttng_perf_counter_init();
1853 * Invoke ust malloc wrapper init before starting other threads.
1855 lttng_ust_malloc_wrapper_init();
1857 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1859 get_allow_blocking();
1861 ret
= sem_init(&constructor_wait
, 0, 0);
1866 ret
= setup_global_apps();
1868 assert(global_apps
.allowed
== 0);
1869 DBG("global apps setup returned %d", ret
);
1872 ret
= setup_local_apps();
1874 assert(local_apps
.allowed
== 0);
1875 DBG("local apps setup returned %d", ret
);
1878 /* A new thread created by pthread_create inherits the signal mask
1879 * from the parent. To avoid any signal being received by the
1880 * listener thread, we block all signals temporarily in the parent,
1881 * while we create the listener thread.
1883 sigfillset(&sig_all_blocked
);
1884 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1886 ERR("pthread_sigmask: %s", strerror(ret
));
1889 ret
= pthread_attr_init(&thread_attr
);
1891 ERR("pthread_attr_init: %s", strerror(ret
));
1893 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1895 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1898 if (global_apps
.allowed
) {
1899 pthread_mutex_lock(&ust_exit_mutex
);
1900 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1901 ust_listener_thread
, &global_apps
);
1903 ERR("pthread_create global: %s", strerror(ret
));
1905 global_apps
.thread_active
= 1;
1906 pthread_mutex_unlock(&ust_exit_mutex
);
1908 handle_register_done(&global_apps
);
1911 if (local_apps
.allowed
) {
1912 pthread_mutex_lock(&ust_exit_mutex
);
1913 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1914 ust_listener_thread
, &local_apps
);
1916 ERR("pthread_create local: %s", strerror(ret
));
1918 local_apps
.thread_active
= 1;
1919 pthread_mutex_unlock(&ust_exit_mutex
);
1921 handle_register_done(&local_apps
);
1923 ret
= pthread_attr_destroy(&thread_attr
);
1925 ERR("pthread_attr_destroy: %s", strerror(ret
));
1928 /* Restore original signal mask in parent */
1929 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1931 ERR("pthread_sigmask: %s", strerror(ret
));
1934 switch (timeout_mode
) {
1935 case 1: /* timeout wait */
1937 ret
= sem_timedwait(&constructor_wait
,
1938 &constructor_timeout
);
1939 } while (ret
< 0 && errno
== EINTR
);
1943 ERR("Timed out waiting for lttng-sessiond");
1946 PERROR("sem_timedwait");
1949 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1954 case -1:/* wait forever */
1956 ret
= sem_wait(&constructor_wait
);
1957 } while (ret
< 0 && errno
== EINTR
);
1964 ERR("Unexpected error \"%s\" returned by sem_wait",
1969 case 0: /* no timeout */
1975 void lttng_ust_cleanup(int exiting
)
1977 cleanup_sock_info(&global_apps
, exiting
);
1978 cleanup_sock_info(&local_apps
, exiting
);
1979 local_apps
.allowed
= 0;
1980 global_apps
.allowed
= 0;
1982 * The teardown in this function all affect data structures
1983 * accessed under the UST lock by the listener thread. This
1984 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1985 * that none of these threads are accessing this data at this
1988 lttng_ust_abi_exit();
1989 lttng_ust_events_exit();
1990 lttng_perf_counter_exit();
1991 lttng_ring_buffer_client_discard_rt_exit();
1992 lttng_ring_buffer_client_discard_exit();
1993 lttng_ring_buffer_client_overwrite_rt_exit();
1994 lttng_ring_buffer_client_overwrite_exit();
1995 lttng_ring_buffer_metadata_client_exit();
1996 lttng_ust_statedump_destroy();
1999 /* Reinitialize values for fork */
2000 sem_count
= sem_count_initial_value
;
2001 lttng_ust_comm_should_quit
= 0;
2006 void __attribute__((destructor
)) lttng_ust_exit(void)
2011 * Using pthread_cancel here because:
2012 * A) we don't want to hang application teardown.
2013 * B) the thread is not allocating any resource.
2017 * Require the communication thread to quit. Synchronize with
2018 * mutexes to ensure it is not in a mutex critical section when
2019 * pthread_cancel is later called.
2022 lttng_ust_comm_should_quit
= 1;
2025 pthread_mutex_lock(&ust_exit_mutex
);
2026 /* cancel threads */
2027 if (global_apps
.thread_active
) {
2028 ret
= pthread_cancel(global_apps
.ust_listener
);
2030 ERR("Error cancelling global ust listener thread: %s",
2033 global_apps
.thread_active
= 0;
2036 if (local_apps
.thread_active
) {
2037 ret
= pthread_cancel(local_apps
.ust_listener
);
2039 ERR("Error cancelling local ust listener thread: %s",
2042 local_apps
.thread_active
= 0;
2045 pthread_mutex_unlock(&ust_exit_mutex
);
2048 * Do NOT join threads: use of sys_futex makes it impossible to
2049 * join the threads without using async-cancel, but async-cancel
2050 * is delivered by a signal, which could hit the target thread
2051 * anywhere in its code path, including while the ust_lock() is
2052 * held, causing a deadlock for the other thread. Let the OS
2053 * cleanup the threads if there are stalled in a syscall.
2055 lttng_ust_cleanup(1);
2059 void ust_context_ns_reset(void)
2061 lttng_context_pid_ns_reset();
2062 lttng_context_cgroup_ns_reset();
2063 lttng_context_ipc_ns_reset();
2064 lttng_context_mnt_ns_reset();
2065 lttng_context_net_ns_reset();
2066 lttng_context_user_ns_reset();
2067 lttng_context_uts_ns_reset();
2071 void ust_context_vuids_reset(void)
2073 lttng_context_vuid_reset();
2074 lttng_context_veuid_reset();
2075 lttng_context_vsuid_reset();
2079 void ust_context_vgids_reset(void)
2081 lttng_context_vgid_reset();
2082 lttng_context_vegid_reset();
2083 lttng_context_vsgid_reset();
2087 * We exclude the worker threads across fork and clone (except
2088 * CLONE_VM), because these system calls only keep the forking thread
2089 * running in the child. Therefore, we don't want to call fork or clone
2090 * in the middle of an tracepoint or ust tracing state modification.
2091 * Holding this mutex protects these structures across fork and clone.
2093 void ust_before_fork(sigset_t
*save_sigset
)
2096 * Disable signals. This is to avoid that the child intervenes
2097 * before it is properly setup for tracing. It is safer to
2098 * disable all signals, because then we know we are not breaking
2099 * anything by restoring the original mask.
2104 /* Fixup lttng-ust TLS. */
2105 lttng_ust_fixup_tls();
2107 if (URCU_TLS(lttng_ust_nest_count
))
2109 /* Disable signals */
2110 sigfillset(&all_sigs
);
2111 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2113 PERROR("sigprocmask");
2116 pthread_mutex_lock(&ust_fork_mutex
);
2119 urcu_bp_before_fork();
2120 lttng_ust_lock_fd_tracker();
2124 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2128 DBG("process %d", getpid());
2129 lttng_perf_unlock();
2130 lttng_ust_unlock_fd_tracker();
2133 pthread_mutex_unlock(&ust_fork_mutex
);
2135 /* Restore signals */
2136 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2138 PERROR("sigprocmask");
2142 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2144 if (URCU_TLS(lttng_ust_nest_count
))
2146 DBG("process %d", getpid());
2147 urcu_bp_after_fork_parent();
2148 /* Release mutexes and reenable signals */
2149 ust_after_fork_common(restore_sigset
);
2153 * After fork, in the child, we need to cleanup all the leftover state,
2154 * except the worker thread which already magically disappeared thanks
2155 * to the weird Linux fork semantics. After tyding up, we call
2156 * lttng_ust_init() again to start over as a new PID.
2158 * This is meant for forks() that have tracing in the child between the
2159 * fork and following exec call (if there is any).
2161 void ust_after_fork_child(sigset_t
*restore_sigset
)
2163 if (URCU_TLS(lttng_ust_nest_count
))
2165 lttng_context_vpid_reset();
2166 lttng_context_vtid_reset();
2167 lttng_context_procname_reset();
2168 ust_context_ns_reset();
2169 ust_context_vuids_reset();
2170 ust_context_vgids_reset();
2171 DBG("process %d", getpid());
2172 /* Release urcu mutexes */
2173 urcu_bp_after_fork_child();
2174 lttng_ust_cleanup(0);
2175 /* Release mutexes and reenable signals */
2176 ust_after_fork_common(restore_sigset
);
2180 void ust_after_setns(void)
2182 ust_context_ns_reset();
2183 ust_context_vuids_reset();
2184 ust_context_vgids_reset();
2187 void ust_after_unshare(void)
2189 ust_context_ns_reset();
2190 ust_context_vuids_reset();
2191 ust_context_vgids_reset();
2194 void ust_after_setuid(void)
2196 ust_context_vuids_reset();
2199 void ust_after_seteuid(void)
2201 ust_context_vuids_reset();
2204 void ust_after_setreuid(void)
2206 ust_context_vuids_reset();
2209 void ust_after_setresuid(void)
2211 ust_context_vuids_reset();
2214 void ust_after_setgid(void)
2216 ust_context_vgids_reset();
2219 void ust_after_setegid(void)
2221 ust_context_vgids_reset();
2224 void ust_after_setregid(void)
2226 ust_context_vgids_reset();
2229 void ust_after_setresgid(void)
2231 ust_context_vgids_reset();
2234 void lttng_ust_sockinfo_session_enabled(void *owner
)
2236 struct sock_info
*sock_info
= owner
;
2237 sock_info
->statedump_pending
= 1;