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>
39 #include <urcu/uatomic.h>
40 #include <urcu/futex.h>
41 #include <urcu/compiler.h>
43 #include <lttng/ust-events.h>
44 #include <lttng/ust-abi.h>
45 #include <lttng/ust.h>
46 #include <lttng/ust-error.h>
47 #include <lttng/ust-ctl.h>
48 #include <urcu/tls-compat.h>
51 #include <usterr-signal-safe.h>
53 #include "tracepoint-internal.h"
54 #include "lttng-tracer-core.h"
56 #include "../libringbuffer/rb-init.h"
57 #include "lttng-ust-statedump.h"
59 #include "../libringbuffer/getcpu.h"
63 * Has lttng ust comm constructor been called ?
65 static int initialized
;
68 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
69 * Held when handling a command, also held by fork() to deal with
70 * removal of threads, and by exit path.
72 * The UST lock is the centralized mutex across UST tracing control and
75 * ust_exit_mutex must never nest in ust_mutex.
77 * ust_fork_mutex must never nest in ust_mutex.
79 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
80 * counter lazy initialization called by events within the statedump,
81 * which traces while the ust_mutex is held.
83 * ust_lock nests within the dynamic loader lock (within glibc) because
84 * it is taken within the library constructor.
86 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
88 /* Allow nesting the ust_mutex within the same thread. */
89 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
92 * ust_exit_mutex protects thread_active variable wrt thread exit. It
93 * cannot be done by ust_mutex because pthread_cancel(), which takes an
94 * internal libc lock, cannot nest within ust_mutex.
96 * It never nests within a ust_mutex.
98 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
101 * ust_fork_mutex protects base address statedump tracing against forks. It
102 * prevents the dynamic loader lock to be taken (by base address statedump
103 * tracing) while a fork is happening, thus preventing deadlock issues with
104 * the dynamic loader lock.
106 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
108 /* Should the ust comm thread quit ? */
109 static int lttng_ust_comm_should_quit
;
112 * This variable can be tested by applications to check whether
113 * lttng-ust is loaded. They simply have to define their own
114 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
115 * library constructor.
117 int lttng_ust_loaded
__attribute__((weak
));
120 * Return 0 on success, -1 if should quit.
121 * The lock is taken in both cases.
126 sigset_t sig_all_blocked
, orig_mask
;
129 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
131 ERR("pthread_setcancelstate: %s", strerror(ret
));
133 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
134 ERR("pthread_setcancelstate: unexpected oldstate");
136 sigfillset(&sig_all_blocked
);
137 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
139 ERR("pthread_sigmask: %s", strerror(ret
));
141 if (!URCU_TLS(ust_mutex_nest
)++)
142 pthread_mutex_lock(&ust_mutex
);
143 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
145 ERR("pthread_sigmask: %s", strerror(ret
));
147 if (lttng_ust_comm_should_quit
) {
155 * ust_lock_nocheck() can be used in constructors/destructors, because
156 * they are already nested within the dynamic loader lock, and therefore
157 * have exclusive access against execution of liblttng-ust destructor.
160 void ust_lock_nocheck(void)
162 sigset_t sig_all_blocked
, orig_mask
;
165 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
167 ERR("pthread_setcancelstate: %s", strerror(ret
));
169 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
170 ERR("pthread_setcancelstate: unexpected oldstate");
172 sigfillset(&sig_all_blocked
);
173 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
175 ERR("pthread_sigmask: %s", strerror(ret
));
177 if (!URCU_TLS(ust_mutex_nest
)++)
178 pthread_mutex_lock(&ust_mutex
);
179 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
181 ERR("pthread_sigmask: %s", strerror(ret
));
188 void ust_unlock(void)
190 sigset_t sig_all_blocked
, orig_mask
;
193 sigfillset(&sig_all_blocked
);
194 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
196 ERR("pthread_sigmask: %s", strerror(ret
));
198 if (!--URCU_TLS(ust_mutex_nest
))
199 pthread_mutex_unlock(&ust_mutex
);
200 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
202 ERR("pthread_sigmask: %s", strerror(ret
));
204 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
206 ERR("pthread_setcancelstate: %s", strerror(ret
));
208 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
209 ERR("pthread_setcancelstate: unexpected oldstate");
214 * Wait for either of these before continuing to the main
216 * - the register_done message from sessiond daemon
217 * (will let the sessiond daemon enable sessions before main
219 * - sessiond daemon is not reachable.
220 * - timeout (ensuring applications are resilient to session
223 static sem_t constructor_wait
;
225 * Doing this for both the global and local sessiond.
228 sem_count_initial_value
= 4,
231 static int sem_count
= sem_count_initial_value
;
234 * Counting nesting within lttng-ust. Used to ensure that calling fork()
235 * from liblttng-ust does not execute the pre/post fork handlers.
237 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
240 * Info about socket and associated listener thread.
244 pthread_t ust_listener
; /* listener thread */
246 int registration_done
;
251 char sock_path
[PATH_MAX
];
255 char wait_shm_path
[PATH_MAX
];
257 /* Keep track of lazy state dump not performed yet. */
258 int statedump_pending
;
259 int initial_statedump_done
;
262 /* Socket from app (connect) to session daemon (listen) for communication */
263 struct sock_info global_apps
= {
268 .registration_done
= 0,
272 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
276 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
278 .statedump_pending
= 0,
279 .initial_statedump_done
= 0,
282 /* TODO: allow global_apps_sock_path override */
284 struct sock_info local_apps
= {
288 .registration_done
= 0,
289 .allowed
= 0, /* Check setuid bit first */
295 .statedump_pending
= 0,
296 .initial_statedump_done
= 0,
299 static int wait_poll_fallback
;
301 static const char *cmd_name_mapping
[] = {
302 [ LTTNG_UST_RELEASE
] = "Release",
303 [ LTTNG_UST_SESSION
] = "Create Session",
304 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
306 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
307 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
308 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
309 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
311 /* Session FD commands */
312 [ LTTNG_UST_CHANNEL
] = "Create Channel",
313 [ LTTNG_UST_SESSION_START
] = "Start Session",
314 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
316 /* Channel FD commands */
317 [ LTTNG_UST_STREAM
] = "Create Stream",
318 [ LTTNG_UST_EVENT
] = "Create Event",
320 /* Event and Channel FD commands */
321 [ LTTNG_UST_CONTEXT
] = "Create Context",
322 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
324 /* Event, Channel and Session commands */
325 [ LTTNG_UST_ENABLE
] = "Enable",
326 [ LTTNG_UST_DISABLE
] = "Disable",
328 /* Tracepoint list commands */
329 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
330 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
332 /* Event FD commands */
333 [ LTTNG_UST_FILTER
] = "Create Filter",
334 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
337 static const char *str_timeout
;
338 static int got_timeout_env
;
340 extern void lttng_ring_buffer_client_overwrite_init(void);
341 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
342 extern void lttng_ring_buffer_client_discard_init(void);
343 extern void lttng_ring_buffer_client_discard_rt_init(void);
344 extern void lttng_ring_buffer_metadata_client_init(void);
345 extern void lttng_ring_buffer_client_overwrite_exit(void);
346 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
347 extern void lttng_ring_buffer_client_discard_exit(void);
348 extern void lttng_ring_buffer_client_discard_rt_exit(void);
349 extern void lttng_ring_buffer_metadata_client_exit(void);
351 static char *get_map_shm(struct sock_info
*sock_info
);
353 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
356 size_t copied
= 0, to_copy
= len
;
359 ret
= read(fd
, buf
+ copied
, to_copy
);
364 } while ((ret
> 0 && to_copy
> 0)
365 || (ret
< 0 && errno
== EINTR
));
372 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
376 const char *get_lttng_home_dir(void)
380 val
= (const char *) lttng_getenv("LTTNG_HOME");
384 return (const char *) lttng_getenv("HOME");
388 * Force a read (imply TLS fixup for dlopen) of TLS variables.
391 void lttng_fixup_nest_count_tls(void)
393 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
397 void lttng_fixup_ust_mutex_nest_tls(void)
399 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
406 void lttng_fixup_urcu_bp_tls(void)
412 void lttng_ust_fixup_tls(void)
414 lttng_fixup_urcu_bp_tls();
415 lttng_fixup_ringbuffer_tls();
416 lttng_fixup_vtid_tls();
417 lttng_fixup_nest_count_tls();
418 lttng_fixup_procname_tls();
419 lttng_fixup_ust_mutex_nest_tls();
420 lttng_ust_fixup_fd_tracker_tls();
423 int lttng_get_notify_socket(void *owner
)
425 struct sock_info
*info
= owner
;
427 return info
->notify_socket
;
431 void print_cmd(int cmd
, int handle
)
433 const char *cmd_name
= "Unknown";
435 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
436 && cmd_name_mapping
[cmd
]) {
437 cmd_name
= cmd_name_mapping
[cmd
];
439 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
441 lttng_ust_obj_get_name(handle
), handle
);
445 int setup_global_apps(void)
448 assert(!global_apps
.wait_shm_mmap
);
450 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
451 if (!global_apps
.wait_shm_mmap
) {
452 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
453 global_apps
.allowed
= 0;
458 global_apps
.allowed
= 1;
463 int setup_local_apps(void)
466 const char *home_dir
;
469 assert(!local_apps
.wait_shm_mmap
);
473 * Disallow per-user tracing for setuid binaries.
475 if (uid
!= geteuid()) {
476 assert(local_apps
.allowed
== 0);
480 home_dir
= get_lttng_home_dir();
482 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
483 assert(local_apps
.allowed
== 0);
487 local_apps
.allowed
= 1;
488 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
490 LTTNG_DEFAULT_HOME_RUNDIR
,
491 LTTNG_UST_SOCK_FILENAME
);
492 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
493 LTTNG_UST_WAIT_FILENAME
,
496 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
497 if (!local_apps
.wait_shm_mmap
) {
498 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
499 local_apps
.allowed
= 0;
508 * Get socket timeout, in ms.
509 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
512 long get_timeout(void)
514 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
516 if (!got_timeout_env
) {
517 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
521 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
522 /* All negative values are considered as "-1". */
523 if (constructor_delay_ms
< -1)
524 constructor_delay_ms
= -1;
525 return constructor_delay_ms
;
528 /* Timeout for notify socket send and recv. */
530 long get_notify_sock_timeout(void)
532 return get_timeout();
535 /* Timeout for connecting to cmd and notify sockets. */
537 long get_connect_sock_timeout(void)
539 return get_timeout();
543 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
546 int get_constructor_timeout(struct timespec
*constructor_timeout
)
548 long constructor_delay_ms
;
551 constructor_delay_ms
= get_timeout();
553 switch (constructor_delay_ms
) {
554 case -1:/* fall-through */
556 return constructor_delay_ms
;
562 * If we are unable to find the current time, don't wait.
564 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
569 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
570 constructor_timeout
->tv_nsec
+=
571 (constructor_delay_ms
% 1000UL) * 1000000UL;
572 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
573 constructor_timeout
->tv_sec
++;
574 constructor_timeout
->tv_nsec
-= 1000000000UL;
576 /* Timeout wait (constructor_delay_ms). */
581 void get_allow_blocking(void)
583 const char *str_allow_blocking
=
584 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
586 if (str_allow_blocking
) {
587 DBG("%s environment variable is set",
588 "LTTNG_UST_ALLOW_BLOCKING");
589 lttng_ust_ringbuffer_set_allow_blocking();
594 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
596 return ustcomm_send_reg_msg(socket
,
599 lttng_alignof(uint8_t) * CHAR_BIT
,
600 lttng_alignof(uint16_t) * CHAR_BIT
,
601 lttng_alignof(uint32_t) * CHAR_BIT
,
602 lttng_alignof(uint64_t) * CHAR_BIT
,
603 lttng_alignof(unsigned long) * CHAR_BIT
);
607 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
611 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
614 DBG("message successfully sent");
617 if (len
== -ECONNRESET
) {
618 DBG("remote end closed connection");
623 DBG("incorrect message size: %zd", len
);
629 void decrement_sem_count(unsigned int count
)
633 assert(uatomic_read(&sem_count
) >= count
);
635 if (uatomic_read(&sem_count
) <= 0) {
639 ret
= uatomic_add_return(&sem_count
, -count
);
641 ret
= sem_post(&constructor_wait
);
647 int handle_register_done(struct sock_info
*sock_info
)
649 if (sock_info
->registration_done
)
651 sock_info
->registration_done
= 1;
653 decrement_sem_count(1);
659 int handle_register_failed(struct sock_info
*sock_info
)
661 if (sock_info
->registration_done
)
663 sock_info
->registration_done
= 1;
664 sock_info
->initial_statedump_done
= 1;
666 decrement_sem_count(2);
672 * Only execute pending statedump after the constructor semaphore has
673 * been posted by the current listener thread. This means statedump will
674 * only be performed after the "registration done" command is received
675 * from this thread's session daemon.
677 * This ensures we don't run into deadlock issues with the dynamic
678 * loader mutex, which is held while the constructor is called and
679 * waiting on the constructor semaphore. All operations requiring this
680 * dynamic loader lock need to be postponed using this mechanism.
682 * In a scenario with two session daemons connected to the application,
683 * it is possible that the first listener thread which receives the
684 * registration done command issues its statedump while the dynamic
685 * loader lock is still held by the application constructor waiting on
686 * the semaphore. It will however be allowed to proceed when the
687 * second session daemon sends the registration done command to the
688 * second listener thread. This situation therefore does not produce
692 void handle_pending_statedump(struct sock_info
*sock_info
)
694 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
695 sock_info
->statedump_pending
= 0;
696 pthread_mutex_lock(&ust_fork_mutex
);
697 lttng_handle_pending_statedump(sock_info
);
698 pthread_mutex_unlock(&ust_fork_mutex
);
700 if (!sock_info
->initial_statedump_done
) {
701 sock_info
->initial_statedump_done
= 1;
702 decrement_sem_count(1);
708 int handle_message(struct sock_info
*sock_info
,
709 int sock
, struct ustcomm_ust_msg
*lum
)
712 const struct lttng_ust_objd_ops
*ops
;
713 struct ustcomm_ust_reply lur
;
715 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
718 memset(&lur
, 0, sizeof(lur
));
721 ret
= -LTTNG_UST_ERR_EXITING
;
725 ops
= objd_ops(lum
->handle
);
732 case LTTNG_UST_REGISTER_DONE
:
733 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
734 ret
= handle_register_done(sock_info
);
738 case LTTNG_UST_RELEASE
:
739 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
742 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
744 case LTTNG_UST_FILTER
:
746 /* Receive filter data */
747 struct lttng_ust_filter_bytecode_node
*bytecode
;
749 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
750 ERR("Filter data size is too large: %u bytes",
751 lum
->u
.filter
.data_size
);
756 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
757 ERR("Filter reloc offset %u is not within data",
758 lum
->u
.filter
.reloc_offset
);
763 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
768 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
769 lum
->u
.filter
.data_size
);
771 case 0: /* orderly shutdown */
776 if (len
== lum
->u
.filter
.data_size
) {
777 DBG("filter data received");
779 } else if (len
< 0) {
780 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
781 if (len
== -ECONNRESET
) {
782 ERR("%s remote end closed connection", sock_info
->name
);
791 DBG("incorrect filter data message size: %zd", len
);
797 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
798 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
799 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
801 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
802 (unsigned long) bytecode
,
807 /* don't free bytecode if everything went fine. */
814 case LTTNG_UST_EXCLUSION
:
816 /* Receive exclusion names */
817 struct lttng_ust_excluder_node
*node
;
820 count
= lum
->u
.exclusion
.count
;
822 /* There are no names to read */
826 node
= zmalloc(sizeof(*node
) +
827 count
* LTTNG_UST_SYM_NAME_LEN
);
832 node
->excluder
.count
= count
;
833 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
834 count
* LTTNG_UST_SYM_NAME_LEN
);
836 case 0: /* orderly shutdown */
841 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
842 DBG("Exclusion data received");
844 } else if (len
< 0) {
845 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
846 if (len
== -ECONNRESET
) {
847 ERR("%s remote end closed connection", sock_info
->name
);
856 DBG("Incorrect exclusion data message size: %zd", len
);
863 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
864 (unsigned long) node
,
869 /* Don't free exclusion data if everything went fine. */
876 case LTTNG_UST_CHANNEL
:
881 len
= ustcomm_recv_channel_from_sessiond(sock
,
882 &chan_data
, lum
->u
.channel
.len
,
885 case 0: /* orderly shutdown */
889 if (len
== lum
->u
.channel
.len
) {
890 DBG("channel data received");
892 } else if (len
< 0) {
893 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
894 if (len
== -ECONNRESET
) {
895 ERR("%s remote end closed connection", sock_info
->name
);
902 DBG("incorrect channel data message size: %zd", len
);
907 args
.channel
.chan_data
= chan_data
;
908 args
.channel
.wakeup_fd
= wakeup_fd
;
910 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
911 (unsigned long) &lum
->u
,
917 case LTTNG_UST_STREAM
:
919 /* Receive shm_fd, wakeup_fd */
920 ret
= ustcomm_recv_stream_from_sessiond(sock
,
923 &args
.stream
.wakeup_fd
);
929 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
930 (unsigned long) &lum
->u
,
936 case LTTNG_UST_CONTEXT
:
937 switch (lum
->u
.context
.ctx
) {
938 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
941 size_t ctxlen
, recvlen
;
943 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
944 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
945 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
946 ERR("Application context string length size is too large: %zu bytes",
951 strcpy(ctxstr
, "$app.");
952 p
= &ctxstr
[strlen("$app.")];
953 recvlen
= ctxlen
- strlen("$app.");
954 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
956 case 0: /* orderly shutdown */
960 if (len
== recvlen
) {
961 DBG("app context data received");
963 } else if (len
< 0) {
964 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
965 if (len
== -ECONNRESET
) {
966 ERR("%s remote end closed connection", sock_info
->name
);
973 DBG("incorrect app context data message size: %zd", len
);
978 /* Put : between provider and ctxname. */
979 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
980 args
.app_context
.ctxname
= ctxstr
;
987 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
988 (unsigned long) &lum
->u
,
996 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
997 (unsigned long) &lum
->u
,
1004 lur
.handle
= lum
->handle
;
1008 lur
.ret_code
= LTTNG_UST_OK
;
1011 * Use -LTTNG_UST_ERR as wildcard for UST internal
1012 * error that are not caused by the transport, except if
1013 * we already have a more precise error message to
1016 if (ret
> -LTTNG_UST_ERR
) {
1017 /* Translate code to UST error. */
1020 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1023 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1026 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1029 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1032 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1035 lur
.ret_code
= -LTTNG_UST_ERR
;
1044 case LTTNG_UST_TRACER_VERSION
:
1045 lur
.u
.version
= lum
->u
.version
;
1047 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1048 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1052 DBG("Return value: %d", lur
.ret_val
);
1057 * Performed delayed statedump operations outside of the UST
1058 * lock. We need to take the dynamic loader lock before we take
1059 * the UST lock internally within handle_pending_statedump().
1061 handle_pending_statedump(sock_info
);
1064 ret
= -LTTNG_UST_ERR_EXITING
;
1068 ret
= send_reply(sock
, &lur
);
1070 DBG("error sending reply");
1075 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1078 if (lur
.ret_code
== LTTNG_UST_OK
) {
1080 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1081 len
= ustcomm_send_unix_sock(sock
,
1082 &args
.field_list
.entry
,
1083 sizeof(args
.field_list
.entry
));
1088 if (len
!= sizeof(args
.field_list
.entry
)) {
1102 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1106 if (sock_info
->root_handle
!= -1) {
1107 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1109 ERR("Error unref root handle");
1111 sock_info
->root_handle
= -1;
1113 sock_info
->registration_done
= 0;
1114 sock_info
->initial_statedump_done
= 0;
1117 * wait_shm_mmap, socket and notify socket are used by listener
1118 * threads outside of the ust lock, so we cannot tear them down
1119 * ourselves, because we cannot join on these threads. Leave
1120 * responsibility of cleaning up these resources to the OS
1126 if (sock_info
->socket
!= -1) {
1127 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1129 ERR("Error closing ust cmd socket");
1131 sock_info
->socket
= -1;
1133 if (sock_info
->notify_socket
!= -1) {
1134 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1136 ERR("Error closing ust notify socket");
1138 sock_info
->notify_socket
= -1;
1140 if (sock_info
->wait_shm_mmap
) {
1143 page_size
= sysconf(_SC_PAGE_SIZE
);
1144 if (page_size
<= 0) {
1148 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1150 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1152 ERR("Error unmapping wait shm");
1155 sock_info
->wait_shm_mmap
= NULL
;
1160 * Using fork to set umask in the child process (not multi-thread safe).
1161 * We deal with the shm_open vs ftruncate race (happening when the
1162 * sessiond owns the shm and does not let everybody modify it, to ensure
1163 * safety against shm_unlink) by simply letting the mmap fail and
1164 * retrying after a few seconds.
1165 * For global shm, everybody has rw access to it until the sessiond
1169 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1171 int wait_shm_fd
, ret
;
1175 * Try to open read-only.
1177 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1178 if (wait_shm_fd
>= 0) {
1181 size_t bytes_read
= 0;
1184 * Try to read the fd. If unable to do so, try opening
1188 len
= read(wait_shm_fd
,
1189 &((char *) &tmp_read
)[bytes_read
],
1190 sizeof(tmp_read
) - bytes_read
);
1194 } while ((len
< 0 && errno
== EINTR
)
1195 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1196 if (bytes_read
!= sizeof(tmp_read
)) {
1197 ret
= close(wait_shm_fd
);
1199 ERR("close wait_shm_fd");
1204 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1206 * Real-only open did not work, and it's not because the
1207 * entry was not present. It's a failure that prohibits
1210 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1216 * If the open failed because the file did not exist, or because
1217 * the file was not truncated yet, try creating it ourself.
1219 URCU_TLS(lttng_ust_nest_count
)++;
1221 URCU_TLS(lttng_ust_nest_count
)--;
1226 * Parent: wait for child to return, in which case the
1227 * shared memory map will have been created.
1229 pid
= wait(&status
);
1230 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1235 * Try to open read-only again after creation.
1237 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1238 if (wait_shm_fd
< 0) {
1240 * Real-only open did not work. It's a failure
1241 * that prohibits using shm.
1243 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1247 } else if (pid
== 0) {
1251 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1252 if (sock_info
->global
)
1253 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1255 * We're alone in a child process, so we can modify the
1256 * process-wide umask.
1258 umask(~create_mode
);
1260 * Try creating shm (or get rw access).
1261 * We don't do an exclusive open, because we allow other
1262 * processes to create+ftruncate it concurrently.
1264 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1265 O_RDWR
| O_CREAT
, create_mode
);
1266 if (wait_shm_fd
>= 0) {
1267 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1269 PERROR("ftruncate");
1270 _exit(EXIT_FAILURE
);
1272 _exit(EXIT_SUCCESS
);
1275 * For local shm, we need to have rw access to accept
1276 * opening it: this means the local sessiond will be
1277 * able to wake us up. For global shm, we open it even
1278 * if rw access is not granted, because the root.root
1279 * sessiond will be able to override all rights and wake
1282 if (!sock_info
->global
&& errno
!= EACCES
) {
1283 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1284 _exit(EXIT_FAILURE
);
1287 * The shm exists, but we cannot open it RW. Report
1290 _exit(EXIT_SUCCESS
);
1295 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1296 struct stat statbuf
;
1299 * Ensure that our user is the owner of the shm file for
1300 * local shm. If we do not own the file, it means our
1301 * sessiond will not have access to wake us up (there is
1302 * probably a rogue process trying to fake our
1303 * sessiond). Fallback to polling method in this case.
1305 ret
= fstat(wait_shm_fd
, &statbuf
);
1310 if (statbuf
.st_uid
!= getuid())
1316 ret
= close(wait_shm_fd
);
1318 PERROR("Error closing fd");
1324 char *get_map_shm(struct sock_info
*sock_info
)
1327 int wait_shm_fd
, ret
;
1328 char *wait_shm_mmap
;
1330 page_size
= sysconf(_SC_PAGE_SIZE
);
1331 if (page_size
<= 0) {
1335 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1339 lttng_ust_lock_fd_tracker();
1340 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1341 if (wait_shm_fd
< 0) {
1342 lttng_ust_unlock_fd_tracker();
1346 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1348 ret
= close(wait_shm_fd
);
1350 PERROR("Error closing fd");
1352 lttng_ust_unlock_fd_tracker();
1357 lttng_ust_unlock_fd_tracker();
1359 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1360 MAP_SHARED
, wait_shm_fd
, 0);
1362 /* close shm fd immediately after taking the mmap reference */
1363 lttng_ust_lock_fd_tracker();
1364 ret
= close(wait_shm_fd
);
1366 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1368 PERROR("Error closing fd");
1370 lttng_ust_unlock_fd_tracker();
1372 if (wait_shm_mmap
== MAP_FAILED
) {
1373 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1376 return wait_shm_mmap
;
1383 void wait_for_sessiond(struct sock_info
*sock_info
)
1385 /* Use ust_lock to check if we should quit. */
1389 if (wait_poll_fallback
) {
1394 assert(sock_info
->wait_shm_mmap
);
1396 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1397 /* Wait for futex wakeup */
1398 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1401 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1402 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1405 /* Value already changed. */
1408 /* Retry if interrupted by signal. */
1409 break; /* Get out of switch. */
1411 wait_poll_fallback
= 1;
1413 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1414 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1415 "Please upgrade your kernel "
1416 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1417 "mainline). LTTng-UST will use polling mode fallback.");
1436 * This thread does not allocate any resource, except within
1437 * handle_message, within mutex protection. This mutex protects against
1439 * The other moment it allocates resources is at socket connection, which
1440 * is also protected by the mutex.
1443 void *ust_listener_thread(void *arg
)
1445 struct sock_info
*sock_info
= arg
;
1446 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1449 lttng_ust_fixup_tls();
1451 * If available, add '-ust' to the end of this thread's
1454 ret
= lttng_ust_setustprocname();
1456 ERR("Unable to set UST process name");
1459 /* Restart trying to connect to the session daemon */
1461 if (prev_connect_failed
) {
1462 /* Wait for sessiond availability with pipe */
1463 wait_for_sessiond(sock_info
);
1467 * Sleep for 5 seconds before retrying after a
1468 * sequence of failure / wait / failure. This
1469 * deals with a killed or broken session daemon.
1475 prev_connect_failed
= 0;
1482 if (sock_info
->socket
!= -1) {
1483 /* FD tracker is updated by ustcomm_close_unix_sock() */
1484 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1486 ERR("Error closing %s ust cmd socket",
1489 sock_info
->socket
= -1;
1491 if (sock_info
->notify_socket
!= -1) {
1492 /* FD tracker is updated by ustcomm_close_unix_sock() */
1493 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1495 ERR("Error closing %s ust notify socket",
1498 sock_info
->notify_socket
= -1;
1503 * Register. We need to perform both connect and sending
1504 * registration message before doing the next connect otherwise
1505 * we may reach unix socket connect queue max limits and block
1506 * on the 2nd connect while the session daemon is awaiting the
1507 * first connect registration message.
1509 /* Connect cmd socket */
1510 lttng_ust_lock_fd_tracker();
1511 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1512 get_connect_sock_timeout());
1514 lttng_ust_unlock_fd_tracker();
1515 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1516 prev_connect_failed
= 1;
1519 * If we cannot find the sessiond daemon, don't delay
1520 * constructor execution.
1522 ret
= handle_register_failed(sock_info
);
1528 ret
= lttng_ust_add_fd_to_tracker(fd
);
1532 PERROR("close on sock_info->socket");
1535 lttng_ust_unlock_fd_tracker();
1540 sock_info
->socket
= ret
;
1541 lttng_ust_unlock_fd_tracker();
1545 * Unlock/relock ust lock because connect is blocking (with
1546 * timeout). Don't delay constructors on the ust lock for too
1554 * Create only one root handle per listener thread for the whole
1555 * process lifetime, so we ensure we get ID which is statically
1556 * assigned to the root handle.
1558 if (sock_info
->root_handle
== -1) {
1559 ret
= lttng_abi_create_root_handle();
1561 ERR("Error creating root handle");
1564 sock_info
->root_handle
= ret
;
1567 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1569 ERR("Error registering to %s ust cmd socket",
1571 prev_connect_failed
= 1;
1573 * If we cannot register to the sessiond daemon, don't
1574 * delay constructor execution.
1576 ret
= handle_register_failed(sock_info
);
1584 * Unlock/relock ust lock because connect is blocking (with
1585 * timeout). Don't delay constructors on the ust lock for too
1592 /* Connect notify socket */
1593 lttng_ust_lock_fd_tracker();
1594 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1595 get_connect_sock_timeout());
1597 lttng_ust_unlock_fd_tracker();
1598 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1599 prev_connect_failed
= 1;
1602 * If we cannot find the sessiond daemon, don't delay
1603 * constructor execution.
1605 ret
= handle_register_failed(sock_info
);
1612 ret
= lttng_ust_add_fd_to_tracker(fd
);
1616 PERROR("close on sock_info->notify_socket");
1619 lttng_ust_unlock_fd_tracker();
1624 sock_info
->notify_socket
= ret
;
1625 lttng_ust_unlock_fd_tracker();
1629 * Unlock/relock ust lock because connect is blocking (with
1630 * timeout). Don't delay constructors on the ust lock for too
1637 timeout
= get_notify_sock_timeout();
1640 * Give at least 10ms to sessiond to reply to
1645 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1648 WARN("Error setting socket receive timeout");
1650 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1653 WARN("Error setting socket send timeout");
1655 } else if (timeout
< -1) {
1656 WARN("Unsupported timeout value %ld", timeout
);
1659 ret
= register_to_sessiond(sock_info
->notify_socket
,
1660 USTCTL_SOCKET_NOTIFY
);
1662 ERR("Error registering to %s ust notify socket",
1664 prev_connect_failed
= 1;
1666 * If we cannot register to the sessiond daemon, don't
1667 * delay constructor execution.
1669 ret
= handle_register_failed(sock_info
);
1674 sock
= sock_info
->socket
;
1680 struct ustcomm_ust_msg lum
;
1682 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1684 case 0: /* orderly shutdown */
1685 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1690 * Either sessiond has shutdown or refused us by closing the socket.
1691 * In either case, we don't want to delay construction execution,
1692 * and we need to wait before retry.
1694 prev_connect_failed
= 1;
1696 * If we cannot register to the sessiond daemon, don't
1697 * delay constructor execution.
1699 ret
= handle_register_failed(sock_info
);
1704 print_cmd(lum
.cmd
, lum
.handle
);
1705 ret
= handle_message(sock_info
, sock
, &lum
);
1707 ERR("Error handling message for %s socket",
1710 * Close socket if protocol error is
1718 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1720 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1722 if (len
== -ECONNRESET
) {
1723 DBG("%s remote end closed connection", sock_info
->name
);
1734 /* Cleanup socket handles before trying to reconnect */
1735 lttng_ust_objd_table_owner_cleanup(sock_info
);
1737 goto restart
; /* try to reconnect */
1742 pthread_mutex_lock(&ust_exit_mutex
);
1743 sock_info
->thread_active
= 0;
1744 pthread_mutex_unlock(&ust_exit_mutex
);
1749 * Weak symbol to call when the ust malloc wrapper is not loaded.
1751 __attribute__((weak
))
1752 void lttng_ust_malloc_wrapper_init(void)
1757 * sessiond monitoring thread: monitor presence of global and per-user
1758 * sessiond by polling the application common named pipe.
1760 void __attribute__((constructor
)) lttng_ust_init(void)
1762 struct timespec constructor_timeout
;
1763 sigset_t sig_all_blocked
, orig_parent_mask
;
1764 pthread_attr_t thread_attr
;
1768 if (uatomic_xchg(&initialized
, 1) == 1)
1772 * Fixup interdependency between TLS fixup mutex (which happens
1773 * to be the dynamic linker mutex) and ust_lock, taken within
1776 lttng_ust_fixup_tls();
1778 lttng_ust_loaded
= 1;
1781 * We want precise control over the order in which we construct
1782 * our sub-libraries vs starting to receive commands from
1783 * sessiond (otherwise leading to errors when trying to create
1784 * sessiond before the init functions are completed).
1787 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1789 lttng_ust_init_fd_tracker();
1790 lttng_ust_clock_init();
1791 lttng_ust_getcpu_init();
1792 lttng_ust_statedump_init();
1793 lttng_ring_buffer_metadata_client_init();
1794 lttng_ring_buffer_client_overwrite_init();
1795 lttng_ring_buffer_client_overwrite_rt_init();
1796 lttng_ring_buffer_client_discard_init();
1797 lttng_ring_buffer_client_discard_rt_init();
1798 lttng_perf_counter_init();
1800 * Invoke ust malloc wrapper init before starting other threads.
1802 lttng_ust_malloc_wrapper_init();
1804 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1806 get_allow_blocking();
1808 ret
= sem_init(&constructor_wait
, 0, 0);
1813 ret
= setup_global_apps();
1815 assert(global_apps
.allowed
== 0);
1816 DBG("global apps setup returned %d", ret
);
1819 ret
= setup_local_apps();
1821 assert(local_apps
.allowed
== 0);
1822 DBG("local apps setup returned %d", ret
);
1825 /* A new thread created by pthread_create inherits the signal mask
1826 * from the parent. To avoid any signal being received by the
1827 * listener thread, we block all signals temporarily in the parent,
1828 * while we create the listener thread.
1830 sigfillset(&sig_all_blocked
);
1831 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1833 ERR("pthread_sigmask: %s", strerror(ret
));
1836 ret
= pthread_attr_init(&thread_attr
);
1838 ERR("pthread_attr_init: %s", strerror(ret
));
1840 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1842 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1845 if (global_apps
.allowed
) {
1846 pthread_mutex_lock(&ust_exit_mutex
);
1847 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1848 ust_listener_thread
, &global_apps
);
1850 ERR("pthread_create global: %s", strerror(ret
));
1852 global_apps
.thread_active
= 1;
1853 pthread_mutex_unlock(&ust_exit_mutex
);
1855 handle_register_done(&global_apps
);
1858 if (local_apps
.allowed
) {
1859 pthread_mutex_lock(&ust_exit_mutex
);
1860 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1861 ust_listener_thread
, &local_apps
);
1863 ERR("pthread_create local: %s", strerror(ret
));
1865 local_apps
.thread_active
= 1;
1866 pthread_mutex_unlock(&ust_exit_mutex
);
1868 handle_register_done(&local_apps
);
1870 ret
= pthread_attr_destroy(&thread_attr
);
1872 ERR("pthread_attr_destroy: %s", strerror(ret
));
1875 /* Restore original signal mask in parent */
1876 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1878 ERR("pthread_sigmask: %s", strerror(ret
));
1881 switch (timeout_mode
) {
1882 case 1: /* timeout wait */
1884 ret
= sem_timedwait(&constructor_wait
,
1885 &constructor_timeout
);
1886 } while (ret
< 0 && errno
== EINTR
);
1890 ERR("Timed out waiting for lttng-sessiond");
1893 PERROR("sem_timedwait");
1896 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1901 case -1:/* wait forever */
1903 ret
= sem_wait(&constructor_wait
);
1904 } while (ret
< 0 && errno
== EINTR
);
1911 ERR("Unexpected error \"%s\" returned by sem_wait",
1916 case 0: /* no timeout */
1922 void lttng_ust_cleanup(int exiting
)
1924 cleanup_sock_info(&global_apps
, exiting
);
1925 cleanup_sock_info(&local_apps
, exiting
);
1926 local_apps
.allowed
= 0;
1927 global_apps
.allowed
= 0;
1929 * The teardown in this function all affect data structures
1930 * accessed under the UST lock by the listener thread. This
1931 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1932 * that none of these threads are accessing this data at this
1935 lttng_ust_abi_exit();
1936 lttng_ust_events_exit();
1937 lttng_perf_counter_exit();
1938 lttng_ring_buffer_client_discard_rt_exit();
1939 lttng_ring_buffer_client_discard_exit();
1940 lttng_ring_buffer_client_overwrite_rt_exit();
1941 lttng_ring_buffer_client_overwrite_exit();
1942 lttng_ring_buffer_metadata_client_exit();
1943 lttng_ust_statedump_destroy();
1946 /* Reinitialize values for fork */
1947 sem_count
= sem_count_initial_value
;
1948 lttng_ust_comm_should_quit
= 0;
1953 void __attribute__((destructor
)) lttng_ust_exit(void)
1958 * Using pthread_cancel here because:
1959 * A) we don't want to hang application teardown.
1960 * B) the thread is not allocating any resource.
1964 * Require the communication thread to quit. Synchronize with
1965 * mutexes to ensure it is not in a mutex critical section when
1966 * pthread_cancel is later called.
1969 lttng_ust_comm_should_quit
= 1;
1972 pthread_mutex_lock(&ust_exit_mutex
);
1973 /* cancel threads */
1974 if (global_apps
.thread_active
) {
1975 ret
= pthread_cancel(global_apps
.ust_listener
);
1977 ERR("Error cancelling global ust listener thread: %s",
1980 global_apps
.thread_active
= 0;
1983 if (local_apps
.thread_active
) {
1984 ret
= pthread_cancel(local_apps
.ust_listener
);
1986 ERR("Error cancelling local ust listener thread: %s",
1989 local_apps
.thread_active
= 0;
1992 pthread_mutex_unlock(&ust_exit_mutex
);
1995 * Do NOT join threads: use of sys_futex makes it impossible to
1996 * join the threads without using async-cancel, but async-cancel
1997 * is delivered by a signal, which could hit the target thread
1998 * anywhere in its code path, including while the ust_lock() is
1999 * held, causing a deadlock for the other thread. Let the OS
2000 * cleanup the threads if there are stalled in a syscall.
2002 lttng_ust_cleanup(1);
2006 * We exclude the worker threads across fork and clone (except
2007 * CLONE_VM), because these system calls only keep the forking thread
2008 * running in the child. Therefore, we don't want to call fork or clone
2009 * in the middle of an tracepoint or ust tracing state modification.
2010 * Holding this mutex protects these structures across fork and clone.
2012 void ust_before_fork(sigset_t
*save_sigset
)
2015 * Disable signals. This is to avoid that the child intervenes
2016 * before it is properly setup for tracing. It is safer to
2017 * disable all signals, because then we know we are not breaking
2018 * anything by restoring the original mask.
2023 /* Fixup lttng-ust TLS. */
2024 lttng_ust_fixup_tls();
2026 if (URCU_TLS(lttng_ust_nest_count
))
2028 /* Disable signals */
2029 sigfillset(&all_sigs
);
2030 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2032 PERROR("sigprocmask");
2035 pthread_mutex_lock(&ust_fork_mutex
);
2038 rcu_bp_before_fork();
2041 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2045 DBG("process %d", getpid());
2048 pthread_mutex_unlock(&ust_fork_mutex
);
2050 /* Restore signals */
2051 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2053 PERROR("sigprocmask");
2057 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2059 if (URCU_TLS(lttng_ust_nest_count
))
2061 DBG("process %d", getpid());
2062 rcu_bp_after_fork_parent();
2063 /* Release mutexes and reenable signals */
2064 ust_after_fork_common(restore_sigset
);
2068 * After fork, in the child, we need to cleanup all the leftover state,
2069 * except the worker thread which already magically disappeared thanks
2070 * to the weird Linux fork semantics. After tyding up, we call
2071 * lttng_ust_init() again to start over as a new PID.
2073 * This is meant for forks() that have tracing in the child between the
2074 * fork and following exec call (if there is any).
2076 void ust_after_fork_child(sigset_t
*restore_sigset
)
2078 if (URCU_TLS(lttng_ust_nest_count
))
2080 lttng_context_vpid_reset();
2081 lttng_context_vtid_reset();
2082 lttng_context_procname_reset();
2083 DBG("process %d", getpid());
2084 /* Release urcu mutexes */
2085 rcu_bp_after_fork_child();
2086 lttng_ust_cleanup(0);
2087 /* Release mutexes and reenable signals */
2088 ust_after_fork_common(restore_sigset
);
2092 void lttng_ust_sockinfo_session_enabled(void *owner
)
2094 struct sock_info
*sock_info
= owner
;
2095 sock_info
->statedump_pending
= 1;