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_discard_init(void);
182 extern void lttng_ring_buffer_metadata_client_init(void);
183 extern void lttng_ring_buffer_client_overwrite_exit(void);
184 extern void lttng_ring_buffer_client_discard_exit(void);
185 extern void lttng_ring_buffer_metadata_client_exit(void);
188 * Force a read (imply TLS fixup for dlopen) of TLS variables.
191 void lttng_fixup_nest_count_tls(void)
193 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
196 int lttng_get_notify_socket(void *owner
)
198 struct sock_info
*info
= owner
;
200 return info
->notify_socket
;
204 void print_cmd(int cmd
, int handle
)
206 const char *cmd_name
= "Unknown";
208 if (cmd_name_mapping
[cmd
]) {
209 cmd_name
= cmd_name_mapping
[cmd
];
211 DBG("Message Received \"%s\", Handle \"%s\" (%d)", cmd_name
,
212 lttng_ust_obj_get_name(handle
), handle
);
216 int setup_local_apps(void)
218 const char *home_dir
;
223 * Disallow per-user tracing for setuid binaries.
225 if (uid
!= geteuid()) {
226 assert(local_apps
.allowed
== 0);
229 home_dir
= (const char *) getenv("HOME");
231 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
232 assert(local_apps
.allowed
== 0);
235 local_apps
.allowed
= 1;
236 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
238 LTTNG_DEFAULT_HOME_RUNDIR
,
239 LTTNG_UST_SOCK_FILENAME
);
240 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
241 LTTNG_UST_WAIT_FILENAME
,
247 * Get notify_sock timeout, in ms.
248 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
251 long get_timeout(void)
253 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
255 if (!got_timeout_env
) {
256 str_timeout
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
260 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
261 return constructor_delay_ms
;
265 long get_notify_sock_timeout(void)
267 return get_timeout();
271 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
274 int get_constructor_timeout(struct timespec
*constructor_timeout
)
276 long constructor_delay_ms
;
279 constructor_delay_ms
= get_timeout();
281 switch (constructor_delay_ms
) {
282 case -1:/* fall-through */
284 return constructor_delay_ms
;
290 * If we are unable to find the current time, don't wait.
292 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
296 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
297 constructor_timeout
->tv_nsec
+=
298 (constructor_delay_ms
% 1000UL) * 1000000UL;
299 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
300 constructor_timeout
->tv_sec
++;
301 constructor_timeout
->tv_nsec
-= 1000000000UL;
307 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
309 return ustcomm_send_reg_msg(socket
,
312 lttng_alignof(uint8_t) * CHAR_BIT
,
313 lttng_alignof(uint16_t) * CHAR_BIT
,
314 lttng_alignof(uint32_t) * CHAR_BIT
,
315 lttng_alignof(uint64_t) * CHAR_BIT
,
316 lttng_alignof(unsigned long) * CHAR_BIT
);
320 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
324 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
327 DBG("message successfully sent");
330 if (len
== -ECONNRESET
) {
331 DBG("remote end closed connection");
336 DBG("incorrect message size: %zd", len
);
342 int handle_register_done(struct sock_info
*sock_info
)
346 if (sock_info
->constructor_sem_posted
)
348 sock_info
->constructor_sem_posted
= 1;
349 if (uatomic_read(&sem_count
) <= 0) {
352 ret
= uatomic_add_return(&sem_count
, -1);
354 ret
= sem_post(&constructor_wait
);
361 int handle_message(struct sock_info
*sock_info
,
362 int sock
, struct ustcomm_ust_msg
*lum
)
365 const struct lttng_ust_objd_ops
*ops
;
366 struct ustcomm_ust_reply lur
;
372 memset(&lur
, 0, sizeof(lur
));
374 if (lttng_ust_comm_should_quit
) {
375 ret
= -LTTNG_UST_ERR_EXITING
;
379 ops
= objd_ops(lum
->handle
);
386 case LTTNG_UST_REGISTER_DONE
:
387 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
388 ret
= handle_register_done(sock_info
);
392 case LTTNG_UST_RELEASE
:
393 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
396 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
398 case LTTNG_UST_FILTER
:
400 /* Receive filter data */
401 struct lttng_ust_filter_bytecode_node
*bytecode
;
403 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
404 ERR("Filter data size is too large: %u bytes",
405 lum
->u
.filter
.data_size
);
410 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
411 ERR("Filter reloc offset %u is not within data",
412 lum
->u
.filter
.reloc_offset
);
417 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
422 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
423 lum
->u
.filter
.data_size
);
425 case 0: /* orderly shutdown */
430 if (len
== lum
->u
.filter
.data_size
) {
431 DBG("filter data received");
433 } else if (len
< 0) {
434 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
435 if (len
== -ECONNRESET
) {
436 ERR("%s remote end closed connection", sock_info
->name
);
444 DBG("incorrect filter data message size: %zd", len
);
450 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
451 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
452 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
454 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
455 (unsigned long) bytecode
,
460 /* don't free bytecode if everything went fine. */
467 case LTTNG_UST_CHANNEL
:
472 len
= ustcomm_recv_channel_from_sessiond(sock
,
473 &chan_data
, lum
->u
.channel
.len
,
476 case 0: /* orderly shutdown */
480 if (len
== lum
->u
.channel
.len
) {
481 DBG("channel data received");
483 } else if (len
< 0) {
484 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
485 if (len
== -ECONNRESET
) {
486 ERR("%s remote end closed connection", sock_info
->name
);
493 DBG("incorrect channel data message size: %zd", len
);
498 args
.channel
.chan_data
= chan_data
;
499 args
.channel
.wakeup_fd
= wakeup_fd
;
501 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
502 (unsigned long) &lum
->u
,
508 case LTTNG_UST_STREAM
:
510 /* Receive shm_fd, wakeup_fd */
511 ret
= ustcomm_recv_stream_from_sessiond(sock
,
514 &args
.stream
.wakeup_fd
);
519 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
520 (unsigned long) &lum
->u
,
528 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
529 (unsigned long) &lum
->u
,
537 lur
.handle
= lum
->handle
;
541 lur
.ret_code
= LTTNG_UST_OK
;
544 * Use -LTTNG_UST_ERR as wildcard for UST internal
545 * error that are not caused by the transport, except if
546 * we already have a more precise error message to
549 if (ret
> -LTTNG_UST_ERR
) {
550 /* Translate code to UST error. */
553 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
556 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
559 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
562 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
565 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
568 lur
.ret_code
= -LTTNG_UST_ERR
;
577 case LTTNG_UST_TRACER_VERSION
:
578 lur
.u
.version
= lum
->u
.version
;
580 case LTTNG_UST_TRACEPOINT_LIST_GET
:
581 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
585 DBG("Return value: %d", lur
.ret_val
);
586 ret
= send_reply(sock
, &lur
);
588 DBG("error sending reply");
593 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
596 if (lur
.ret_code
== LTTNG_UST_OK
) {
598 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
599 len
= ustcomm_send_unix_sock(sock
,
600 &args
.field_list
.entry
,
601 sizeof(args
.field_list
.entry
));
606 if (len
!= sizeof(args
.field_list
.entry
)) {
619 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
623 if (sock_info
->socket
!= -1) {
624 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
626 ERR("Error closing ust cmd socket");
628 sock_info
->socket
= -1;
630 if (sock_info
->notify_socket
!= -1) {
631 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
633 ERR("Error closing ust notify socket");
635 sock_info
->notify_socket
= -1;
637 if (sock_info
->root_handle
!= -1) {
638 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
640 ERR("Error unref root handle");
642 sock_info
->root_handle
= -1;
644 sock_info
->constructor_sem_posted
= 0;
646 * wait_shm_mmap is used by listener threads outside of the
647 * ust lock, so we cannot tear it down ourselves, because we
648 * cannot join on these threads. Leave this task to the OS
651 if (!exiting
&& sock_info
->wait_shm_mmap
) {
652 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
654 ERR("Error unmapping wait shm");
656 sock_info
->wait_shm_mmap
= NULL
;
661 * Using fork to set umask in the child process (not multi-thread safe).
662 * We deal with the shm_open vs ftruncate race (happening when the
663 * sessiond owns the shm and does not let everybody modify it, to ensure
664 * safety against shm_unlink) by simply letting the mmap fail and
665 * retrying after a few seconds.
666 * For global shm, everybody has rw access to it until the sessiond
670 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
672 int wait_shm_fd
, ret
;
676 * Try to open read-only.
678 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
679 if (wait_shm_fd
>= 0) {
681 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
683 * Real-only open did not work, and it's not because the
684 * entry was not present. It's a failure that prohibits
687 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
691 * If the open failed because the file did not exist, try
692 * creating it ourself.
694 URCU_TLS(lttng_ust_nest_count
)++;
696 URCU_TLS(lttng_ust_nest_count
)--;
701 * Parent: wait for child to return, in which case the
702 * shared memory map will have been created.
705 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
710 * Try to open read-only again after creation.
712 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
713 if (wait_shm_fd
< 0) {
715 * Real-only open did not work. It's a failure
716 * that prohibits using shm.
718 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
722 } else if (pid
== 0) {
726 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
727 if (sock_info
->global
)
728 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
730 * We're alone in a child process, so we can modify the
731 * process-wide umask.
735 * Try creating shm (or get rw access).
736 * We don't do an exclusive open, because we allow other
737 * processes to create+ftruncate it concurrently.
739 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
740 O_RDWR
| O_CREAT
, create_mode
);
741 if (wait_shm_fd
>= 0) {
742 ret
= ftruncate(wait_shm_fd
, mmap_size
);
750 * For local shm, we need to have rw access to accept
751 * opening it: this means the local sessiond will be
752 * able to wake us up. For global shm, we open it even
753 * if rw access is not granted, because the root.root
754 * sessiond will be able to override all rights and wake
757 if (!sock_info
->global
&& errno
!= EACCES
) {
758 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
762 * The shm exists, but we cannot open it RW. Report
770 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
774 * Ensure that our user is the owner of the shm file for
775 * local shm. If we do not own the file, it means our
776 * sessiond will not have access to wake us up (there is
777 * probably a rogue process trying to fake our
778 * sessiond). Fallback to polling method in this case.
780 ret
= fstat(wait_shm_fd
, &statbuf
);
785 if (statbuf
.st_uid
!= getuid())
791 ret
= close(wait_shm_fd
);
793 PERROR("Error closing fd");
799 char *get_map_shm(struct sock_info
*sock_info
)
801 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
802 int wait_shm_fd
, ret
;
805 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
806 if (wait_shm_fd
< 0) {
809 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
810 MAP_SHARED
, wait_shm_fd
, 0);
811 /* close shm fd immediately after taking the mmap reference */
812 ret
= close(wait_shm_fd
);
814 PERROR("Error closing fd");
816 if (wait_shm_mmap
== MAP_FAILED
) {
817 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
820 return wait_shm_mmap
;
827 void wait_for_sessiond(struct sock_info
*sock_info
)
832 if (lttng_ust_comm_should_quit
) {
835 if (wait_poll_fallback
) {
838 if (!sock_info
->wait_shm_mmap
) {
839 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
840 if (!sock_info
->wait_shm_mmap
)
845 DBG("Waiting for %s apps sessiond", sock_info
->name
);
846 /* Wait for futex wakeup */
847 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
848 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
849 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
851 if (errno
== EFAULT
) {
852 wait_poll_fallback
= 1;
854 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
855 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
856 "Please upgrade your kernel "
857 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
858 "mainline). LTTng-UST will use polling mode fallback.");
876 * This thread does not allocate any resource, except within
877 * handle_message, within mutex protection. This mutex protects against
879 * The other moment it allocates resources is at socket connection, which
880 * is also protected by the mutex.
883 void *ust_listener_thread(void *arg
)
885 struct sock_info
*sock_info
= arg
;
886 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
891 /* Restart trying to connect to the session daemon */
893 if (prev_connect_failed
) {
894 /* Wait for sessiond availability with pipe */
895 wait_for_sessiond(sock_info
);
899 * Sleep for 5 seconds before retrying after a
900 * sequence of failure / wait / failure. This
901 * deals with a killed or broken session daemon.
906 prev_connect_failed
= 0;
910 if (lttng_ust_comm_should_quit
) {
914 if (sock_info
->socket
!= -1) {
915 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
917 ERR("Error closing %s ust cmd socket",
920 sock_info
->socket
= -1;
922 if (sock_info
->notify_socket
!= -1) {
923 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
925 ERR("Error closing %s ust notify socket",
928 sock_info
->notify_socket
= -1;
932 for (i
= 0; i
< 2; i
++) {
933 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
935 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
936 prev_connect_failed
= 1;
938 * If we cannot find the sessiond daemon, don't delay
939 * constructor execution.
941 ret
= handle_register_done(sock_info
);
949 sock_info
->socket
= open_sock
[0];
950 sock_info
->notify_socket
= open_sock
[1];
952 timeout
= get_notify_sock_timeout();
955 * Give at least 10ms to sessiond to reply to
960 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
963 WARN("Error setting socket receive timeout");
965 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
968 WARN("Error setting socket send timeout");
970 } else if (timeout
< -1) {
971 WARN("Unsuppoorted timeout value %ld", timeout
);
975 * Create only one root handle per listener thread for the whole
976 * process lifetime, so we ensure we get ID which is statically
977 * assigned to the root handle.
979 if (sock_info
->root_handle
== -1) {
980 ret
= lttng_abi_create_root_handle();
982 ERR("Error creating root handle");
985 sock_info
->root_handle
= ret
;
988 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
990 ERR("Error registering to %s ust cmd socket",
992 prev_connect_failed
= 1;
994 * If we cannot register to the sessiond daemon, don't
995 * delay constructor execution.
997 ret
= handle_register_done(sock_info
);
1002 ret
= register_to_sessiond(sock_info
->notify_socket
,
1003 USTCTL_SOCKET_NOTIFY
);
1005 ERR("Error registering to %s ust notify socket",
1007 prev_connect_failed
= 1;
1009 * If we cannot register to the sessiond daemon, don't
1010 * delay constructor execution.
1012 ret
= handle_register_done(sock_info
);
1017 sock
= sock_info
->socket
;
1023 struct ustcomm_ust_msg lum
;
1025 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1027 case 0: /* orderly shutdown */
1028 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1030 if (lttng_ust_comm_should_quit
) {
1034 * Either sessiond has shutdown or refused us by closing the socket.
1035 * In either case, we don't want to delay construction execution,
1036 * and we need to wait before retry.
1038 prev_connect_failed
= 1;
1040 * If we cannot register to the sessiond daemon, don't
1041 * delay constructor execution.
1043 ret
= handle_register_done(sock_info
);
1048 print_cmd(lum
.cmd
, lum
.handle
);
1049 ret
= handle_message(sock_info
, sock
, &lum
);
1051 ERR("Error handling message for %s socket", sock_info
->name
);
1056 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1058 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1060 if (len
== -ECONNRESET
) {
1061 DBG("%s remote end closed connection", sock_info
->name
);
1070 if (lttng_ust_comm_should_quit
) {
1073 /* Cleanup socket handles before trying to reconnect */
1074 lttng_ust_objd_table_owner_cleanup(sock_info
);
1076 goto restart
; /* try to reconnect */
1079 sock_info
->thread_active
= 0;
1085 * sessiond monitoring thread: monitor presence of global and per-user
1086 * sessiond by polling the application common named pipe.
1088 void __attribute__((constructor
)) lttng_ust_init(void)
1090 struct timespec constructor_timeout
;
1091 sigset_t sig_all_blocked
, orig_parent_mask
;
1092 pthread_attr_t thread_attr
;
1096 if (uatomic_xchg(&initialized
, 1) == 1)
1100 * Fixup interdependency between TLS fixup mutex (which happens
1101 * to be the dynamic linker mutex) and ust_lock, taken within
1104 lttng_fixup_ringbuffer_tls();
1105 lttng_fixup_vtid_tls();
1106 lttng_fixup_nest_count_tls();
1107 lttng_fixup_procname_tls();
1110 * We want precise control over the order in which we construct
1111 * our sub-libraries vs starting to receive commands from
1112 * sessiond (otherwise leading to errors when trying to create
1113 * sessiond before the init functions are completed).
1117 lttng_ring_buffer_metadata_client_init();
1118 lttng_ring_buffer_client_overwrite_init();
1119 lttng_ring_buffer_client_discard_init();
1121 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1123 ret
= sem_init(&constructor_wait
, 0, 0);
1126 ret
= setup_local_apps();
1128 DBG("local apps setup returned %d", ret
);
1131 /* A new thread created by pthread_create inherits the signal mask
1132 * from the parent. To avoid any signal being received by the
1133 * listener thread, we block all signals temporarily in the parent,
1134 * while we create the listener thread.
1136 sigfillset(&sig_all_blocked
);
1137 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1139 ERR("pthread_sigmask: %s", strerror(ret
));
1142 ret
= pthread_attr_init(&thread_attr
);
1144 ERR("pthread_attr_init: %s", strerror(ret
));
1146 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1148 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1152 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1153 ust_listener_thread
, &global_apps
);
1155 ERR("pthread_create global: %s", strerror(ret
));
1157 global_apps
.thread_active
= 1;
1160 if (local_apps
.allowed
) {
1162 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1163 ust_listener_thread
, &local_apps
);
1165 ERR("pthread_create local: %s", strerror(ret
));
1167 local_apps
.thread_active
= 1;
1170 handle_register_done(&local_apps
);
1172 ret
= pthread_attr_destroy(&thread_attr
);
1174 ERR("pthread_attr_destroy: %s", strerror(ret
));
1177 /* Restore original signal mask in parent */
1178 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1180 ERR("pthread_sigmask: %s", strerror(ret
));
1183 switch (timeout_mode
) {
1184 case 1: /* timeout wait */
1186 ret
= sem_timedwait(&constructor_wait
,
1187 &constructor_timeout
);
1188 } while (ret
< 0 && errno
== EINTR
);
1189 if (ret
< 0 && errno
== ETIMEDOUT
) {
1190 ERR("Timed out waiting for lttng-sessiond");
1195 case -1:/* wait forever */
1197 ret
= sem_wait(&constructor_wait
);
1198 } while (ret
< 0 && errno
== EINTR
);
1201 case 0: /* no timeout */
1207 void lttng_ust_cleanup(int exiting
)
1209 cleanup_sock_info(&global_apps
, exiting
);
1210 if (local_apps
.allowed
) {
1211 cleanup_sock_info(&local_apps
, exiting
);
1214 * The teardown in this function all affect data structures
1215 * accessed under the UST lock by the listener thread. This
1216 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1217 * that none of these threads are accessing this data at this
1220 lttng_ust_abi_exit();
1221 lttng_ust_events_exit();
1222 lttng_ring_buffer_client_discard_exit();
1223 lttng_ring_buffer_client_overwrite_exit();
1224 lttng_ring_buffer_metadata_client_exit();
1227 /* Reinitialize values for fork */
1229 lttng_ust_comm_should_quit
= 0;
1234 void __attribute__((destructor
)) lttng_ust_exit(void)
1239 * Using pthread_cancel here because:
1240 * A) we don't want to hang application teardown.
1241 * B) the thread is not allocating any resource.
1245 * Require the communication thread to quit. Synchronize with
1246 * mutexes to ensure it is not in a mutex critical section when
1247 * pthread_cancel is later called.
1250 lttng_ust_comm_should_quit
= 1;
1252 /* cancel threads */
1253 if (global_apps
.thread_active
) {
1254 ret
= pthread_cancel(global_apps
.ust_listener
);
1256 ERR("Error cancelling global ust listener thread: %s",
1259 global_apps
.thread_active
= 0;
1262 if (local_apps
.thread_active
) {
1263 ret
= pthread_cancel(local_apps
.ust_listener
);
1265 ERR("Error cancelling local ust listener thread: %s",
1268 local_apps
.thread_active
= 0;
1274 * Do NOT join threads: use of sys_futex makes it impossible to
1275 * join the threads without using async-cancel, but async-cancel
1276 * is delivered by a signal, which could hit the target thread
1277 * anywhere in its code path, including while the ust_lock() is
1278 * held, causing a deadlock for the other thread. Let the OS
1279 * cleanup the threads if there are stalled in a syscall.
1281 lttng_ust_cleanup(1);
1285 * We exclude the worker threads across fork and clone (except
1286 * CLONE_VM), because these system calls only keep the forking thread
1287 * running in the child. Therefore, we don't want to call fork or clone
1288 * in the middle of an tracepoint or ust tracing state modification.
1289 * Holding this mutex protects these structures across fork and clone.
1291 void ust_before_fork(sigset_t
*save_sigset
)
1294 * Disable signals. This is to avoid that the child intervenes
1295 * before it is properly setup for tracing. It is safer to
1296 * disable all signals, because then we know we are not breaking
1297 * anything by restoring the original mask.
1302 if (URCU_TLS(lttng_ust_nest_count
))
1304 /* Disable signals */
1305 sigfillset(&all_sigs
);
1306 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1308 PERROR("sigprocmask");
1311 rcu_bp_before_fork();
1314 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1318 DBG("process %d", getpid());
1320 /* Restore signals */
1321 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1323 PERROR("sigprocmask");
1327 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1329 if (URCU_TLS(lttng_ust_nest_count
))
1331 DBG("process %d", getpid());
1332 rcu_bp_after_fork_parent();
1333 /* Release mutexes and reenable signals */
1334 ust_after_fork_common(restore_sigset
);
1338 * After fork, in the child, we need to cleanup all the leftover state,
1339 * except the worker thread which already magically disappeared thanks
1340 * to the weird Linux fork semantics. After tyding up, we call
1341 * lttng_ust_init() again to start over as a new PID.
1343 * This is meant for forks() that have tracing in the child between the
1344 * fork and following exec call (if there is any).
1346 void ust_after_fork_child(sigset_t
*restore_sigset
)
1348 if (URCU_TLS(lttng_ust_nest_count
))
1350 DBG("process %d", getpid());
1351 /* Release urcu mutexes */
1352 rcu_bp_after_fork_child();
1353 lttng_ust_cleanup(0);
1354 lttng_context_vtid_reset();
1355 /* Release mutexes and reenable signals */
1356 ust_after_fork_common(restore_sigset
);