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>
34 #include <semaphore.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/futex.h>
40 #include <urcu/compiler.h>
42 #include <lttng/ust-events.h>
43 #include <lttng/ust-abi.h>
44 #include <lttng/ust.h>
45 #include <lttng/ust-error.h>
46 #include <lttng/ust-ctl.h>
47 #include <urcu/tls-compat.h>
50 #include <usterr-signal-safe.h>
52 #include "tracepoint-internal.h"
53 #include "lttng-tracer-core.h"
55 #include "../libringbuffer/tlsfixup.h"
56 #include "lttng-ust-statedump.h"
58 #include "../libringbuffer/getcpu.h"
62 * Has lttng ust comm constructor been called ?
64 static int initialized
;
67 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
68 * Held when handling a command, also held by fork() to deal with
69 * removal of threads, and by exit path.
71 * The UST lock is the centralized mutex across UST tracing control and
74 * ust_exit_mutex must never nest in ust_mutex.
76 * ust_fork_mutex must never nest in ust_mutex.
78 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
79 * counter lazy initialization called by events within the statedump,
80 * which traces while the ust_mutex is held.
82 * ust_lock nests within the dynamic loader lock (within glibc) because
83 * it is taken within the library constructor.
85 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
87 /* Allow nesting the ust_mutex within the same thread. */
88 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
91 * ust_exit_mutex protects thread_active variable wrt thread exit. It
92 * cannot be done by ust_mutex because pthread_cancel(), which takes an
93 * internal libc lock, cannot nest within ust_mutex.
95 * It never nests within a ust_mutex.
97 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
100 * ust_fork_mutex protects base address statedump tracing against forks. It
101 * prevents the dynamic loader lock to be taken (by base address statedump
102 * tracing) while a fork is happening, thus preventing deadlock issues with
103 * the dynamic loader lock.
105 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
107 /* Should the ust comm thread quit ? */
108 static int lttng_ust_comm_should_quit
;
111 * This variable can be tested by applications to check whether
112 * lttng-ust is loaded. They simply have to define their own
113 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
114 * library constructor.
116 int lttng_ust_loaded
__attribute__((weak
));
119 * Return 0 on success, -1 if should quit.
120 * The lock is taken in both cases.
125 sigset_t sig_all_blocked
, orig_mask
;
128 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
130 ERR("pthread_setcancelstate: %s", strerror(ret
));
132 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
133 ERR("pthread_setcancelstate: unexpected oldstate");
135 sigfillset(&sig_all_blocked
);
136 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
138 ERR("pthread_sigmask: %s", strerror(ret
));
140 if (!URCU_TLS(ust_mutex_nest
)++)
141 pthread_mutex_lock(&ust_mutex
);
142 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
144 ERR("pthread_sigmask: %s", strerror(ret
));
146 if (lttng_ust_comm_should_quit
) {
154 * ust_lock_nocheck() can be used in constructors/destructors, because
155 * they are already nested within the dynamic loader lock, and therefore
156 * have exclusive access against execution of liblttng-ust destructor.
159 void ust_lock_nocheck(void)
161 sigset_t sig_all_blocked
, orig_mask
;
164 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
166 ERR("pthread_setcancelstate: %s", strerror(ret
));
168 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
169 ERR("pthread_setcancelstate: unexpected oldstate");
171 sigfillset(&sig_all_blocked
);
172 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
174 ERR("pthread_sigmask: %s", strerror(ret
));
176 if (!URCU_TLS(ust_mutex_nest
)++)
177 pthread_mutex_lock(&ust_mutex
);
178 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
180 ERR("pthread_sigmask: %s", strerror(ret
));
187 void ust_unlock(void)
189 sigset_t sig_all_blocked
, orig_mask
;
192 sigfillset(&sig_all_blocked
);
193 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
195 ERR("pthread_sigmask: %s", strerror(ret
));
197 if (!--URCU_TLS(ust_mutex_nest
))
198 pthread_mutex_unlock(&ust_mutex
);
199 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
201 ERR("pthread_sigmask: %s", strerror(ret
));
203 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
205 ERR("pthread_setcancelstate: %s", strerror(ret
));
207 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
208 ERR("pthread_setcancelstate: unexpected oldstate");
213 * Wait for either of these before continuing to the main
215 * - the register_done message from sessiond daemon
216 * (will let the sessiond daemon enable sessions before main
218 * - sessiond daemon is not reachable.
219 * - timeout (ensuring applications are resilient to session
222 static sem_t constructor_wait
;
224 * Doing this for both the global and local sessiond.
227 sem_count_initial_value
= 4,
230 static int sem_count
= sem_count_initial_value
;
233 * Counting nesting within lttng-ust. Used to ensure that calling fork()
234 * from liblttng-ust does not execute the pre/post fork handlers.
236 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
239 * Info about socket and associated listener thread.
243 pthread_t ust_listener
; /* listener thread */
245 int registration_done
;
250 char sock_path
[PATH_MAX
];
254 char wait_shm_path
[PATH_MAX
];
256 /* Keep track of lazy state dump not performed yet. */
257 int statedump_pending
;
258 int initial_statedump_done
;
261 /* Socket from app (connect) to session daemon (listen) for communication */
262 struct sock_info global_apps
= {
267 .registration_done
= 0,
271 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
275 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
277 .statedump_pending
= 0,
278 .initial_statedump_done
= 0,
281 /* TODO: allow global_apps_sock_path override */
283 struct sock_info local_apps
= {
287 .registration_done
= 0,
288 .allowed
= 0, /* Check setuid bit first */
294 .statedump_pending
= 0,
295 .initial_statedump_done
= 0,
298 static int wait_poll_fallback
;
300 static const char *cmd_name_mapping
[] = {
301 [ LTTNG_UST_RELEASE
] = "Release",
302 [ LTTNG_UST_SESSION
] = "Create Session",
303 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
305 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
306 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
307 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
308 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
310 /* Session FD commands */
311 [ LTTNG_UST_CHANNEL
] = "Create Channel",
312 [ LTTNG_UST_SESSION_START
] = "Start Session",
313 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
315 /* Channel FD commands */
316 [ LTTNG_UST_STREAM
] = "Create Stream",
317 [ LTTNG_UST_EVENT
] = "Create Event",
319 /* Event and Channel FD commands */
320 [ LTTNG_UST_CONTEXT
] = "Create Context",
321 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
323 /* Event, Channel and Session commands */
324 [ LTTNG_UST_ENABLE
] = "Enable",
325 [ LTTNG_UST_DISABLE
] = "Disable",
327 /* Tracepoint list commands */
328 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
329 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
331 /* Event FD commands */
332 [ LTTNG_UST_FILTER
] = "Create Filter",
333 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
336 static const char *str_timeout
;
337 static int got_timeout_env
;
339 extern void lttng_ring_buffer_client_overwrite_init(void);
340 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
341 extern void lttng_ring_buffer_client_discard_init(void);
342 extern void lttng_ring_buffer_client_discard_rt_init(void);
343 extern void lttng_ring_buffer_metadata_client_init(void);
344 extern void lttng_ring_buffer_client_overwrite_exit(void);
345 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
346 extern void lttng_ring_buffer_client_discard_exit(void);
347 extern void lttng_ring_buffer_client_discard_rt_exit(void);
348 extern void lttng_ring_buffer_metadata_client_exit(void);
350 static char *get_map_shm(struct sock_info
*sock_info
);
352 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
355 size_t copied
= 0, to_copy
= len
;
358 ret
= read(fd
, buf
+ copied
, to_copy
);
363 } while ((ret
> 0 && to_copy
> 0)
364 || (ret
< 0 && errno
== EINTR
));
371 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
375 const char *get_lttng_home_dir(void)
379 val
= (const char *) lttng_getenv("LTTNG_HOME");
383 return (const char *) lttng_getenv("HOME");
387 * Force a read (imply TLS fixup for dlopen) of TLS variables.
390 void lttng_fixup_nest_count_tls(void)
392 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
396 void lttng_fixup_ust_mutex_nest_tls(void)
398 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
405 void lttng_fixup_urcu_bp_tls(void)
411 void lttng_ust_fixup_tls(void)
413 lttng_fixup_urcu_bp_tls();
414 lttng_fixup_ringbuffer_tls();
415 lttng_fixup_vtid_tls();
416 lttng_fixup_nest_count_tls();
417 lttng_fixup_procname_tls();
418 lttng_fixup_ust_mutex_nest_tls();
419 lttng_ust_fixup_fd_tracker_tls();
422 int lttng_get_notify_socket(void *owner
)
424 struct sock_info
*info
= owner
;
426 return info
->notify_socket
;
430 void print_cmd(int cmd
, int handle
)
432 const char *cmd_name
= "Unknown";
434 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
435 && cmd_name_mapping
[cmd
]) {
436 cmd_name
= cmd_name_mapping
[cmd
];
438 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
440 lttng_ust_obj_get_name(handle
), handle
);
444 int setup_global_apps(void)
447 assert(!global_apps
.wait_shm_mmap
);
449 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
450 if (!global_apps
.wait_shm_mmap
) {
451 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
452 global_apps
.allowed
= 0;
457 global_apps
.allowed
= 1;
462 int setup_local_apps(void)
465 const char *home_dir
;
468 assert(!local_apps
.wait_shm_mmap
);
472 * Disallow per-user tracing for setuid binaries.
474 if (uid
!= geteuid()) {
475 assert(local_apps
.allowed
== 0);
479 home_dir
= get_lttng_home_dir();
481 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
482 assert(local_apps
.allowed
== 0);
486 local_apps
.allowed
= 1;
487 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
489 LTTNG_DEFAULT_HOME_RUNDIR
,
490 LTTNG_UST_SOCK_FILENAME
);
491 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
492 LTTNG_UST_WAIT_FILENAME
,
495 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
496 if (!local_apps
.wait_shm_mmap
) {
497 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
498 local_apps
.allowed
= 0;
507 * Get socket timeout, in ms.
508 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
511 long get_timeout(void)
513 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
515 if (!got_timeout_env
) {
516 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
520 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
521 /* All negative values are considered as "-1". */
522 if (constructor_delay_ms
< -1)
523 constructor_delay_ms
= -1;
524 return constructor_delay_ms
;
527 /* Timeout for notify socket send and recv. */
529 long get_notify_sock_timeout(void)
531 return get_timeout();
534 /* Timeout for connecting to cmd and notify sockets. */
536 long get_connect_sock_timeout(void)
538 return get_timeout();
542 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
545 int get_constructor_timeout(struct timespec
*constructor_timeout
)
547 long constructor_delay_ms
;
550 constructor_delay_ms
= get_timeout();
552 switch (constructor_delay_ms
) {
553 case -1:/* fall-through */
555 return constructor_delay_ms
;
561 * If we are unable to find the current time, don't wait.
563 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
568 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
569 constructor_timeout
->tv_nsec
+=
570 (constructor_delay_ms
% 1000UL) * 1000000UL;
571 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
572 constructor_timeout
->tv_sec
++;
573 constructor_timeout
->tv_nsec
-= 1000000000UL;
575 /* Timeout wait (constructor_delay_ms). */
580 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
582 return ustcomm_send_reg_msg(socket
,
585 lttng_alignof(uint8_t) * CHAR_BIT
,
586 lttng_alignof(uint16_t) * CHAR_BIT
,
587 lttng_alignof(uint32_t) * CHAR_BIT
,
588 lttng_alignof(uint64_t) * CHAR_BIT
,
589 lttng_alignof(unsigned long) * CHAR_BIT
);
593 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
597 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
600 DBG("message successfully sent");
603 if (len
== -ECONNRESET
) {
604 DBG("remote end closed connection");
609 DBG("incorrect message size: %zd", len
);
615 void decrement_sem_count(unsigned int count
)
619 assert(uatomic_read(&sem_count
) >= count
);
621 if (uatomic_read(&sem_count
) <= 0) {
625 ret
= uatomic_add_return(&sem_count
, -count
);
627 ret
= sem_post(&constructor_wait
);
633 int handle_register_done(struct sock_info
*sock_info
)
635 if (sock_info
->registration_done
)
637 sock_info
->registration_done
= 1;
639 decrement_sem_count(1);
645 int handle_register_failed(struct sock_info
*sock_info
)
647 if (sock_info
->registration_done
)
649 sock_info
->registration_done
= 1;
650 sock_info
->initial_statedump_done
= 1;
652 decrement_sem_count(2);
658 * Only execute pending statedump after the constructor semaphore has
659 * been posted by the current listener thread. This means statedump will
660 * only be performed after the "registration done" command is received
661 * from this thread's session daemon.
663 * This ensures we don't run into deadlock issues with the dynamic
664 * loader mutex, which is held while the constructor is called and
665 * waiting on the constructor semaphore. All operations requiring this
666 * dynamic loader lock need to be postponed using this mechanism.
668 * In a scenario with two session daemons connected to the application,
669 * it is possible that the first listener thread which receives the
670 * registration done command issues its statedump while the dynamic
671 * loader lock is still held by the application constructor waiting on
672 * the semaphore. It will however be allowed to proceed when the
673 * second session daemon sends the registration done command to the
674 * second listener thread. This situation therefore does not produce
678 void handle_pending_statedump(struct sock_info
*sock_info
)
680 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
681 sock_info
->statedump_pending
= 0;
682 pthread_mutex_lock(&ust_fork_mutex
);
683 lttng_handle_pending_statedump(sock_info
);
684 pthread_mutex_unlock(&ust_fork_mutex
);
686 if (!sock_info
->initial_statedump_done
) {
687 sock_info
->initial_statedump_done
= 1;
688 decrement_sem_count(1);
694 int handle_message(struct sock_info
*sock_info
,
695 int sock
, struct ustcomm_ust_msg
*lum
)
698 const struct lttng_ust_objd_ops
*ops
;
699 struct ustcomm_ust_reply lur
;
701 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
704 memset(&lur
, 0, sizeof(lur
));
707 ret
= -LTTNG_UST_ERR_EXITING
;
711 ops
= objd_ops(lum
->handle
);
718 case LTTNG_UST_REGISTER_DONE
:
719 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
720 ret
= handle_register_done(sock_info
);
724 case LTTNG_UST_RELEASE
:
725 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
728 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
730 case LTTNG_UST_FILTER
:
732 /* Receive filter data */
733 struct lttng_ust_filter_bytecode_node
*bytecode
;
735 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
736 ERR("Filter data size is too large: %u bytes",
737 lum
->u
.filter
.data_size
);
742 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
743 ERR("Filter reloc offset %u is not within data",
744 lum
->u
.filter
.reloc_offset
);
749 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
754 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
755 lum
->u
.filter
.data_size
);
757 case 0: /* orderly shutdown */
762 if (len
== lum
->u
.filter
.data_size
) {
763 DBG("filter data received");
765 } else if (len
< 0) {
766 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
767 if (len
== -ECONNRESET
) {
768 ERR("%s remote end closed connection", sock_info
->name
);
777 DBG("incorrect filter data message size: %zd", len
);
783 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
784 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
785 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
787 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
788 (unsigned long) bytecode
,
793 /* don't free bytecode if everything went fine. */
800 case LTTNG_UST_EXCLUSION
:
802 /* Receive exclusion names */
803 struct lttng_ust_excluder_node
*node
;
806 count
= lum
->u
.exclusion
.count
;
808 /* There are no names to read */
812 node
= zmalloc(sizeof(*node
) +
813 count
* LTTNG_UST_SYM_NAME_LEN
);
818 node
->excluder
.count
= count
;
819 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
820 count
* LTTNG_UST_SYM_NAME_LEN
);
822 case 0: /* orderly shutdown */
827 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
828 DBG("Exclusion data received");
830 } else if (len
< 0) {
831 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
832 if (len
== -ECONNRESET
) {
833 ERR("%s remote end closed connection", sock_info
->name
);
842 DBG("Incorrect exclusion data message size: %zd", len
);
849 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
850 (unsigned long) node
,
855 /* Don't free exclusion data if everything went fine. */
862 case LTTNG_UST_CHANNEL
:
867 len
= ustcomm_recv_channel_from_sessiond(sock
,
868 &chan_data
, lum
->u
.channel
.len
,
871 case 0: /* orderly shutdown */
875 if (len
== lum
->u
.channel
.len
) {
876 DBG("channel data received");
878 } else if (len
< 0) {
879 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
880 if (len
== -ECONNRESET
) {
881 ERR("%s remote end closed connection", sock_info
->name
);
888 DBG("incorrect channel data message size: %zd", len
);
893 args
.channel
.chan_data
= chan_data
;
894 args
.channel
.wakeup_fd
= wakeup_fd
;
896 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
897 (unsigned long) &lum
->u
,
903 case LTTNG_UST_STREAM
:
905 /* Receive shm_fd, wakeup_fd */
906 ret
= ustcomm_recv_stream_from_sessiond(sock
,
909 &args
.stream
.wakeup_fd
);
915 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
916 (unsigned long) &lum
->u
,
922 case LTTNG_UST_CONTEXT
:
923 switch (lum
->u
.context
.ctx
) {
924 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
927 size_t ctxlen
, recvlen
;
929 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
930 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
931 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
932 ERR("Application context string length size is too large: %zu bytes",
937 strcpy(ctxstr
, "$app.");
938 p
= &ctxstr
[strlen("$app.")];
939 recvlen
= ctxlen
- strlen("$app.");
940 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
942 case 0: /* orderly shutdown */
946 if (len
== recvlen
) {
947 DBG("app context data received");
949 } else if (len
< 0) {
950 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
951 if (len
== -ECONNRESET
) {
952 ERR("%s remote end closed connection", sock_info
->name
);
959 DBG("incorrect app context data message size: %zd", len
);
964 /* Put : between provider and ctxname. */
965 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
966 args
.app_context
.ctxname
= ctxstr
;
973 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
974 (unsigned long) &lum
->u
,
982 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
983 (unsigned long) &lum
->u
,
990 lur
.handle
= lum
->handle
;
994 lur
.ret_code
= LTTNG_UST_OK
;
997 * Use -LTTNG_UST_ERR as wildcard for UST internal
998 * error that are not caused by the transport, except if
999 * we already have a more precise error message to
1002 if (ret
> -LTTNG_UST_ERR
) {
1003 /* Translate code to UST error. */
1006 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1009 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1012 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1015 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1018 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1021 lur
.ret_code
= -LTTNG_UST_ERR
;
1030 case LTTNG_UST_TRACER_VERSION
:
1031 lur
.u
.version
= lum
->u
.version
;
1033 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1034 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1038 DBG("Return value: %d", lur
.ret_val
);
1043 * Performed delayed statedump operations outside of the UST
1044 * lock. We need to take the dynamic loader lock before we take
1045 * the UST lock internally within handle_pending_statedump().
1047 handle_pending_statedump(sock_info
);
1050 ret
= -LTTNG_UST_ERR_EXITING
;
1054 ret
= send_reply(sock
, &lur
);
1056 DBG("error sending reply");
1061 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1064 if (lur
.ret_code
== LTTNG_UST_OK
) {
1066 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1067 len
= ustcomm_send_unix_sock(sock
,
1068 &args
.field_list
.entry
,
1069 sizeof(args
.field_list
.entry
));
1074 if (len
!= sizeof(args
.field_list
.entry
)) {
1088 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1092 if (sock_info
->root_handle
!= -1) {
1093 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1095 ERR("Error unref root handle");
1097 sock_info
->root_handle
= -1;
1099 sock_info
->registration_done
= 0;
1100 sock_info
->initial_statedump_done
= 0;
1103 * wait_shm_mmap, socket and notify socket are used by listener
1104 * threads outside of the ust lock, so we cannot tear them down
1105 * ourselves, because we cannot join on these threads. Leave
1106 * responsibility of cleaning up these resources to the OS
1112 if (sock_info
->socket
!= -1) {
1113 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1115 ERR("Error closing ust cmd socket");
1117 sock_info
->socket
= -1;
1119 if (sock_info
->notify_socket
!= -1) {
1120 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1122 ERR("Error closing ust notify socket");
1124 sock_info
->notify_socket
= -1;
1126 if (sock_info
->wait_shm_mmap
) {
1129 page_size
= sysconf(_SC_PAGE_SIZE
);
1130 if (page_size
<= 0) {
1134 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1136 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1138 ERR("Error unmapping wait shm");
1141 sock_info
->wait_shm_mmap
= NULL
;
1146 * Using fork to set umask in the child process (not multi-thread safe).
1147 * We deal with the shm_open vs ftruncate race (happening when the
1148 * sessiond owns the shm and does not let everybody modify it, to ensure
1149 * safety against shm_unlink) by simply letting the mmap fail and
1150 * retrying after a few seconds.
1151 * For global shm, everybody has rw access to it until the sessiond
1155 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1157 int wait_shm_fd
, ret
;
1161 * Try to open read-only.
1163 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1164 if (wait_shm_fd
>= 0) {
1167 size_t bytes_read
= 0;
1170 * Try to read the fd. If unable to do so, try opening
1174 len
= read(wait_shm_fd
,
1175 &((char *) &tmp_read
)[bytes_read
],
1176 sizeof(tmp_read
) - bytes_read
);
1180 } while ((len
< 0 && errno
== EINTR
)
1181 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1182 if (bytes_read
!= sizeof(tmp_read
)) {
1183 ret
= close(wait_shm_fd
);
1185 ERR("close wait_shm_fd");
1190 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1192 * Real-only open did not work, and it's not because the
1193 * entry was not present. It's a failure that prohibits
1196 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1202 * If the open failed because the file did not exist, or because
1203 * the file was not truncated yet, try creating it ourself.
1205 URCU_TLS(lttng_ust_nest_count
)++;
1207 URCU_TLS(lttng_ust_nest_count
)--;
1212 * Parent: wait for child to return, in which case the
1213 * shared memory map will have been created.
1215 pid
= wait(&status
);
1216 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1221 * Try to open read-only again after creation.
1223 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1224 if (wait_shm_fd
< 0) {
1226 * Real-only open did not work. It's a failure
1227 * that prohibits using shm.
1229 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1233 } else if (pid
== 0) {
1237 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1238 if (sock_info
->global
)
1239 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1241 * We're alone in a child process, so we can modify the
1242 * process-wide umask.
1244 umask(~create_mode
);
1246 * Try creating shm (or get rw access).
1247 * We don't do an exclusive open, because we allow other
1248 * processes to create+ftruncate it concurrently.
1250 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1251 O_RDWR
| O_CREAT
, create_mode
);
1252 if (wait_shm_fd
>= 0) {
1253 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1255 PERROR("ftruncate");
1256 _exit(EXIT_FAILURE
);
1258 _exit(EXIT_SUCCESS
);
1261 * For local shm, we need to have rw access to accept
1262 * opening it: this means the local sessiond will be
1263 * able to wake us up. For global shm, we open it even
1264 * if rw access is not granted, because the root.root
1265 * sessiond will be able to override all rights and wake
1268 if (!sock_info
->global
&& errno
!= EACCES
) {
1269 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1270 _exit(EXIT_FAILURE
);
1273 * The shm exists, but we cannot open it RW. Report
1276 _exit(EXIT_SUCCESS
);
1281 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1282 struct stat statbuf
;
1285 * Ensure that our user is the owner of the shm file for
1286 * local shm. If we do not own the file, it means our
1287 * sessiond will not have access to wake us up (there is
1288 * probably a rogue process trying to fake our
1289 * sessiond). Fallback to polling method in this case.
1291 ret
= fstat(wait_shm_fd
, &statbuf
);
1296 if (statbuf
.st_uid
!= getuid())
1302 ret
= close(wait_shm_fd
);
1304 PERROR("Error closing fd");
1310 char *get_map_shm(struct sock_info
*sock_info
)
1313 int wait_shm_fd
, ret
;
1314 char *wait_shm_mmap
;
1316 page_size
= sysconf(_SC_PAGE_SIZE
);
1317 if (page_size
<= 0) {
1321 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1325 lttng_ust_lock_fd_tracker();
1326 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1327 if (wait_shm_fd
< 0) {
1328 lttng_ust_unlock_fd_tracker();
1332 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1334 ret
= close(wait_shm_fd
);
1336 PERROR("Error closing fd");
1338 lttng_ust_unlock_fd_tracker();
1343 lttng_ust_unlock_fd_tracker();
1345 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1346 MAP_SHARED
, wait_shm_fd
, 0);
1348 /* close shm fd immediately after taking the mmap reference */
1349 lttng_ust_lock_fd_tracker();
1350 ret
= close(wait_shm_fd
);
1352 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1354 PERROR("Error closing fd");
1356 lttng_ust_unlock_fd_tracker();
1358 if (wait_shm_mmap
== MAP_FAILED
) {
1359 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1362 return wait_shm_mmap
;
1369 void wait_for_sessiond(struct sock_info
*sock_info
)
1371 /* Use ust_lock to check if we should quit. */
1375 if (wait_poll_fallback
) {
1380 assert(sock_info
->wait_shm_mmap
);
1382 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1383 /* Wait for futex wakeup */
1384 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1387 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1388 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1391 /* Value already changed. */
1394 /* Retry if interrupted by signal. */
1395 break; /* Get out of switch. */
1397 wait_poll_fallback
= 1;
1399 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1400 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1401 "Please upgrade your kernel "
1402 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1403 "mainline). LTTng-UST will use polling mode fallback.");
1422 * This thread does not allocate any resource, except within
1423 * handle_message, within mutex protection. This mutex protects against
1425 * The other moment it allocates resources is at socket connection, which
1426 * is also protected by the mutex.
1429 void *ust_listener_thread(void *arg
)
1431 struct sock_info
*sock_info
= arg
;
1432 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1435 lttng_ust_fixup_tls();
1437 * If available, add '-ust' to the end of this thread's
1440 ret
= lttng_ust_setustprocname();
1442 ERR("Unable to set UST process name");
1445 /* Restart trying to connect to the session daemon */
1447 if (prev_connect_failed
) {
1448 /* Wait for sessiond availability with pipe */
1449 wait_for_sessiond(sock_info
);
1453 * Sleep for 5 seconds before retrying after a
1454 * sequence of failure / wait / failure. This
1455 * deals with a killed or broken session daemon.
1461 prev_connect_failed
= 0;
1468 if (sock_info
->socket
!= -1) {
1469 /* FD tracker is updated by ustcomm_close_unix_sock() */
1470 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1472 ERR("Error closing %s ust cmd socket",
1475 sock_info
->socket
= -1;
1477 if (sock_info
->notify_socket
!= -1) {
1478 /* FD tracker is updated by ustcomm_close_unix_sock() */
1479 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1481 ERR("Error closing %s ust notify socket",
1484 sock_info
->notify_socket
= -1;
1489 * Register. We need to perform both connect and sending
1490 * registration message before doing the next connect otherwise
1491 * we may reach unix socket connect queue max limits and block
1492 * on the 2nd connect while the session daemon is awaiting the
1493 * first connect registration message.
1495 /* Connect cmd socket */
1496 lttng_ust_lock_fd_tracker();
1497 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1498 get_connect_sock_timeout());
1500 lttng_ust_unlock_fd_tracker();
1501 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1502 prev_connect_failed
= 1;
1505 * If we cannot find the sessiond daemon, don't delay
1506 * constructor execution.
1508 ret
= handle_register_failed(sock_info
);
1514 ret
= lttng_ust_add_fd_to_tracker(fd
);
1518 PERROR("close on sock_info->socket");
1521 lttng_ust_unlock_fd_tracker();
1526 sock_info
->socket
= ret
;
1527 lttng_ust_unlock_fd_tracker();
1531 * Unlock/relock ust lock because connect is blocking (with
1532 * timeout). Don't delay constructors on the ust lock for too
1540 * Create only one root handle per listener thread for the whole
1541 * process lifetime, so we ensure we get ID which is statically
1542 * assigned to the root handle.
1544 if (sock_info
->root_handle
== -1) {
1545 ret
= lttng_abi_create_root_handle();
1547 ERR("Error creating root handle");
1550 sock_info
->root_handle
= ret
;
1553 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1555 ERR("Error registering to %s ust cmd socket",
1557 prev_connect_failed
= 1;
1559 * If we cannot register to the sessiond daemon, don't
1560 * delay constructor execution.
1562 ret
= handle_register_failed(sock_info
);
1570 * Unlock/relock ust lock because connect is blocking (with
1571 * timeout). Don't delay constructors on the ust lock for too
1578 /* Connect notify socket */
1579 lttng_ust_lock_fd_tracker();
1580 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1581 get_connect_sock_timeout());
1583 lttng_ust_unlock_fd_tracker();
1584 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1585 prev_connect_failed
= 1;
1588 * If we cannot find the sessiond daemon, don't delay
1589 * constructor execution.
1591 ret
= handle_register_failed(sock_info
);
1598 ret
= lttng_ust_add_fd_to_tracker(fd
);
1602 PERROR("close on sock_info->notify_socket");
1605 lttng_ust_unlock_fd_tracker();
1610 sock_info
->notify_socket
= ret
;
1611 lttng_ust_unlock_fd_tracker();
1615 * Unlock/relock ust lock because connect is blocking (with
1616 * timeout). Don't delay constructors on the ust lock for too
1623 timeout
= get_notify_sock_timeout();
1626 * Give at least 10ms to sessiond to reply to
1631 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1634 WARN("Error setting socket receive timeout");
1636 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1639 WARN("Error setting socket send timeout");
1641 } else if (timeout
< -1) {
1642 WARN("Unsupported timeout value %ld", timeout
);
1645 ret
= register_to_sessiond(sock_info
->notify_socket
,
1646 USTCTL_SOCKET_NOTIFY
);
1648 ERR("Error registering to %s ust notify socket",
1650 prev_connect_failed
= 1;
1652 * If we cannot register to the sessiond daemon, don't
1653 * delay constructor execution.
1655 ret
= handle_register_failed(sock_info
);
1660 sock
= sock_info
->socket
;
1666 struct ustcomm_ust_msg lum
;
1668 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1670 case 0: /* orderly shutdown */
1671 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1676 * Either sessiond has shutdown or refused us by closing the socket.
1677 * In either case, we don't want to delay construction execution,
1678 * and we need to wait before retry.
1680 prev_connect_failed
= 1;
1682 * If we cannot register to the sessiond daemon, don't
1683 * delay constructor execution.
1685 ret
= handle_register_failed(sock_info
);
1690 print_cmd(lum
.cmd
, lum
.handle
);
1691 ret
= handle_message(sock_info
, sock
, &lum
);
1693 ERR("Error handling message for %s socket",
1696 * Close socket if protocol error is
1704 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1706 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1708 if (len
== -ECONNRESET
) {
1709 DBG("%s remote end closed connection", sock_info
->name
);
1720 /* Cleanup socket handles before trying to reconnect */
1721 lttng_ust_objd_table_owner_cleanup(sock_info
);
1723 goto restart
; /* try to reconnect */
1728 pthread_mutex_lock(&ust_exit_mutex
);
1729 sock_info
->thread_active
= 0;
1730 pthread_mutex_unlock(&ust_exit_mutex
);
1735 * Weak symbol to call when the ust malloc wrapper is not loaded.
1737 __attribute__((weak
))
1738 void lttng_ust_malloc_wrapper_init(void)
1743 * sessiond monitoring thread: monitor presence of global and per-user
1744 * sessiond by polling the application common named pipe.
1746 void __attribute__((constructor
)) lttng_ust_init(void)
1748 struct timespec constructor_timeout
;
1749 sigset_t sig_all_blocked
, orig_parent_mask
;
1750 pthread_attr_t thread_attr
;
1754 if (uatomic_xchg(&initialized
, 1) == 1)
1758 * Fixup interdependency between TLS fixup mutex (which happens
1759 * to be the dynamic linker mutex) and ust_lock, taken within
1762 lttng_ust_fixup_tls();
1764 lttng_ust_loaded
= 1;
1767 * We want precise control over the order in which we construct
1768 * our sub-libraries vs starting to receive commands from
1769 * sessiond (otherwise leading to errors when trying to create
1770 * sessiond before the init functions are completed).
1773 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1775 lttng_ust_init_fd_tracker();
1776 lttng_ust_clock_init();
1777 lttng_ust_getcpu_init();
1778 lttng_ust_statedump_init();
1779 lttng_ring_buffer_metadata_client_init();
1780 lttng_ring_buffer_client_overwrite_init();
1781 lttng_ring_buffer_client_overwrite_rt_init();
1782 lttng_ring_buffer_client_discard_init();
1783 lttng_ring_buffer_client_discard_rt_init();
1784 lttng_perf_counter_init();
1786 * Invoke ust malloc wrapper init before starting other threads.
1788 lttng_ust_malloc_wrapper_init();
1790 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1792 ret
= sem_init(&constructor_wait
, 0, 0);
1797 ret
= setup_global_apps();
1799 assert(global_apps
.allowed
== 0);
1800 DBG("global apps setup returned %d", ret
);
1803 ret
= setup_local_apps();
1805 assert(local_apps
.allowed
== 0);
1806 DBG("local apps setup returned %d", ret
);
1809 /* A new thread created by pthread_create inherits the signal mask
1810 * from the parent. To avoid any signal being received by the
1811 * listener thread, we block all signals temporarily in the parent,
1812 * while we create the listener thread.
1814 sigfillset(&sig_all_blocked
);
1815 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1817 ERR("pthread_sigmask: %s", strerror(ret
));
1820 ret
= pthread_attr_init(&thread_attr
);
1822 ERR("pthread_attr_init: %s", strerror(ret
));
1824 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1826 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1829 if (global_apps
.allowed
) {
1830 pthread_mutex_lock(&ust_exit_mutex
);
1831 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1832 ust_listener_thread
, &global_apps
);
1834 ERR("pthread_create global: %s", strerror(ret
));
1836 global_apps
.thread_active
= 1;
1837 pthread_mutex_unlock(&ust_exit_mutex
);
1839 handle_register_done(&global_apps
);
1842 if (local_apps
.allowed
) {
1843 pthread_mutex_lock(&ust_exit_mutex
);
1844 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1845 ust_listener_thread
, &local_apps
);
1847 ERR("pthread_create local: %s", strerror(ret
));
1849 local_apps
.thread_active
= 1;
1850 pthread_mutex_unlock(&ust_exit_mutex
);
1852 handle_register_done(&local_apps
);
1854 ret
= pthread_attr_destroy(&thread_attr
);
1856 ERR("pthread_attr_destroy: %s", strerror(ret
));
1859 /* Restore original signal mask in parent */
1860 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1862 ERR("pthread_sigmask: %s", strerror(ret
));
1865 switch (timeout_mode
) {
1866 case 1: /* timeout wait */
1868 ret
= sem_timedwait(&constructor_wait
,
1869 &constructor_timeout
);
1870 } while (ret
< 0 && errno
== EINTR
);
1874 ERR("Timed out waiting for lttng-sessiond");
1877 PERROR("sem_timedwait");
1880 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1885 case -1:/* wait forever */
1887 ret
= sem_wait(&constructor_wait
);
1888 } while (ret
< 0 && errno
== EINTR
);
1895 ERR("Unexpected error \"%s\" returned by sem_wait",
1900 case 0: /* no timeout */
1906 void lttng_ust_cleanup(int exiting
)
1908 cleanup_sock_info(&global_apps
, exiting
);
1909 cleanup_sock_info(&local_apps
, exiting
);
1910 local_apps
.allowed
= 0;
1911 global_apps
.allowed
= 0;
1913 * The teardown in this function all affect data structures
1914 * accessed under the UST lock by the listener thread. This
1915 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1916 * that none of these threads are accessing this data at this
1919 lttng_ust_abi_exit();
1920 lttng_ust_events_exit();
1921 lttng_perf_counter_exit();
1922 lttng_ring_buffer_client_discard_rt_exit();
1923 lttng_ring_buffer_client_discard_exit();
1924 lttng_ring_buffer_client_overwrite_rt_exit();
1925 lttng_ring_buffer_client_overwrite_exit();
1926 lttng_ring_buffer_metadata_client_exit();
1927 lttng_ust_statedump_destroy();
1930 /* Reinitialize values for fork */
1931 sem_count
= sem_count_initial_value
;
1932 lttng_ust_comm_should_quit
= 0;
1937 void __attribute__((destructor
)) lttng_ust_exit(void)
1942 * Using pthread_cancel here because:
1943 * A) we don't want to hang application teardown.
1944 * B) the thread is not allocating any resource.
1948 * Require the communication thread to quit. Synchronize with
1949 * mutexes to ensure it is not in a mutex critical section when
1950 * pthread_cancel is later called.
1953 lttng_ust_comm_should_quit
= 1;
1956 pthread_mutex_lock(&ust_exit_mutex
);
1957 /* cancel threads */
1958 if (global_apps
.thread_active
) {
1959 ret
= pthread_cancel(global_apps
.ust_listener
);
1961 ERR("Error cancelling global ust listener thread: %s",
1964 global_apps
.thread_active
= 0;
1967 if (local_apps
.thread_active
) {
1968 ret
= pthread_cancel(local_apps
.ust_listener
);
1970 ERR("Error cancelling local ust listener thread: %s",
1973 local_apps
.thread_active
= 0;
1976 pthread_mutex_unlock(&ust_exit_mutex
);
1979 * Do NOT join threads: use of sys_futex makes it impossible to
1980 * join the threads without using async-cancel, but async-cancel
1981 * is delivered by a signal, which could hit the target thread
1982 * anywhere in its code path, including while the ust_lock() is
1983 * held, causing a deadlock for the other thread. Let the OS
1984 * cleanup the threads if there are stalled in a syscall.
1986 lttng_ust_cleanup(1);
1990 * We exclude the worker threads across fork and clone (except
1991 * CLONE_VM), because these system calls only keep the forking thread
1992 * running in the child. Therefore, we don't want to call fork or clone
1993 * in the middle of an tracepoint or ust tracing state modification.
1994 * Holding this mutex protects these structures across fork and clone.
1996 void ust_before_fork(sigset_t
*save_sigset
)
1999 * Disable signals. This is to avoid that the child intervenes
2000 * before it is properly setup for tracing. It is safer to
2001 * disable all signals, because then we know we are not breaking
2002 * anything by restoring the original mask.
2007 /* Fixup lttng-ust TLS. */
2008 lttng_ust_fixup_tls();
2010 if (URCU_TLS(lttng_ust_nest_count
))
2012 /* Disable signals */
2013 sigfillset(&all_sigs
);
2014 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2016 PERROR("sigprocmask");
2019 pthread_mutex_lock(&ust_fork_mutex
);
2022 rcu_bp_before_fork();
2025 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2029 DBG("process %d", getpid());
2032 pthread_mutex_unlock(&ust_fork_mutex
);
2034 /* Restore signals */
2035 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2037 PERROR("sigprocmask");
2041 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2043 if (URCU_TLS(lttng_ust_nest_count
))
2045 DBG("process %d", getpid());
2046 rcu_bp_after_fork_parent();
2047 /* Release mutexes and reenable signals */
2048 ust_after_fork_common(restore_sigset
);
2052 * After fork, in the child, we need to cleanup all the leftover state,
2053 * except the worker thread which already magically disappeared thanks
2054 * to the weird Linux fork semantics. After tyding up, we call
2055 * lttng_ust_init() again to start over as a new PID.
2057 * This is meant for forks() that have tracing in the child between the
2058 * fork and following exec call (if there is any).
2060 void ust_after_fork_child(sigset_t
*restore_sigset
)
2062 if (URCU_TLS(lttng_ust_nest_count
))
2064 lttng_context_vpid_reset();
2065 lttng_context_vtid_reset();
2066 lttng_context_procname_reset();
2067 DBG("process %d", getpid());
2068 /* Release urcu mutexes */
2069 rcu_bp_after_fork_child();
2070 lttng_ust_cleanup(0);
2071 /* Release mutexes and reenable signals */
2072 ust_after_fork_common(restore_sigset
);
2076 void lttng_ust_sockinfo_session_enabled(void *owner
)
2078 struct sock_info
*sock_info
= owner
;
2079 sock_info
->statedump_pending
= 1;