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_fd_tracker_tls();
429 int lttng_get_notify_socket(void *owner
)
431 struct sock_info
*info
= owner
;
433 return info
->notify_socket
;
437 void print_cmd(int cmd
, int handle
)
439 const char *cmd_name
= "Unknown";
441 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
442 && cmd_name_mapping
[cmd
]) {
443 cmd_name
= cmd_name_mapping
[cmd
];
445 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
447 lttng_ust_obj_get_name(handle
), handle
);
451 int setup_global_apps(void)
454 assert(!global_apps
.wait_shm_mmap
);
456 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
457 if (!global_apps
.wait_shm_mmap
) {
458 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
459 global_apps
.allowed
= 0;
464 global_apps
.allowed
= 1;
469 int setup_local_apps(void)
472 const char *home_dir
;
475 assert(!local_apps
.wait_shm_mmap
);
479 * Disallow per-user tracing for setuid binaries.
481 if (uid
!= geteuid()) {
482 assert(local_apps
.allowed
== 0);
486 home_dir
= get_lttng_home_dir();
488 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
489 assert(local_apps
.allowed
== 0);
493 local_apps
.allowed
= 1;
494 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
496 LTTNG_DEFAULT_HOME_RUNDIR
,
497 LTTNG_UST_SOCK_FILENAME
);
498 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
499 LTTNG_UST_WAIT_FILENAME
,
502 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
503 if (!local_apps
.wait_shm_mmap
) {
504 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
505 local_apps
.allowed
= 0;
514 * Get socket timeout, in ms.
515 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
518 long get_timeout(void)
520 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
522 if (!got_timeout_env
) {
523 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
527 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
528 /* All negative values are considered as "-1". */
529 if (constructor_delay_ms
< -1)
530 constructor_delay_ms
= -1;
531 return constructor_delay_ms
;
534 /* Timeout for notify socket send and recv. */
536 long get_notify_sock_timeout(void)
538 return get_timeout();
541 /* Timeout for connecting to cmd and notify sockets. */
543 long get_connect_sock_timeout(void)
545 return get_timeout();
549 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
552 int get_constructor_timeout(struct timespec
*constructor_timeout
)
554 long constructor_delay_ms
;
557 constructor_delay_ms
= get_timeout();
559 switch (constructor_delay_ms
) {
560 case -1:/* fall-through */
562 return constructor_delay_ms
;
568 * If we are unable to find the current time, don't wait.
570 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
575 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
576 constructor_timeout
->tv_nsec
+=
577 (constructor_delay_ms
% 1000UL) * 1000000UL;
578 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
579 constructor_timeout
->tv_sec
++;
580 constructor_timeout
->tv_nsec
-= 1000000000UL;
582 /* Timeout wait (constructor_delay_ms). */
587 void get_allow_blocking(void)
589 const char *str_allow_blocking
=
590 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
592 if (str_allow_blocking
) {
593 DBG("%s environment variable is set",
594 "LTTNG_UST_ALLOW_BLOCKING");
595 lttng_ust_ringbuffer_set_allow_blocking();
600 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
602 return ustcomm_send_reg_msg(socket
,
605 lttng_alignof(uint8_t) * CHAR_BIT
,
606 lttng_alignof(uint16_t) * CHAR_BIT
,
607 lttng_alignof(uint32_t) * CHAR_BIT
,
608 lttng_alignof(uint64_t) * CHAR_BIT
,
609 lttng_alignof(unsigned long) * CHAR_BIT
);
613 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
617 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
620 DBG("message successfully sent");
623 if (len
== -ECONNRESET
) {
624 DBG("remote end closed connection");
629 DBG("incorrect message size: %zd", len
);
635 void decrement_sem_count(unsigned int count
)
639 assert(uatomic_read(&sem_count
) >= count
);
641 if (uatomic_read(&sem_count
) <= 0) {
645 ret
= uatomic_add_return(&sem_count
, -count
);
647 ret
= sem_post(&constructor_wait
);
653 int handle_register_done(struct sock_info
*sock_info
)
655 if (sock_info
->registration_done
)
657 sock_info
->registration_done
= 1;
659 decrement_sem_count(1);
660 if (!sock_info
->statedump_pending
) {
661 sock_info
->initial_statedump_done
= 1;
662 decrement_sem_count(1);
669 int handle_register_failed(struct sock_info
*sock_info
)
671 if (sock_info
->registration_done
)
673 sock_info
->registration_done
= 1;
674 sock_info
->initial_statedump_done
= 1;
676 decrement_sem_count(2);
682 * Only execute pending statedump after the constructor semaphore has
683 * been posted by the current listener thread. This means statedump will
684 * only be performed after the "registration done" command is received
685 * from this thread's session daemon.
687 * This ensures we don't run into deadlock issues with the dynamic
688 * loader mutex, which is held while the constructor is called and
689 * waiting on the constructor semaphore. All operations requiring this
690 * dynamic loader lock need to be postponed using this mechanism.
692 * In a scenario with two session daemons connected to the application,
693 * it is possible that the first listener thread which receives the
694 * registration done command issues its statedump while the dynamic
695 * loader lock is still held by the application constructor waiting on
696 * the semaphore. It will however be allowed to proceed when the
697 * second session daemon sends the registration done command to the
698 * second listener thread. This situation therefore does not produce
702 void handle_pending_statedump(struct sock_info
*sock_info
)
704 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
705 sock_info
->statedump_pending
= 0;
706 pthread_mutex_lock(&ust_fork_mutex
);
707 lttng_handle_pending_statedump(sock_info
);
708 pthread_mutex_unlock(&ust_fork_mutex
);
710 if (!sock_info
->initial_statedump_done
) {
711 sock_info
->initial_statedump_done
= 1;
712 decrement_sem_count(1);
718 int handle_message(struct sock_info
*sock_info
,
719 int sock
, struct ustcomm_ust_msg
*lum
)
722 const struct lttng_ust_objd_ops
*ops
;
723 struct ustcomm_ust_reply lur
;
725 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
728 memset(&lur
, 0, sizeof(lur
));
731 ret
= -LTTNG_UST_ERR_EXITING
;
735 ops
= objd_ops(lum
->handle
);
742 case LTTNG_UST_REGISTER_DONE
:
743 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
744 ret
= handle_register_done(sock_info
);
748 case LTTNG_UST_RELEASE
:
749 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
752 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
754 case LTTNG_UST_FILTER
:
756 /* Receive filter data */
757 struct lttng_ust_filter_bytecode_node
*bytecode
;
759 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
760 ERR("Filter data size is too large: %u bytes",
761 lum
->u
.filter
.data_size
);
766 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
767 ERR("Filter reloc offset %u is not within data",
768 lum
->u
.filter
.reloc_offset
);
773 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
778 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
779 lum
->u
.filter
.data_size
);
781 case 0: /* orderly shutdown */
786 if (len
== lum
->u
.filter
.data_size
) {
787 DBG("filter data received");
789 } else if (len
< 0) {
790 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
791 if (len
== -ECONNRESET
) {
792 ERR("%s remote end closed connection", sock_info
->name
);
801 DBG("incorrect filter data message size: %zd", len
);
807 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
808 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
809 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
811 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
812 (unsigned long) bytecode
,
817 /* don't free bytecode if everything went fine. */
824 case LTTNG_UST_EXCLUSION
:
826 /* Receive exclusion names */
827 struct lttng_ust_excluder_node
*node
;
830 count
= lum
->u
.exclusion
.count
;
832 /* There are no names to read */
836 node
= zmalloc(sizeof(*node
) +
837 count
* LTTNG_UST_SYM_NAME_LEN
);
842 node
->excluder
.count
= count
;
843 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
844 count
* LTTNG_UST_SYM_NAME_LEN
);
846 case 0: /* orderly shutdown */
851 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
852 DBG("Exclusion data received");
854 } else if (len
< 0) {
855 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
856 if (len
== -ECONNRESET
) {
857 ERR("%s remote end closed connection", sock_info
->name
);
866 DBG("Incorrect exclusion data message size: %zd", len
);
873 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
874 (unsigned long) node
,
879 /* Don't free exclusion data if everything went fine. */
886 case LTTNG_UST_CHANNEL
:
891 len
= ustcomm_recv_channel_from_sessiond(sock
,
892 &chan_data
, lum
->u
.channel
.len
,
895 case 0: /* orderly shutdown */
899 if (len
== lum
->u
.channel
.len
) {
900 DBG("channel data received");
902 } else if (len
< 0) {
903 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
904 if (len
== -ECONNRESET
) {
905 ERR("%s remote end closed connection", sock_info
->name
);
912 DBG("incorrect channel data message size: %zd", len
);
917 args
.channel
.chan_data
= chan_data
;
918 args
.channel
.wakeup_fd
= wakeup_fd
;
920 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
921 (unsigned long) &lum
->u
,
927 case LTTNG_UST_STREAM
:
929 /* Receive shm_fd, wakeup_fd */
930 ret
= ustcomm_recv_stream_from_sessiond(sock
,
933 &args
.stream
.wakeup_fd
);
939 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
940 (unsigned long) &lum
->u
,
946 case LTTNG_UST_CONTEXT
:
947 switch (lum
->u
.context
.ctx
) {
948 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
951 size_t ctxlen
, recvlen
;
953 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
954 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
955 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
956 ERR("Application context string length size is too large: %zu bytes",
961 strcpy(ctxstr
, "$app.");
962 p
= &ctxstr
[strlen("$app.")];
963 recvlen
= ctxlen
- strlen("$app.");
964 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
966 case 0: /* orderly shutdown */
970 if (len
== recvlen
) {
971 DBG("app context data received");
973 } else if (len
< 0) {
974 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
975 if (len
== -ECONNRESET
) {
976 ERR("%s remote end closed connection", sock_info
->name
);
983 DBG("incorrect app context data message size: %zd", len
);
988 /* Put : between provider and ctxname. */
989 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
990 args
.app_context
.ctxname
= ctxstr
;
997 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
998 (unsigned long) &lum
->u
,
1006 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
1007 (unsigned long) &lum
->u
,
1014 lur
.handle
= lum
->handle
;
1018 lur
.ret_code
= LTTNG_UST_OK
;
1021 * Use -LTTNG_UST_ERR as wildcard for UST internal
1022 * error that are not caused by the transport, except if
1023 * we already have a more precise error message to
1026 if (ret
> -LTTNG_UST_ERR
) {
1027 /* Translate code to UST error. */
1030 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1033 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1036 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1039 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1042 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1045 lur
.ret_code
= -LTTNG_UST_ERR
;
1054 case LTTNG_UST_TRACER_VERSION
:
1055 lur
.u
.version
= lum
->u
.version
;
1057 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1058 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1062 DBG("Return value: %d", lur
.ret_val
);
1067 * Performed delayed statedump operations outside of the UST
1068 * lock. We need to take the dynamic loader lock before we take
1069 * the UST lock internally within handle_pending_statedump().
1071 handle_pending_statedump(sock_info
);
1074 ret
= -LTTNG_UST_ERR_EXITING
;
1078 ret
= send_reply(sock
, &lur
);
1080 DBG("error sending reply");
1085 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1088 if (lur
.ret_code
== LTTNG_UST_OK
) {
1090 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1091 len
= ustcomm_send_unix_sock(sock
,
1092 &args
.field_list
.entry
,
1093 sizeof(args
.field_list
.entry
));
1098 if (len
!= sizeof(args
.field_list
.entry
)) {
1112 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1116 if (sock_info
->root_handle
!= -1) {
1117 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1119 ERR("Error unref root handle");
1121 sock_info
->root_handle
= -1;
1123 sock_info
->registration_done
= 0;
1124 sock_info
->initial_statedump_done
= 0;
1127 * wait_shm_mmap, socket and notify socket are used by listener
1128 * threads outside of the ust lock, so we cannot tear them down
1129 * ourselves, because we cannot join on these threads. Leave
1130 * responsibility of cleaning up these resources to the OS
1136 if (sock_info
->socket
!= -1) {
1137 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1139 ERR("Error closing ust cmd socket");
1141 sock_info
->socket
= -1;
1143 if (sock_info
->notify_socket
!= -1) {
1144 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1146 ERR("Error closing ust notify socket");
1148 sock_info
->notify_socket
= -1;
1150 if (sock_info
->wait_shm_mmap
) {
1153 page_size
= sysconf(_SC_PAGE_SIZE
);
1154 if (page_size
<= 0) {
1158 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1160 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1162 ERR("Error unmapping wait shm");
1165 sock_info
->wait_shm_mmap
= NULL
;
1170 * Using fork to set umask in the child process (not multi-thread safe).
1171 * We deal with the shm_open vs ftruncate race (happening when the
1172 * sessiond owns the shm and does not let everybody modify it, to ensure
1173 * safety against shm_unlink) by simply letting the mmap fail and
1174 * retrying after a few seconds.
1175 * For global shm, everybody has rw access to it until the sessiond
1179 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1181 int wait_shm_fd
, ret
;
1185 * Try to open read-only.
1187 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1188 if (wait_shm_fd
>= 0) {
1191 size_t bytes_read
= 0;
1194 * Try to read the fd. If unable to do so, try opening
1198 len
= read(wait_shm_fd
,
1199 &((char *) &tmp_read
)[bytes_read
],
1200 sizeof(tmp_read
) - bytes_read
);
1204 } while ((len
< 0 && errno
== EINTR
)
1205 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1206 if (bytes_read
!= sizeof(tmp_read
)) {
1207 ret
= close(wait_shm_fd
);
1209 ERR("close wait_shm_fd");
1214 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1216 * Real-only open did not work, and it's not because the
1217 * entry was not present. It's a failure that prohibits
1220 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1226 * If the open failed because the file did not exist, or because
1227 * the file was not truncated yet, try creating it ourself.
1229 URCU_TLS(lttng_ust_nest_count
)++;
1231 URCU_TLS(lttng_ust_nest_count
)--;
1236 * Parent: wait for child to return, in which case the
1237 * shared memory map will have been created.
1239 pid
= wait(&status
);
1240 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1245 * Try to open read-only again after creation.
1247 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1248 if (wait_shm_fd
< 0) {
1250 * Real-only open did not work. It's a failure
1251 * that prohibits using shm.
1253 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1257 } else if (pid
== 0) {
1261 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1262 if (sock_info
->global
)
1263 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1265 * We're alone in a child process, so we can modify the
1266 * process-wide umask.
1268 umask(~create_mode
);
1270 * Try creating shm (or get rw access).
1271 * We don't do an exclusive open, because we allow other
1272 * processes to create+ftruncate it concurrently.
1274 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1275 O_RDWR
| O_CREAT
, create_mode
);
1276 if (wait_shm_fd
>= 0) {
1277 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1279 PERROR("ftruncate");
1280 _exit(EXIT_FAILURE
);
1282 _exit(EXIT_SUCCESS
);
1285 * For local shm, we need to have rw access to accept
1286 * opening it: this means the local sessiond will be
1287 * able to wake us up. For global shm, we open it even
1288 * if rw access is not granted, because the root.root
1289 * sessiond will be able to override all rights and wake
1292 if (!sock_info
->global
&& errno
!= EACCES
) {
1293 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1294 _exit(EXIT_FAILURE
);
1297 * The shm exists, but we cannot open it RW. Report
1300 _exit(EXIT_SUCCESS
);
1305 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1306 struct stat statbuf
;
1309 * Ensure that our user is the owner of the shm file for
1310 * local shm. If we do not own the file, it means our
1311 * sessiond will not have access to wake us up (there is
1312 * probably a rogue process trying to fake our
1313 * sessiond). Fallback to polling method in this case.
1315 ret
= fstat(wait_shm_fd
, &statbuf
);
1320 if (statbuf
.st_uid
!= getuid())
1326 ret
= close(wait_shm_fd
);
1328 PERROR("Error closing fd");
1334 char *get_map_shm(struct sock_info
*sock_info
)
1337 int wait_shm_fd
, ret
;
1338 char *wait_shm_mmap
;
1340 page_size
= sysconf(_SC_PAGE_SIZE
);
1341 if (page_size
<= 0) {
1345 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1349 lttng_ust_lock_fd_tracker();
1350 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1351 if (wait_shm_fd
< 0) {
1352 lttng_ust_unlock_fd_tracker();
1356 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1358 ret
= close(wait_shm_fd
);
1360 PERROR("Error closing fd");
1362 lttng_ust_unlock_fd_tracker();
1367 lttng_ust_unlock_fd_tracker();
1369 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1370 MAP_SHARED
, wait_shm_fd
, 0);
1372 /* close shm fd immediately after taking the mmap reference */
1373 lttng_ust_lock_fd_tracker();
1374 ret
= close(wait_shm_fd
);
1376 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1378 PERROR("Error closing fd");
1380 lttng_ust_unlock_fd_tracker();
1382 if (wait_shm_mmap
== MAP_FAILED
) {
1383 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1386 return wait_shm_mmap
;
1393 void wait_for_sessiond(struct sock_info
*sock_info
)
1395 /* Use ust_lock to check if we should quit. */
1399 if (wait_poll_fallback
) {
1404 assert(sock_info
->wait_shm_mmap
);
1406 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1407 /* Wait for futex wakeup */
1408 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1411 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1412 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1415 /* Value already changed. */
1418 /* Retry if interrupted by signal. */
1419 break; /* Get out of switch. */
1421 wait_poll_fallback
= 1;
1423 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1424 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1425 "Please upgrade your kernel "
1426 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1427 "mainline). LTTng-UST will use polling mode fallback.");
1446 * This thread does not allocate any resource, except within
1447 * handle_message, within mutex protection. This mutex protects against
1449 * The other moment it allocates resources is at socket connection, which
1450 * is also protected by the mutex.
1453 void *ust_listener_thread(void *arg
)
1455 struct sock_info
*sock_info
= arg
;
1456 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1459 lttng_ust_fixup_tls();
1461 * If available, add '-ust' to the end of this thread's
1464 ret
= lttng_ust_setustprocname();
1466 ERR("Unable to set UST process name");
1469 /* Restart trying to connect to the session daemon */
1471 if (prev_connect_failed
) {
1472 /* Wait for sessiond availability with pipe */
1473 wait_for_sessiond(sock_info
);
1477 * Sleep for 5 seconds before retrying after a
1478 * sequence of failure / wait / failure. This
1479 * deals with a killed or broken session daemon.
1485 prev_connect_failed
= 0;
1492 if (sock_info
->socket
!= -1) {
1493 /* FD tracker is updated by ustcomm_close_unix_sock() */
1494 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1496 ERR("Error closing %s ust cmd socket",
1499 sock_info
->socket
= -1;
1501 if (sock_info
->notify_socket
!= -1) {
1502 /* FD tracker is updated by ustcomm_close_unix_sock() */
1503 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1505 ERR("Error closing %s ust notify socket",
1508 sock_info
->notify_socket
= -1;
1513 * Register. We need to perform both connect and sending
1514 * registration message before doing the next connect otherwise
1515 * we may reach unix socket connect queue max limits and block
1516 * on the 2nd connect while the session daemon is awaiting the
1517 * first connect registration message.
1519 /* Connect cmd socket */
1520 lttng_ust_lock_fd_tracker();
1521 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1522 get_connect_sock_timeout());
1524 lttng_ust_unlock_fd_tracker();
1525 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1526 prev_connect_failed
= 1;
1529 * If we cannot find the sessiond daemon, don't delay
1530 * constructor execution.
1532 ret
= handle_register_failed(sock_info
);
1538 ret
= lttng_ust_add_fd_to_tracker(fd
);
1542 PERROR("close on sock_info->socket");
1545 lttng_ust_unlock_fd_tracker();
1550 sock_info
->socket
= ret
;
1551 lttng_ust_unlock_fd_tracker();
1555 * Unlock/relock ust lock because connect is blocking (with
1556 * timeout). Don't delay constructors on the ust lock for too
1564 * Create only one root handle per listener thread for the whole
1565 * process lifetime, so we ensure we get ID which is statically
1566 * assigned to the root handle.
1568 if (sock_info
->root_handle
== -1) {
1569 ret
= lttng_abi_create_root_handle();
1571 ERR("Error creating root handle");
1574 sock_info
->root_handle
= ret
;
1577 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1579 ERR("Error registering to %s ust cmd socket",
1581 prev_connect_failed
= 1;
1583 * If we cannot register to the sessiond daemon, don't
1584 * delay constructor execution.
1586 ret
= handle_register_failed(sock_info
);
1594 * Unlock/relock ust lock because connect is blocking (with
1595 * timeout). Don't delay constructors on the ust lock for too
1602 /* Connect notify socket */
1603 lttng_ust_lock_fd_tracker();
1604 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1605 get_connect_sock_timeout());
1607 lttng_ust_unlock_fd_tracker();
1608 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1609 prev_connect_failed
= 1;
1612 * If we cannot find the sessiond daemon, don't delay
1613 * constructor execution.
1615 ret
= handle_register_failed(sock_info
);
1622 ret
= lttng_ust_add_fd_to_tracker(fd
);
1626 PERROR("close on sock_info->notify_socket");
1629 lttng_ust_unlock_fd_tracker();
1634 sock_info
->notify_socket
= ret
;
1635 lttng_ust_unlock_fd_tracker();
1639 * Unlock/relock ust lock because connect is blocking (with
1640 * timeout). Don't delay constructors on the ust lock for too
1647 timeout
= get_notify_sock_timeout();
1650 * Give at least 10ms to sessiond to reply to
1655 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1658 WARN("Error setting socket receive timeout");
1660 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1663 WARN("Error setting socket send timeout");
1665 } else if (timeout
< -1) {
1666 WARN("Unsupported timeout value %ld", timeout
);
1669 ret
= register_to_sessiond(sock_info
->notify_socket
,
1670 USTCTL_SOCKET_NOTIFY
);
1672 ERR("Error registering to %s ust notify socket",
1674 prev_connect_failed
= 1;
1676 * If we cannot register to the sessiond daemon, don't
1677 * delay constructor execution.
1679 ret
= handle_register_failed(sock_info
);
1684 sock
= sock_info
->socket
;
1690 struct ustcomm_ust_msg lum
;
1692 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1694 case 0: /* orderly shutdown */
1695 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1700 * Either sessiond has shutdown or refused us by closing the socket.
1701 * In either case, we don't want to delay construction execution,
1702 * and we need to wait before retry.
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 print_cmd(lum
.cmd
, lum
.handle
);
1715 ret
= handle_message(sock_info
, sock
, &lum
);
1717 ERR("Error handling message for %s socket",
1720 * Close socket if protocol error is
1728 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1730 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1732 if (len
== -ECONNRESET
) {
1733 DBG("%s remote end closed connection", sock_info
->name
);
1744 /* Cleanup socket handles before trying to reconnect */
1745 lttng_ust_objd_table_owner_cleanup(sock_info
);
1747 goto restart
; /* try to reconnect */
1752 pthread_mutex_lock(&ust_exit_mutex
);
1753 sock_info
->thread_active
= 0;
1754 pthread_mutex_unlock(&ust_exit_mutex
);
1759 * Weak symbol to call when the ust malloc wrapper is not loaded.
1761 __attribute__((weak
))
1762 void lttng_ust_malloc_wrapper_init(void)
1767 * sessiond monitoring thread: monitor presence of global and per-user
1768 * sessiond by polling the application common named pipe.
1770 void __attribute__((constructor
)) lttng_ust_init(void)
1772 struct timespec constructor_timeout
;
1773 sigset_t sig_all_blocked
, orig_parent_mask
;
1774 pthread_attr_t thread_attr
;
1779 if (uatomic_xchg(&initialized
, 1) == 1)
1783 * Fixup interdependency between TLS fixup mutex (which happens
1784 * to be the dynamic linker mutex) and ust_lock, taken within
1787 lttng_ust_fixup_tls();
1789 lttng_ust_loaded
= 1;
1792 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1793 * the unloading of code used by the ust_listener_threads as we can not
1794 * reliably know when they exited. To do that, manually load
1795 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1796 * this library so it never becomes zero, thus never gets unloaded from the
1797 * address space of the process. Since we are already running in the
1798 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1799 * simply increment the refcount and no additionnal work is needed by the
1800 * dynamic loader as the shared library is already loaded in the address
1801 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1802 * unloading of the UST library if its refcount becomes zero (which should
1803 * never happen). Do the return value check but discard the handle at the
1804 * end of the function as it's not needed.
1806 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1808 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1812 * We want precise control over the order in which we construct
1813 * our sub-libraries vs starting to receive commands from
1814 * sessiond (otherwise leading to errors when trying to create
1815 * sessiond before the init functions are completed).
1818 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1820 lttng_ust_init_fd_tracker();
1821 lttng_ust_clock_init();
1822 lttng_ust_getcpu_init();
1823 lttng_ust_statedump_init();
1824 lttng_ring_buffer_metadata_client_init();
1825 lttng_ring_buffer_client_overwrite_init();
1826 lttng_ring_buffer_client_overwrite_rt_init();
1827 lttng_ring_buffer_client_discard_init();
1828 lttng_ring_buffer_client_discard_rt_init();
1829 lttng_perf_counter_init();
1831 * Invoke ust malloc wrapper init before starting other threads.
1833 lttng_ust_malloc_wrapper_init();
1835 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1837 get_allow_blocking();
1839 ret
= sem_init(&constructor_wait
, 0, 0);
1844 ret
= setup_global_apps();
1846 assert(global_apps
.allowed
== 0);
1847 DBG("global apps setup returned %d", ret
);
1850 ret
= setup_local_apps();
1852 assert(local_apps
.allowed
== 0);
1853 DBG("local apps setup returned %d", ret
);
1856 /* A new thread created by pthread_create inherits the signal mask
1857 * from the parent. To avoid any signal being received by the
1858 * listener thread, we block all signals temporarily in the parent,
1859 * while we create the listener thread.
1861 sigfillset(&sig_all_blocked
);
1862 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1864 ERR("pthread_sigmask: %s", strerror(ret
));
1867 ret
= pthread_attr_init(&thread_attr
);
1869 ERR("pthread_attr_init: %s", strerror(ret
));
1871 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1873 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1876 if (global_apps
.allowed
) {
1877 pthread_mutex_lock(&ust_exit_mutex
);
1878 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1879 ust_listener_thread
, &global_apps
);
1881 ERR("pthread_create global: %s", strerror(ret
));
1883 global_apps
.thread_active
= 1;
1884 pthread_mutex_unlock(&ust_exit_mutex
);
1886 handle_register_done(&global_apps
);
1889 if (local_apps
.allowed
) {
1890 pthread_mutex_lock(&ust_exit_mutex
);
1891 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1892 ust_listener_thread
, &local_apps
);
1894 ERR("pthread_create local: %s", strerror(ret
));
1896 local_apps
.thread_active
= 1;
1897 pthread_mutex_unlock(&ust_exit_mutex
);
1899 handle_register_done(&local_apps
);
1901 ret
= pthread_attr_destroy(&thread_attr
);
1903 ERR("pthread_attr_destroy: %s", strerror(ret
));
1906 /* Restore original signal mask in parent */
1907 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1909 ERR("pthread_sigmask: %s", strerror(ret
));
1912 switch (timeout_mode
) {
1913 case 1: /* timeout wait */
1915 ret
= sem_timedwait(&constructor_wait
,
1916 &constructor_timeout
);
1917 } while (ret
< 0 && errno
== EINTR
);
1921 ERR("Timed out waiting for lttng-sessiond");
1924 PERROR("sem_timedwait");
1927 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1932 case -1:/* wait forever */
1934 ret
= sem_wait(&constructor_wait
);
1935 } while (ret
< 0 && errno
== EINTR
);
1942 ERR("Unexpected error \"%s\" returned by sem_wait",
1947 case 0: /* no timeout */
1953 void lttng_ust_cleanup(int exiting
)
1955 cleanup_sock_info(&global_apps
, exiting
);
1956 cleanup_sock_info(&local_apps
, exiting
);
1957 local_apps
.allowed
= 0;
1958 global_apps
.allowed
= 0;
1960 * The teardown in this function all affect data structures
1961 * accessed under the UST lock by the listener thread. This
1962 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1963 * that none of these threads are accessing this data at this
1966 lttng_ust_abi_exit();
1967 lttng_ust_events_exit();
1968 lttng_perf_counter_exit();
1969 lttng_ring_buffer_client_discard_rt_exit();
1970 lttng_ring_buffer_client_discard_exit();
1971 lttng_ring_buffer_client_overwrite_rt_exit();
1972 lttng_ring_buffer_client_overwrite_exit();
1973 lttng_ring_buffer_metadata_client_exit();
1974 lttng_ust_statedump_destroy();
1977 /* Reinitialize values for fork */
1978 sem_count
= sem_count_initial_value
;
1979 lttng_ust_comm_should_quit
= 0;
1984 void __attribute__((destructor
)) lttng_ust_exit(void)
1989 * Using pthread_cancel here because:
1990 * A) we don't want to hang application teardown.
1991 * B) the thread is not allocating any resource.
1995 * Require the communication thread to quit. Synchronize with
1996 * mutexes to ensure it is not in a mutex critical section when
1997 * pthread_cancel is later called.
2000 lttng_ust_comm_should_quit
= 1;
2003 pthread_mutex_lock(&ust_exit_mutex
);
2004 /* cancel threads */
2005 if (global_apps
.thread_active
) {
2006 ret
= pthread_cancel(global_apps
.ust_listener
);
2008 ERR("Error cancelling global ust listener thread: %s",
2011 global_apps
.thread_active
= 0;
2014 if (local_apps
.thread_active
) {
2015 ret
= pthread_cancel(local_apps
.ust_listener
);
2017 ERR("Error cancelling local ust listener thread: %s",
2020 local_apps
.thread_active
= 0;
2023 pthread_mutex_unlock(&ust_exit_mutex
);
2026 * Do NOT join threads: use of sys_futex makes it impossible to
2027 * join the threads without using async-cancel, but async-cancel
2028 * is delivered by a signal, which could hit the target thread
2029 * anywhere in its code path, including while the ust_lock() is
2030 * held, causing a deadlock for the other thread. Let the OS
2031 * cleanup the threads if there are stalled in a syscall.
2033 lttng_ust_cleanup(1);
2037 * We exclude the worker threads across fork and clone (except
2038 * CLONE_VM), because these system calls only keep the forking thread
2039 * running in the child. Therefore, we don't want to call fork or clone
2040 * in the middle of an tracepoint or ust tracing state modification.
2041 * Holding this mutex protects these structures across fork and clone.
2043 void ust_before_fork(sigset_t
*save_sigset
)
2046 * Disable signals. This is to avoid that the child intervenes
2047 * before it is properly setup for tracing. It is safer to
2048 * disable all signals, because then we know we are not breaking
2049 * anything by restoring the original mask.
2054 /* Fixup lttng-ust TLS. */
2055 lttng_ust_fixup_tls();
2057 if (URCU_TLS(lttng_ust_nest_count
))
2059 /* Disable signals */
2060 sigfillset(&all_sigs
);
2061 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2063 PERROR("sigprocmask");
2066 pthread_mutex_lock(&ust_fork_mutex
);
2069 rcu_bp_before_fork();
2070 lttng_ust_lock_fd_tracker();
2073 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2077 DBG("process %d", getpid());
2078 lttng_ust_unlock_fd_tracker();
2081 pthread_mutex_unlock(&ust_fork_mutex
);
2083 /* Restore signals */
2084 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2086 PERROR("sigprocmask");
2090 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2092 if (URCU_TLS(lttng_ust_nest_count
))
2094 DBG("process %d", getpid());
2095 rcu_bp_after_fork_parent();
2096 /* Release mutexes and reenable signals */
2097 ust_after_fork_common(restore_sigset
);
2101 * After fork, in the child, we need to cleanup all the leftover state,
2102 * except the worker thread which already magically disappeared thanks
2103 * to the weird Linux fork semantics. After tyding up, we call
2104 * lttng_ust_init() again to start over as a new PID.
2106 * This is meant for forks() that have tracing in the child between the
2107 * fork and following exec call (if there is any).
2109 void ust_after_fork_child(sigset_t
*restore_sigset
)
2111 if (URCU_TLS(lttng_ust_nest_count
))
2113 lttng_context_vpid_reset();
2114 lttng_context_vtid_reset();
2115 lttng_context_procname_reset();
2116 DBG("process %d", getpid());
2117 /* Release urcu mutexes */
2118 rcu_bp_after_fork_child();
2119 lttng_ust_cleanup(0);
2120 /* Release mutexes and reenable signals */
2121 ust_after_fork_common(restore_sigset
);
2125 void lttng_ust_sockinfo_session_enabled(void *owner
)
2127 struct sock_info
*sock_info
= owner
;
2128 sock_info
->statedump_pending
= 1;