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
23 #include <sys/types.h>
24 #include <sys/socket.h>
27 #include <sys/types.h>
33 #include <semaphore.h>
37 #include <urcu/uatomic.h>
38 #include <urcu/futex.h>
39 #include <urcu/compiler.h>
41 #include <lttng/ust-events.h>
42 #include <lttng/ust-abi.h>
43 #include <lttng/ust.h>
44 #include <lttng/ust-error.h>
45 #include <lttng/ust-ctl.h>
46 #include <urcu/tls-compat.h>
48 #include <usterr-signal-safe.h>
50 #include "tracepoint-internal.h"
51 #include "lttng-tracer-core.h"
53 #include "../libringbuffer/tlsfixup.h"
56 * Has lttng ust comm constructor been called ?
58 static int initialized
;
61 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
62 * Held when handling a command, also held by fork() to deal with
63 * removal of threads, and by exit path.
66 /* Should the ust comm thread quit ? */
67 static int lttng_ust_comm_should_quit
;
70 * Wait for either of these before continuing to the main
72 * - the register_done message from sessiond daemon
73 * (will let the sessiond daemon enable sessions before main
75 * - sessiond daemon is not reachable.
76 * - timeout (ensuring applications are resilient to session
79 static sem_t constructor_wait
;
81 * Doing this for both the global and local sessiond.
83 static int sem_count
= { 2 };
86 * Counting nesting within lttng-ust. Used to ensure that calling fork()
87 * from liblttng-ust does not execute the pre/post fork handlers.
89 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
92 * Info about socket and associated listener thread.
96 pthread_t ust_listener
; /* listener thread */
98 int constructor_sem_posted
;
103 char sock_path
[PATH_MAX
];
107 char wait_shm_path
[PATH_MAX
];
111 /* Socket from app (connect) to session daemon (listen) for communication */
112 struct sock_info global_apps
= {
120 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
124 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
127 /* TODO: allow global_apps_sock_path override */
129 struct sock_info local_apps
= {
133 .allowed
= 0, /* Check setuid bit first */
140 static int wait_poll_fallback
;
142 static const char *cmd_name_mapping
[] = {
143 [ LTTNG_UST_RELEASE
] = "Release",
144 [ LTTNG_UST_SESSION
] = "Create Session",
145 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
147 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
148 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
149 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
150 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
152 /* Session FD commands */
153 [ LTTNG_UST_CHANNEL
] = "Create Channel",
154 [ LTTNG_UST_SESSION_START
] = "Start Session",
155 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
157 /* Channel FD commands */
158 [ LTTNG_UST_STREAM
] = "Create Stream",
159 [ LTTNG_UST_EVENT
] = "Create Event",
161 /* Event and Channel FD commands */
162 [ LTTNG_UST_CONTEXT
] = "Create Context",
163 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
165 /* Event, Channel and Session commands */
166 [ LTTNG_UST_ENABLE
] = "Enable",
167 [ LTTNG_UST_DISABLE
] = "Disable",
169 /* Tracepoint list commands */
170 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
171 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
173 /* Event FD commands */
174 [ LTTNG_UST_FILTER
] = "Create Filter",
177 static const char *str_timeout
;
178 static int got_timeout_env
;
180 extern void lttng_ring_buffer_client_overwrite_init(void);
181 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
182 extern void lttng_ring_buffer_client_discard_init(void);
183 extern void lttng_ring_buffer_client_discard_rt_init(void);
184 extern void lttng_ring_buffer_metadata_client_init(void);
185 extern void lttng_ring_buffer_client_overwrite_exit(void);
186 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
187 extern void lttng_ring_buffer_client_discard_exit(void);
188 extern void lttng_ring_buffer_client_discard_rt_exit(void);
189 extern void lttng_ring_buffer_metadata_client_exit(void);
192 * Force a read (imply TLS fixup for dlopen) of TLS variables.
195 void lttng_fixup_nest_count_tls(void)
197 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
200 int lttng_get_notify_socket(void *owner
)
202 struct sock_info
*info
= owner
;
204 return info
->notify_socket
;
208 void print_cmd(int cmd
, int handle
)
210 const char *cmd_name
= "Unknown";
212 if (cmd_name_mapping
[cmd
]) {
213 cmd_name
= cmd_name_mapping
[cmd
];
215 DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name
,
216 lttng_ust_obj_get_name(handle
), handle
);
220 int setup_local_apps(void)
222 const char *home_dir
;
227 * Disallow per-user tracing for setuid binaries.
229 if (uid
!= geteuid()) {
230 assert(local_apps
.allowed
== 0);
233 home_dir
= (const char *) getenv("HOME");
235 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
236 assert(local_apps
.allowed
== 0);
239 local_apps
.allowed
= 1;
240 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
242 LTTNG_DEFAULT_HOME_RUNDIR
,
243 LTTNG_UST_SOCK_FILENAME
);
244 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
245 LTTNG_UST_WAIT_FILENAME
,
251 * Get notify_sock timeout, in ms.
252 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
255 long get_timeout(void)
257 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
259 if (!got_timeout_env
) {
260 str_timeout
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
264 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
265 return constructor_delay_ms
;
269 long get_notify_sock_timeout(void)
271 return get_timeout();
275 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
278 int get_constructor_timeout(struct timespec
*constructor_timeout
)
280 long constructor_delay_ms
;
283 constructor_delay_ms
= get_timeout();
285 switch (constructor_delay_ms
) {
286 case -1:/* fall-through */
288 return constructor_delay_ms
;
294 * If we are unable to find the current time, don't wait.
296 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
300 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
301 constructor_timeout
->tv_nsec
+=
302 (constructor_delay_ms
% 1000UL) * 1000000UL;
303 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
304 constructor_timeout
->tv_sec
++;
305 constructor_timeout
->tv_nsec
-= 1000000000UL;
311 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
313 return ustcomm_send_reg_msg(socket
,
316 lttng_alignof(uint8_t) * CHAR_BIT
,
317 lttng_alignof(uint16_t) * CHAR_BIT
,
318 lttng_alignof(uint32_t) * CHAR_BIT
,
319 lttng_alignof(uint64_t) * CHAR_BIT
,
320 lttng_alignof(unsigned long) * CHAR_BIT
);
324 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
328 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
331 DBG("message successfully sent");
334 if (len
== -ECONNRESET
) {
335 DBG("remote end closed connection");
340 DBG("incorrect message size: %zd", len
);
346 int handle_register_done(struct sock_info
*sock_info
)
350 if (sock_info
->constructor_sem_posted
)
352 sock_info
->constructor_sem_posted
= 1;
353 if (uatomic_read(&sem_count
) <= 0) {
356 ret
= uatomic_add_return(&sem_count
, -1);
358 ret
= sem_post(&constructor_wait
);
365 int handle_message(struct sock_info
*sock_info
,
366 int sock
, struct ustcomm_ust_msg
*lum
)
369 const struct lttng_ust_objd_ops
*ops
;
370 struct ustcomm_ust_reply lur
;
376 memset(&lur
, 0, sizeof(lur
));
378 if (lttng_ust_comm_should_quit
) {
379 ret
= -LTTNG_UST_ERR_EXITING
;
383 ops
= objd_ops(lum
->handle
);
390 case LTTNG_UST_REGISTER_DONE
:
391 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
392 ret
= handle_register_done(sock_info
);
396 case LTTNG_UST_RELEASE
:
397 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
400 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
402 case LTTNG_UST_FILTER
:
404 /* Receive filter data */
405 struct lttng_ust_filter_bytecode_node
*bytecode
;
407 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
408 ERR("Filter data size is too large: %u bytes",
409 lum
->u
.filter
.data_size
);
414 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
415 ERR("Filter reloc offset %u is not within data",
416 lum
->u
.filter
.reloc_offset
);
421 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
426 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
427 lum
->u
.filter
.data_size
);
429 case 0: /* orderly shutdown */
434 if (len
== lum
->u
.filter
.data_size
) {
435 DBG("filter data received");
437 } else if (len
< 0) {
438 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
439 if (len
== -ECONNRESET
) {
440 ERR("%s remote end closed connection", sock_info
->name
);
448 DBG("incorrect filter data message size: %zd", len
);
454 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
455 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
456 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
458 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
459 (unsigned long) bytecode
,
464 /* don't free bytecode if everything went fine. */
471 case LTTNG_UST_CHANNEL
:
476 len
= ustcomm_recv_channel_from_sessiond(sock
,
477 &chan_data
, lum
->u
.channel
.len
,
480 case 0: /* orderly shutdown */
484 if (len
== lum
->u
.channel
.len
) {
485 DBG("channel data received");
487 } else if (len
< 0) {
488 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
489 if (len
== -ECONNRESET
) {
490 ERR("%s remote end closed connection", sock_info
->name
);
497 DBG("incorrect channel data message size: %zd", len
);
502 args
.channel
.chan_data
= chan_data
;
503 args
.channel
.wakeup_fd
= wakeup_fd
;
505 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
506 (unsigned long) &lum
->u
,
512 case LTTNG_UST_STREAM
:
514 /* Receive shm_fd, wakeup_fd */
515 ret
= ustcomm_recv_stream_from_sessiond(sock
,
518 &args
.stream
.wakeup_fd
);
523 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
524 (unsigned long) &lum
->u
,
532 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
533 (unsigned long) &lum
->u
,
541 lur
.handle
= lum
->handle
;
545 lur
.ret_code
= LTTNG_UST_OK
;
548 * Use -LTTNG_UST_ERR as wildcard for UST internal
549 * error that are not caused by the transport, except if
550 * we already have a more precise error message to
553 if (ret
> -LTTNG_UST_ERR
) {
554 /* Translate code to UST error. */
557 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
560 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
563 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
566 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
569 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
572 lur
.ret_code
= -LTTNG_UST_ERR
;
581 case LTTNG_UST_TRACER_VERSION
:
582 lur
.u
.version
= lum
->u
.version
;
584 case LTTNG_UST_TRACEPOINT_LIST_GET
:
585 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
589 DBG("Return value: %d", lur
.ret_val
);
590 ret
= send_reply(sock
, &lur
);
592 DBG("error sending reply");
597 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
600 if (lur
.ret_code
== LTTNG_UST_OK
) {
602 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
603 len
= ustcomm_send_unix_sock(sock
,
604 &args
.field_list
.entry
,
605 sizeof(args
.field_list
.entry
));
610 if (len
!= sizeof(args
.field_list
.entry
)) {
623 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
627 if (sock_info
->socket
!= -1) {
628 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
630 ERR("Error closing ust cmd socket");
632 sock_info
->socket
= -1;
634 if (sock_info
->notify_socket
!= -1) {
635 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
637 ERR("Error closing ust notify socket");
639 sock_info
->notify_socket
= -1;
641 if (sock_info
->root_handle
!= -1) {
642 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
644 ERR("Error unref root handle");
646 sock_info
->root_handle
= -1;
648 sock_info
->constructor_sem_posted
= 0;
650 * wait_shm_mmap is used by listener threads outside of the
651 * ust lock, so we cannot tear it down ourselves, because we
652 * cannot join on these threads. Leave this task to the OS
655 if (!exiting
&& sock_info
->wait_shm_mmap
) {
656 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
658 ERR("Error unmapping wait shm");
660 sock_info
->wait_shm_mmap
= NULL
;
665 * Using fork to set umask in the child process (not multi-thread safe).
666 * We deal with the shm_open vs ftruncate race (happening when the
667 * sessiond owns the shm and does not let everybody modify it, to ensure
668 * safety against shm_unlink) by simply letting the mmap fail and
669 * retrying after a few seconds.
670 * For global shm, everybody has rw access to it until the sessiond
674 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
676 int wait_shm_fd
, ret
;
680 * Try to open read-only.
682 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
683 if (wait_shm_fd
>= 0) {
685 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
687 * Real-only open did not work, and it's not because the
688 * entry was not present. It's a failure that prohibits
691 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
695 * If the open failed because the file did not exist, try
696 * creating it ourself.
698 URCU_TLS(lttng_ust_nest_count
)++;
700 URCU_TLS(lttng_ust_nest_count
)--;
705 * Parent: wait for child to return, in which case the
706 * shared memory map will have been created.
709 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
714 * Try to open read-only again after creation.
716 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
717 if (wait_shm_fd
< 0) {
719 * Real-only open did not work. It's a failure
720 * that prohibits using shm.
722 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
726 } else if (pid
== 0) {
730 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
731 if (sock_info
->global
)
732 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
734 * We're alone in a child process, so we can modify the
735 * process-wide umask.
739 * Try creating shm (or get rw access).
740 * We don't do an exclusive open, because we allow other
741 * processes to create+ftruncate it concurrently.
743 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
744 O_RDWR
| O_CREAT
, create_mode
);
745 if (wait_shm_fd
>= 0) {
746 ret
= ftruncate(wait_shm_fd
, mmap_size
);
754 * For local shm, we need to have rw access to accept
755 * opening it: this means the local sessiond will be
756 * able to wake us up. For global shm, we open it even
757 * if rw access is not granted, because the root.root
758 * sessiond will be able to override all rights and wake
761 if (!sock_info
->global
&& errno
!= EACCES
) {
762 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
766 * The shm exists, but we cannot open it RW. Report
774 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
778 * Ensure that our user is the owner of the shm file for
779 * local shm. If we do not own the file, it means our
780 * sessiond will not have access to wake us up (there is
781 * probably a rogue process trying to fake our
782 * sessiond). Fallback to polling method in this case.
784 ret
= fstat(wait_shm_fd
, &statbuf
);
789 if (statbuf
.st_uid
!= getuid())
795 ret
= close(wait_shm_fd
);
797 PERROR("Error closing fd");
803 char *get_map_shm(struct sock_info
*sock_info
)
805 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
806 int wait_shm_fd
, ret
;
809 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
810 if (wait_shm_fd
< 0) {
813 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
814 MAP_SHARED
, wait_shm_fd
, 0);
815 /* close shm fd immediately after taking the mmap reference */
816 ret
= close(wait_shm_fd
);
818 PERROR("Error closing fd");
820 if (wait_shm_mmap
== MAP_FAILED
) {
821 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
824 return wait_shm_mmap
;
831 void wait_for_sessiond(struct sock_info
*sock_info
)
836 if (lttng_ust_comm_should_quit
) {
839 if (wait_poll_fallback
) {
842 if (!sock_info
->wait_shm_mmap
) {
843 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
844 if (!sock_info
->wait_shm_mmap
)
849 DBG("Waiting for %s apps sessiond", sock_info
->name
);
850 /* Wait for futex wakeup */
851 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
852 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
853 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
855 if (errno
== EFAULT
) {
856 wait_poll_fallback
= 1;
858 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
859 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
860 "Please upgrade your kernel "
861 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
862 "mainline). LTTng-UST will use polling mode fallback.");
880 * This thread does not allocate any resource, except within
881 * handle_message, within mutex protection. This mutex protects against
883 * The other moment it allocates resources is at socket connection, which
884 * is also protected by the mutex.
887 void *ust_listener_thread(void *arg
)
889 struct sock_info
*sock_info
= arg
;
890 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
895 /* Restart trying to connect to the session daemon */
897 if (prev_connect_failed
) {
898 /* Wait for sessiond availability with pipe */
899 wait_for_sessiond(sock_info
);
903 * Sleep for 5 seconds before retrying after a
904 * sequence of failure / wait / failure. This
905 * deals with a killed or broken session daemon.
910 prev_connect_failed
= 0;
914 if (lttng_ust_comm_should_quit
) {
918 if (sock_info
->socket
!= -1) {
919 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
921 ERR("Error closing %s ust cmd socket",
924 sock_info
->socket
= -1;
926 if (sock_info
->notify_socket
!= -1) {
927 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
929 ERR("Error closing %s ust notify socket",
932 sock_info
->notify_socket
= -1;
936 for (i
= 0; i
< 2; i
++) {
937 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
939 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
940 prev_connect_failed
= 1;
942 * If we cannot find the sessiond daemon, don't delay
943 * constructor execution.
945 ret
= handle_register_done(sock_info
);
953 sock_info
->socket
= open_sock
[0];
954 sock_info
->notify_socket
= open_sock
[1];
956 timeout
= get_notify_sock_timeout();
959 * Give at least 10ms to sessiond to reply to
964 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
967 WARN("Error setting socket receive timeout");
969 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
972 WARN("Error setting socket send timeout");
974 } else if (timeout
< -1) {
975 WARN("Unsuppoorted timeout value %ld", timeout
);
979 * Create only one root handle per listener thread for the whole
980 * process lifetime, so we ensure we get ID which is statically
981 * assigned to the root handle.
983 if (sock_info
->root_handle
== -1) {
984 ret
= lttng_abi_create_root_handle();
986 ERR("Error creating root handle");
989 sock_info
->root_handle
= ret
;
992 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
994 ERR("Error registering to %s ust cmd socket",
996 prev_connect_failed
= 1;
998 * If we cannot register to the sessiond daemon, don't
999 * delay constructor execution.
1001 ret
= handle_register_done(sock_info
);
1006 ret
= register_to_sessiond(sock_info
->notify_socket
,
1007 USTCTL_SOCKET_NOTIFY
);
1009 ERR("Error registering to %s ust notify socket",
1011 prev_connect_failed
= 1;
1013 * If we cannot register to the sessiond daemon, don't
1014 * delay constructor execution.
1016 ret
= handle_register_done(sock_info
);
1021 sock
= sock_info
->socket
;
1027 struct ustcomm_ust_msg lum
;
1029 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1031 case 0: /* orderly shutdown */
1032 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1034 if (lttng_ust_comm_should_quit
) {
1038 * Either sessiond has shutdown or refused us by closing the socket.
1039 * In either case, we don't want to delay construction execution,
1040 * and we need to wait before retry.
1042 prev_connect_failed
= 1;
1044 * If we cannot register to the sessiond daemon, don't
1045 * delay constructor execution.
1047 ret
= handle_register_done(sock_info
);
1052 print_cmd(lum
.cmd
, lum
.handle
);
1053 ret
= handle_message(sock_info
, sock
, &lum
);
1055 ERR("Error handling message for %s socket", sock_info
->name
);
1060 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1062 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1064 if (len
== -ECONNRESET
) {
1065 DBG("%s remote end closed connection", sock_info
->name
);
1074 if (lttng_ust_comm_should_quit
) {
1077 /* Cleanup socket handles before trying to reconnect */
1078 lttng_ust_objd_table_owner_cleanup(sock_info
);
1080 goto restart
; /* try to reconnect */
1083 sock_info
->thread_active
= 0;
1089 * sessiond monitoring thread: monitor presence of global and per-user
1090 * sessiond by polling the application common named pipe.
1092 void __attribute__((constructor
)) lttng_ust_init(void)
1094 struct timespec constructor_timeout
;
1095 sigset_t sig_all_blocked
, orig_parent_mask
;
1096 pthread_attr_t thread_attr
;
1100 if (uatomic_xchg(&initialized
, 1) == 1)
1104 * Fixup interdependency between TLS fixup mutex (which happens
1105 * to be the dynamic linker mutex) and ust_lock, taken within
1108 lttng_fixup_ringbuffer_tls();
1109 lttng_fixup_vtid_tls();
1110 lttng_fixup_nest_count_tls();
1111 lttng_fixup_procname_tls();
1114 * We want precise control over the order in which we construct
1115 * our sub-libraries vs starting to receive commands from
1116 * sessiond (otherwise leading to errors when trying to create
1117 * sessiond before the init functions are completed).
1121 lttng_ring_buffer_metadata_client_init();
1122 lttng_ring_buffer_client_overwrite_init();
1123 lttng_ring_buffer_client_overwrite_rt_init();
1124 lttng_ring_buffer_client_discard_init();
1125 lttng_ring_buffer_client_discard_rt_init();
1126 lttng_context_init();
1128 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1130 ret
= sem_init(&constructor_wait
, 0, 0);
1133 ret
= setup_local_apps();
1135 DBG("local apps setup returned %d", ret
);
1138 /* A new thread created by pthread_create inherits the signal mask
1139 * from the parent. To avoid any signal being received by the
1140 * listener thread, we block all signals temporarily in the parent,
1141 * while we create the listener thread.
1143 sigfillset(&sig_all_blocked
);
1144 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1146 ERR("pthread_sigmask: %s", strerror(ret
));
1149 ret
= pthread_attr_init(&thread_attr
);
1151 ERR("pthread_attr_init: %s", strerror(ret
));
1153 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1155 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1159 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1160 ust_listener_thread
, &global_apps
);
1162 ERR("pthread_create global: %s", strerror(ret
));
1164 global_apps
.thread_active
= 1;
1167 if (local_apps
.allowed
) {
1169 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1170 ust_listener_thread
, &local_apps
);
1172 ERR("pthread_create local: %s", strerror(ret
));
1174 local_apps
.thread_active
= 1;
1177 handle_register_done(&local_apps
);
1179 ret
= pthread_attr_destroy(&thread_attr
);
1181 ERR("pthread_attr_destroy: %s", strerror(ret
));
1184 /* Restore original signal mask in parent */
1185 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1187 ERR("pthread_sigmask: %s", strerror(ret
));
1190 switch (timeout_mode
) {
1191 case 1: /* timeout wait */
1193 ret
= sem_timedwait(&constructor_wait
,
1194 &constructor_timeout
);
1195 } while (ret
< 0 && errno
== EINTR
);
1196 if (ret
< 0 && errno
== ETIMEDOUT
) {
1197 ERR("Timed out waiting for lttng-sessiond");
1202 case -1:/* wait forever */
1204 ret
= sem_wait(&constructor_wait
);
1205 } while (ret
< 0 && errno
== EINTR
);
1208 case 0: /* no timeout */
1214 void lttng_ust_cleanup(int exiting
)
1216 cleanup_sock_info(&global_apps
, exiting
);
1217 if (local_apps
.allowed
) {
1218 cleanup_sock_info(&local_apps
, exiting
);
1221 * The teardown in this function all affect data structures
1222 * accessed under the UST lock by the listener thread. This
1223 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1224 * that none of these threads are accessing this data at this
1227 lttng_ust_abi_exit();
1228 lttng_ust_events_exit();
1229 lttng_context_exit();
1230 lttng_ring_buffer_client_discard_rt_exit();
1231 lttng_ring_buffer_client_discard_exit();
1232 lttng_ring_buffer_client_overwrite_rt_exit();
1233 lttng_ring_buffer_client_overwrite_exit();
1234 lttng_ring_buffer_metadata_client_exit();
1237 /* Reinitialize values for fork */
1239 lttng_ust_comm_should_quit
= 0;
1244 void __attribute__((destructor
)) lttng_ust_exit(void)
1249 * Using pthread_cancel here because:
1250 * A) we don't want to hang application teardown.
1251 * B) the thread is not allocating any resource.
1255 * Require the communication thread to quit. Synchronize with
1256 * mutexes to ensure it is not in a mutex critical section when
1257 * pthread_cancel is later called.
1260 lttng_ust_comm_should_quit
= 1;
1262 /* cancel threads */
1263 if (global_apps
.thread_active
) {
1264 ret
= pthread_cancel(global_apps
.ust_listener
);
1266 ERR("Error cancelling global ust listener thread: %s",
1269 global_apps
.thread_active
= 0;
1272 if (local_apps
.thread_active
) {
1273 ret
= pthread_cancel(local_apps
.ust_listener
);
1275 ERR("Error cancelling local ust listener thread: %s",
1278 local_apps
.thread_active
= 0;
1284 * Do NOT join threads: use of sys_futex makes it impossible to
1285 * join the threads without using async-cancel, but async-cancel
1286 * is delivered by a signal, which could hit the target thread
1287 * anywhere in its code path, including while the ust_lock() is
1288 * held, causing a deadlock for the other thread. Let the OS
1289 * cleanup the threads if there are stalled in a syscall.
1291 lttng_ust_cleanup(1);
1295 * We exclude the worker threads across fork and clone (except
1296 * CLONE_VM), because these system calls only keep the forking thread
1297 * running in the child. Therefore, we don't want to call fork or clone
1298 * in the middle of an tracepoint or ust tracing state modification.
1299 * Holding this mutex protects these structures across fork and clone.
1301 void ust_before_fork(sigset_t
*save_sigset
)
1304 * Disable signals. This is to avoid that the child intervenes
1305 * before it is properly setup for tracing. It is safer to
1306 * disable all signals, because then we know we are not breaking
1307 * anything by restoring the original mask.
1312 if (URCU_TLS(lttng_ust_nest_count
))
1314 /* Disable signals */
1315 sigfillset(&all_sigs
);
1316 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1318 PERROR("sigprocmask");
1321 rcu_bp_before_fork();
1324 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1328 DBG("process %d", getpid());
1330 /* Restore signals */
1331 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1333 PERROR("sigprocmask");
1337 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1339 if (URCU_TLS(lttng_ust_nest_count
))
1341 DBG("process %d", getpid());
1342 rcu_bp_after_fork_parent();
1343 /* Release mutexes and reenable signals */
1344 ust_after_fork_common(restore_sigset
);
1348 * After fork, in the child, we need to cleanup all the leftover state,
1349 * except the worker thread which already magically disappeared thanks
1350 * to the weird Linux fork semantics. After tyding up, we call
1351 * lttng_ust_init() again to start over as a new PID.
1353 * This is meant for forks() that have tracing in the child between the
1354 * fork and following exec call (if there is any).
1356 void ust_after_fork_child(sigset_t
*restore_sigset
)
1358 if (URCU_TLS(lttng_ust_nest_count
))
1360 DBG("process %d", getpid());
1361 /* Release urcu mutexes */
1362 rcu_bp_after_fork_child();
1363 lttng_ust_cleanup(0);
1364 lttng_context_vtid_reset();
1365 /* Release mutexes and reenable signals */
1366 ust_after_fork_common(restore_sigset
);