4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <semaphore.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/futex.h>
42 #include <urcu/compiler.h>
44 #include <lttng/ust-events.h>
45 #include <lttng/ust-abi.h>
46 #include <lttng/ust.h>
47 #include <lttng/ust-error.h>
48 #include <lttng/ust-ctl.h>
49 #include <lttng/ust-cancelstate.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 if (lttng_ust_cancelstate_disable_push()) {
137 ERR("lttng_ust_cancelstate_disable_push");
139 sigfillset(&sig_all_blocked
);
140 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
142 ERR("pthread_sigmask: %s", strerror(ret
));
144 if (!URCU_TLS(ust_mutex_nest
)++)
145 pthread_mutex_lock(&ust_mutex
);
146 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
148 ERR("pthread_sigmask: %s", strerror(ret
));
150 if (lttng_ust_comm_should_quit
) {
158 * ust_lock_nocheck() can be used in constructors/destructors, because
159 * they are already nested within the dynamic loader lock, and therefore
160 * have exclusive access against execution of liblttng-ust destructor.
163 void ust_lock_nocheck(void)
165 sigset_t sig_all_blocked
, orig_mask
;
168 if (lttng_ust_cancelstate_disable_push()) {
169 ERR("lttng_ust_cancelstate_disable_push");
171 sigfillset(&sig_all_blocked
);
172 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
174 ERR("pthread_sigmask: %s", strerror(ret
));
176 if (!URCU_TLS(ust_mutex_nest
)++)
177 pthread_mutex_lock(&ust_mutex
);
178 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
180 ERR("pthread_sigmask: %s", strerror(ret
));
187 void ust_unlock(void)
189 sigset_t sig_all_blocked
, orig_mask
;
192 sigfillset(&sig_all_blocked
);
193 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
195 ERR("pthread_sigmask: %s", strerror(ret
));
197 if (!--URCU_TLS(ust_mutex_nest
))
198 pthread_mutex_unlock(&ust_mutex
);
199 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
201 ERR("pthread_sigmask: %s", strerror(ret
));
203 if (lttng_ust_cancelstate_disable_pop()) {
204 ERR("lttng_ust_cancelstate_disable_pop");
209 * Wait for either of these before continuing to the main
211 * - the register_done message from sessiond daemon
212 * (will let the sessiond daemon enable sessions before main
214 * - sessiond daemon is not reachable.
215 * - timeout (ensuring applications are resilient to session
218 static sem_t constructor_wait
;
220 * Doing this for both the global and local sessiond.
223 sem_count_initial_value
= 4,
226 static int sem_count
= sem_count_initial_value
;
229 * Counting nesting within lttng-ust. Used to ensure that calling fork()
230 * from liblttng-ust does not execute the pre/post fork handlers.
232 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
235 * Info about socket and associated listener thread.
239 pthread_t ust_listener
; /* listener thread */
241 int registration_done
;
246 char sock_path
[PATH_MAX
];
250 char wait_shm_path
[PATH_MAX
];
252 /* Keep track of lazy state dump not performed yet. */
253 int statedump_pending
;
254 int initial_statedump_done
;
255 /* Keep procname for statedump */
256 char procname
[LTTNG_UST_PROCNAME_LEN
];
259 /* Socket from app (connect) to session daemon (listen) for communication */
260 struct sock_info global_apps
= {
265 .registration_done
= 0,
269 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
273 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
275 .statedump_pending
= 0,
276 .initial_statedump_done
= 0,
280 /* TODO: allow global_apps_sock_path override */
282 struct sock_info local_apps
= {
286 .registration_done
= 0,
287 .allowed
= 0, /* Check setuid bit first */
293 .statedump_pending
= 0,
294 .initial_statedump_done
= 0,
298 static int wait_poll_fallback
;
300 static const char *cmd_name_mapping
[] = {
301 [ LTTNG_UST_RELEASE
] = "Release",
302 [ LTTNG_UST_SESSION
] = "Create Session",
303 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
305 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
306 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
307 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
308 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
310 /* Session FD commands */
311 [ LTTNG_UST_CHANNEL
] = "Create Channel",
312 [ LTTNG_UST_SESSION_START
] = "Start Session",
313 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
315 /* Channel FD commands */
316 [ LTTNG_UST_STREAM
] = "Create Stream",
317 [ LTTNG_UST_EVENT
] = "Create Event",
319 /* Event and Channel FD commands */
320 [ LTTNG_UST_CONTEXT
] = "Create Context",
321 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
323 /* Event, Channel and Session commands */
324 [ LTTNG_UST_ENABLE
] = "Enable",
325 [ LTTNG_UST_DISABLE
] = "Disable",
327 /* Tracepoint list commands */
328 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
329 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
331 /* Event FD commands */
332 [ LTTNG_UST_FILTER
] = "Create Filter",
333 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
336 static const char *str_timeout
;
337 static int got_timeout_env
;
339 extern void lttng_ring_buffer_client_overwrite_init(void);
340 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
341 extern void lttng_ring_buffer_client_discard_init(void);
342 extern void lttng_ring_buffer_client_discard_rt_init(void);
343 extern void lttng_ring_buffer_metadata_client_init(void);
344 extern void lttng_ring_buffer_client_overwrite_exit(void);
345 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
346 extern void lttng_ring_buffer_client_discard_exit(void);
347 extern void lttng_ring_buffer_client_discard_rt_exit(void);
348 extern void lttng_ring_buffer_metadata_client_exit(void);
350 static char *get_map_shm(struct sock_info
*sock_info
);
352 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
355 size_t copied
= 0, to_copy
= len
;
358 ret
= read(fd
, buf
+ copied
, to_copy
);
363 } while ((ret
> 0 && to_copy
> 0)
364 || (ret
< 0 && errno
== EINTR
));
371 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
375 const char *get_lttng_home_dir(void)
379 val
= (const char *) lttng_getenv("LTTNG_HOME");
383 return (const char *) lttng_getenv("HOME");
387 * Force a read (imply TLS fixup for dlopen) of TLS variables.
390 void lttng_fixup_nest_count_tls(void)
392 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
396 void lttng_fixup_ust_mutex_nest_tls(void)
398 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
405 void lttng_fixup_urcu_bp_tls(void)
411 void lttng_ust_fixup_tls(void)
413 lttng_fixup_urcu_bp_tls();
414 lttng_fixup_ringbuffer_tls();
415 lttng_fixup_vtid_tls();
416 lttng_fixup_nest_count_tls();
417 lttng_fixup_procname_tls();
418 lttng_fixup_ust_mutex_nest_tls();
419 lttng_ust_fixup_perf_counter_tls();
420 lttng_ust_fixup_fd_tracker_tls();
421 lttng_fixup_cgroup_ns_tls();
422 lttng_fixup_ipc_ns_tls();
423 lttng_fixup_net_ns_tls();
424 lttng_fixup_uts_ns_tls();
427 int lttng_get_notify_socket(void *owner
)
429 struct sock_info
*info
= owner
;
431 return info
->notify_socket
;
436 char* lttng_ust_sockinfo_get_procname(void *owner
)
438 struct sock_info
*info
= owner
;
440 return info
->procname
;
444 void print_cmd(int cmd
, int handle
)
446 const char *cmd_name
= "Unknown";
448 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
449 && cmd_name_mapping
[cmd
]) {
450 cmd_name
= cmd_name_mapping
[cmd
];
452 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
454 lttng_ust_obj_get_name(handle
), handle
);
458 int setup_global_apps(void)
461 assert(!global_apps
.wait_shm_mmap
);
463 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
464 if (!global_apps
.wait_shm_mmap
) {
465 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
466 global_apps
.allowed
= 0;
471 global_apps
.allowed
= 1;
472 lttng_ust_getprocname(global_apps
.procname
);
477 int setup_local_apps(void)
480 const char *home_dir
;
483 assert(!local_apps
.wait_shm_mmap
);
487 * Disallow per-user tracing for setuid binaries.
489 if (uid
!= geteuid()) {
490 assert(local_apps
.allowed
== 0);
494 home_dir
= get_lttng_home_dir();
496 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
497 assert(local_apps
.allowed
== 0);
501 local_apps
.allowed
= 1;
502 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
504 LTTNG_DEFAULT_HOME_RUNDIR
,
505 LTTNG_UST_SOCK_FILENAME
);
506 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
507 LTTNG_UST_WAIT_FILENAME
,
510 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
511 if (!local_apps
.wait_shm_mmap
) {
512 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
513 local_apps
.allowed
= 0;
518 lttng_ust_getprocname(local_apps
.procname
);
524 * Get socket timeout, in ms.
525 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
528 long get_timeout(void)
530 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
532 if (!got_timeout_env
) {
533 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
537 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
538 /* All negative values are considered as "-1". */
539 if (constructor_delay_ms
< -1)
540 constructor_delay_ms
= -1;
541 return constructor_delay_ms
;
544 /* Timeout for notify socket send and recv. */
546 long get_notify_sock_timeout(void)
548 return get_timeout();
551 /* Timeout for connecting to cmd and notify sockets. */
553 long get_connect_sock_timeout(void)
555 return get_timeout();
559 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
562 int get_constructor_timeout(struct timespec
*constructor_timeout
)
564 long constructor_delay_ms
;
567 constructor_delay_ms
= get_timeout();
569 switch (constructor_delay_ms
) {
570 case -1:/* fall-through */
572 return constructor_delay_ms
;
578 * If we are unable to find the current time, don't wait.
580 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
585 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
586 constructor_timeout
->tv_nsec
+=
587 (constructor_delay_ms
% 1000UL) * 1000000UL;
588 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
589 constructor_timeout
->tv_sec
++;
590 constructor_timeout
->tv_nsec
-= 1000000000UL;
592 /* Timeout wait (constructor_delay_ms). */
597 void get_allow_blocking(void)
599 const char *str_allow_blocking
=
600 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
602 if (str_allow_blocking
) {
603 DBG("%s environment variable is set",
604 "LTTNG_UST_ALLOW_BLOCKING");
605 lttng_ust_ringbuffer_set_allow_blocking();
610 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
612 return ustcomm_send_reg_msg(socket
,
615 lttng_alignof(uint8_t) * CHAR_BIT
,
616 lttng_alignof(uint16_t) * CHAR_BIT
,
617 lttng_alignof(uint32_t) * CHAR_BIT
,
618 lttng_alignof(uint64_t) * CHAR_BIT
,
619 lttng_alignof(unsigned long) * CHAR_BIT
);
623 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
627 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
630 DBG("message successfully sent");
633 if (len
== -ECONNRESET
) {
634 DBG("remote end closed connection");
639 DBG("incorrect message size: %zd", len
);
645 void decrement_sem_count(unsigned int count
)
649 assert(uatomic_read(&sem_count
) >= count
);
651 if (uatomic_read(&sem_count
) <= 0) {
655 ret
= uatomic_add_return(&sem_count
, -count
);
657 ret
= sem_post(&constructor_wait
);
663 int handle_register_done(struct sock_info
*sock_info
)
665 if (sock_info
->registration_done
)
667 sock_info
->registration_done
= 1;
669 decrement_sem_count(1);
670 if (!sock_info
->statedump_pending
) {
671 sock_info
->initial_statedump_done
= 1;
672 decrement_sem_count(1);
679 int handle_register_failed(struct sock_info
*sock_info
)
681 if (sock_info
->registration_done
)
683 sock_info
->registration_done
= 1;
684 sock_info
->initial_statedump_done
= 1;
686 decrement_sem_count(2);
692 * Only execute pending statedump after the constructor semaphore has
693 * been posted by the current listener thread. This means statedump will
694 * only be performed after the "registration done" command is received
695 * from this thread's session daemon.
697 * This ensures we don't run into deadlock issues with the dynamic
698 * loader mutex, which is held while the constructor is called and
699 * waiting on the constructor semaphore. All operations requiring this
700 * dynamic loader lock need to be postponed using this mechanism.
702 * In a scenario with two session daemons connected to the application,
703 * it is possible that the first listener thread which receives the
704 * registration done command issues its statedump while the dynamic
705 * loader lock is still held by the application constructor waiting on
706 * the semaphore. It will however be allowed to proceed when the
707 * second session daemon sends the registration done command to the
708 * second listener thread. This situation therefore does not produce
712 void handle_pending_statedump(struct sock_info
*sock_info
)
714 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
715 sock_info
->statedump_pending
= 0;
716 pthread_mutex_lock(&ust_fork_mutex
);
717 lttng_handle_pending_statedump(sock_info
);
718 pthread_mutex_unlock(&ust_fork_mutex
);
720 if (!sock_info
->initial_statedump_done
) {
721 sock_info
->initial_statedump_done
= 1;
722 decrement_sem_count(1);
728 int handle_message(struct sock_info
*sock_info
,
729 int sock
, struct ustcomm_ust_msg
*lum
)
732 const struct lttng_ust_objd_ops
*ops
;
733 struct ustcomm_ust_reply lur
;
735 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
738 memset(&lur
, 0, sizeof(lur
));
741 ret
= -LTTNG_UST_ERR_EXITING
;
745 ops
= objd_ops(lum
->handle
);
752 case LTTNG_UST_REGISTER_DONE
:
753 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
754 ret
= handle_register_done(sock_info
);
758 case LTTNG_UST_RELEASE
:
759 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
762 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
764 case LTTNG_UST_FILTER
:
766 /* Receive filter data */
767 struct lttng_ust_filter_bytecode_node
*bytecode
;
769 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
770 ERR("Filter data size is too large: %u bytes",
771 lum
->u
.filter
.data_size
);
776 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
777 ERR("Filter reloc offset %u is not within data",
778 lum
->u
.filter
.reloc_offset
);
783 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
788 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
789 lum
->u
.filter
.data_size
);
791 case 0: /* orderly shutdown */
796 if (len
== lum
->u
.filter
.data_size
) {
797 DBG("filter data received");
799 } else if (len
< 0) {
800 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
801 if (len
== -ECONNRESET
) {
802 ERR("%s remote end closed connection", sock_info
->name
);
811 DBG("incorrect filter data message size: %zd", len
);
817 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
818 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
819 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
821 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
822 (unsigned long) bytecode
,
827 /* don't free bytecode if everything went fine. */
834 case LTTNG_UST_EXCLUSION
:
836 /* Receive exclusion names */
837 struct lttng_ust_excluder_node
*node
;
840 count
= lum
->u
.exclusion
.count
;
842 /* There are no names to read */
846 node
= zmalloc(sizeof(*node
) +
847 count
* LTTNG_UST_SYM_NAME_LEN
);
852 node
->excluder
.count
= count
;
853 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
854 count
* LTTNG_UST_SYM_NAME_LEN
);
856 case 0: /* orderly shutdown */
861 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
862 DBG("Exclusion data received");
864 } else if (len
< 0) {
865 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
866 if (len
== -ECONNRESET
) {
867 ERR("%s remote end closed connection", sock_info
->name
);
876 DBG("Incorrect exclusion data message size: %zd", len
);
883 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
884 (unsigned long) node
,
889 /* Don't free exclusion data if everything went fine. */
896 case LTTNG_UST_CHANNEL
:
901 len
= ustcomm_recv_channel_from_sessiond(sock
,
902 &chan_data
, lum
->u
.channel
.len
,
905 case 0: /* orderly shutdown */
909 if (len
== lum
->u
.channel
.len
) {
910 DBG("channel data received");
912 } else if (len
< 0) {
913 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
914 if (len
== -ECONNRESET
) {
915 ERR("%s remote end closed connection", sock_info
->name
);
922 DBG("incorrect channel data message size: %zd", len
);
927 args
.channel
.chan_data
= chan_data
;
928 args
.channel
.wakeup_fd
= wakeup_fd
;
930 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
931 (unsigned long) &lum
->u
,
935 if (args
.channel
.wakeup_fd
>= 0) {
938 lttng_ust_lock_fd_tracker();
939 close_ret
= close(args
.channel
.wakeup_fd
);
940 lttng_ust_unlock_fd_tracker();
941 args
.channel
.wakeup_fd
= -1;
945 free(args
.channel
.chan_data
);
948 case LTTNG_UST_STREAM
:
952 /* Receive shm_fd, wakeup_fd */
953 ret
= ustcomm_recv_stream_from_sessiond(sock
,
956 &args
.stream
.wakeup_fd
);
962 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
963 (unsigned long) &lum
->u
,
967 if (args
.stream
.shm_fd
>= 0) {
968 lttng_ust_lock_fd_tracker();
969 close_ret
= close(args
.stream
.shm_fd
);
970 lttng_ust_unlock_fd_tracker();
971 args
.stream
.shm_fd
= -1;
975 if (args
.stream
.wakeup_fd
>= 0) {
976 lttng_ust_lock_fd_tracker();
977 close_ret
= close(args
.stream
.wakeup_fd
);
978 lttng_ust_unlock_fd_tracker();
979 args
.stream
.wakeup_fd
= -1;
985 case LTTNG_UST_CONTEXT
:
986 switch (lum
->u
.context
.ctx
) {
987 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
990 size_t ctxlen
, recvlen
;
992 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
993 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
994 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
995 ERR("Application context string length size is too large: %zu bytes",
1000 strcpy(ctxstr
, "$app.");
1001 p
= &ctxstr
[strlen("$app.")];
1002 recvlen
= ctxlen
- strlen("$app.");
1003 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
1005 case 0: /* orderly shutdown */
1009 if (len
== recvlen
) {
1010 DBG("app context data received");
1012 } else if (len
< 0) {
1013 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1014 if (len
== -ECONNRESET
) {
1015 ERR("%s remote end closed connection", sock_info
->name
);
1022 DBG("incorrect app context data message size: %zd", len
);
1027 /* Put : between provider and ctxname. */
1028 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1029 args
.app_context
.ctxname
= ctxstr
;
1036 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1037 (unsigned long) &lum
->u
,
1045 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1046 (unsigned long) &lum
->u
,
1053 lur
.handle
= lum
->handle
;
1057 lur
.ret_code
= LTTNG_UST_OK
;
1060 * Use -LTTNG_UST_ERR as wildcard for UST internal
1061 * error that are not caused by the transport, except if
1062 * we already have a more precise error message to
1065 if (ret
> -LTTNG_UST_ERR
) {
1066 /* Translate code to UST error. */
1069 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1072 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1075 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1078 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1081 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1084 lur
.ret_code
= -LTTNG_UST_ERR
;
1093 case LTTNG_UST_TRACER_VERSION
:
1094 lur
.u
.version
= lum
->u
.version
;
1096 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1097 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1101 DBG("Return value: %d", lur
.ret_val
);
1106 * Performed delayed statedump operations outside of the UST
1107 * lock. We need to take the dynamic loader lock before we take
1108 * the UST lock internally within handle_pending_statedump().
1110 handle_pending_statedump(sock_info
);
1113 ret
= -LTTNG_UST_ERR_EXITING
;
1117 ret
= send_reply(sock
, &lur
);
1119 DBG("error sending reply");
1124 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1127 if (lur
.ret_code
== LTTNG_UST_OK
) {
1129 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1130 len
= ustcomm_send_unix_sock(sock
,
1131 &args
.field_list
.entry
,
1132 sizeof(args
.field_list
.entry
));
1137 if (len
!= sizeof(args
.field_list
.entry
)) {
1151 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1155 if (sock_info
->root_handle
!= -1) {
1156 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1158 ERR("Error unref root handle");
1160 sock_info
->root_handle
= -1;
1165 * wait_shm_mmap, socket and notify socket are used by listener
1166 * threads outside of the ust lock, so we cannot tear them down
1167 * ourselves, because we cannot join on these threads. Leave
1168 * responsibility of cleaning up these resources to the OS
1174 sock_info
->registration_done
= 0;
1175 sock_info
->initial_statedump_done
= 0;
1177 if (sock_info
->socket
!= -1) {
1178 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1180 ERR("Error closing ust cmd socket");
1182 sock_info
->socket
= -1;
1184 if (sock_info
->notify_socket
!= -1) {
1185 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1187 ERR("Error closing ust notify socket");
1189 sock_info
->notify_socket
= -1;
1191 if (sock_info
->wait_shm_mmap
) {
1194 page_size
= sysconf(_SC_PAGE_SIZE
);
1195 if (page_size
<= 0) {
1199 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1201 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1203 ERR("Error unmapping wait shm");
1206 sock_info
->wait_shm_mmap
= NULL
;
1211 * Using fork to set umask in the child process (not multi-thread safe).
1212 * We deal with the shm_open vs ftruncate race (happening when the
1213 * sessiond owns the shm and does not let everybody modify it, to ensure
1214 * safety against shm_unlink) by simply letting the mmap fail and
1215 * retrying after a few seconds.
1216 * For global shm, everybody has rw access to it until the sessiond
1220 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1222 int wait_shm_fd
, ret
;
1226 * Try to open read-only.
1228 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1229 if (wait_shm_fd
>= 0) {
1232 size_t bytes_read
= 0;
1235 * Try to read the fd. If unable to do so, try opening
1239 len
= read(wait_shm_fd
,
1240 &((char *) &tmp_read
)[bytes_read
],
1241 sizeof(tmp_read
) - bytes_read
);
1245 } while ((len
< 0 && errno
== EINTR
)
1246 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1247 if (bytes_read
!= sizeof(tmp_read
)) {
1248 ret
= close(wait_shm_fd
);
1250 ERR("close wait_shm_fd");
1255 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1257 * Real-only open did not work, and it's not because the
1258 * entry was not present. It's a failure that prohibits
1261 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1267 * If the open failed because the file did not exist, or because
1268 * the file was not truncated yet, try creating it ourself.
1270 URCU_TLS(lttng_ust_nest_count
)++;
1272 URCU_TLS(lttng_ust_nest_count
)--;
1277 * Parent: wait for child to return, in which case the
1278 * shared memory map will have been created.
1280 pid
= wait(&status
);
1281 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1286 * Try to open read-only again after creation.
1288 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1289 if (wait_shm_fd
< 0) {
1291 * Real-only open did not work. It's a failure
1292 * that prohibits using shm.
1294 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1298 } else if (pid
== 0) {
1302 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1303 if (sock_info
->global
)
1304 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1306 * We're alone in a child process, so we can modify the
1307 * process-wide umask.
1309 umask(~create_mode
);
1311 * Try creating shm (or get rw access).
1312 * We don't do an exclusive open, because we allow other
1313 * processes to create+ftruncate it concurrently.
1315 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1316 O_RDWR
| O_CREAT
, create_mode
);
1317 if (wait_shm_fd
>= 0) {
1318 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1320 PERROR("ftruncate");
1321 _exit(EXIT_FAILURE
);
1323 _exit(EXIT_SUCCESS
);
1326 * For local shm, we need to have rw access to accept
1327 * opening it: this means the local sessiond will be
1328 * able to wake us up. For global shm, we open it even
1329 * if rw access is not granted, because the root.root
1330 * sessiond will be able to override all rights and wake
1333 if (!sock_info
->global
&& errno
!= EACCES
) {
1334 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1335 _exit(EXIT_FAILURE
);
1338 * The shm exists, but we cannot open it RW. Report
1341 _exit(EXIT_SUCCESS
);
1346 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1347 struct stat statbuf
;
1350 * Ensure that our user is the owner of the shm file for
1351 * local shm. If we do not own the file, it means our
1352 * sessiond will not have access to wake us up (there is
1353 * probably a rogue process trying to fake our
1354 * sessiond). Fallback to polling method in this case.
1356 ret
= fstat(wait_shm_fd
, &statbuf
);
1361 if (statbuf
.st_uid
!= getuid())
1367 ret
= close(wait_shm_fd
);
1369 PERROR("Error closing fd");
1375 char *get_map_shm(struct sock_info
*sock_info
)
1378 int wait_shm_fd
, ret
;
1379 char *wait_shm_mmap
;
1381 page_size
= sysconf(_SC_PAGE_SIZE
);
1382 if (page_size
<= 0) {
1386 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1390 lttng_ust_lock_fd_tracker();
1391 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1392 if (wait_shm_fd
< 0) {
1393 lttng_ust_unlock_fd_tracker();
1397 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1399 ret
= close(wait_shm_fd
);
1401 PERROR("Error closing fd");
1403 lttng_ust_unlock_fd_tracker();
1408 lttng_ust_unlock_fd_tracker();
1410 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1411 MAP_SHARED
, wait_shm_fd
, 0);
1413 /* close shm fd immediately after taking the mmap reference */
1414 lttng_ust_lock_fd_tracker();
1415 ret
= close(wait_shm_fd
);
1417 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1419 PERROR("Error closing fd");
1421 lttng_ust_unlock_fd_tracker();
1423 if (wait_shm_mmap
== MAP_FAILED
) {
1424 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1427 return wait_shm_mmap
;
1434 void wait_for_sessiond(struct sock_info
*sock_info
)
1436 /* Use ust_lock to check if we should quit. */
1440 if (wait_poll_fallback
) {
1445 assert(sock_info
->wait_shm_mmap
);
1447 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1448 /* Wait for futex wakeup */
1449 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1452 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1453 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1456 /* Value already changed. */
1459 /* Retry if interrupted by signal. */
1460 break; /* Get out of switch. */
1462 wait_poll_fallback
= 1;
1464 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1465 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1466 "Please upgrade your kernel "
1467 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1468 "mainline). LTTng-UST will use polling mode fallback.");
1487 * This thread does not allocate any resource, except within
1488 * handle_message, within mutex protection. This mutex protects against
1490 * The other moment it allocates resources is at socket connection, which
1491 * is also protected by the mutex.
1494 void *ust_listener_thread(void *arg
)
1496 struct sock_info
*sock_info
= arg
;
1497 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1500 lttng_ust_fixup_tls();
1502 * If available, add '-ust' to the end of this thread's
1505 ret
= lttng_ust_setustprocname();
1507 ERR("Unable to set UST process name");
1510 /* Restart trying to connect to the session daemon */
1512 if (prev_connect_failed
) {
1513 /* Wait for sessiond availability with pipe */
1514 wait_for_sessiond(sock_info
);
1518 * Sleep for 5 seconds before retrying after a
1519 * sequence of failure / wait / failure. This
1520 * deals with a killed or broken session daemon.
1526 prev_connect_failed
= 0;
1533 if (sock_info
->socket
!= -1) {
1534 /* FD tracker is updated by ustcomm_close_unix_sock() */
1535 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1537 ERR("Error closing %s ust cmd socket",
1540 sock_info
->socket
= -1;
1542 if (sock_info
->notify_socket
!= -1) {
1543 /* FD tracker is updated by ustcomm_close_unix_sock() */
1544 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1546 ERR("Error closing %s ust notify socket",
1549 sock_info
->notify_socket
= -1;
1554 * Register. We need to perform both connect and sending
1555 * registration message before doing the next connect otherwise
1556 * we may reach unix socket connect queue max limits and block
1557 * on the 2nd connect while the session daemon is awaiting the
1558 * first connect registration message.
1560 /* Connect cmd socket */
1561 lttng_ust_lock_fd_tracker();
1562 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1563 get_connect_sock_timeout());
1565 lttng_ust_unlock_fd_tracker();
1566 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1567 prev_connect_failed
= 1;
1570 * If we cannot find the sessiond daemon, don't delay
1571 * constructor execution.
1573 ret
= handle_register_failed(sock_info
);
1579 ret
= lttng_ust_add_fd_to_tracker(fd
);
1583 PERROR("close on sock_info->socket");
1586 lttng_ust_unlock_fd_tracker();
1591 sock_info
->socket
= ret
;
1592 lttng_ust_unlock_fd_tracker();
1596 * Unlock/relock ust lock because connect is blocking (with
1597 * timeout). Don't delay constructors on the ust lock for too
1605 * Create only one root handle per listener thread for the whole
1606 * process lifetime, so we ensure we get ID which is statically
1607 * assigned to the root handle.
1609 if (sock_info
->root_handle
== -1) {
1610 ret
= lttng_abi_create_root_handle();
1612 ERR("Error creating root handle");
1615 sock_info
->root_handle
= ret
;
1618 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1620 ERR("Error registering to %s ust cmd socket",
1622 prev_connect_failed
= 1;
1624 * If we cannot register to the sessiond daemon, don't
1625 * delay constructor execution.
1627 ret
= handle_register_failed(sock_info
);
1635 * Unlock/relock ust lock because connect is blocking (with
1636 * timeout). Don't delay constructors on the ust lock for too
1643 /* Connect notify socket */
1644 lttng_ust_lock_fd_tracker();
1645 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1646 get_connect_sock_timeout());
1648 lttng_ust_unlock_fd_tracker();
1649 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1650 prev_connect_failed
= 1;
1653 * If we cannot find the sessiond daemon, don't delay
1654 * constructor execution.
1656 ret
= handle_register_failed(sock_info
);
1663 ret
= lttng_ust_add_fd_to_tracker(fd
);
1667 PERROR("close on sock_info->notify_socket");
1670 lttng_ust_unlock_fd_tracker();
1675 sock_info
->notify_socket
= ret
;
1676 lttng_ust_unlock_fd_tracker();
1680 * Unlock/relock ust lock because connect is blocking (with
1681 * timeout). Don't delay constructors on the ust lock for too
1688 timeout
= get_notify_sock_timeout();
1691 * Give at least 10ms to sessiond to reply to
1696 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1699 WARN("Error setting socket receive timeout");
1701 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1704 WARN("Error setting socket send timeout");
1706 } else if (timeout
< -1) {
1707 WARN("Unsupported timeout value %ld", timeout
);
1710 ret
= register_to_sessiond(sock_info
->notify_socket
,
1711 USTCTL_SOCKET_NOTIFY
);
1713 ERR("Error registering to %s ust notify socket",
1715 prev_connect_failed
= 1;
1717 * If we cannot register to the sessiond daemon, don't
1718 * delay constructor execution.
1720 ret
= handle_register_failed(sock_info
);
1725 sock
= sock_info
->socket
;
1731 struct ustcomm_ust_msg lum
;
1733 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1735 case 0: /* orderly shutdown */
1736 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1741 * Either sessiond has shutdown or refused us by closing the socket.
1742 * In either case, we don't want to delay construction execution,
1743 * and we need to wait before retry.
1745 prev_connect_failed
= 1;
1747 * If we cannot register to the sessiond daemon, don't
1748 * delay constructor execution.
1750 ret
= handle_register_failed(sock_info
);
1755 print_cmd(lum
.cmd
, lum
.handle
);
1756 ret
= handle_message(sock_info
, sock
, &lum
);
1758 ERR("Error handling message for %s socket",
1761 * Close socket if protocol error is
1769 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1771 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1773 if (len
== -ECONNRESET
) {
1774 DBG("%s remote end closed connection", sock_info
->name
);
1785 /* Cleanup socket handles before trying to reconnect */
1786 lttng_ust_objd_table_owner_cleanup(sock_info
);
1788 goto restart
; /* try to reconnect */
1793 pthread_mutex_lock(&ust_exit_mutex
);
1794 sock_info
->thread_active
= 0;
1795 pthread_mutex_unlock(&ust_exit_mutex
);
1800 * Weak symbol to call when the ust malloc wrapper is not loaded.
1802 __attribute__((weak
))
1803 void lttng_ust_malloc_wrapper_init(void)
1808 * sessiond monitoring thread: monitor presence of global and per-user
1809 * sessiond by polling the application common named pipe.
1811 void __attribute__((constructor
)) lttng_ust_init(void)
1813 struct timespec constructor_timeout
;
1814 sigset_t sig_all_blocked
, orig_parent_mask
;
1815 pthread_attr_t thread_attr
;
1820 if (uatomic_xchg(&initialized
, 1) == 1)
1824 * Fixup interdependency between TLS fixup mutex (which happens
1825 * to be the dynamic linker mutex) and ust_lock, taken within
1828 lttng_ust_fixup_tls();
1830 lttng_ust_loaded
= 1;
1833 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1834 * the unloading of code used by the ust_listener_threads as we can not
1835 * reliably know when they exited. To do that, manually load
1836 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1837 * this library so it never becomes zero, thus never gets unloaded from the
1838 * address space of the process. Since we are already running in the
1839 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1840 * simply increment the refcount and no additionnal work is needed by the
1841 * dynamic loader as the shared library is already loaded in the address
1842 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1843 * unloading of the UST library if its refcount becomes zero (which should
1844 * never happen). Do the return value check but discard the handle at the
1845 * end of the function as it's not needed.
1847 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1849 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1853 * We want precise control over the order in which we construct
1854 * our sub-libraries vs starting to receive commands from
1855 * sessiond (otherwise leading to errors when trying to create
1856 * sessiond before the init functions are completed).
1859 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1861 lttng_ust_init_fd_tracker();
1862 lttng_ust_clock_init();
1863 lttng_ust_getcpu_init();
1864 lttng_ust_statedump_init();
1865 lttng_ring_buffer_metadata_client_init();
1866 lttng_ring_buffer_client_overwrite_init();
1867 lttng_ring_buffer_client_overwrite_rt_init();
1868 lttng_ring_buffer_client_discard_init();
1869 lttng_ring_buffer_client_discard_rt_init();
1870 lttng_perf_counter_init();
1872 * Invoke ust malloc wrapper init before starting other threads.
1874 lttng_ust_malloc_wrapper_init();
1876 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1878 get_allow_blocking();
1880 ret
= sem_init(&constructor_wait
, 0, 0);
1885 ret
= setup_global_apps();
1887 assert(global_apps
.allowed
== 0);
1888 DBG("global apps setup returned %d", ret
);
1891 ret
= setup_local_apps();
1893 assert(local_apps
.allowed
== 0);
1894 DBG("local apps setup returned %d", ret
);
1897 /* A new thread created by pthread_create inherits the signal mask
1898 * from the parent. To avoid any signal being received by the
1899 * listener thread, we block all signals temporarily in the parent,
1900 * while we create the listener thread.
1902 sigfillset(&sig_all_blocked
);
1903 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1905 ERR("pthread_sigmask: %s", strerror(ret
));
1908 ret
= pthread_attr_init(&thread_attr
);
1910 ERR("pthread_attr_init: %s", strerror(ret
));
1912 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1914 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1917 if (global_apps
.allowed
) {
1918 pthread_mutex_lock(&ust_exit_mutex
);
1919 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1920 ust_listener_thread
, &global_apps
);
1922 ERR("pthread_create global: %s", strerror(ret
));
1924 global_apps
.thread_active
= 1;
1925 pthread_mutex_unlock(&ust_exit_mutex
);
1927 handle_register_done(&global_apps
);
1930 if (local_apps
.allowed
) {
1931 pthread_mutex_lock(&ust_exit_mutex
);
1932 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1933 ust_listener_thread
, &local_apps
);
1935 ERR("pthread_create local: %s", strerror(ret
));
1937 local_apps
.thread_active
= 1;
1938 pthread_mutex_unlock(&ust_exit_mutex
);
1940 handle_register_done(&local_apps
);
1942 ret
= pthread_attr_destroy(&thread_attr
);
1944 ERR("pthread_attr_destroy: %s", strerror(ret
));
1947 /* Restore original signal mask in parent */
1948 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1950 ERR("pthread_sigmask: %s", strerror(ret
));
1953 switch (timeout_mode
) {
1954 case 1: /* timeout wait */
1956 ret
= sem_timedwait(&constructor_wait
,
1957 &constructor_timeout
);
1958 } while (ret
< 0 && errno
== EINTR
);
1962 ERR("Timed out waiting for lttng-sessiond");
1965 PERROR("sem_timedwait");
1968 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1973 case -1:/* wait forever */
1975 ret
= sem_wait(&constructor_wait
);
1976 } while (ret
< 0 && errno
== EINTR
);
1983 ERR("Unexpected error \"%s\" returned by sem_wait",
1988 case 0: /* no timeout */
1994 void lttng_ust_cleanup(int exiting
)
1996 cleanup_sock_info(&global_apps
, exiting
);
1997 cleanup_sock_info(&local_apps
, exiting
);
1998 local_apps
.allowed
= 0;
1999 global_apps
.allowed
= 0;
2001 * The teardown in this function all affect data structures
2002 * accessed under the UST lock by the listener thread. This
2003 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2004 * that none of these threads are accessing this data at this
2007 lttng_ust_abi_exit();
2008 lttng_ust_events_exit();
2009 lttng_perf_counter_exit();
2010 lttng_ring_buffer_client_discard_rt_exit();
2011 lttng_ring_buffer_client_discard_exit();
2012 lttng_ring_buffer_client_overwrite_rt_exit();
2013 lttng_ring_buffer_client_overwrite_exit();
2014 lttng_ring_buffer_metadata_client_exit();
2015 lttng_ust_statedump_destroy();
2018 /* Reinitialize values for fork */
2019 sem_count
= sem_count_initial_value
;
2020 lttng_ust_comm_should_quit
= 0;
2025 void __attribute__((destructor
)) lttng_ust_exit(void)
2030 * Using pthread_cancel here because:
2031 * A) we don't want to hang application teardown.
2032 * B) the thread is not allocating any resource.
2036 * Require the communication thread to quit. Synchronize with
2037 * mutexes to ensure it is not in a mutex critical section when
2038 * pthread_cancel is later called.
2041 lttng_ust_comm_should_quit
= 1;
2044 pthread_mutex_lock(&ust_exit_mutex
);
2045 /* cancel threads */
2046 if (global_apps
.thread_active
) {
2047 ret
= pthread_cancel(global_apps
.ust_listener
);
2049 ERR("Error cancelling global ust listener thread: %s",
2052 global_apps
.thread_active
= 0;
2055 if (local_apps
.thread_active
) {
2056 ret
= pthread_cancel(local_apps
.ust_listener
);
2058 ERR("Error cancelling local ust listener thread: %s",
2061 local_apps
.thread_active
= 0;
2064 pthread_mutex_unlock(&ust_exit_mutex
);
2067 * Do NOT join threads: use of sys_futex makes it impossible to
2068 * join the threads without using async-cancel, but async-cancel
2069 * is delivered by a signal, which could hit the target thread
2070 * anywhere in its code path, including while the ust_lock() is
2071 * held, causing a deadlock for the other thread. Let the OS
2072 * cleanup the threads if there are stalled in a syscall.
2074 lttng_ust_cleanup(1);
2078 void ust_context_ns_reset(void)
2080 lttng_context_pid_ns_reset();
2081 lttng_context_cgroup_ns_reset();
2082 lttng_context_ipc_ns_reset();
2083 lttng_context_mnt_ns_reset();
2084 lttng_context_net_ns_reset();
2085 lttng_context_user_ns_reset();
2086 lttng_context_uts_ns_reset();
2090 void ust_context_vuids_reset(void)
2092 lttng_context_vuid_reset();
2093 lttng_context_veuid_reset();
2094 lttng_context_vsuid_reset();
2098 void ust_context_vgids_reset(void)
2100 lttng_context_vgid_reset();
2101 lttng_context_vegid_reset();
2102 lttng_context_vsgid_reset();
2106 * We exclude the worker threads across fork and clone (except
2107 * CLONE_VM), because these system calls only keep the forking thread
2108 * running in the child. Therefore, we don't want to call fork or clone
2109 * in the middle of an tracepoint or ust tracing state modification.
2110 * Holding this mutex protects these structures across fork and clone.
2112 void ust_before_fork(sigset_t
*save_sigset
)
2115 * Disable signals. This is to avoid that the child intervenes
2116 * before it is properly setup for tracing. It is safer to
2117 * disable all signals, because then we know we are not breaking
2118 * anything by restoring the original mask.
2123 /* Fixup lttng-ust TLS. */
2124 lttng_ust_fixup_tls();
2126 if (URCU_TLS(lttng_ust_nest_count
))
2128 /* Disable signals */
2129 sigfillset(&all_sigs
);
2130 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2132 PERROR("sigprocmask");
2135 pthread_mutex_lock(&ust_fork_mutex
);
2138 urcu_bp_before_fork();
2139 lttng_ust_lock_fd_tracker();
2143 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2147 DBG("process %d", getpid());
2148 lttng_perf_unlock();
2149 lttng_ust_unlock_fd_tracker();
2152 pthread_mutex_unlock(&ust_fork_mutex
);
2154 /* Restore signals */
2155 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2157 PERROR("sigprocmask");
2161 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2163 if (URCU_TLS(lttng_ust_nest_count
))
2165 DBG("process %d", getpid());
2166 urcu_bp_after_fork_parent();
2167 /* Release mutexes and reenable signals */
2168 ust_after_fork_common(restore_sigset
);
2172 * After fork, in the child, we need to cleanup all the leftover state,
2173 * except the worker thread which already magically disappeared thanks
2174 * to the weird Linux fork semantics. After tyding up, we call
2175 * lttng_ust_init() again to start over as a new PID.
2177 * This is meant for forks() that have tracing in the child between the
2178 * fork and following exec call (if there is any).
2180 void ust_after_fork_child(sigset_t
*restore_sigset
)
2182 if (URCU_TLS(lttng_ust_nest_count
))
2184 lttng_context_vpid_reset();
2185 lttng_context_vtid_reset();
2186 lttng_context_procname_reset();
2187 ust_context_ns_reset();
2188 ust_context_vuids_reset();
2189 ust_context_vgids_reset();
2190 DBG("process %d", getpid());
2191 /* Release urcu mutexes */
2192 urcu_bp_after_fork_child();
2193 lttng_ust_cleanup(0);
2194 /* Release mutexes and reenable signals */
2195 ust_after_fork_common(restore_sigset
);
2199 void ust_after_setns(void)
2201 ust_context_ns_reset();
2202 ust_context_vuids_reset();
2203 ust_context_vgids_reset();
2206 void ust_after_unshare(void)
2208 ust_context_ns_reset();
2209 ust_context_vuids_reset();
2210 ust_context_vgids_reset();
2213 void ust_after_setuid(void)
2215 ust_context_vuids_reset();
2218 void ust_after_seteuid(void)
2220 ust_context_vuids_reset();
2223 void ust_after_setreuid(void)
2225 ust_context_vuids_reset();
2228 void ust_after_setresuid(void)
2230 ust_context_vuids_reset();
2233 void ust_after_setgid(void)
2235 ust_context_vgids_reset();
2238 void ust_after_setegid(void)
2240 ust_context_vgids_reset();
2243 void ust_after_setregid(void)
2245 ust_context_vgids_reset();
2248 void ust_after_setresgid(void)
2250 ust_context_vgids_reset();
2253 void lttng_ust_sockinfo_session_enabled(void *owner
)
2255 struct sock_info
*sock_info
= owner
;
2256 sock_info
->statedump_pending
= 1;