4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <semaphore.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/futex.h>
42 #include <urcu/compiler.h>
44 #include <lttng/ust-events.h>
45 #include <lttng/ust-abi.h>
46 #include <lttng/ust.h>
47 #include <lttng/ust-error.h>
48 #include <lttng/ust-ctl.h>
49 #include <urcu/tls-compat.h>
52 #include <usterr-signal-safe.h>
54 #include "tracepoint-internal.h"
55 #include "lttng-tracer-core.h"
57 #include "../libringbuffer/rb-init.h"
58 #include "lttng-ust-statedump.h"
60 #include "../libringbuffer/getcpu.h"
63 /* Concatenate lttng ust shared library name with its major version number. */
64 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." LTTNG_UST_LIBRARY_VERSION_MAJOR
67 * Has lttng ust comm constructor been called ?
69 static int initialized
;
72 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
73 * Held when handling a command, also held by fork() to deal with
74 * removal of threads, and by exit path.
76 * The UST lock is the centralized mutex across UST tracing control and
79 * ust_exit_mutex must never nest in ust_mutex.
81 * ust_fork_mutex must never nest in ust_mutex.
83 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
84 * counter lazy initialization called by events within the statedump,
85 * which traces while the ust_mutex is held.
87 * ust_lock nests within the dynamic loader lock (within glibc) because
88 * it is taken within the library constructor.
90 * The ust fd tracker lock nests within the ust_mutex.
92 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
94 /* Allow nesting the ust_mutex within the same thread. */
95 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
98 * ust_exit_mutex protects thread_active variable wrt thread exit. It
99 * cannot be done by ust_mutex because pthread_cancel(), which takes an
100 * internal libc lock, cannot nest within ust_mutex.
102 * It never nests within a ust_mutex.
104 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 * ust_fork_mutex protects base address statedump tracing against forks. It
108 * prevents the dynamic loader lock to be taken (by base address statedump
109 * tracing) while a fork is happening, thus preventing deadlock issues with
110 * the dynamic loader lock.
112 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
114 /* Should the ust comm thread quit ? */
115 static int lttng_ust_comm_should_quit
;
118 * This variable can be tested by applications to check whether
119 * lttng-ust is loaded. They simply have to define their own
120 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
121 * library constructor.
123 int lttng_ust_loaded
__attribute__((weak
));
126 * Return 0 on success, -1 if should quit.
127 * The lock is taken in both cases.
132 sigset_t sig_all_blocked
, orig_mask
;
135 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
137 ERR("pthread_setcancelstate: %s", strerror(ret
));
139 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
140 ERR("pthread_setcancelstate: unexpected oldstate");
142 sigfillset(&sig_all_blocked
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (!URCU_TLS(ust_mutex_nest
)++)
148 pthread_mutex_lock(&ust_mutex
);
149 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
151 ERR("pthread_sigmask: %s", strerror(ret
));
153 if (lttng_ust_comm_should_quit
) {
161 * ust_lock_nocheck() can be used in constructors/destructors, because
162 * they are already nested within the dynamic loader lock, and therefore
163 * have exclusive access against execution of liblttng-ust destructor.
166 void ust_lock_nocheck(void)
168 sigset_t sig_all_blocked
, orig_mask
;
171 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
173 ERR("pthread_setcancelstate: %s", strerror(ret
));
175 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
176 ERR("pthread_setcancelstate: unexpected oldstate");
178 sigfillset(&sig_all_blocked
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
183 if (!URCU_TLS(ust_mutex_nest
)++)
184 pthread_mutex_lock(&ust_mutex
);
185 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
187 ERR("pthread_sigmask: %s", strerror(ret
));
194 void ust_unlock(void)
196 sigset_t sig_all_blocked
, orig_mask
;
199 sigfillset(&sig_all_blocked
);
200 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
202 ERR("pthread_sigmask: %s", strerror(ret
));
204 if (!--URCU_TLS(ust_mutex_nest
))
205 pthread_mutex_unlock(&ust_mutex
);
206 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
208 ERR("pthread_sigmask: %s", strerror(ret
));
210 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
212 ERR("pthread_setcancelstate: %s", strerror(ret
));
214 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
215 ERR("pthread_setcancelstate: unexpected oldstate");
220 * Wait for either of these before continuing to the main
222 * - the register_done message from sessiond daemon
223 * (will let the sessiond daemon enable sessions before main
225 * - sessiond daemon is not reachable.
226 * - timeout (ensuring applications are resilient to session
229 static sem_t constructor_wait
;
231 * Doing this for both the global and local sessiond.
234 sem_count_initial_value
= 4,
237 static int sem_count
= sem_count_initial_value
;
240 * Counting nesting within lttng-ust. Used to ensure that calling fork()
241 * from liblttng-ust does not execute the pre/post fork handlers.
243 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
246 * Info about socket and associated listener thread.
250 pthread_t ust_listener
; /* listener thread */
252 int registration_done
;
257 char sock_path
[PATH_MAX
];
261 char wait_shm_path
[PATH_MAX
];
263 /* Keep track of lazy state dump not performed yet. */
264 int statedump_pending
;
265 int initial_statedump_done
;
268 /* Socket from app (connect) to session daemon (listen) for communication */
269 struct sock_info global_apps
= {
274 .registration_done
= 0,
278 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
282 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
284 .statedump_pending
= 0,
285 .initial_statedump_done
= 0,
288 /* TODO: allow global_apps_sock_path override */
290 struct sock_info local_apps
= {
294 .registration_done
= 0,
295 .allowed
= 0, /* Check setuid bit first */
301 .statedump_pending
= 0,
302 .initial_statedump_done
= 0,
305 static int wait_poll_fallback
;
307 static const char *cmd_name_mapping
[] = {
308 [ LTTNG_UST_RELEASE
] = "Release",
309 [ LTTNG_UST_SESSION
] = "Create Session",
310 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
312 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
313 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
314 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
315 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
317 /* Session FD commands */
318 [ LTTNG_UST_CHANNEL
] = "Create Channel",
319 [ LTTNG_UST_SESSION_START
] = "Start Session",
320 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
322 /* Channel FD commands */
323 [ LTTNG_UST_STREAM
] = "Create Stream",
324 [ LTTNG_UST_EVENT
] = "Create Event",
326 /* Event and Channel FD commands */
327 [ LTTNG_UST_CONTEXT
] = "Create Context",
328 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
330 /* Event, Channel and Session commands */
331 [ LTTNG_UST_ENABLE
] = "Enable",
332 [ LTTNG_UST_DISABLE
] = "Disable",
334 /* Tracepoint list commands */
335 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
336 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
338 /* Event FD commands */
339 [ LTTNG_UST_FILTER
] = "Create Filter",
340 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
343 static const char *str_timeout
;
344 static int got_timeout_env
;
346 extern void lttng_ring_buffer_client_overwrite_init(void);
347 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
348 extern void lttng_ring_buffer_client_discard_init(void);
349 extern void lttng_ring_buffer_client_discard_rt_init(void);
350 extern void lttng_ring_buffer_metadata_client_init(void);
351 extern void lttng_ring_buffer_client_overwrite_exit(void);
352 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
353 extern void lttng_ring_buffer_client_discard_exit(void);
354 extern void lttng_ring_buffer_client_discard_rt_exit(void);
355 extern void lttng_ring_buffer_metadata_client_exit(void);
357 static char *get_map_shm(struct sock_info
*sock_info
);
359 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
362 size_t copied
= 0, to_copy
= len
;
365 ret
= read(fd
, buf
+ copied
, to_copy
);
370 } while ((ret
> 0 && to_copy
> 0)
371 || (ret
< 0 && errno
== EINTR
));
378 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
382 const char *get_lttng_home_dir(void)
386 val
= (const char *) lttng_getenv("LTTNG_HOME");
390 return (const char *) lttng_getenv("HOME");
394 * Force a read (imply TLS fixup for dlopen) of TLS variables.
397 void lttng_fixup_nest_count_tls(void)
399 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
403 void lttng_fixup_ust_mutex_nest_tls(void)
405 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
412 void lttng_fixup_urcu_bp_tls(void)
418 void lttng_ust_fixup_tls(void)
420 lttng_fixup_urcu_bp_tls();
421 lttng_fixup_ringbuffer_tls();
422 lttng_fixup_vtid_tls();
423 lttng_fixup_nest_count_tls();
424 lttng_fixup_procname_tls();
425 lttng_fixup_ust_mutex_nest_tls();
426 lttng_ust_fixup_perf_counter_tls();
427 lttng_ust_fixup_fd_tracker_tls();
430 int lttng_get_notify_socket(void *owner
)
432 struct sock_info
*info
= owner
;
434 return info
->notify_socket
;
438 void print_cmd(int cmd
, int handle
)
440 const char *cmd_name
= "Unknown";
442 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
443 && cmd_name_mapping
[cmd
]) {
444 cmd_name
= cmd_name_mapping
[cmd
];
446 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
448 lttng_ust_obj_get_name(handle
), handle
);
452 int setup_global_apps(void)
455 assert(!global_apps
.wait_shm_mmap
);
457 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
458 if (!global_apps
.wait_shm_mmap
) {
459 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
460 global_apps
.allowed
= 0;
465 global_apps
.allowed
= 1;
470 int setup_local_apps(void)
473 const char *home_dir
;
476 assert(!local_apps
.wait_shm_mmap
);
480 * Disallow per-user tracing for setuid binaries.
482 if (uid
!= geteuid()) {
483 assert(local_apps
.allowed
== 0);
487 home_dir
= get_lttng_home_dir();
489 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
490 assert(local_apps
.allowed
== 0);
494 local_apps
.allowed
= 1;
495 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
497 LTTNG_DEFAULT_HOME_RUNDIR
,
498 LTTNG_UST_SOCK_FILENAME
);
499 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
500 LTTNG_UST_WAIT_FILENAME
,
503 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
504 if (!local_apps
.wait_shm_mmap
) {
505 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
506 local_apps
.allowed
= 0;
515 * Get socket timeout, in ms.
516 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
519 long get_timeout(void)
521 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
523 if (!got_timeout_env
) {
524 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
528 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
529 /* All negative values are considered as "-1". */
530 if (constructor_delay_ms
< -1)
531 constructor_delay_ms
= -1;
532 return constructor_delay_ms
;
535 /* Timeout for notify socket send and recv. */
537 long get_notify_sock_timeout(void)
539 return get_timeout();
542 /* Timeout for connecting to cmd and notify sockets. */
544 long get_connect_sock_timeout(void)
546 return get_timeout();
550 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
553 int get_constructor_timeout(struct timespec
*constructor_timeout
)
555 long constructor_delay_ms
;
558 constructor_delay_ms
= get_timeout();
560 switch (constructor_delay_ms
) {
561 case -1:/* fall-through */
563 return constructor_delay_ms
;
569 * If we are unable to find the current time, don't wait.
571 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
576 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
577 constructor_timeout
->tv_nsec
+=
578 (constructor_delay_ms
% 1000UL) * 1000000UL;
579 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
580 constructor_timeout
->tv_sec
++;
581 constructor_timeout
->tv_nsec
-= 1000000000UL;
583 /* Timeout wait (constructor_delay_ms). */
588 void get_allow_blocking(void)
590 const char *str_allow_blocking
=
591 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
593 if (str_allow_blocking
) {
594 DBG("%s environment variable is set",
595 "LTTNG_UST_ALLOW_BLOCKING");
596 lttng_ust_ringbuffer_set_allow_blocking();
601 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
603 return ustcomm_send_reg_msg(socket
,
606 lttng_alignof(uint8_t) * CHAR_BIT
,
607 lttng_alignof(uint16_t) * CHAR_BIT
,
608 lttng_alignof(uint32_t) * CHAR_BIT
,
609 lttng_alignof(uint64_t) * CHAR_BIT
,
610 lttng_alignof(unsigned long) * CHAR_BIT
);
614 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
618 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
621 DBG("message successfully sent");
624 if (len
== -ECONNRESET
) {
625 DBG("remote end closed connection");
630 DBG("incorrect message size: %zd", len
);
636 void decrement_sem_count(unsigned int count
)
640 assert(uatomic_read(&sem_count
) >= count
);
642 if (uatomic_read(&sem_count
) <= 0) {
646 ret
= uatomic_add_return(&sem_count
, -count
);
648 ret
= sem_post(&constructor_wait
);
654 int handle_register_done(struct sock_info
*sock_info
)
656 if (sock_info
->registration_done
)
658 sock_info
->registration_done
= 1;
660 decrement_sem_count(1);
661 if (!sock_info
->statedump_pending
) {
662 sock_info
->initial_statedump_done
= 1;
663 decrement_sem_count(1);
670 int handle_register_failed(struct sock_info
*sock_info
)
672 if (sock_info
->registration_done
)
674 sock_info
->registration_done
= 1;
675 sock_info
->initial_statedump_done
= 1;
677 decrement_sem_count(2);
683 * Only execute pending statedump after the constructor semaphore has
684 * been posted by the current listener thread. This means statedump will
685 * only be performed after the "registration done" command is received
686 * from this thread's session daemon.
688 * This ensures we don't run into deadlock issues with the dynamic
689 * loader mutex, which is held while the constructor is called and
690 * waiting on the constructor semaphore. All operations requiring this
691 * dynamic loader lock need to be postponed using this mechanism.
693 * In a scenario with two session daemons connected to the application,
694 * it is possible that the first listener thread which receives the
695 * registration done command issues its statedump while the dynamic
696 * loader lock is still held by the application constructor waiting on
697 * the semaphore. It will however be allowed to proceed when the
698 * second session daemon sends the registration done command to the
699 * second listener thread. This situation therefore does not produce
703 void handle_pending_statedump(struct sock_info
*sock_info
)
705 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
706 sock_info
->statedump_pending
= 0;
707 pthread_mutex_lock(&ust_fork_mutex
);
708 lttng_handle_pending_statedump(sock_info
);
709 pthread_mutex_unlock(&ust_fork_mutex
);
711 if (!sock_info
->initial_statedump_done
) {
712 sock_info
->initial_statedump_done
= 1;
713 decrement_sem_count(1);
719 int handle_message(struct sock_info
*sock_info
,
720 int sock
, struct ustcomm_ust_msg
*lum
)
723 const struct lttng_ust_objd_ops
*ops
;
724 struct ustcomm_ust_reply lur
;
726 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
729 memset(&lur
, 0, sizeof(lur
));
732 ret
= -LTTNG_UST_ERR_EXITING
;
736 ops
= objd_ops(lum
->handle
);
743 case LTTNG_UST_REGISTER_DONE
:
744 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
745 ret
= handle_register_done(sock_info
);
749 case LTTNG_UST_RELEASE
:
750 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
753 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
755 case LTTNG_UST_FILTER
:
757 /* Receive filter data */
758 struct lttng_ust_filter_bytecode_node
*bytecode
;
760 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
761 ERR("Filter data size is too large: %u bytes",
762 lum
->u
.filter
.data_size
);
767 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
768 ERR("Filter reloc offset %u is not within data",
769 lum
->u
.filter
.reloc_offset
);
774 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
779 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
780 lum
->u
.filter
.data_size
);
782 case 0: /* orderly shutdown */
787 if (len
== lum
->u
.filter
.data_size
) {
788 DBG("filter data received");
790 } else if (len
< 0) {
791 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
792 if (len
== -ECONNRESET
) {
793 ERR("%s remote end closed connection", sock_info
->name
);
802 DBG("incorrect filter data message size: %zd", len
);
808 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
809 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
810 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
812 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
813 (unsigned long) bytecode
,
818 /* don't free bytecode if everything went fine. */
825 case LTTNG_UST_EXCLUSION
:
827 /* Receive exclusion names */
828 struct lttng_ust_excluder_node
*node
;
831 count
= lum
->u
.exclusion
.count
;
833 /* There are no names to read */
837 node
= zmalloc(sizeof(*node
) +
838 count
* LTTNG_UST_SYM_NAME_LEN
);
843 node
->excluder
.count
= count
;
844 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
845 count
* LTTNG_UST_SYM_NAME_LEN
);
847 case 0: /* orderly shutdown */
852 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
853 DBG("Exclusion data received");
855 } else if (len
< 0) {
856 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
857 if (len
== -ECONNRESET
) {
858 ERR("%s remote end closed connection", sock_info
->name
);
867 DBG("Incorrect exclusion data message size: %zd", len
);
874 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
875 (unsigned long) node
,
880 /* Don't free exclusion data if everything went fine. */
887 case LTTNG_UST_CHANNEL
:
892 len
= ustcomm_recv_channel_from_sessiond(sock
,
893 &chan_data
, lum
->u
.channel
.len
,
896 case 0: /* orderly shutdown */
900 if (len
== lum
->u
.channel
.len
) {
901 DBG("channel data received");
903 } else if (len
< 0) {
904 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
905 if (len
== -ECONNRESET
) {
906 ERR("%s remote end closed connection", sock_info
->name
);
913 DBG("incorrect channel data message size: %zd", len
);
918 args
.channel
.chan_data
= chan_data
;
919 args
.channel
.wakeup_fd
= wakeup_fd
;
921 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
922 (unsigned long) &lum
->u
,
926 if (args
.channel
.wakeup_fd
>= 0) {
929 lttng_ust_lock_fd_tracker();
930 close_ret
= close(args
.channel
.wakeup_fd
);
931 lttng_ust_unlock_fd_tracker();
932 args
.channel
.wakeup_fd
= -1;
936 free(args
.channel
.chan_data
);
939 case LTTNG_UST_STREAM
:
943 /* Receive shm_fd, wakeup_fd */
944 ret
= ustcomm_recv_stream_from_sessiond(sock
,
947 &args
.stream
.wakeup_fd
);
953 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
954 (unsigned long) &lum
->u
,
958 if (args
.stream
.shm_fd
>= 0) {
959 lttng_ust_lock_fd_tracker();
960 close_ret
= close(args
.stream
.shm_fd
);
961 lttng_ust_unlock_fd_tracker();
962 args
.stream
.shm_fd
= -1;
966 if (args
.stream
.wakeup_fd
>= 0) {
967 lttng_ust_lock_fd_tracker();
968 close_ret
= close(args
.stream
.wakeup_fd
);
969 lttng_ust_unlock_fd_tracker();
970 args
.stream
.wakeup_fd
= -1;
976 case LTTNG_UST_CONTEXT
:
977 switch (lum
->u
.context
.ctx
) {
978 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
981 size_t ctxlen
, recvlen
;
983 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
984 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
985 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
986 ERR("Application context string length size is too large: %zu bytes",
991 strcpy(ctxstr
, "$app.");
992 p
= &ctxstr
[strlen("$app.")];
993 recvlen
= ctxlen
- strlen("$app.");
994 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
996 case 0: /* orderly shutdown */
1000 if (len
== recvlen
) {
1001 DBG("app context data received");
1003 } else if (len
< 0) {
1004 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1005 if (len
== -ECONNRESET
) {
1006 ERR("%s remote end closed connection", sock_info
->name
);
1013 DBG("incorrect app context data message size: %zd", len
);
1018 /* Put : between provider and ctxname. */
1019 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
1020 args
.app_context
.ctxname
= ctxstr
;
1027 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1028 (unsigned long) &lum
->u
,
1036 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1037 (unsigned long) &lum
->u
,
1044 lur
.handle
= lum
->handle
;
1048 lur
.ret_code
= LTTNG_UST_OK
;
1051 * Use -LTTNG_UST_ERR as wildcard for UST internal
1052 * error that are not caused by the transport, except if
1053 * we already have a more precise error message to
1056 if (ret
> -LTTNG_UST_ERR
) {
1057 /* Translate code to UST error. */
1060 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1063 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1066 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1069 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1072 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1075 lur
.ret_code
= -LTTNG_UST_ERR
;
1084 case LTTNG_UST_TRACER_VERSION
:
1085 lur
.u
.version
= lum
->u
.version
;
1087 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1088 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1092 DBG("Return value: %d", lur
.ret_val
);
1097 * Performed delayed statedump operations outside of the UST
1098 * lock. We need to take the dynamic loader lock before we take
1099 * the UST lock internally within handle_pending_statedump().
1101 handle_pending_statedump(sock_info
);
1104 ret
= -LTTNG_UST_ERR_EXITING
;
1108 ret
= send_reply(sock
, &lur
);
1110 DBG("error sending reply");
1115 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1118 if (lur
.ret_code
== LTTNG_UST_OK
) {
1120 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1121 len
= ustcomm_send_unix_sock(sock
,
1122 &args
.field_list
.entry
,
1123 sizeof(args
.field_list
.entry
));
1128 if (len
!= sizeof(args
.field_list
.entry
)) {
1142 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1146 if (sock_info
->root_handle
!= -1) {
1147 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1149 ERR("Error unref root handle");
1151 sock_info
->root_handle
= -1;
1153 sock_info
->registration_done
= 0;
1154 sock_info
->initial_statedump_done
= 0;
1157 * wait_shm_mmap, socket and notify socket are used by listener
1158 * threads outside of the ust lock, so we cannot tear them down
1159 * ourselves, because we cannot join on these threads. Leave
1160 * responsibility of cleaning up these resources to the OS
1166 if (sock_info
->socket
!= -1) {
1167 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1169 ERR("Error closing ust cmd socket");
1171 sock_info
->socket
= -1;
1173 if (sock_info
->notify_socket
!= -1) {
1174 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1176 ERR("Error closing ust notify socket");
1178 sock_info
->notify_socket
= -1;
1180 if (sock_info
->wait_shm_mmap
) {
1183 page_size
= sysconf(_SC_PAGE_SIZE
);
1184 if (page_size
<= 0) {
1188 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1190 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1192 ERR("Error unmapping wait shm");
1195 sock_info
->wait_shm_mmap
= NULL
;
1200 * Using fork to set umask in the child process (not multi-thread safe).
1201 * We deal with the shm_open vs ftruncate race (happening when the
1202 * sessiond owns the shm and does not let everybody modify it, to ensure
1203 * safety against shm_unlink) by simply letting the mmap fail and
1204 * retrying after a few seconds.
1205 * For global shm, everybody has rw access to it until the sessiond
1209 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1211 int wait_shm_fd
, ret
;
1215 * Try to open read-only.
1217 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1218 if (wait_shm_fd
>= 0) {
1221 size_t bytes_read
= 0;
1224 * Try to read the fd. If unable to do so, try opening
1228 len
= read(wait_shm_fd
,
1229 &((char *) &tmp_read
)[bytes_read
],
1230 sizeof(tmp_read
) - bytes_read
);
1234 } while ((len
< 0 && errno
== EINTR
)
1235 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1236 if (bytes_read
!= sizeof(tmp_read
)) {
1237 ret
= close(wait_shm_fd
);
1239 ERR("close wait_shm_fd");
1244 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1246 * Real-only open did not work, and it's not because the
1247 * entry was not present. It's a failure that prohibits
1250 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1256 * If the open failed because the file did not exist, or because
1257 * the file was not truncated yet, try creating it ourself.
1259 URCU_TLS(lttng_ust_nest_count
)++;
1261 URCU_TLS(lttng_ust_nest_count
)--;
1266 * Parent: wait for child to return, in which case the
1267 * shared memory map will have been created.
1269 pid
= wait(&status
);
1270 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1275 * Try to open read-only again after creation.
1277 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1278 if (wait_shm_fd
< 0) {
1280 * Real-only open did not work. It's a failure
1281 * that prohibits using shm.
1283 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1287 } else if (pid
== 0) {
1291 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1292 if (sock_info
->global
)
1293 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1295 * We're alone in a child process, so we can modify the
1296 * process-wide umask.
1298 umask(~create_mode
);
1300 * Try creating shm (or get rw access).
1301 * We don't do an exclusive open, because we allow other
1302 * processes to create+ftruncate it concurrently.
1304 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1305 O_RDWR
| O_CREAT
, create_mode
);
1306 if (wait_shm_fd
>= 0) {
1307 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1309 PERROR("ftruncate");
1310 _exit(EXIT_FAILURE
);
1312 _exit(EXIT_SUCCESS
);
1315 * For local shm, we need to have rw access to accept
1316 * opening it: this means the local sessiond will be
1317 * able to wake us up. For global shm, we open it even
1318 * if rw access is not granted, because the root.root
1319 * sessiond will be able to override all rights and wake
1322 if (!sock_info
->global
&& errno
!= EACCES
) {
1323 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1324 _exit(EXIT_FAILURE
);
1327 * The shm exists, but we cannot open it RW. Report
1330 _exit(EXIT_SUCCESS
);
1335 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1336 struct stat statbuf
;
1339 * Ensure that our user is the owner of the shm file for
1340 * local shm. If we do not own the file, it means our
1341 * sessiond will not have access to wake us up (there is
1342 * probably a rogue process trying to fake our
1343 * sessiond). Fallback to polling method in this case.
1345 ret
= fstat(wait_shm_fd
, &statbuf
);
1350 if (statbuf
.st_uid
!= getuid())
1356 ret
= close(wait_shm_fd
);
1358 PERROR("Error closing fd");
1364 char *get_map_shm(struct sock_info
*sock_info
)
1367 int wait_shm_fd
, ret
;
1368 char *wait_shm_mmap
;
1370 page_size
= sysconf(_SC_PAGE_SIZE
);
1371 if (page_size
<= 0) {
1375 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1379 lttng_ust_lock_fd_tracker();
1380 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1381 if (wait_shm_fd
< 0) {
1382 lttng_ust_unlock_fd_tracker();
1386 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1388 ret
= close(wait_shm_fd
);
1390 PERROR("Error closing fd");
1392 lttng_ust_unlock_fd_tracker();
1397 lttng_ust_unlock_fd_tracker();
1399 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1400 MAP_SHARED
, wait_shm_fd
, 0);
1402 /* close shm fd immediately after taking the mmap reference */
1403 lttng_ust_lock_fd_tracker();
1404 ret
= close(wait_shm_fd
);
1406 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1408 PERROR("Error closing fd");
1410 lttng_ust_unlock_fd_tracker();
1412 if (wait_shm_mmap
== MAP_FAILED
) {
1413 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1416 return wait_shm_mmap
;
1423 void wait_for_sessiond(struct sock_info
*sock_info
)
1425 /* Use ust_lock to check if we should quit. */
1429 if (wait_poll_fallback
) {
1434 assert(sock_info
->wait_shm_mmap
);
1436 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1437 /* Wait for futex wakeup */
1438 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1441 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1442 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1445 /* Value already changed. */
1448 /* Retry if interrupted by signal. */
1449 break; /* Get out of switch. */
1451 wait_poll_fallback
= 1;
1453 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1454 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1455 "Please upgrade your kernel "
1456 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1457 "mainline). LTTng-UST will use polling mode fallback.");
1476 * This thread does not allocate any resource, except within
1477 * handle_message, within mutex protection. This mutex protects against
1479 * The other moment it allocates resources is at socket connection, which
1480 * is also protected by the mutex.
1483 void *ust_listener_thread(void *arg
)
1485 struct sock_info
*sock_info
= arg
;
1486 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1489 lttng_ust_fixup_tls();
1491 * If available, add '-ust' to the end of this thread's
1494 ret
= lttng_ust_setustprocname();
1496 ERR("Unable to set UST process name");
1499 /* Restart trying to connect to the session daemon */
1501 if (prev_connect_failed
) {
1502 /* Wait for sessiond availability with pipe */
1503 wait_for_sessiond(sock_info
);
1507 * Sleep for 5 seconds before retrying after a
1508 * sequence of failure / wait / failure. This
1509 * deals with a killed or broken session daemon.
1515 prev_connect_failed
= 0;
1522 if (sock_info
->socket
!= -1) {
1523 /* FD tracker is updated by ustcomm_close_unix_sock() */
1524 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1526 ERR("Error closing %s ust cmd socket",
1529 sock_info
->socket
= -1;
1531 if (sock_info
->notify_socket
!= -1) {
1532 /* FD tracker is updated by ustcomm_close_unix_sock() */
1533 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1535 ERR("Error closing %s ust notify socket",
1538 sock_info
->notify_socket
= -1;
1543 * Register. We need to perform both connect and sending
1544 * registration message before doing the next connect otherwise
1545 * we may reach unix socket connect queue max limits and block
1546 * on the 2nd connect while the session daemon is awaiting the
1547 * first connect registration message.
1549 /* Connect cmd socket */
1550 lttng_ust_lock_fd_tracker();
1551 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1552 get_connect_sock_timeout());
1554 lttng_ust_unlock_fd_tracker();
1555 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1556 prev_connect_failed
= 1;
1559 * If we cannot find the sessiond daemon, don't delay
1560 * constructor execution.
1562 ret
= handle_register_failed(sock_info
);
1568 ret
= lttng_ust_add_fd_to_tracker(fd
);
1572 PERROR("close on sock_info->socket");
1575 lttng_ust_unlock_fd_tracker();
1580 sock_info
->socket
= ret
;
1581 lttng_ust_unlock_fd_tracker();
1585 * Unlock/relock ust lock because connect is blocking (with
1586 * timeout). Don't delay constructors on the ust lock for too
1594 * Create only one root handle per listener thread for the whole
1595 * process lifetime, so we ensure we get ID which is statically
1596 * assigned to the root handle.
1598 if (sock_info
->root_handle
== -1) {
1599 ret
= lttng_abi_create_root_handle();
1601 ERR("Error creating root handle");
1604 sock_info
->root_handle
= ret
;
1607 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1609 ERR("Error registering to %s ust cmd socket",
1611 prev_connect_failed
= 1;
1613 * If we cannot register to the sessiond daemon, don't
1614 * delay constructor execution.
1616 ret
= handle_register_failed(sock_info
);
1624 * Unlock/relock ust lock because connect is blocking (with
1625 * timeout). Don't delay constructors on the ust lock for too
1632 /* Connect notify socket */
1633 lttng_ust_lock_fd_tracker();
1634 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1635 get_connect_sock_timeout());
1637 lttng_ust_unlock_fd_tracker();
1638 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1639 prev_connect_failed
= 1;
1642 * If we cannot find the sessiond daemon, don't delay
1643 * constructor execution.
1645 ret
= handle_register_failed(sock_info
);
1652 ret
= lttng_ust_add_fd_to_tracker(fd
);
1656 PERROR("close on sock_info->notify_socket");
1659 lttng_ust_unlock_fd_tracker();
1664 sock_info
->notify_socket
= ret
;
1665 lttng_ust_unlock_fd_tracker();
1669 * Unlock/relock ust lock because connect is blocking (with
1670 * timeout). Don't delay constructors on the ust lock for too
1677 timeout
= get_notify_sock_timeout();
1680 * Give at least 10ms to sessiond to reply to
1685 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1688 WARN("Error setting socket receive timeout");
1690 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1693 WARN("Error setting socket send timeout");
1695 } else if (timeout
< -1) {
1696 WARN("Unsupported timeout value %ld", timeout
);
1699 ret
= register_to_sessiond(sock_info
->notify_socket
,
1700 USTCTL_SOCKET_NOTIFY
);
1702 ERR("Error registering to %s ust notify socket",
1704 prev_connect_failed
= 1;
1706 * If we cannot register to the sessiond daemon, don't
1707 * delay constructor execution.
1709 ret
= handle_register_failed(sock_info
);
1714 sock
= sock_info
->socket
;
1720 struct ustcomm_ust_msg lum
;
1722 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1724 case 0: /* orderly shutdown */
1725 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1730 * Either sessiond has shutdown or refused us by closing the socket.
1731 * In either case, we don't want to delay construction execution,
1732 * and we need to wait before retry.
1734 prev_connect_failed
= 1;
1736 * If we cannot register to the sessiond daemon, don't
1737 * delay constructor execution.
1739 ret
= handle_register_failed(sock_info
);
1744 print_cmd(lum
.cmd
, lum
.handle
);
1745 ret
= handle_message(sock_info
, sock
, &lum
);
1747 ERR("Error handling message for %s socket",
1750 * Close socket if protocol error is
1758 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1760 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1762 if (len
== -ECONNRESET
) {
1763 DBG("%s remote end closed connection", sock_info
->name
);
1774 /* Cleanup socket handles before trying to reconnect */
1775 lttng_ust_objd_table_owner_cleanup(sock_info
);
1777 goto restart
; /* try to reconnect */
1782 pthread_mutex_lock(&ust_exit_mutex
);
1783 sock_info
->thread_active
= 0;
1784 pthread_mutex_unlock(&ust_exit_mutex
);
1789 * Weak symbol to call when the ust malloc wrapper is not loaded.
1791 __attribute__((weak
))
1792 void lttng_ust_malloc_wrapper_init(void)
1797 * sessiond monitoring thread: monitor presence of global and per-user
1798 * sessiond by polling the application common named pipe.
1800 void __attribute__((constructor
)) lttng_ust_init(void)
1802 struct timespec constructor_timeout
;
1803 sigset_t sig_all_blocked
, orig_parent_mask
;
1804 pthread_attr_t thread_attr
;
1809 if (uatomic_xchg(&initialized
, 1) == 1)
1813 * Fixup interdependency between TLS fixup mutex (which happens
1814 * to be the dynamic linker mutex) and ust_lock, taken within
1817 lttng_ust_fixup_tls();
1819 lttng_ust_loaded
= 1;
1822 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1823 * the unloading of code used by the ust_listener_threads as we can not
1824 * reliably know when they exited. To do that, manually load
1825 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1826 * this library so it never becomes zero, thus never gets unloaded from the
1827 * address space of the process. Since we are already running in the
1828 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1829 * simply increment the refcount and no additionnal work is needed by the
1830 * dynamic loader as the shared library is already loaded in the address
1831 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1832 * unloading of the UST library if its refcount becomes zero (which should
1833 * never happen). Do the return value check but discard the handle at the
1834 * end of the function as it's not needed.
1836 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1838 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1842 * We want precise control over the order in which we construct
1843 * our sub-libraries vs starting to receive commands from
1844 * sessiond (otherwise leading to errors when trying to create
1845 * sessiond before the init functions are completed).
1848 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1850 lttng_ust_init_fd_tracker();
1851 lttng_ust_clock_init();
1852 lttng_ust_getcpu_init();
1853 lttng_ust_statedump_init();
1854 lttng_ring_buffer_metadata_client_init();
1855 lttng_ring_buffer_client_overwrite_init();
1856 lttng_ring_buffer_client_overwrite_rt_init();
1857 lttng_ring_buffer_client_discard_init();
1858 lttng_ring_buffer_client_discard_rt_init();
1859 lttng_perf_counter_init();
1861 * Invoke ust malloc wrapper init before starting other threads.
1863 lttng_ust_malloc_wrapper_init();
1865 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1867 get_allow_blocking();
1869 ret
= sem_init(&constructor_wait
, 0, 0);
1874 ret
= setup_global_apps();
1876 assert(global_apps
.allowed
== 0);
1877 DBG("global apps setup returned %d", ret
);
1880 ret
= setup_local_apps();
1882 assert(local_apps
.allowed
== 0);
1883 DBG("local apps setup returned %d", ret
);
1886 /* A new thread created by pthread_create inherits the signal mask
1887 * from the parent. To avoid any signal being received by the
1888 * listener thread, we block all signals temporarily in the parent,
1889 * while we create the listener thread.
1891 sigfillset(&sig_all_blocked
);
1892 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1894 ERR("pthread_sigmask: %s", strerror(ret
));
1897 ret
= pthread_attr_init(&thread_attr
);
1899 ERR("pthread_attr_init: %s", strerror(ret
));
1901 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1903 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1906 if (global_apps
.allowed
) {
1907 pthread_mutex_lock(&ust_exit_mutex
);
1908 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1909 ust_listener_thread
, &global_apps
);
1911 ERR("pthread_create global: %s", strerror(ret
));
1913 global_apps
.thread_active
= 1;
1914 pthread_mutex_unlock(&ust_exit_mutex
);
1916 handle_register_done(&global_apps
);
1919 if (local_apps
.allowed
) {
1920 pthread_mutex_lock(&ust_exit_mutex
);
1921 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1922 ust_listener_thread
, &local_apps
);
1924 ERR("pthread_create local: %s", strerror(ret
));
1926 local_apps
.thread_active
= 1;
1927 pthread_mutex_unlock(&ust_exit_mutex
);
1929 handle_register_done(&local_apps
);
1931 ret
= pthread_attr_destroy(&thread_attr
);
1933 ERR("pthread_attr_destroy: %s", strerror(ret
));
1936 /* Restore original signal mask in parent */
1937 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1939 ERR("pthread_sigmask: %s", strerror(ret
));
1942 switch (timeout_mode
) {
1943 case 1: /* timeout wait */
1945 ret
= sem_timedwait(&constructor_wait
,
1946 &constructor_timeout
);
1947 } while (ret
< 0 && errno
== EINTR
);
1951 ERR("Timed out waiting for lttng-sessiond");
1954 PERROR("sem_timedwait");
1957 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1962 case -1:/* wait forever */
1964 ret
= sem_wait(&constructor_wait
);
1965 } while (ret
< 0 && errno
== EINTR
);
1972 ERR("Unexpected error \"%s\" returned by sem_wait",
1977 case 0: /* no timeout */
1983 void lttng_ust_cleanup(int exiting
)
1985 cleanup_sock_info(&global_apps
, exiting
);
1986 cleanup_sock_info(&local_apps
, exiting
);
1987 local_apps
.allowed
= 0;
1988 global_apps
.allowed
= 0;
1990 * The teardown in this function all affect data structures
1991 * accessed under the UST lock by the listener thread. This
1992 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1993 * that none of these threads are accessing this data at this
1996 lttng_ust_abi_exit();
1997 lttng_ust_events_exit();
1998 lttng_perf_counter_exit();
1999 lttng_ring_buffer_client_discard_rt_exit();
2000 lttng_ring_buffer_client_discard_exit();
2001 lttng_ring_buffer_client_overwrite_rt_exit();
2002 lttng_ring_buffer_client_overwrite_exit();
2003 lttng_ring_buffer_metadata_client_exit();
2004 lttng_ust_statedump_destroy();
2007 /* Reinitialize values for fork */
2008 sem_count
= sem_count_initial_value
;
2009 lttng_ust_comm_should_quit
= 0;
2014 void __attribute__((destructor
)) lttng_ust_exit(void)
2019 * Using pthread_cancel here because:
2020 * A) we don't want to hang application teardown.
2021 * B) the thread is not allocating any resource.
2025 * Require the communication thread to quit. Synchronize with
2026 * mutexes to ensure it is not in a mutex critical section when
2027 * pthread_cancel is later called.
2030 lttng_ust_comm_should_quit
= 1;
2033 pthread_mutex_lock(&ust_exit_mutex
);
2034 /* cancel threads */
2035 if (global_apps
.thread_active
) {
2036 ret
= pthread_cancel(global_apps
.ust_listener
);
2038 ERR("Error cancelling global ust listener thread: %s",
2041 global_apps
.thread_active
= 0;
2044 if (local_apps
.thread_active
) {
2045 ret
= pthread_cancel(local_apps
.ust_listener
);
2047 ERR("Error cancelling local ust listener thread: %s",
2050 local_apps
.thread_active
= 0;
2053 pthread_mutex_unlock(&ust_exit_mutex
);
2056 * Do NOT join threads: use of sys_futex makes it impossible to
2057 * join the threads without using async-cancel, but async-cancel
2058 * is delivered by a signal, which could hit the target thread
2059 * anywhere in its code path, including while the ust_lock() is
2060 * held, causing a deadlock for the other thread. Let the OS
2061 * cleanup the threads if there are stalled in a syscall.
2063 lttng_ust_cleanup(1);
2067 * We exclude the worker threads across fork and clone (except
2068 * CLONE_VM), because these system calls only keep the forking thread
2069 * running in the child. Therefore, we don't want to call fork or clone
2070 * in the middle of an tracepoint or ust tracing state modification.
2071 * Holding this mutex protects these structures across fork and clone.
2073 void ust_before_fork(sigset_t
*save_sigset
)
2076 * Disable signals. This is to avoid that the child intervenes
2077 * before it is properly setup for tracing. It is safer to
2078 * disable all signals, because then we know we are not breaking
2079 * anything by restoring the original mask.
2084 /* Fixup lttng-ust TLS. */
2085 lttng_ust_fixup_tls();
2087 if (URCU_TLS(lttng_ust_nest_count
))
2089 /* Disable signals */
2090 sigfillset(&all_sigs
);
2091 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2093 PERROR("sigprocmask");
2096 pthread_mutex_lock(&ust_fork_mutex
);
2099 rcu_bp_before_fork();
2100 lttng_ust_lock_fd_tracker();
2104 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2108 DBG("process %d", getpid());
2109 lttng_perf_unlock();
2110 lttng_ust_unlock_fd_tracker();
2113 pthread_mutex_unlock(&ust_fork_mutex
);
2115 /* Restore signals */
2116 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2118 PERROR("sigprocmask");
2122 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2124 if (URCU_TLS(lttng_ust_nest_count
))
2126 DBG("process %d", getpid());
2127 rcu_bp_after_fork_parent();
2128 /* Release mutexes and reenable signals */
2129 ust_after_fork_common(restore_sigset
);
2133 * After fork, in the child, we need to cleanup all the leftover state,
2134 * except the worker thread which already magically disappeared thanks
2135 * to the weird Linux fork semantics. After tyding up, we call
2136 * lttng_ust_init() again to start over as a new PID.
2138 * This is meant for forks() that have tracing in the child between the
2139 * fork and following exec call (if there is any).
2141 void ust_after_fork_child(sigset_t
*restore_sigset
)
2143 if (URCU_TLS(lttng_ust_nest_count
))
2145 lttng_context_vpid_reset();
2146 lttng_context_vtid_reset();
2147 lttng_context_procname_reset();
2148 DBG("process %d", getpid());
2149 /* Release urcu mutexes */
2150 rcu_bp_after_fork_child();
2151 lttng_ust_cleanup(0);
2152 /* Release mutexes and reenable signals */
2153 ust_after_fork_common(restore_sigset
);
2157 void lttng_ust_sockinfo_session_enabled(void *owner
)
2159 struct sock_info
*sock_info
= owner
;
2160 sock_info
->statedump_pending
= 1;