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 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
92 /* Allow nesting the ust_mutex within the same thread. */
93 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
96 * ust_exit_mutex protects thread_active variable wrt thread exit. It
97 * cannot be done by ust_mutex because pthread_cancel(), which takes an
98 * internal libc lock, cannot nest within ust_mutex.
100 * It never nests within a ust_mutex.
102 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 * ust_fork_mutex protects base address statedump tracing against forks. It
106 * prevents the dynamic loader lock to be taken (by base address statedump
107 * tracing) while a fork is happening, thus preventing deadlock issues with
108 * the dynamic loader lock.
110 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 /* Should the ust comm thread quit ? */
113 static int lttng_ust_comm_should_quit
;
116 * This variable can be tested by applications to check whether
117 * lttng-ust is loaded. They simply have to define their own
118 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
119 * library constructor.
121 int lttng_ust_loaded
__attribute__((weak
));
124 * Return 0 on success, -1 if should quit.
125 * The lock is taken in both cases.
130 sigset_t sig_all_blocked
, orig_mask
;
133 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
135 ERR("pthread_setcancelstate: %s", strerror(ret
));
137 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
138 ERR("pthread_setcancelstate: unexpected oldstate");
140 sigfillset(&sig_all_blocked
);
141 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
143 ERR("pthread_sigmask: %s", strerror(ret
));
145 if (!URCU_TLS(ust_mutex_nest
)++)
146 pthread_mutex_lock(&ust_mutex
);
147 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
149 ERR("pthread_sigmask: %s", strerror(ret
));
151 if (lttng_ust_comm_should_quit
) {
159 * ust_lock_nocheck() can be used in constructors/destructors, because
160 * they are already nested within the dynamic loader lock, and therefore
161 * have exclusive access against execution of liblttng-ust destructor.
164 void ust_lock_nocheck(void)
166 sigset_t sig_all_blocked
, orig_mask
;
169 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
171 ERR("pthread_setcancelstate: %s", strerror(ret
));
173 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
174 ERR("pthread_setcancelstate: unexpected oldstate");
176 sigfillset(&sig_all_blocked
);
177 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
179 ERR("pthread_sigmask: %s", strerror(ret
));
181 if (!URCU_TLS(ust_mutex_nest
)++)
182 pthread_mutex_lock(&ust_mutex
);
183 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
185 ERR("pthread_sigmask: %s", strerror(ret
));
192 void ust_unlock(void)
194 sigset_t sig_all_blocked
, orig_mask
;
197 sigfillset(&sig_all_blocked
);
198 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
200 ERR("pthread_sigmask: %s", strerror(ret
));
202 if (!--URCU_TLS(ust_mutex_nest
))
203 pthread_mutex_unlock(&ust_mutex
);
204 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
206 ERR("pthread_sigmask: %s", strerror(ret
));
208 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
210 ERR("pthread_setcancelstate: %s", strerror(ret
));
212 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
213 ERR("pthread_setcancelstate: unexpected oldstate");
218 * Wait for either of these before continuing to the main
220 * - the register_done message from sessiond daemon
221 * (will let the sessiond daemon enable sessions before main
223 * - sessiond daemon is not reachable.
224 * - timeout (ensuring applications are resilient to session
227 static sem_t constructor_wait
;
229 * Doing this for both the global and local sessiond.
231 static int sem_count
= { 2 };
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 constructor_sem_posted
;
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
;
261 /* Socket from app (connect) to session daemon (listen) for communication */
262 struct sock_info global_apps
= {
270 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
274 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
276 .statedump_pending
= 0,
279 /* TODO: allow global_apps_sock_path override */
281 struct sock_info local_apps
= {
285 .allowed
= 0, /* Check setuid bit first */
291 .statedump_pending
= 0,
294 static int wait_poll_fallback
;
296 static const char *cmd_name_mapping
[] = {
297 [ LTTNG_UST_RELEASE
] = "Release",
298 [ LTTNG_UST_SESSION
] = "Create Session",
299 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
301 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
302 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
303 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
304 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
306 /* Session FD commands */
307 [ LTTNG_UST_CHANNEL
] = "Create Channel",
308 [ LTTNG_UST_SESSION_START
] = "Start Session",
309 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
311 /* Channel FD commands */
312 [ LTTNG_UST_STREAM
] = "Create Stream",
313 [ LTTNG_UST_EVENT
] = "Create Event",
315 /* Event and Channel FD commands */
316 [ LTTNG_UST_CONTEXT
] = "Create Context",
317 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
319 /* Event, Channel and Session commands */
320 [ LTTNG_UST_ENABLE
] = "Enable",
321 [ LTTNG_UST_DISABLE
] = "Disable",
323 /* Tracepoint list commands */
324 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
325 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
327 /* Event FD commands */
328 [ LTTNG_UST_FILTER
] = "Create Filter",
329 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
332 static const char *str_timeout
;
333 static int got_timeout_env
;
335 extern void lttng_ring_buffer_client_overwrite_init(void);
336 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
337 extern void lttng_ring_buffer_client_discard_init(void);
338 extern void lttng_ring_buffer_client_discard_rt_init(void);
339 extern void lttng_ring_buffer_metadata_client_init(void);
340 extern void lttng_ring_buffer_client_overwrite_exit(void);
341 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
342 extern void lttng_ring_buffer_client_discard_exit(void);
343 extern void lttng_ring_buffer_client_discard_rt_exit(void);
344 extern void lttng_ring_buffer_metadata_client_exit(void);
346 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
349 size_t copied
= 0, to_copy
= len
;
352 ret
= read(fd
, buf
+ copied
, to_copy
);
357 } while ((ret
> 0 && to_copy
> 0)
358 || (ret
< 0 && errno
== EINTR
));
365 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
369 const char *get_lttng_home_dir(void)
373 val
= (const char *) lttng_getenv("LTTNG_HOME");
377 return (const char *) lttng_getenv("HOME");
381 * Force a read (imply TLS fixup for dlopen) of TLS variables.
384 void lttng_fixup_nest_count_tls(void)
386 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
390 void lttng_fixup_ust_mutex_nest_tls(void)
392 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
399 void lttng_fixup_urcu_bp_tls(void)
405 void lttng_ust_fixup_tls(void)
407 lttng_fixup_urcu_bp_tls();
408 lttng_fixup_ringbuffer_tls();
409 lttng_fixup_vtid_tls();
410 lttng_fixup_nest_count_tls();
411 lttng_fixup_procname_tls();
412 lttng_fixup_ust_mutex_nest_tls();
413 lttng_ust_fixup_fd_tracker_tls();
416 int lttng_get_notify_socket(void *owner
)
418 struct sock_info
*info
= owner
;
420 return info
->notify_socket
;
424 void print_cmd(int cmd
, int handle
)
426 const char *cmd_name
= "Unknown";
428 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
429 && cmd_name_mapping
[cmd
]) {
430 cmd_name
= cmd_name_mapping
[cmd
];
432 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
434 lttng_ust_obj_get_name(handle
), handle
);
438 int setup_local_apps(void)
440 const char *home_dir
;
445 * Disallow per-user tracing for setuid binaries.
447 if (uid
!= geteuid()) {
448 assert(local_apps
.allowed
== 0);
451 home_dir
= get_lttng_home_dir();
453 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
454 assert(local_apps
.allowed
== 0);
457 local_apps
.allowed
= 1;
458 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
460 LTTNG_DEFAULT_HOME_RUNDIR
,
461 LTTNG_UST_SOCK_FILENAME
);
462 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
463 LTTNG_UST_WAIT_FILENAME
,
469 * Get socket timeout, in ms.
470 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
473 long get_timeout(void)
475 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
477 if (!got_timeout_env
) {
478 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
482 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
483 /* All negative values are considered as "-1". */
484 if (constructor_delay_ms
< -1)
485 constructor_delay_ms
= -1;
486 return constructor_delay_ms
;
489 /* Timeout for notify socket send and recv. */
491 long get_notify_sock_timeout(void)
493 return get_timeout();
496 /* Timeout for connecting to cmd and notify sockets. */
498 long get_connect_sock_timeout(void)
500 return get_timeout();
504 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
507 int get_constructor_timeout(struct timespec
*constructor_timeout
)
509 long constructor_delay_ms
;
512 constructor_delay_ms
= get_timeout();
514 switch (constructor_delay_ms
) {
515 case -1:/* fall-through */
517 return constructor_delay_ms
;
523 * If we are unable to find the current time, don't wait.
525 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
530 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
531 constructor_timeout
->tv_nsec
+=
532 (constructor_delay_ms
% 1000UL) * 1000000UL;
533 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
534 constructor_timeout
->tv_sec
++;
535 constructor_timeout
->tv_nsec
-= 1000000000UL;
537 /* Timeout wait (constructor_delay_ms). */
542 void get_allow_blocking(void)
544 const char *str_allow_blocking
=
545 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
547 if (str_allow_blocking
) {
548 DBG("%s environment variable is set",
549 "LTTNG_UST_ALLOW_BLOCKING");
550 lttng_ust_ringbuffer_set_allow_blocking();
555 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
557 return ustcomm_send_reg_msg(socket
,
560 lttng_alignof(uint8_t) * CHAR_BIT
,
561 lttng_alignof(uint16_t) * CHAR_BIT
,
562 lttng_alignof(uint32_t) * CHAR_BIT
,
563 lttng_alignof(uint64_t) * CHAR_BIT
,
564 lttng_alignof(unsigned long) * CHAR_BIT
);
568 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
572 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
575 DBG("message successfully sent");
578 if (len
== -ECONNRESET
) {
579 DBG("remote end closed connection");
584 DBG("incorrect message size: %zd", len
);
590 int handle_register_done(struct sock_info
*sock_info
)
594 if (sock_info
->constructor_sem_posted
)
596 sock_info
->constructor_sem_posted
= 1;
597 if (uatomic_read(&sem_count
) <= 0) {
600 ret
= uatomic_add_return(&sem_count
, -1);
602 ret
= sem_post(&constructor_wait
);
609 * Only execute pending statedump after the constructor semaphore has
610 * been posted by each listener thread. This means statedump will only
611 * be performed after the "registration done" command is received from
612 * each session daemon the application is connected to.
614 * This ensures we don't run into deadlock issues with the dynamic
615 * loader mutex, which is held while the constructor is called and
616 * waiting on the constructor semaphore. All operations requiring this
617 * dynamic loader lock need to be postponed using this mechanism.
620 void handle_pending_statedump(struct sock_info
*sock_info
)
622 int ctor_passed
= sock_info
->constructor_sem_posted
;
624 if (ctor_passed
&& sock_info
->statedump_pending
) {
625 sock_info
->statedump_pending
= 0;
626 pthread_mutex_lock(&ust_fork_mutex
);
627 lttng_handle_pending_statedump(sock_info
);
628 pthread_mutex_unlock(&ust_fork_mutex
);
633 int handle_message(struct sock_info
*sock_info
,
634 int sock
, struct ustcomm_ust_msg
*lum
)
637 const struct lttng_ust_objd_ops
*ops
;
638 struct ustcomm_ust_reply lur
;
640 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
643 memset(&lur
, 0, sizeof(lur
));
646 ret
= -LTTNG_UST_ERR_EXITING
;
650 ops
= objd_ops(lum
->handle
);
657 case LTTNG_UST_REGISTER_DONE
:
658 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
659 ret
= handle_register_done(sock_info
);
663 case LTTNG_UST_RELEASE
:
664 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
667 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
669 case LTTNG_UST_FILTER
:
671 /* Receive filter data */
672 struct lttng_ust_filter_bytecode_node
*bytecode
;
674 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
675 ERR("Filter data size is too large: %u bytes",
676 lum
->u
.filter
.data_size
);
681 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
682 ERR("Filter reloc offset %u is not within data",
683 lum
->u
.filter
.reloc_offset
);
688 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
693 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
694 lum
->u
.filter
.data_size
);
696 case 0: /* orderly shutdown */
701 if (len
== lum
->u
.filter
.data_size
) {
702 DBG("filter data received");
704 } else if (len
< 0) {
705 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
706 if (len
== -ECONNRESET
) {
707 ERR("%s remote end closed connection", sock_info
->name
);
716 DBG("incorrect filter data message size: %zd", len
);
722 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
723 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
724 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
726 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
727 (unsigned long) bytecode
,
732 /* don't free bytecode if everything went fine. */
739 case LTTNG_UST_EXCLUSION
:
741 /* Receive exclusion names */
742 struct lttng_ust_excluder_node
*node
;
745 count
= lum
->u
.exclusion
.count
;
747 /* There are no names to read */
751 node
= zmalloc(sizeof(*node
) +
752 count
* LTTNG_UST_SYM_NAME_LEN
);
757 node
->excluder
.count
= count
;
758 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
759 count
* LTTNG_UST_SYM_NAME_LEN
);
761 case 0: /* orderly shutdown */
766 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
767 DBG("Exclusion data received");
769 } else if (len
< 0) {
770 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
771 if (len
== -ECONNRESET
) {
772 ERR("%s remote end closed connection", sock_info
->name
);
781 DBG("Incorrect exclusion data message size: %zd", len
);
788 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
789 (unsigned long) node
,
794 /* Don't free exclusion data if everything went fine. */
801 case LTTNG_UST_CHANNEL
:
806 len
= ustcomm_recv_channel_from_sessiond(sock
,
807 &chan_data
, lum
->u
.channel
.len
,
810 case 0: /* orderly shutdown */
814 if (len
== lum
->u
.channel
.len
) {
815 DBG("channel data received");
817 } else if (len
< 0) {
818 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
819 if (len
== -ECONNRESET
) {
820 ERR("%s remote end closed connection", sock_info
->name
);
827 DBG("incorrect channel data message size: %zd", len
);
832 args
.channel
.chan_data
= chan_data
;
833 args
.channel
.wakeup_fd
= wakeup_fd
;
835 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
836 (unsigned long) &lum
->u
,
842 case LTTNG_UST_STREAM
:
844 /* Receive shm_fd, wakeup_fd */
845 ret
= ustcomm_recv_stream_from_sessiond(sock
,
848 &args
.stream
.wakeup_fd
);
853 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
854 (unsigned long) &lum
->u
,
860 case LTTNG_UST_CONTEXT
:
861 switch (lum
->u
.context
.ctx
) {
862 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
865 size_t ctxlen
, recvlen
;
867 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
868 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
869 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
870 ERR("Application context string length size is too large: %zu bytes",
875 strcpy(ctxstr
, "$app.");
876 p
= &ctxstr
[strlen("$app.")];
877 recvlen
= ctxlen
- strlen("$app.");
878 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
880 case 0: /* orderly shutdown */
884 if (len
== recvlen
) {
885 DBG("app context data received");
887 } else if (len
< 0) {
888 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
889 if (len
== -ECONNRESET
) {
890 ERR("%s remote end closed connection", sock_info
->name
);
897 DBG("incorrect app context data message size: %zd", len
);
902 /* Put : between provider and ctxname. */
903 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
904 args
.app_context
.ctxname
= ctxstr
;
911 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
912 (unsigned long) &lum
->u
,
920 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
921 (unsigned long) &lum
->u
,
928 lur
.handle
= lum
->handle
;
932 lur
.ret_code
= LTTNG_UST_OK
;
935 * Use -LTTNG_UST_ERR as wildcard for UST internal
936 * error that are not caused by the transport, except if
937 * we already have a more precise error message to
940 if (ret
> -LTTNG_UST_ERR
) {
941 /* Translate code to UST error. */
944 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
947 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
950 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
953 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
956 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
959 lur
.ret_code
= -LTTNG_UST_ERR
;
968 case LTTNG_UST_TRACER_VERSION
:
969 lur
.u
.version
= lum
->u
.version
;
971 case LTTNG_UST_TRACEPOINT_LIST_GET
:
972 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
976 DBG("Return value: %d", lur
.ret_val
);
981 * Performed delayed statedump operations outside of the UST
982 * lock. We need to take the dynamic loader lock before we take
983 * the UST lock internally within handle_pending_statedump().
985 handle_pending_statedump(sock_info
);
988 ret
= -LTTNG_UST_ERR_EXITING
;
992 ret
= send_reply(sock
, &lur
);
994 DBG("error sending reply");
999 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1002 if (lur
.ret_code
== LTTNG_UST_OK
) {
1004 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1005 len
= ustcomm_send_unix_sock(sock
,
1006 &args
.field_list
.entry
,
1007 sizeof(args
.field_list
.entry
));
1012 if (len
!= sizeof(args
.field_list
.entry
)) {
1026 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1030 if (sock_info
->root_handle
!= -1) {
1031 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1033 ERR("Error unref root handle");
1035 sock_info
->root_handle
= -1;
1037 sock_info
->constructor_sem_posted
= 0;
1040 * wait_shm_mmap, socket and notify socket are used by listener
1041 * threads outside of the ust lock, so we cannot tear them down
1042 * ourselves, because we cannot join on these threads. Leave
1043 * responsibility of cleaning up these resources to the OS
1049 if (sock_info
->socket
!= -1) {
1050 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1052 ERR("Error closing ust cmd socket");
1054 sock_info
->socket
= -1;
1056 if (sock_info
->notify_socket
!= -1) {
1057 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1059 ERR("Error closing ust notify socket");
1061 sock_info
->notify_socket
= -1;
1063 if (sock_info
->wait_shm_mmap
) {
1066 page_size
= sysconf(_SC_PAGE_SIZE
);
1067 if (page_size
<= 0) {
1071 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1073 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1075 ERR("Error unmapping wait shm");
1078 sock_info
->wait_shm_mmap
= NULL
;
1083 * Using fork to set umask in the child process (not multi-thread safe).
1084 * We deal with the shm_open vs ftruncate race (happening when the
1085 * sessiond owns the shm and does not let everybody modify it, to ensure
1086 * safety against shm_unlink) by simply letting the mmap fail and
1087 * retrying after a few seconds.
1088 * For global shm, everybody has rw access to it until the sessiond
1092 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1094 int wait_shm_fd
, ret
;
1098 * Try to open read-only.
1100 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1101 if (wait_shm_fd
>= 0) {
1104 size_t bytes_read
= 0;
1107 * Try to read the fd. If unable to do so, try opening
1111 len
= read(wait_shm_fd
,
1112 &((char *) &tmp_read
)[bytes_read
],
1113 sizeof(tmp_read
) - bytes_read
);
1117 } while ((len
< 0 && errno
== EINTR
)
1118 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1119 if (bytes_read
!= sizeof(tmp_read
)) {
1120 ret
= close(wait_shm_fd
);
1122 ERR("close wait_shm_fd");
1127 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1129 * Real-only open did not work, and it's not because the
1130 * entry was not present. It's a failure that prohibits
1133 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1139 * If the open failed because the file did not exist, or because
1140 * the file was not truncated yet, try creating it ourself.
1142 URCU_TLS(lttng_ust_nest_count
)++;
1144 URCU_TLS(lttng_ust_nest_count
)--;
1149 * Parent: wait for child to return, in which case the
1150 * shared memory map will have been created.
1152 pid
= wait(&status
);
1153 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1158 * Try to open read-only again after creation.
1160 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1161 if (wait_shm_fd
< 0) {
1163 * Real-only open did not work. It's a failure
1164 * that prohibits using shm.
1166 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1170 } else if (pid
== 0) {
1174 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1175 if (sock_info
->global
)
1176 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1178 * We're alone in a child process, so we can modify the
1179 * process-wide umask.
1181 umask(~create_mode
);
1183 * Try creating shm (or get rw access).
1184 * We don't do an exclusive open, because we allow other
1185 * processes to create+ftruncate it concurrently.
1187 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1188 O_RDWR
| O_CREAT
, create_mode
);
1189 if (wait_shm_fd
>= 0) {
1190 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1192 PERROR("ftruncate");
1193 _exit(EXIT_FAILURE
);
1195 _exit(EXIT_SUCCESS
);
1198 * For local shm, we need to have rw access to accept
1199 * opening it: this means the local sessiond will be
1200 * able to wake us up. For global shm, we open it even
1201 * if rw access is not granted, because the root.root
1202 * sessiond will be able to override all rights and wake
1205 if (!sock_info
->global
&& errno
!= EACCES
) {
1206 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1207 _exit(EXIT_FAILURE
);
1210 * The shm exists, but we cannot open it RW. Report
1213 _exit(EXIT_SUCCESS
);
1218 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1219 struct stat statbuf
;
1222 * Ensure that our user is the owner of the shm file for
1223 * local shm. If we do not own the file, it means our
1224 * sessiond will not have access to wake us up (there is
1225 * probably a rogue process trying to fake our
1226 * sessiond). Fallback to polling method in this case.
1228 ret
= fstat(wait_shm_fd
, &statbuf
);
1233 if (statbuf
.st_uid
!= getuid())
1239 ret
= close(wait_shm_fd
);
1241 PERROR("Error closing fd");
1247 char *get_map_shm(struct sock_info
*sock_info
)
1250 int wait_shm_fd
, ret
;
1251 char *wait_shm_mmap
;
1253 page_size
= sysconf(_SC_PAGE_SIZE
);
1254 if (page_size
<= 0) {
1258 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1262 lttng_ust_lock_fd_tracker();
1263 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1264 if (wait_shm_fd
< 0) {
1265 lttng_ust_unlock_fd_tracker();
1269 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1271 ret
= close(wait_shm_fd
);
1273 PERROR("Error closing fd");
1275 lttng_ust_unlock_fd_tracker();
1280 lttng_ust_unlock_fd_tracker();
1282 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1283 MAP_SHARED
, wait_shm_fd
, 0);
1285 /* close shm fd immediately after taking the mmap reference */
1286 lttng_ust_lock_fd_tracker();
1287 ret
= close(wait_shm_fd
);
1289 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1291 PERROR("Error closing fd");
1293 lttng_ust_unlock_fd_tracker();
1295 if (wait_shm_mmap
== MAP_FAILED
) {
1296 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1299 return wait_shm_mmap
;
1306 void wait_for_sessiond(struct sock_info
*sock_info
)
1311 if (wait_poll_fallback
) {
1314 if (!sock_info
->wait_shm_mmap
) {
1315 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
1316 if (!sock_info
->wait_shm_mmap
)
1321 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1322 /* Wait for futex wakeup */
1323 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1326 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1327 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1330 /* Value already changed. */
1333 /* Retry if interrupted by signal. */
1334 break; /* Get out of switch. */
1336 wait_poll_fallback
= 1;
1338 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1339 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1340 "Please upgrade your kernel "
1341 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1342 "mainline). LTTng-UST will use polling mode fallback.");
1361 * This thread does not allocate any resource, except within
1362 * handle_message, within mutex protection. This mutex protects against
1364 * The other moment it allocates resources is at socket connection, which
1365 * is also protected by the mutex.
1368 void *ust_listener_thread(void *arg
)
1370 struct sock_info
*sock_info
= arg
;
1371 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1374 lttng_ust_fixup_tls();
1376 * If available, add '-ust' to the end of this thread's
1379 ret
= lttng_ust_setustprocname();
1381 ERR("Unable to set UST process name");
1384 /* Restart trying to connect to the session daemon */
1386 if (prev_connect_failed
) {
1387 /* Wait for sessiond availability with pipe */
1388 wait_for_sessiond(sock_info
);
1392 * Sleep for 5 seconds before retrying after a
1393 * sequence of failure / wait / failure. This
1394 * deals with a killed or broken session daemon.
1400 prev_connect_failed
= 0;
1407 if (sock_info
->socket
!= -1) {
1408 /* FD tracker is updated by ustcomm_close_unix_sock() */
1409 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1411 ERR("Error closing %s ust cmd socket",
1414 sock_info
->socket
= -1;
1416 if (sock_info
->notify_socket
!= -1) {
1417 /* FD tracker is updated by ustcomm_close_unix_sock() */
1418 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1420 ERR("Error closing %s ust notify socket",
1423 sock_info
->notify_socket
= -1;
1428 * Register. We need to perform both connect and sending
1429 * registration message before doing the next connect otherwise
1430 * we may reach unix socket connect queue max limits and block
1431 * on the 2nd connect while the session daemon is awaiting the
1432 * first connect registration message.
1434 /* Connect cmd socket */
1435 lttng_ust_lock_fd_tracker();
1436 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1437 get_connect_sock_timeout());
1439 lttng_ust_unlock_fd_tracker();
1440 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1441 prev_connect_failed
= 1;
1444 * If we cannot find the sessiond daemon, don't delay
1445 * constructor execution.
1447 ret
= handle_register_done(sock_info
);
1453 ret
= lttng_ust_add_fd_to_tracker(fd
);
1457 PERROR("close on sock_info->socket");
1460 lttng_ust_unlock_fd_tracker();
1465 sock_info
->socket
= ret
;
1466 lttng_ust_unlock_fd_tracker();
1470 * Unlock/relock ust lock because connect is blocking (with
1471 * timeout). Don't delay constructors on the ust lock for too
1479 * Create only one root handle per listener thread for the whole
1480 * process lifetime, so we ensure we get ID which is statically
1481 * assigned to the root handle.
1483 if (sock_info
->root_handle
== -1) {
1484 ret
= lttng_abi_create_root_handle();
1486 ERR("Error creating root handle");
1489 sock_info
->root_handle
= ret
;
1492 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1494 ERR("Error registering to %s ust cmd socket",
1496 prev_connect_failed
= 1;
1498 * If we cannot register to the sessiond daemon, don't
1499 * delay constructor execution.
1501 ret
= handle_register_done(sock_info
);
1509 * Unlock/relock ust lock because connect is blocking (with
1510 * timeout). Don't delay constructors on the ust lock for too
1517 /* Connect notify socket */
1518 lttng_ust_lock_fd_tracker();
1519 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1520 get_connect_sock_timeout());
1522 lttng_ust_unlock_fd_tracker();
1523 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1524 prev_connect_failed
= 1;
1527 * If we cannot find the sessiond daemon, don't delay
1528 * constructor execution.
1530 ret
= handle_register_done(sock_info
);
1537 ret
= lttng_ust_add_fd_to_tracker(fd
);
1541 PERROR("close on sock_info->notify_socket");
1544 lttng_ust_unlock_fd_tracker();
1549 sock_info
->notify_socket
= ret
;
1550 lttng_ust_unlock_fd_tracker();
1554 * Unlock/relock ust lock because connect is blocking (with
1555 * timeout). Don't delay constructors on the ust lock for too
1562 timeout
= get_notify_sock_timeout();
1565 * Give at least 10ms to sessiond to reply to
1570 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1573 WARN("Error setting socket receive timeout");
1575 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1578 WARN("Error setting socket send timeout");
1580 } else if (timeout
< -1) {
1581 WARN("Unsupported timeout value %ld", timeout
);
1584 ret
= register_to_sessiond(sock_info
->notify_socket
,
1585 USTCTL_SOCKET_NOTIFY
);
1587 ERR("Error registering to %s ust notify socket",
1589 prev_connect_failed
= 1;
1591 * If we cannot register to the sessiond daemon, don't
1592 * delay constructor execution.
1594 ret
= handle_register_done(sock_info
);
1599 sock
= sock_info
->socket
;
1605 struct ustcomm_ust_msg lum
;
1607 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1609 case 0: /* orderly shutdown */
1610 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1615 * Either sessiond has shutdown or refused us by closing the socket.
1616 * In either case, we don't want to delay construction execution,
1617 * and we need to wait before retry.
1619 prev_connect_failed
= 1;
1621 * If we cannot register to the sessiond daemon, don't
1622 * delay constructor execution.
1624 ret
= handle_register_done(sock_info
);
1629 print_cmd(lum
.cmd
, lum
.handle
);
1630 ret
= handle_message(sock_info
, sock
, &lum
);
1632 ERR("Error handling message for %s socket",
1635 * Close socket if protocol error is
1643 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1645 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1647 if (len
== -ECONNRESET
) {
1648 DBG("%s remote end closed connection", sock_info
->name
);
1659 /* Cleanup socket handles before trying to reconnect */
1660 lttng_ust_objd_table_owner_cleanup(sock_info
);
1662 goto restart
; /* try to reconnect */
1667 pthread_mutex_lock(&ust_exit_mutex
);
1668 sock_info
->thread_active
= 0;
1669 pthread_mutex_unlock(&ust_exit_mutex
);
1674 * Weak symbol to call when the ust malloc wrapper is not loaded.
1676 __attribute__((weak
))
1677 void lttng_ust_malloc_wrapper_init(void)
1682 * sessiond monitoring thread: monitor presence of global and per-user
1683 * sessiond by polling the application common named pipe.
1685 void __attribute__((constructor
)) lttng_ust_init(void)
1687 struct timespec constructor_timeout
;
1688 sigset_t sig_all_blocked
, orig_parent_mask
;
1689 pthread_attr_t thread_attr
;
1694 if (uatomic_xchg(&initialized
, 1) == 1)
1698 * Fixup interdependency between TLS fixup mutex (which happens
1699 * to be the dynamic linker mutex) and ust_lock, taken within
1702 lttng_ust_fixup_tls();
1704 lttng_ust_loaded
= 1;
1707 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1708 * the unloading of code used by the ust_listener_threads as we can not
1709 * reliably know when they exited. To do that, manually load
1710 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1711 * this library so it never becomes zero, thus never gets unloaded from the
1712 * address space of the process. Since we are already running in the
1713 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1714 * simply increment the refcount and no additionnal work is needed by the
1715 * dynamic loader as the shared library is already loaded in the address
1716 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1717 * unloading of the UST library if its refcount becomes zero (which should
1718 * never happen). Do the return value check but discard the handle at the
1719 * end of the function as it's not needed.
1721 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1723 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1727 * We want precise control over the order in which we construct
1728 * our sub-libraries vs starting to receive commands from
1729 * sessiond (otherwise leading to errors when trying to create
1730 * sessiond before the init functions are completed).
1733 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1735 lttng_ust_init_fd_tracker();
1736 lttng_ust_clock_init();
1737 lttng_ust_getcpu_init();
1738 lttng_ust_statedump_init();
1739 lttng_ring_buffer_metadata_client_init();
1740 lttng_ring_buffer_client_overwrite_init();
1741 lttng_ring_buffer_client_overwrite_rt_init();
1742 lttng_ring_buffer_client_discard_init();
1743 lttng_ring_buffer_client_discard_rt_init();
1744 lttng_perf_counter_init();
1746 * Invoke ust malloc wrapper init before starting other threads.
1748 lttng_ust_malloc_wrapper_init();
1750 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1752 get_allow_blocking();
1754 ret
= sem_init(&constructor_wait
, 0, 0);
1759 ret
= setup_local_apps();
1761 DBG("local apps setup returned %d", ret
);
1764 /* A new thread created by pthread_create inherits the signal mask
1765 * from the parent. To avoid any signal being received by the
1766 * listener thread, we block all signals temporarily in the parent,
1767 * while we create the listener thread.
1769 sigfillset(&sig_all_blocked
);
1770 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1772 ERR("pthread_sigmask: %s", strerror(ret
));
1775 ret
= pthread_attr_init(&thread_attr
);
1777 ERR("pthread_attr_init: %s", strerror(ret
));
1779 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1781 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1784 pthread_mutex_lock(&ust_exit_mutex
);
1785 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1786 ust_listener_thread
, &global_apps
);
1788 ERR("pthread_create global: %s", strerror(ret
));
1790 global_apps
.thread_active
= 1;
1791 pthread_mutex_unlock(&ust_exit_mutex
);
1793 if (local_apps
.allowed
) {
1794 pthread_mutex_lock(&ust_exit_mutex
);
1795 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1796 ust_listener_thread
, &local_apps
);
1798 ERR("pthread_create local: %s", strerror(ret
));
1800 local_apps
.thread_active
= 1;
1801 pthread_mutex_unlock(&ust_exit_mutex
);
1803 handle_register_done(&local_apps
);
1805 ret
= pthread_attr_destroy(&thread_attr
);
1807 ERR("pthread_attr_destroy: %s", strerror(ret
));
1810 /* Restore original signal mask in parent */
1811 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1813 ERR("pthread_sigmask: %s", strerror(ret
));
1816 switch (timeout_mode
) {
1817 case 1: /* timeout wait */
1819 ret
= sem_timedwait(&constructor_wait
,
1820 &constructor_timeout
);
1821 } while (ret
< 0 && errno
== EINTR
);
1825 ERR("Timed out waiting for lttng-sessiond");
1828 PERROR("sem_timedwait");
1831 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1836 case -1:/* wait forever */
1838 ret
= sem_wait(&constructor_wait
);
1839 } while (ret
< 0 && errno
== EINTR
);
1846 ERR("Unexpected error \"%s\" returned by sem_wait",
1851 case 0: /* no timeout */
1857 void lttng_ust_cleanup(int exiting
)
1859 cleanup_sock_info(&global_apps
, exiting
);
1860 cleanup_sock_info(&local_apps
, exiting
);
1861 local_apps
.allowed
= 0;
1863 * The teardown in this function all affect data structures
1864 * accessed under the UST lock by the listener thread. This
1865 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1866 * that none of these threads are accessing this data at this
1869 lttng_ust_abi_exit();
1870 lttng_ust_events_exit();
1871 lttng_perf_counter_exit();
1872 lttng_ring_buffer_client_discard_rt_exit();
1873 lttng_ring_buffer_client_discard_exit();
1874 lttng_ring_buffer_client_overwrite_rt_exit();
1875 lttng_ring_buffer_client_overwrite_exit();
1876 lttng_ring_buffer_metadata_client_exit();
1877 lttng_ust_statedump_destroy();
1880 /* Reinitialize values for fork */
1882 lttng_ust_comm_should_quit
= 0;
1887 void __attribute__((destructor
)) lttng_ust_exit(void)
1892 * Using pthread_cancel here because:
1893 * A) we don't want to hang application teardown.
1894 * B) the thread is not allocating any resource.
1898 * Require the communication thread to quit. Synchronize with
1899 * mutexes to ensure it is not in a mutex critical section when
1900 * pthread_cancel is later called.
1903 lttng_ust_comm_should_quit
= 1;
1906 pthread_mutex_lock(&ust_exit_mutex
);
1907 /* cancel threads */
1908 if (global_apps
.thread_active
) {
1909 ret
= pthread_cancel(global_apps
.ust_listener
);
1911 ERR("Error cancelling global ust listener thread: %s",
1914 global_apps
.thread_active
= 0;
1917 if (local_apps
.thread_active
) {
1918 ret
= pthread_cancel(local_apps
.ust_listener
);
1920 ERR("Error cancelling local ust listener thread: %s",
1923 local_apps
.thread_active
= 0;
1926 pthread_mutex_unlock(&ust_exit_mutex
);
1929 * Do NOT join threads: use of sys_futex makes it impossible to
1930 * join the threads without using async-cancel, but async-cancel
1931 * is delivered by a signal, which could hit the target thread
1932 * anywhere in its code path, including while the ust_lock() is
1933 * held, causing a deadlock for the other thread. Let the OS
1934 * cleanup the threads if there are stalled in a syscall.
1936 lttng_ust_cleanup(1);
1940 * We exclude the worker threads across fork and clone (except
1941 * CLONE_VM), because these system calls only keep the forking thread
1942 * running in the child. Therefore, we don't want to call fork or clone
1943 * in the middle of an tracepoint or ust tracing state modification.
1944 * Holding this mutex protects these structures across fork and clone.
1946 void ust_before_fork(sigset_t
*save_sigset
)
1949 * Disable signals. This is to avoid that the child intervenes
1950 * before it is properly setup for tracing. It is safer to
1951 * disable all signals, because then we know we are not breaking
1952 * anything by restoring the original mask.
1957 /* Fixup lttng-ust TLS. */
1958 lttng_ust_fixup_tls();
1960 if (URCU_TLS(lttng_ust_nest_count
))
1962 /* Disable signals */
1963 sigfillset(&all_sigs
);
1964 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1966 PERROR("sigprocmask");
1969 pthread_mutex_lock(&ust_fork_mutex
);
1972 rcu_bp_before_fork();
1975 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1979 DBG("process %d", getpid());
1982 pthread_mutex_unlock(&ust_fork_mutex
);
1984 /* Restore signals */
1985 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1987 PERROR("sigprocmask");
1991 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1993 if (URCU_TLS(lttng_ust_nest_count
))
1995 DBG("process %d", getpid());
1996 rcu_bp_after_fork_parent();
1997 /* Release mutexes and reenable signals */
1998 ust_after_fork_common(restore_sigset
);
2002 * After fork, in the child, we need to cleanup all the leftover state,
2003 * except the worker thread which already magically disappeared thanks
2004 * to the weird Linux fork semantics. After tyding up, we call
2005 * lttng_ust_init() again to start over as a new PID.
2007 * This is meant for forks() that have tracing in the child between the
2008 * fork and following exec call (if there is any).
2010 void ust_after_fork_child(sigset_t
*restore_sigset
)
2012 if (URCU_TLS(lttng_ust_nest_count
))
2014 lttng_context_vpid_reset();
2015 lttng_context_vtid_reset();
2016 lttng_context_procname_reset();
2017 DBG("process %d", getpid());
2018 /* Release urcu mutexes */
2019 rcu_bp_after_fork_child();
2020 lttng_ust_cleanup(0);
2021 /* Release mutexes and reenable signals */
2022 ust_after_fork_common(restore_sigset
);
2026 void lttng_ust_sockinfo_session_enabled(void *owner
)
2028 struct sock_info
*sock_info
= owner
;
2029 sock_info
->statedump_pending
= 1;