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.
226 static int sem_count
= { 2 };
229 * Counting nesting within lttng-ust. Used to ensure that calling fork()
230 * from liblttng-ust does not execute the pre/post fork handlers.
232 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
235 * Info about socket and associated listener thread.
239 pthread_t ust_listener
; /* listener thread */
241 int constructor_sem_posted
;
246 char sock_path
[PATH_MAX
];
250 char wait_shm_path
[PATH_MAX
];
252 /* Keep track of lazy state dump not performed yet. */
253 int statedump_pending
;
256 /* Socket from app (connect) to session daemon (listen) for communication */
257 struct sock_info global_apps
= {
265 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
269 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
271 .statedump_pending
= 0,
274 /* TODO: allow global_apps_sock_path override */
276 struct sock_info local_apps
= {
280 .allowed
= 0, /* Check setuid bit first */
286 .statedump_pending
= 0,
289 static int wait_poll_fallback
;
291 static const char *cmd_name_mapping
[] = {
292 [ LTTNG_UST_RELEASE
] = "Release",
293 [ LTTNG_UST_SESSION
] = "Create Session",
294 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
296 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
297 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
298 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
299 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
301 /* Session FD commands */
302 [ LTTNG_UST_CHANNEL
] = "Create Channel",
303 [ LTTNG_UST_SESSION_START
] = "Start Session",
304 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
306 /* Channel FD commands */
307 [ LTTNG_UST_STREAM
] = "Create Stream",
308 [ LTTNG_UST_EVENT
] = "Create Event",
310 /* Event and Channel FD commands */
311 [ LTTNG_UST_CONTEXT
] = "Create Context",
312 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
314 /* Event, Channel and Session commands */
315 [ LTTNG_UST_ENABLE
] = "Enable",
316 [ LTTNG_UST_DISABLE
] = "Disable",
318 /* Tracepoint list commands */
319 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
320 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
322 /* Event FD commands */
323 [ LTTNG_UST_FILTER
] = "Create Filter",
324 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
327 static const char *str_timeout
;
328 static int got_timeout_env
;
330 extern void lttng_ring_buffer_client_overwrite_init(void);
331 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
332 extern void lttng_ring_buffer_client_discard_init(void);
333 extern void lttng_ring_buffer_client_discard_rt_init(void);
334 extern void lttng_ring_buffer_metadata_client_init(void);
335 extern void lttng_ring_buffer_client_overwrite_exit(void);
336 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
337 extern void lttng_ring_buffer_client_discard_exit(void);
338 extern void lttng_ring_buffer_client_discard_rt_exit(void);
339 extern void lttng_ring_buffer_metadata_client_exit(void);
341 static char *get_map_shm(struct sock_info
*sock_info
);
343 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
346 size_t copied
= 0, to_copy
= len
;
349 ret
= read(fd
, buf
+ copied
, to_copy
);
354 } while ((ret
> 0 && to_copy
> 0)
355 || (ret
< 0 && errno
== EINTR
));
362 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
366 const char *get_lttng_home_dir(void)
370 val
= (const char *) lttng_getenv("LTTNG_HOME");
374 return (const char *) lttng_getenv("HOME");
378 * Force a read (imply TLS fixup for dlopen) of TLS variables.
381 void lttng_fixup_nest_count_tls(void)
383 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
387 void lttng_fixup_ust_mutex_nest_tls(void)
389 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
396 void lttng_fixup_urcu_bp_tls(void)
402 void lttng_ust_fixup_tls(void)
404 lttng_fixup_urcu_bp_tls();
405 lttng_fixup_ringbuffer_tls();
406 lttng_fixup_vtid_tls();
407 lttng_fixup_nest_count_tls();
408 lttng_fixup_procname_tls();
409 lttng_fixup_ust_mutex_nest_tls();
410 lttng_ust_fixup_fd_tracker_tls();
413 int lttng_get_notify_socket(void *owner
)
415 struct sock_info
*info
= owner
;
417 return info
->notify_socket
;
421 void print_cmd(int cmd
, int handle
)
423 const char *cmd_name
= "Unknown";
425 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
426 && cmd_name_mapping
[cmd
]) {
427 cmd_name
= cmd_name_mapping
[cmd
];
429 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
431 lttng_ust_obj_get_name(handle
), handle
);
435 int setup_global_apps(void)
438 assert(!global_apps
.wait_shm_mmap
);
440 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
441 if (!global_apps
.wait_shm_mmap
) {
442 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
443 global_apps
.allowed
= 0;
448 global_apps
.allowed
= 1;
453 int setup_local_apps(void)
456 const char *home_dir
;
459 assert(!local_apps
.wait_shm_mmap
);
463 * Disallow per-user tracing for setuid binaries.
465 if (uid
!= geteuid()) {
466 assert(local_apps
.allowed
== 0);
470 home_dir
= get_lttng_home_dir();
472 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
473 assert(local_apps
.allowed
== 0);
477 local_apps
.allowed
= 1;
478 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
480 LTTNG_DEFAULT_HOME_RUNDIR
,
481 LTTNG_UST_SOCK_FILENAME
);
482 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
483 LTTNG_UST_WAIT_FILENAME
,
486 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
487 if (!local_apps
.wait_shm_mmap
) {
488 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
489 local_apps
.allowed
= 0;
498 * Get socket timeout, in ms.
499 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
502 long get_timeout(void)
504 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
506 if (!got_timeout_env
) {
507 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
511 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
512 /* All negative values are considered as "-1". */
513 if (constructor_delay_ms
< -1)
514 constructor_delay_ms
= -1;
515 return constructor_delay_ms
;
518 /* Timeout for notify socket send and recv. */
520 long get_notify_sock_timeout(void)
522 return get_timeout();
525 /* Timeout for connecting to cmd and notify sockets. */
527 long get_connect_sock_timeout(void)
529 return get_timeout();
533 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
536 int get_constructor_timeout(struct timespec
*constructor_timeout
)
538 long constructor_delay_ms
;
541 constructor_delay_ms
= get_timeout();
543 switch (constructor_delay_ms
) {
544 case -1:/* fall-through */
546 return constructor_delay_ms
;
552 * If we are unable to find the current time, don't wait.
554 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
559 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
560 constructor_timeout
->tv_nsec
+=
561 (constructor_delay_ms
% 1000UL) * 1000000UL;
562 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
563 constructor_timeout
->tv_sec
++;
564 constructor_timeout
->tv_nsec
-= 1000000000UL;
566 /* Timeout wait (constructor_delay_ms). */
571 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
573 return ustcomm_send_reg_msg(socket
,
576 lttng_alignof(uint8_t) * CHAR_BIT
,
577 lttng_alignof(uint16_t) * CHAR_BIT
,
578 lttng_alignof(uint32_t) * CHAR_BIT
,
579 lttng_alignof(uint64_t) * CHAR_BIT
,
580 lttng_alignof(unsigned long) * CHAR_BIT
);
584 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
588 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
591 DBG("message successfully sent");
594 if (len
== -ECONNRESET
) {
595 DBG("remote end closed connection");
600 DBG("incorrect message size: %zd", len
);
606 int handle_register_done(struct sock_info
*sock_info
)
610 if (sock_info
->constructor_sem_posted
)
612 sock_info
->constructor_sem_posted
= 1;
613 if (uatomic_read(&sem_count
) <= 0) {
616 ret
= uatomic_add_return(&sem_count
, -1);
618 ret
= sem_post(&constructor_wait
);
625 * Only execute pending statedump after the constructor semaphore has
626 * been posted by each listener thread. This means statedump will only
627 * be performed after the "registration done" command is received from
628 * each session daemon the application is connected to.
630 * This ensures we don't run into deadlock issues with the dynamic
631 * loader mutex, which is held while the constructor is called and
632 * waiting on the constructor semaphore. All operations requiring this
633 * dynamic loader lock need to be postponed using this mechanism.
636 void handle_pending_statedump(struct sock_info
*sock_info
)
638 int ctor_passed
= sock_info
->constructor_sem_posted
;
640 if (ctor_passed
&& sock_info
->statedump_pending
) {
641 sock_info
->statedump_pending
= 0;
642 pthread_mutex_lock(&ust_fork_mutex
);
643 lttng_handle_pending_statedump(sock_info
);
644 pthread_mutex_unlock(&ust_fork_mutex
);
649 int handle_message(struct sock_info
*sock_info
,
650 int sock
, struct ustcomm_ust_msg
*lum
)
653 const struct lttng_ust_objd_ops
*ops
;
654 struct ustcomm_ust_reply lur
;
656 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
659 memset(&lur
, 0, sizeof(lur
));
662 ret
= -LTTNG_UST_ERR_EXITING
;
666 ops
= objd_ops(lum
->handle
);
673 case LTTNG_UST_REGISTER_DONE
:
674 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
675 ret
= handle_register_done(sock_info
);
679 case LTTNG_UST_RELEASE
:
680 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
683 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
685 case LTTNG_UST_FILTER
:
687 /* Receive filter data */
688 struct lttng_ust_filter_bytecode_node
*bytecode
;
690 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
691 ERR("Filter data size is too large: %u bytes",
692 lum
->u
.filter
.data_size
);
697 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
698 ERR("Filter reloc offset %u is not within data",
699 lum
->u
.filter
.reloc_offset
);
704 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
709 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
710 lum
->u
.filter
.data_size
);
712 case 0: /* orderly shutdown */
717 if (len
== lum
->u
.filter
.data_size
) {
718 DBG("filter data received");
720 } else if (len
< 0) {
721 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
722 if (len
== -ECONNRESET
) {
723 ERR("%s remote end closed connection", sock_info
->name
);
732 DBG("incorrect filter data message size: %zd", len
);
738 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
739 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
740 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
742 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
743 (unsigned long) bytecode
,
748 /* don't free bytecode if everything went fine. */
755 case LTTNG_UST_EXCLUSION
:
757 /* Receive exclusion names */
758 struct lttng_ust_excluder_node
*node
;
761 count
= lum
->u
.exclusion
.count
;
763 /* There are no names to read */
767 node
= zmalloc(sizeof(*node
) +
768 count
* LTTNG_UST_SYM_NAME_LEN
);
773 node
->excluder
.count
= count
;
774 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
775 count
* LTTNG_UST_SYM_NAME_LEN
);
777 case 0: /* orderly shutdown */
782 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
783 DBG("Exclusion data received");
785 } else if (len
< 0) {
786 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
787 if (len
== -ECONNRESET
) {
788 ERR("%s remote end closed connection", sock_info
->name
);
797 DBG("Incorrect exclusion data message size: %zd", len
);
804 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
805 (unsigned long) node
,
810 /* Don't free exclusion data if everything went fine. */
817 case LTTNG_UST_CHANNEL
:
822 len
= ustcomm_recv_channel_from_sessiond(sock
,
823 &chan_data
, lum
->u
.channel
.len
,
826 case 0: /* orderly shutdown */
830 if (len
== lum
->u
.channel
.len
) {
831 DBG("channel data received");
833 } else if (len
< 0) {
834 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
835 if (len
== -ECONNRESET
) {
836 ERR("%s remote end closed connection", sock_info
->name
);
843 DBG("incorrect channel data message size: %zd", len
);
848 args
.channel
.chan_data
= chan_data
;
849 args
.channel
.wakeup_fd
= wakeup_fd
;
851 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
852 (unsigned long) &lum
->u
,
858 case LTTNG_UST_STREAM
:
860 /* Receive shm_fd, wakeup_fd */
861 ret
= ustcomm_recv_stream_from_sessiond(sock
,
864 &args
.stream
.wakeup_fd
);
870 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
871 (unsigned long) &lum
->u
,
877 case LTTNG_UST_CONTEXT
:
878 switch (lum
->u
.context
.ctx
) {
879 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
882 size_t ctxlen
, recvlen
;
884 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
885 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
886 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
887 ERR("Application context string length size is too large: %zu bytes",
892 strcpy(ctxstr
, "$app.");
893 p
= &ctxstr
[strlen("$app.")];
894 recvlen
= ctxlen
- strlen("$app.");
895 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
897 case 0: /* orderly shutdown */
901 if (len
== recvlen
) {
902 DBG("app context data received");
904 } else if (len
< 0) {
905 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
906 if (len
== -ECONNRESET
) {
907 ERR("%s remote end closed connection", sock_info
->name
);
914 DBG("incorrect app context data message size: %zd", len
);
919 /* Put : between provider and ctxname. */
920 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
921 args
.app_context
.ctxname
= ctxstr
;
928 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
929 (unsigned long) &lum
->u
,
937 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
938 (unsigned long) &lum
->u
,
945 lur
.handle
= lum
->handle
;
949 lur
.ret_code
= LTTNG_UST_OK
;
952 * Use -LTTNG_UST_ERR as wildcard for UST internal
953 * error that are not caused by the transport, except if
954 * we already have a more precise error message to
957 if (ret
> -LTTNG_UST_ERR
) {
958 /* Translate code to UST error. */
961 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
964 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
967 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
970 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
973 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
976 lur
.ret_code
= -LTTNG_UST_ERR
;
985 case LTTNG_UST_TRACER_VERSION
:
986 lur
.u
.version
= lum
->u
.version
;
988 case LTTNG_UST_TRACEPOINT_LIST_GET
:
989 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
993 DBG("Return value: %d", lur
.ret_val
);
998 * Performed delayed statedump operations outside of the UST
999 * lock. We need to take the dynamic loader lock before we take
1000 * the UST lock internally within handle_pending_statedump().
1002 handle_pending_statedump(sock_info
);
1005 ret
= -LTTNG_UST_ERR_EXITING
;
1009 ret
= send_reply(sock
, &lur
);
1011 DBG("error sending reply");
1016 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1019 if (lur
.ret_code
== LTTNG_UST_OK
) {
1021 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1022 len
= ustcomm_send_unix_sock(sock
,
1023 &args
.field_list
.entry
,
1024 sizeof(args
.field_list
.entry
));
1029 if (len
!= sizeof(args
.field_list
.entry
)) {
1043 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1047 if (sock_info
->root_handle
!= -1) {
1048 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1050 ERR("Error unref root handle");
1052 sock_info
->root_handle
= -1;
1054 sock_info
->constructor_sem_posted
= 0;
1057 * wait_shm_mmap, socket and notify socket are used by listener
1058 * threads outside of the ust lock, so we cannot tear them down
1059 * ourselves, because we cannot join on these threads. Leave
1060 * responsibility of cleaning up these resources to the OS
1066 if (sock_info
->socket
!= -1) {
1067 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1069 ERR("Error closing ust cmd socket");
1071 sock_info
->socket
= -1;
1073 if (sock_info
->notify_socket
!= -1) {
1074 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1076 ERR("Error closing ust notify socket");
1078 sock_info
->notify_socket
= -1;
1080 if (sock_info
->wait_shm_mmap
) {
1083 page_size
= sysconf(_SC_PAGE_SIZE
);
1084 if (page_size
<= 0) {
1088 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1090 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1092 ERR("Error unmapping wait shm");
1095 sock_info
->wait_shm_mmap
= NULL
;
1100 * Using fork to set umask in the child process (not multi-thread safe).
1101 * We deal with the shm_open vs ftruncate race (happening when the
1102 * sessiond owns the shm and does not let everybody modify it, to ensure
1103 * safety against shm_unlink) by simply letting the mmap fail and
1104 * retrying after a few seconds.
1105 * For global shm, everybody has rw access to it until the sessiond
1109 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1111 int wait_shm_fd
, ret
;
1115 * Try to open read-only.
1117 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1118 if (wait_shm_fd
>= 0) {
1121 size_t bytes_read
= 0;
1124 * Try to read the fd. If unable to do so, try opening
1128 len
= read(wait_shm_fd
,
1129 &((char *) &tmp_read
)[bytes_read
],
1130 sizeof(tmp_read
) - bytes_read
);
1134 } while ((len
< 0 && errno
== EINTR
)
1135 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1136 if (bytes_read
!= sizeof(tmp_read
)) {
1137 ret
= close(wait_shm_fd
);
1139 ERR("close wait_shm_fd");
1144 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1146 * Real-only open did not work, and it's not because the
1147 * entry was not present. It's a failure that prohibits
1150 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1156 * If the open failed because the file did not exist, or because
1157 * the file was not truncated yet, try creating it ourself.
1159 URCU_TLS(lttng_ust_nest_count
)++;
1161 URCU_TLS(lttng_ust_nest_count
)--;
1166 * Parent: wait for child to return, in which case the
1167 * shared memory map will have been created.
1169 pid
= wait(&status
);
1170 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1175 * Try to open read-only again after creation.
1177 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1178 if (wait_shm_fd
< 0) {
1180 * Real-only open did not work. It's a failure
1181 * that prohibits using shm.
1183 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1187 } else if (pid
== 0) {
1191 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1192 if (sock_info
->global
)
1193 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1195 * We're alone in a child process, so we can modify the
1196 * process-wide umask.
1198 umask(~create_mode
);
1200 * Try creating shm (or get rw access).
1201 * We don't do an exclusive open, because we allow other
1202 * processes to create+ftruncate it concurrently.
1204 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1205 O_RDWR
| O_CREAT
, create_mode
);
1206 if (wait_shm_fd
>= 0) {
1207 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1209 PERROR("ftruncate");
1210 _exit(EXIT_FAILURE
);
1212 _exit(EXIT_SUCCESS
);
1215 * For local shm, we need to have rw access to accept
1216 * opening it: this means the local sessiond will be
1217 * able to wake us up. For global shm, we open it even
1218 * if rw access is not granted, because the root.root
1219 * sessiond will be able to override all rights and wake
1222 if (!sock_info
->global
&& errno
!= EACCES
) {
1223 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1224 _exit(EXIT_FAILURE
);
1227 * The shm exists, but we cannot open it RW. Report
1230 _exit(EXIT_SUCCESS
);
1235 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1236 struct stat statbuf
;
1239 * Ensure that our user is the owner of the shm file for
1240 * local shm. If we do not own the file, it means our
1241 * sessiond will not have access to wake us up (there is
1242 * probably a rogue process trying to fake our
1243 * sessiond). Fallback to polling method in this case.
1245 ret
= fstat(wait_shm_fd
, &statbuf
);
1250 if (statbuf
.st_uid
!= getuid())
1256 ret
= close(wait_shm_fd
);
1258 PERROR("Error closing fd");
1264 char *get_map_shm(struct sock_info
*sock_info
)
1267 int wait_shm_fd
, ret
;
1268 char *wait_shm_mmap
;
1270 page_size
= sysconf(_SC_PAGE_SIZE
);
1271 if (page_size
<= 0) {
1275 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1279 lttng_ust_lock_fd_tracker();
1280 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1281 if (wait_shm_fd
< 0) {
1282 lttng_ust_unlock_fd_tracker();
1286 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1288 ret
= close(wait_shm_fd
);
1290 PERROR("Error closing fd");
1292 lttng_ust_unlock_fd_tracker();
1297 lttng_ust_unlock_fd_tracker();
1299 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1300 MAP_SHARED
, wait_shm_fd
, 0);
1302 /* close shm fd immediately after taking the mmap reference */
1303 lttng_ust_lock_fd_tracker();
1304 ret
= close(wait_shm_fd
);
1306 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1308 PERROR("Error closing fd");
1310 lttng_ust_unlock_fd_tracker();
1312 if (wait_shm_mmap
== MAP_FAILED
) {
1313 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1316 return wait_shm_mmap
;
1323 void wait_for_sessiond(struct sock_info
*sock_info
)
1325 /* Use ust_lock to check if we should quit. */
1329 if (wait_poll_fallback
) {
1334 assert(sock_info
->wait_shm_mmap
);
1336 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1337 /* Wait for futex wakeup */
1338 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1341 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1342 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1345 /* Value already changed. */
1348 /* Retry if interrupted by signal. */
1349 break; /* Get out of switch. */
1351 wait_poll_fallback
= 1;
1353 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1354 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1355 "Please upgrade your kernel "
1356 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1357 "mainline). LTTng-UST will use polling mode fallback.");
1376 * This thread does not allocate any resource, except within
1377 * handle_message, within mutex protection. This mutex protects against
1379 * The other moment it allocates resources is at socket connection, which
1380 * is also protected by the mutex.
1383 void *ust_listener_thread(void *arg
)
1385 struct sock_info
*sock_info
= arg
;
1386 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1389 lttng_ust_fixup_tls();
1391 * If available, add '-ust' to the end of this thread's
1394 ret
= lttng_ust_setustprocname();
1396 ERR("Unable to set UST process name");
1399 /* Restart trying to connect to the session daemon */
1401 if (prev_connect_failed
) {
1402 /* Wait for sessiond availability with pipe */
1403 wait_for_sessiond(sock_info
);
1407 * Sleep for 5 seconds before retrying after a
1408 * sequence of failure / wait / failure. This
1409 * deals with a killed or broken session daemon.
1415 prev_connect_failed
= 0;
1422 if (sock_info
->socket
!= -1) {
1423 /* FD tracker is updated by ustcomm_close_unix_sock() */
1424 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1426 ERR("Error closing %s ust cmd socket",
1429 sock_info
->socket
= -1;
1431 if (sock_info
->notify_socket
!= -1) {
1432 /* FD tracker is updated by ustcomm_close_unix_sock() */
1433 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1435 ERR("Error closing %s ust notify socket",
1438 sock_info
->notify_socket
= -1;
1443 * Register. We need to perform both connect and sending
1444 * registration message before doing the next connect otherwise
1445 * we may reach unix socket connect queue max limits and block
1446 * on the 2nd connect while the session daemon is awaiting the
1447 * first connect registration message.
1449 /* Connect cmd socket */
1450 lttng_ust_lock_fd_tracker();
1451 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1452 get_connect_sock_timeout());
1454 lttng_ust_unlock_fd_tracker();
1455 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1456 prev_connect_failed
= 1;
1459 * If we cannot find the sessiond daemon, don't delay
1460 * constructor execution.
1462 ret
= handle_register_done(sock_info
);
1468 ret
= lttng_ust_add_fd_to_tracker(fd
);
1472 PERROR("close on sock_info->socket");
1475 lttng_ust_unlock_fd_tracker();
1480 sock_info
->socket
= ret
;
1481 lttng_ust_unlock_fd_tracker();
1485 * Unlock/relock ust lock because connect is blocking (with
1486 * timeout). Don't delay constructors on the ust lock for too
1494 * Create only one root handle per listener thread for the whole
1495 * process lifetime, so we ensure we get ID which is statically
1496 * assigned to the root handle.
1498 if (sock_info
->root_handle
== -1) {
1499 ret
= lttng_abi_create_root_handle();
1501 ERR("Error creating root handle");
1504 sock_info
->root_handle
= ret
;
1507 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1509 ERR("Error registering to %s ust cmd socket",
1511 prev_connect_failed
= 1;
1513 * If we cannot register to the sessiond daemon, don't
1514 * delay constructor execution.
1516 ret
= handle_register_done(sock_info
);
1524 * Unlock/relock ust lock because connect is blocking (with
1525 * timeout). Don't delay constructors on the ust lock for too
1532 /* Connect notify socket */
1533 lttng_ust_lock_fd_tracker();
1534 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1535 get_connect_sock_timeout());
1537 lttng_ust_unlock_fd_tracker();
1538 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1539 prev_connect_failed
= 1;
1542 * If we cannot find the sessiond daemon, don't delay
1543 * constructor execution.
1545 ret
= handle_register_done(sock_info
);
1552 ret
= lttng_ust_add_fd_to_tracker(fd
);
1556 PERROR("close on sock_info->notify_socket");
1559 lttng_ust_unlock_fd_tracker();
1564 sock_info
->notify_socket
= ret
;
1565 lttng_ust_unlock_fd_tracker();
1569 * Unlock/relock ust lock because connect is blocking (with
1570 * timeout). Don't delay constructors on the ust lock for too
1577 timeout
= get_notify_sock_timeout();
1580 * Give at least 10ms to sessiond to reply to
1585 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1588 WARN("Error setting socket receive timeout");
1590 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1593 WARN("Error setting socket send timeout");
1595 } else if (timeout
< -1) {
1596 WARN("Unsupported timeout value %ld", timeout
);
1599 ret
= register_to_sessiond(sock_info
->notify_socket
,
1600 USTCTL_SOCKET_NOTIFY
);
1602 ERR("Error registering to %s ust notify socket",
1604 prev_connect_failed
= 1;
1606 * If we cannot register to the sessiond daemon, don't
1607 * delay constructor execution.
1609 ret
= handle_register_done(sock_info
);
1614 sock
= sock_info
->socket
;
1620 struct ustcomm_ust_msg lum
;
1622 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1624 case 0: /* orderly shutdown */
1625 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1630 * Either sessiond has shutdown or refused us by closing the socket.
1631 * In either case, we don't want to delay construction execution,
1632 * and we need to wait before retry.
1634 prev_connect_failed
= 1;
1636 * If we cannot register to the sessiond daemon, don't
1637 * delay constructor execution.
1639 ret
= handle_register_done(sock_info
);
1644 print_cmd(lum
.cmd
, lum
.handle
);
1645 ret
= handle_message(sock_info
, sock
, &lum
);
1647 ERR("Error handling message for %s socket",
1650 * Close socket if protocol error is
1658 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1660 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1662 if (len
== -ECONNRESET
) {
1663 DBG("%s remote end closed connection", sock_info
->name
);
1674 /* Cleanup socket handles before trying to reconnect */
1675 lttng_ust_objd_table_owner_cleanup(sock_info
);
1677 goto restart
; /* try to reconnect */
1682 pthread_mutex_lock(&ust_exit_mutex
);
1683 sock_info
->thread_active
= 0;
1684 pthread_mutex_unlock(&ust_exit_mutex
);
1689 * Weak symbol to call when the ust malloc wrapper is not loaded.
1691 __attribute__((weak
))
1692 void lttng_ust_malloc_wrapper_init(void)
1697 * sessiond monitoring thread: monitor presence of global and per-user
1698 * sessiond by polling the application common named pipe.
1700 void __attribute__((constructor
)) lttng_ust_init(void)
1702 struct timespec constructor_timeout
;
1703 sigset_t sig_all_blocked
, orig_parent_mask
;
1704 pthread_attr_t thread_attr
;
1708 if (uatomic_xchg(&initialized
, 1) == 1)
1712 * Fixup interdependency between TLS fixup mutex (which happens
1713 * to be the dynamic linker mutex) and ust_lock, taken within
1716 lttng_ust_fixup_tls();
1718 lttng_ust_loaded
= 1;
1721 * We want precise control over the order in which we construct
1722 * our sub-libraries vs starting to receive commands from
1723 * sessiond (otherwise leading to errors when trying to create
1724 * sessiond before the init functions are completed).
1727 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1729 lttng_ust_init_fd_tracker();
1730 lttng_ust_clock_init();
1731 lttng_ust_getcpu_init();
1732 lttng_ust_statedump_init();
1733 lttng_ring_buffer_metadata_client_init();
1734 lttng_ring_buffer_client_overwrite_init();
1735 lttng_ring_buffer_client_overwrite_rt_init();
1736 lttng_ring_buffer_client_discard_init();
1737 lttng_ring_buffer_client_discard_rt_init();
1738 lttng_perf_counter_init();
1740 * Invoke ust malloc wrapper init before starting other threads.
1742 lttng_ust_malloc_wrapper_init();
1744 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1746 ret
= sem_init(&constructor_wait
, 0, 0);
1751 ret
= setup_global_apps();
1753 assert(global_apps
.allowed
== 0);
1754 DBG("global apps setup returned %d", ret
);
1757 ret
= setup_local_apps();
1759 assert(local_apps
.allowed
== 0);
1760 DBG("local apps setup returned %d", ret
);
1763 /* A new thread created by pthread_create inherits the signal mask
1764 * from the parent. To avoid any signal being received by the
1765 * listener thread, we block all signals temporarily in the parent,
1766 * while we create the listener thread.
1768 sigfillset(&sig_all_blocked
);
1769 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1771 ERR("pthread_sigmask: %s", strerror(ret
));
1774 ret
= pthread_attr_init(&thread_attr
);
1776 ERR("pthread_attr_init: %s", strerror(ret
));
1778 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1780 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1783 if (global_apps
.allowed
) {
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 handle_register_done(&global_apps
);
1796 if (local_apps
.allowed
) {
1797 pthread_mutex_lock(&ust_exit_mutex
);
1798 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1799 ust_listener_thread
, &local_apps
);
1801 ERR("pthread_create local: %s", strerror(ret
));
1803 local_apps
.thread_active
= 1;
1804 pthread_mutex_unlock(&ust_exit_mutex
);
1806 handle_register_done(&local_apps
);
1808 ret
= pthread_attr_destroy(&thread_attr
);
1810 ERR("pthread_attr_destroy: %s", strerror(ret
));
1813 /* Restore original signal mask in parent */
1814 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1816 ERR("pthread_sigmask: %s", strerror(ret
));
1819 switch (timeout_mode
) {
1820 case 1: /* timeout wait */
1822 ret
= sem_timedwait(&constructor_wait
,
1823 &constructor_timeout
);
1824 } while (ret
< 0 && errno
== EINTR
);
1828 ERR("Timed out waiting for lttng-sessiond");
1831 PERROR("sem_timedwait");
1834 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1839 case -1:/* wait forever */
1841 ret
= sem_wait(&constructor_wait
);
1842 } while (ret
< 0 && errno
== EINTR
);
1849 ERR("Unexpected error \"%s\" returned by sem_wait",
1854 case 0: /* no timeout */
1860 void lttng_ust_cleanup(int exiting
)
1862 cleanup_sock_info(&global_apps
, exiting
);
1863 cleanup_sock_info(&local_apps
, exiting
);
1864 local_apps
.allowed
= 0;
1865 global_apps
.allowed
= 0;
1867 * The teardown in this function all affect data structures
1868 * accessed under the UST lock by the listener thread. This
1869 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1870 * that none of these threads are accessing this data at this
1873 lttng_ust_abi_exit();
1874 lttng_ust_events_exit();
1875 lttng_perf_counter_exit();
1876 lttng_ring_buffer_client_discard_rt_exit();
1877 lttng_ring_buffer_client_discard_exit();
1878 lttng_ring_buffer_client_overwrite_rt_exit();
1879 lttng_ring_buffer_client_overwrite_exit();
1880 lttng_ring_buffer_metadata_client_exit();
1881 lttng_ust_statedump_destroy();
1884 /* Reinitialize values for fork */
1886 lttng_ust_comm_should_quit
= 0;
1891 void __attribute__((destructor
)) lttng_ust_exit(void)
1896 * Using pthread_cancel here because:
1897 * A) we don't want to hang application teardown.
1898 * B) the thread is not allocating any resource.
1902 * Require the communication thread to quit. Synchronize with
1903 * mutexes to ensure it is not in a mutex critical section when
1904 * pthread_cancel is later called.
1907 lttng_ust_comm_should_quit
= 1;
1910 pthread_mutex_lock(&ust_exit_mutex
);
1911 /* cancel threads */
1912 if (global_apps
.thread_active
) {
1913 ret
= pthread_cancel(global_apps
.ust_listener
);
1915 ERR("Error cancelling global ust listener thread: %s",
1918 global_apps
.thread_active
= 0;
1921 if (local_apps
.thread_active
) {
1922 ret
= pthread_cancel(local_apps
.ust_listener
);
1924 ERR("Error cancelling local ust listener thread: %s",
1927 local_apps
.thread_active
= 0;
1930 pthread_mutex_unlock(&ust_exit_mutex
);
1933 * Do NOT join threads: use of sys_futex makes it impossible to
1934 * join the threads without using async-cancel, but async-cancel
1935 * is delivered by a signal, which could hit the target thread
1936 * anywhere in its code path, including while the ust_lock() is
1937 * held, causing a deadlock for the other thread. Let the OS
1938 * cleanup the threads if there are stalled in a syscall.
1940 lttng_ust_cleanup(1);
1944 * We exclude the worker threads across fork and clone (except
1945 * CLONE_VM), because these system calls only keep the forking thread
1946 * running in the child. Therefore, we don't want to call fork or clone
1947 * in the middle of an tracepoint or ust tracing state modification.
1948 * Holding this mutex protects these structures across fork and clone.
1950 void ust_before_fork(sigset_t
*save_sigset
)
1953 * Disable signals. This is to avoid that the child intervenes
1954 * before it is properly setup for tracing. It is safer to
1955 * disable all signals, because then we know we are not breaking
1956 * anything by restoring the original mask.
1961 /* Fixup lttng-ust TLS. */
1962 lttng_ust_fixup_tls();
1964 if (URCU_TLS(lttng_ust_nest_count
))
1966 /* Disable signals */
1967 sigfillset(&all_sigs
);
1968 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1970 PERROR("sigprocmask");
1973 pthread_mutex_lock(&ust_fork_mutex
);
1976 rcu_bp_before_fork();
1979 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1983 DBG("process %d", getpid());
1986 pthread_mutex_unlock(&ust_fork_mutex
);
1988 /* Restore signals */
1989 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1991 PERROR("sigprocmask");
1995 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1997 if (URCU_TLS(lttng_ust_nest_count
))
1999 DBG("process %d", getpid());
2000 rcu_bp_after_fork_parent();
2001 /* Release mutexes and reenable signals */
2002 ust_after_fork_common(restore_sigset
);
2006 * After fork, in the child, we need to cleanup all the leftover state,
2007 * except the worker thread which already magically disappeared thanks
2008 * to the weird Linux fork semantics. After tyding up, we call
2009 * lttng_ust_init() again to start over as a new PID.
2011 * This is meant for forks() that have tracing in the child between the
2012 * fork and following exec call (if there is any).
2014 void ust_after_fork_child(sigset_t
*restore_sigset
)
2016 if (URCU_TLS(lttng_ust_nest_count
))
2018 lttng_context_vpid_reset();
2019 lttng_context_vtid_reset();
2020 lttng_context_procname_reset();
2021 DBG("process %d", getpid());
2022 /* Release urcu mutexes */
2023 rcu_bp_after_fork_child();
2024 lttng_ust_cleanup(0);
2025 /* Release mutexes and reenable signals */
2026 ust_after_fork_common(restore_sigset
);
2030 void lttng_ust_sockinfo_session_enabled(void *owner
)
2032 struct sock_info
*sock_info
= owner
;
2033 sock_info
->statedump_pending
= 1;