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>
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>
49 #include <usterr-signal-safe.h>
51 #include "tracepoint-internal.h"
52 #include "lttng-tracer-core.h"
54 #include "../libringbuffer/tlsfixup.h"
57 * Has lttng ust comm constructor been called ?
59 static int initialized
;
62 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
63 * Held when handling a command, also held by fork() to deal with
64 * removal of threads, and by exit path.
67 /* Should the ust comm thread quit ? */
68 static int lttng_ust_comm_should_quit
;
71 * Wait for either of these before continuing to the main
73 * - the register_done message from sessiond daemon
74 * (will let the sessiond daemon enable sessions before main
76 * - sessiond daemon is not reachable.
77 * - timeout (ensuring applications are resilient to session
80 static sem_t constructor_wait
;
82 * Doing this for both the global and local sessiond.
84 static int sem_count
= { 2 };
87 * Counting nesting within lttng-ust. Used to ensure that calling fork()
88 * from liblttng-ust does not execute the pre/post fork handlers.
90 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
93 * Info about socket and associated listener thread.
97 pthread_t ust_listener
; /* listener thread */
99 int constructor_sem_posted
;
104 char sock_path
[PATH_MAX
];
108 char wait_shm_path
[PATH_MAX
];
110 struct lttng_session
*session_enabled
;
113 /* Socket from app (connect) to session daemon (listen) for communication */
114 struct sock_info global_apps
= {
122 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
126 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
128 .session_enabled
= NULL
,
131 /* TODO: allow global_apps_sock_path override */
133 struct sock_info local_apps
= {
137 .allowed
= 0, /* Check setuid bit first */
143 .session_enabled
= NULL
,
146 static int wait_poll_fallback
;
148 static const char *cmd_name_mapping
[] = {
149 [ LTTNG_UST_RELEASE
] = "Release",
150 [ LTTNG_UST_SESSION
] = "Create Session",
151 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
153 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
154 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
155 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
156 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
158 /* Session FD commands */
159 [ LTTNG_UST_CHANNEL
] = "Create Channel",
160 [ LTTNG_UST_SESSION_START
] = "Start Session",
161 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
163 /* Channel FD commands */
164 [ LTTNG_UST_STREAM
] = "Create Stream",
165 [ LTTNG_UST_EVENT
] = "Create Event",
167 /* Event and Channel FD commands */
168 [ LTTNG_UST_CONTEXT
] = "Create Context",
169 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
171 /* Event, Channel and Session commands */
172 [ LTTNG_UST_ENABLE
] = "Enable",
173 [ LTTNG_UST_DISABLE
] = "Disable",
175 /* Tracepoint list commands */
176 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
177 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
179 /* Event FD commands */
180 [ LTTNG_UST_FILTER
] = "Create Filter",
181 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
184 static const char *str_timeout
;
185 static int got_timeout_env
;
186 static void *ust_baddr_handle
;
188 extern void lttng_ring_buffer_client_overwrite_init(void);
189 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
190 extern void lttng_ring_buffer_client_discard_init(void);
191 extern void lttng_ring_buffer_client_discard_rt_init(void);
192 extern void lttng_ring_buffer_metadata_client_init(void);
193 extern void lttng_ring_buffer_client_overwrite_exit(void);
194 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
195 extern void lttng_ring_buffer_client_discard_exit(void);
196 extern void lttng_ring_buffer_client_discard_rt_exit(void);
197 extern void lttng_ring_buffer_metadata_client_exit(void);
200 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
204 const char *get_lttng_home_dir(void)
208 val
= (const char *) getenv("LTTNG_HOME");
212 return (const char *) getenv("HOME");
216 * Force a read (imply TLS fixup for dlopen) of TLS variables.
219 void lttng_fixup_nest_count_tls(void)
221 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
224 int lttng_get_notify_socket(void *owner
)
226 struct sock_info
*info
= owner
;
228 return info
->notify_socket
;
232 void print_cmd(int cmd
, int handle
)
234 const char *cmd_name
= "Unknown";
236 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
237 && cmd_name_mapping
[cmd
]) {
238 cmd_name
= cmd_name_mapping
[cmd
];
240 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
242 lttng_ust_obj_get_name(handle
), handle
);
246 void *lttng_ust_baddr_handle(void)
248 if (!ust_baddr_handle
) {
249 ust_baddr_handle
= dlopen(
250 "liblttng-ust-baddr.so.0", RTLD_NOW
| RTLD_GLOBAL
);
251 if (ust_baddr_handle
== NULL
)
252 ERR("%s", dlerror());
254 return ust_baddr_handle
;
258 int lttng_ust_baddr_statedump(struct lttng_session
*session
)
261 int (*lttng_ust_baddr_init_fn
)(struct lttng_session
*);
263 if (!lttng_ust_baddr_init_fn
) {
264 void *baddr_handle
= lttng_ust_baddr_handle();
266 lttng_ust_baddr_init_fn
= dlsym(baddr_handle
,
267 "lttng_ust_baddr_statedump");
268 if (lttng_ust_baddr_init_fn
== NULL
)
269 ERR("%s", dlerror());
271 if (!lttng_ust_baddr_init_fn
)
275 return lttng_ust_baddr_init_fn(session
);
279 int setup_local_apps(void)
281 const char *home_dir
;
286 * Disallow per-user tracing for setuid binaries.
288 if (uid
!= geteuid()) {
289 assert(local_apps
.allowed
== 0);
292 home_dir
= get_lttng_home_dir();
294 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
295 assert(local_apps
.allowed
== 0);
298 local_apps
.allowed
= 1;
299 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
301 LTTNG_DEFAULT_HOME_RUNDIR
,
302 LTTNG_UST_SOCK_FILENAME
);
303 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
304 LTTNG_UST_WAIT_FILENAME
,
310 * Get notify_sock timeout, in ms.
311 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
314 long get_timeout(void)
316 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
318 if (!got_timeout_env
) {
319 str_timeout
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
323 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
324 return constructor_delay_ms
;
328 long get_notify_sock_timeout(void)
330 return get_timeout();
334 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
337 int get_constructor_timeout(struct timespec
*constructor_timeout
)
339 long constructor_delay_ms
;
342 constructor_delay_ms
= get_timeout();
344 switch (constructor_delay_ms
) {
345 case -1:/* fall-through */
347 return constructor_delay_ms
;
353 * If we are unable to find the current time, don't wait.
355 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
359 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
360 constructor_timeout
->tv_nsec
+=
361 (constructor_delay_ms
% 1000UL) * 1000000UL;
362 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
363 constructor_timeout
->tv_sec
++;
364 constructor_timeout
->tv_nsec
-= 1000000000UL;
370 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
372 return ustcomm_send_reg_msg(socket
,
375 lttng_alignof(uint8_t) * CHAR_BIT
,
376 lttng_alignof(uint16_t) * CHAR_BIT
,
377 lttng_alignof(uint32_t) * CHAR_BIT
,
378 lttng_alignof(uint64_t) * CHAR_BIT
,
379 lttng_alignof(unsigned long) * CHAR_BIT
);
383 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
387 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
390 DBG("message successfully sent");
393 if (len
== -ECONNRESET
) {
394 DBG("remote end closed connection");
399 DBG("incorrect message size: %zd", len
);
405 int handle_register_done(struct sock_info
*sock_info
)
409 if (sock_info
->constructor_sem_posted
)
411 sock_info
->constructor_sem_posted
= 1;
412 if (uatomic_read(&sem_count
) <= 0) {
415 ret
= uatomic_add_return(&sem_count
, -1);
417 ret
= sem_post(&constructor_wait
);
424 int handle_message(struct sock_info
*sock_info
,
425 int sock
, struct ustcomm_ust_msg
*lum
)
428 const struct lttng_ust_objd_ops
*ops
;
429 struct ustcomm_ust_reply lur
;
435 memset(&lur
, 0, sizeof(lur
));
437 if (lttng_ust_comm_should_quit
) {
438 ret
= -LTTNG_UST_ERR_EXITING
;
442 ops
= objd_ops(lum
->handle
);
449 case LTTNG_UST_REGISTER_DONE
:
450 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
451 ret
= handle_register_done(sock_info
);
455 case LTTNG_UST_RELEASE
:
456 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
459 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
461 case LTTNG_UST_FILTER
:
463 /* Receive filter data */
464 struct lttng_ust_filter_bytecode_node
*bytecode
;
466 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
467 ERR("Filter data size is too large: %u bytes",
468 lum
->u
.filter
.data_size
);
473 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
474 ERR("Filter reloc offset %u is not within data",
475 lum
->u
.filter
.reloc_offset
);
480 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
485 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
486 lum
->u
.filter
.data_size
);
488 case 0: /* orderly shutdown */
493 if (len
== lum
->u
.filter
.data_size
) {
494 DBG("filter data received");
496 } else if (len
< 0) {
497 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
498 if (len
== -ECONNRESET
) {
499 ERR("%s remote end closed connection", sock_info
->name
);
508 DBG("incorrect filter data message size: %zd", len
);
514 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
515 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
516 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
518 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
519 (unsigned long) bytecode
,
524 /* don't free bytecode if everything went fine. */
531 case LTTNG_UST_EXCLUSION
:
533 /* Receive exclusion names */
534 struct lttng_ust_excluder_node
*node
;
537 count
= lum
->u
.exclusion
.count
;
539 /* There are no names to read */
543 node
= zmalloc(sizeof(*node
) +
544 count
* LTTNG_UST_SYM_NAME_LEN
);
549 node
->excluder
.count
= count
;
550 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
551 count
* LTTNG_UST_SYM_NAME_LEN
);
553 case 0: /* orderly shutdown */
558 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
559 DBG("Exclusion data received");
561 } else if (len
< 0) {
562 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
563 if (len
== -ECONNRESET
) {
564 ERR("%s remote end closed connection", sock_info
->name
);
573 DBG("Incorrect exclusion data message size: %zd", len
);
580 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
581 (unsigned long) node
,
586 /* Don't free exclusion data if everything went fine. */
593 case LTTNG_UST_CHANNEL
:
598 len
= ustcomm_recv_channel_from_sessiond(sock
,
599 &chan_data
, lum
->u
.channel
.len
,
602 case 0: /* orderly shutdown */
606 if (len
== lum
->u
.channel
.len
) {
607 DBG("channel data received");
609 } else if (len
< 0) {
610 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
611 if (len
== -ECONNRESET
) {
612 ERR("%s remote end closed connection", sock_info
->name
);
619 DBG("incorrect channel data message size: %zd", len
);
624 args
.channel
.chan_data
= chan_data
;
625 args
.channel
.wakeup_fd
= wakeup_fd
;
627 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
628 (unsigned long) &lum
->u
,
634 case LTTNG_UST_STREAM
:
636 /* Receive shm_fd, wakeup_fd */
637 ret
= ustcomm_recv_stream_from_sessiond(sock
,
640 &args
.stream
.wakeup_fd
);
645 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
646 (unsigned long) &lum
->u
,
654 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
655 (unsigned long) &lum
->u
,
663 lur
.handle
= lum
->handle
;
667 lur
.ret_code
= LTTNG_UST_OK
;
670 * Use -LTTNG_UST_ERR as wildcard for UST internal
671 * error that are not caused by the transport, except if
672 * we already have a more precise error message to
675 if (ret
> -LTTNG_UST_ERR
) {
676 /* Translate code to UST error. */
679 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
682 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
685 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
688 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
691 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
694 lur
.ret_code
= -LTTNG_UST_ERR
;
703 case LTTNG_UST_TRACER_VERSION
:
704 lur
.u
.version
= lum
->u
.version
;
706 case LTTNG_UST_TRACEPOINT_LIST_GET
:
707 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
711 DBG("Return value: %d", lur
.ret_val
);
712 ret
= send_reply(sock
, &lur
);
714 DBG("error sending reply");
719 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
722 if (lur
.ret_code
== LTTNG_UST_OK
) {
724 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
725 len
= ustcomm_send_unix_sock(sock
,
726 &args
.field_list
.entry
,
727 sizeof(args
.field_list
.entry
));
732 if (len
!= sizeof(args
.field_list
.entry
)) {
745 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
749 if (sock_info
->root_handle
!= -1) {
750 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
752 ERR("Error unref root handle");
754 sock_info
->root_handle
= -1;
756 sock_info
->constructor_sem_posted
= 0;
759 * wait_shm_mmap, socket and notify socket are used by listener
760 * threads outside of the ust lock, so we cannot tear them down
761 * ourselves, because we cannot join on these threads. Leave
762 * responsibility of cleaning up these resources to the OS
768 if (sock_info
->socket
!= -1) {
769 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
771 ERR("Error closing ust cmd socket");
773 sock_info
->socket
= -1;
775 if (sock_info
->notify_socket
!= -1) {
776 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
778 ERR("Error closing ust notify socket");
780 sock_info
->notify_socket
= -1;
782 if (sock_info
->wait_shm_mmap
) {
783 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
785 ERR("Error unmapping wait shm");
787 sock_info
->wait_shm_mmap
= NULL
;
792 * Using fork to set umask in the child process (not multi-thread safe).
793 * We deal with the shm_open vs ftruncate race (happening when the
794 * sessiond owns the shm and does not let everybody modify it, to ensure
795 * safety against shm_unlink) by simply letting the mmap fail and
796 * retrying after a few seconds.
797 * For global shm, everybody has rw access to it until the sessiond
801 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
803 int wait_shm_fd
, ret
;
807 * Try to open read-only.
809 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
810 if (wait_shm_fd
>= 0) {
813 size_t bytes_read
= 0;
816 * Try to read the fd. If unable to do so, try opening
820 len
= read(wait_shm_fd
,
821 &((char *) &tmp_read
)[bytes_read
],
822 sizeof(tmp_read
) - bytes_read
);
826 } while ((len
< 0 && errno
== EINTR
)
827 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
828 if (bytes_read
!= sizeof(tmp_read
)) {
829 ret
= close(wait_shm_fd
);
831 ERR("close wait_shm_fd");
836 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
838 * Real-only open did not work, and it's not because the
839 * entry was not present. It's a failure that prohibits
842 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
848 * If the open failed because the file did not exist, or because
849 * the file was not truncated yet, try creating it ourself.
851 URCU_TLS(lttng_ust_nest_count
)++;
853 URCU_TLS(lttng_ust_nest_count
)--;
858 * Parent: wait for child to return, in which case the
859 * shared memory map will have been created.
862 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
867 * Try to open read-only again after creation.
869 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
870 if (wait_shm_fd
< 0) {
872 * Real-only open did not work. It's a failure
873 * that prohibits using shm.
875 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
879 } else if (pid
== 0) {
883 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
884 if (sock_info
->global
)
885 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
887 * We're alone in a child process, so we can modify the
888 * process-wide umask.
892 * Try creating shm (or get rw access).
893 * We don't do an exclusive open, because we allow other
894 * processes to create+ftruncate it concurrently.
896 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
897 O_RDWR
| O_CREAT
, create_mode
);
898 if (wait_shm_fd
>= 0) {
899 ret
= ftruncate(wait_shm_fd
, mmap_size
);
907 * For local shm, we need to have rw access to accept
908 * opening it: this means the local sessiond will be
909 * able to wake us up. For global shm, we open it even
910 * if rw access is not granted, because the root.root
911 * sessiond will be able to override all rights and wake
914 if (!sock_info
->global
&& errno
!= EACCES
) {
915 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
919 * The shm exists, but we cannot open it RW. Report
927 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
931 * Ensure that our user is the owner of the shm file for
932 * local shm. If we do not own the file, it means our
933 * sessiond will not have access to wake us up (there is
934 * probably a rogue process trying to fake our
935 * sessiond). Fallback to polling method in this case.
937 ret
= fstat(wait_shm_fd
, &statbuf
);
942 if (statbuf
.st_uid
!= getuid())
948 ret
= close(wait_shm_fd
);
950 PERROR("Error closing fd");
956 char *get_map_shm(struct sock_info
*sock_info
)
958 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
959 int wait_shm_fd
, ret
;
962 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
963 if (wait_shm_fd
< 0) {
966 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
967 MAP_SHARED
, wait_shm_fd
, 0);
968 /* close shm fd immediately after taking the mmap reference */
969 ret
= close(wait_shm_fd
);
971 PERROR("Error closing fd");
973 if (wait_shm_mmap
== MAP_FAILED
) {
974 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
977 return wait_shm_mmap
;
984 void wait_for_sessiond(struct sock_info
*sock_info
)
989 if (lttng_ust_comm_should_quit
) {
992 if (wait_poll_fallback
) {
995 if (!sock_info
->wait_shm_mmap
) {
996 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
997 if (!sock_info
->wait_shm_mmap
)
1002 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1003 /* Wait for futex wakeup */
1004 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
1005 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1006 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
1008 if (errno
== EFAULT
) {
1009 wait_poll_fallback
= 1;
1011 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1012 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1013 "Please upgrade your kernel "
1014 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1015 "mainline). LTTng-UST will use polling mode fallback.");
1033 * This thread does not allocate any resource, except within
1034 * handle_message, within mutex protection. This mutex protects against
1036 * The other moment it allocates resources is at socket connection, which
1037 * is also protected by the mutex.
1040 void *ust_listener_thread(void *arg
)
1042 struct sock_info
*sock_info
= arg
;
1043 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
1046 /* Restart trying to connect to the session daemon */
1048 if (prev_connect_failed
) {
1049 /* Wait for sessiond availability with pipe */
1050 wait_for_sessiond(sock_info
);
1054 * Sleep for 5 seconds before retrying after a
1055 * sequence of failure / wait / failure. This
1056 * deals with a killed or broken session daemon.
1061 prev_connect_failed
= 0;
1064 if (sock_info
->socket
!= -1) {
1065 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1067 ERR("Error closing %s ust cmd socket",
1070 sock_info
->socket
= -1;
1072 if (sock_info
->notify_socket
!= -1) {
1073 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1075 ERR("Error closing %s ust notify socket",
1078 sock_info
->notify_socket
= -1;
1082 * Register. We need to perform both connect and sending
1083 * registration message before doing the next connect otherwise
1084 * we may reach unix socket connect queue max limits and block
1085 * on the 2nd connect while the session daemon is awaiting the
1086 * first connect registration message.
1088 /* Connect cmd socket */
1089 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
1091 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1092 prev_connect_failed
= 1;
1096 if (lttng_ust_comm_should_quit
) {
1101 * If we cannot find the sessiond daemon, don't delay
1102 * constructor execution.
1104 ret
= handle_register_done(sock_info
);
1109 sock_info
->socket
= ret
;
1113 if (lttng_ust_comm_should_quit
) {
1118 * Create only one root handle per listener thread for the whole
1119 * process lifetime, so we ensure we get ID which is statically
1120 * assigned to the root handle.
1122 if (sock_info
->root_handle
== -1) {
1123 ret
= lttng_abi_create_root_handle();
1125 ERR("Error creating root handle");
1128 sock_info
->root_handle
= ret
;
1131 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1133 ERR("Error registering to %s ust cmd socket",
1135 prev_connect_failed
= 1;
1137 * If we cannot register to the sessiond daemon, don't
1138 * delay constructor execution.
1140 ret
= handle_register_done(sock_info
);
1148 /* Connect notify socket */
1149 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
1151 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1152 prev_connect_failed
= 1;
1156 if (lttng_ust_comm_should_quit
) {
1161 * If we cannot find the sessiond daemon, don't delay
1162 * constructor execution.
1164 ret
= handle_register_done(sock_info
);
1169 sock_info
->notify_socket
= ret
;
1171 timeout
= get_notify_sock_timeout();
1174 * Give at least 10ms to sessiond to reply to
1179 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1182 WARN("Error setting socket receive timeout");
1184 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1187 WARN("Error setting socket send timeout");
1189 } else if (timeout
< -1) {
1190 WARN("Unsupported timeout value %ld", timeout
);
1195 if (lttng_ust_comm_should_quit
) {
1199 ret
= register_to_sessiond(sock_info
->notify_socket
,
1200 USTCTL_SOCKET_NOTIFY
);
1202 ERR("Error registering to %s ust notify socket",
1204 prev_connect_failed
= 1;
1206 * If we cannot register to the sessiond daemon, don't
1207 * delay constructor execution.
1209 ret
= handle_register_done(sock_info
);
1214 sock
= sock_info
->socket
;
1220 struct ustcomm_ust_msg lum
;
1222 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1224 case 0: /* orderly shutdown */
1225 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1227 if (lttng_ust_comm_should_quit
) {
1231 * Either sessiond has shutdown or refused us by closing the socket.
1232 * In either case, we don't want to delay construction execution,
1233 * and we need to wait before retry.
1235 prev_connect_failed
= 1;
1237 * If we cannot register to the sessiond daemon, don't
1238 * delay constructor execution.
1240 ret
= handle_register_done(sock_info
);
1245 print_cmd(lum
.cmd
, lum
.handle
);
1246 ret
= handle_message(sock_info
, sock
, &lum
);
1248 ERR("Error handling message for %s socket", sock_info
->name
);
1250 struct lttng_session
*session
=
1251 sock_info
->session_enabled
;
1253 sock_info
->session_enabled
= NULL
;
1254 lttng_ust_baddr_statedump(session
);
1260 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1262 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1264 if (len
== -ECONNRESET
) {
1265 DBG("%s remote end closed connection", sock_info
->name
);
1274 if (lttng_ust_comm_should_quit
) {
1277 /* Cleanup socket handles before trying to reconnect */
1278 lttng_ust_objd_table_owner_cleanup(sock_info
);
1280 goto restart
; /* try to reconnect */
1283 sock_info
->thread_active
= 0;
1289 * sessiond monitoring thread: monitor presence of global and per-user
1290 * sessiond by polling the application common named pipe.
1292 void __attribute__((constructor
)) lttng_ust_init(void)
1294 struct timespec constructor_timeout
;
1295 sigset_t sig_all_blocked
, orig_parent_mask
;
1296 pthread_attr_t thread_attr
;
1300 if (uatomic_xchg(&initialized
, 1) == 1)
1304 * Fixup interdependency between TLS fixup mutex (which happens
1305 * to be the dynamic linker mutex) and ust_lock, taken within
1308 lttng_fixup_ringbuffer_tls();
1309 lttng_fixup_vtid_tls();
1310 lttng_fixup_nest_count_tls();
1311 lttng_fixup_procname_tls();
1314 * We want precise control over the order in which we construct
1315 * our sub-libraries vs starting to receive commands from
1316 * sessiond (otherwise leading to errors when trying to create
1317 * sessiond before the init functions are completed).
1321 lttng_ring_buffer_metadata_client_init();
1322 lttng_ring_buffer_client_overwrite_init();
1323 lttng_ring_buffer_client_overwrite_rt_init();
1324 lttng_ring_buffer_client_discard_init();
1325 lttng_ring_buffer_client_discard_rt_init();
1326 lttng_context_init();
1328 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1330 ret
= sem_init(&constructor_wait
, 0, 0);
1333 ret
= setup_local_apps();
1335 DBG("local apps setup returned %d", ret
);
1338 /* A new thread created by pthread_create inherits the signal mask
1339 * from the parent. To avoid any signal being received by the
1340 * listener thread, we block all signals temporarily in the parent,
1341 * while we create the listener thread.
1343 sigfillset(&sig_all_blocked
);
1344 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1346 ERR("pthread_sigmask: %s", strerror(ret
));
1349 ret
= pthread_attr_init(&thread_attr
);
1351 ERR("pthread_attr_init: %s", strerror(ret
));
1353 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1355 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1359 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1360 ust_listener_thread
, &global_apps
);
1362 ERR("pthread_create global: %s", strerror(ret
));
1364 global_apps
.thread_active
= 1;
1367 if (local_apps
.allowed
) {
1369 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1370 ust_listener_thread
, &local_apps
);
1372 ERR("pthread_create local: %s", strerror(ret
));
1374 local_apps
.thread_active
= 1;
1377 handle_register_done(&local_apps
);
1379 ret
= pthread_attr_destroy(&thread_attr
);
1381 ERR("pthread_attr_destroy: %s", strerror(ret
));
1384 /* Restore original signal mask in parent */
1385 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1387 ERR("pthread_sigmask: %s", strerror(ret
));
1390 switch (timeout_mode
) {
1391 case 1: /* timeout wait */
1393 ret
= sem_timedwait(&constructor_wait
,
1394 &constructor_timeout
);
1395 } while (ret
< 0 && errno
== EINTR
);
1396 if (ret
< 0 && errno
== ETIMEDOUT
) {
1397 ERR("Timed out waiting for lttng-sessiond");
1402 case -1:/* wait forever */
1404 ret
= sem_wait(&constructor_wait
);
1405 } while (ret
< 0 && errno
== EINTR
);
1408 case 0: /* no timeout */
1414 void lttng_ust_cleanup(int exiting
)
1416 cleanup_sock_info(&global_apps
, exiting
);
1417 if (local_apps
.allowed
) {
1418 cleanup_sock_info(&local_apps
, exiting
);
1421 * The teardown in this function all affect data structures
1422 * accessed under the UST lock by the listener thread. This
1423 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1424 * that none of these threads are accessing this data at this
1427 lttng_ust_abi_exit();
1428 lttng_ust_events_exit();
1429 lttng_context_exit();
1430 lttng_ring_buffer_client_discard_rt_exit();
1431 lttng_ring_buffer_client_discard_exit();
1432 lttng_ring_buffer_client_overwrite_rt_exit();
1433 lttng_ring_buffer_client_overwrite_exit();
1434 lttng_ring_buffer_metadata_client_exit();
1437 /* Reinitialize values for fork */
1439 lttng_ust_comm_should_quit
= 0;
1444 void __attribute__((destructor
)) lttng_ust_exit(void)
1449 * Using pthread_cancel here because:
1450 * A) we don't want to hang application teardown.
1451 * B) the thread is not allocating any resource.
1455 * Require the communication thread to quit. Synchronize with
1456 * mutexes to ensure it is not in a mutex critical section when
1457 * pthread_cancel is later called.
1460 lttng_ust_comm_should_quit
= 1;
1462 /* cancel threads */
1463 if (global_apps
.thread_active
) {
1464 ret
= pthread_cancel(global_apps
.ust_listener
);
1466 ERR("Error cancelling global ust listener thread: %s",
1469 global_apps
.thread_active
= 0;
1472 if (local_apps
.thread_active
) {
1473 ret
= pthread_cancel(local_apps
.ust_listener
);
1475 ERR("Error cancelling local ust listener thread: %s",
1478 local_apps
.thread_active
= 0;
1484 * Do NOT join threads: use of sys_futex makes it impossible to
1485 * join the threads without using async-cancel, but async-cancel
1486 * is delivered by a signal, which could hit the target thread
1487 * anywhere in its code path, including while the ust_lock() is
1488 * held, causing a deadlock for the other thread. Let the OS
1489 * cleanup the threads if there are stalled in a syscall.
1491 lttng_ust_cleanup(1);
1493 if (ust_baddr_handle
) {
1494 int ret
= dlclose(ust_baddr_handle
);
1496 ERR("%s", dlerror());
1501 * We exclude the worker threads across fork and clone (except
1502 * CLONE_VM), because these system calls only keep the forking thread
1503 * running in the child. Therefore, we don't want to call fork or clone
1504 * in the middle of an tracepoint or ust tracing state modification.
1505 * Holding this mutex protects these structures across fork and clone.
1507 void ust_before_fork(sigset_t
*save_sigset
)
1510 * Disable signals. This is to avoid that the child intervenes
1511 * before it is properly setup for tracing. It is safer to
1512 * disable all signals, because then we know we are not breaking
1513 * anything by restoring the original mask.
1518 if (URCU_TLS(lttng_ust_nest_count
))
1520 /* Disable signals */
1521 sigfillset(&all_sigs
);
1522 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1524 PERROR("sigprocmask");
1527 rcu_bp_before_fork();
1530 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1534 DBG("process %d", getpid());
1536 /* Restore signals */
1537 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1539 PERROR("sigprocmask");
1543 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1545 if (URCU_TLS(lttng_ust_nest_count
))
1547 DBG("process %d", getpid());
1548 rcu_bp_after_fork_parent();
1549 /* Release mutexes and reenable signals */
1550 ust_after_fork_common(restore_sigset
);
1554 * After fork, in the child, we need to cleanup all the leftover state,
1555 * except the worker thread which already magically disappeared thanks
1556 * to the weird Linux fork semantics. After tyding up, we call
1557 * lttng_ust_init() again to start over as a new PID.
1559 * This is meant for forks() that have tracing in the child between the
1560 * fork and following exec call (if there is any).
1562 void ust_after_fork_child(sigset_t
*restore_sigset
)
1564 if (URCU_TLS(lttng_ust_nest_count
))
1566 DBG("process %d", getpid());
1567 /* Release urcu mutexes */
1568 rcu_bp_after_fork_child();
1569 lttng_ust_cleanup(0);
1570 lttng_context_vtid_reset();
1571 /* Release mutexes and reenable signals */
1572 ust_after_fork_common(restore_sigset
);
1576 void lttng_ust_sockinfo_session_enabled(void *owner
,
1577 struct lttng_session
*session_enabled
)
1579 struct sock_info
*sock_info
= owner
;
1580 sock_info
->session_enabled
= session_enabled
;