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"
55 #include "lttng-ust-baddr.h"
58 * Has lttng ust comm constructor been called ?
60 static int initialized
;
63 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
64 * Held when handling a command, also held by fork() to deal with
65 * removal of threads, and by exit path.
68 /* Should the ust comm thread quit ? */
69 static int lttng_ust_comm_should_quit
;
72 * Wait for either of these before continuing to the main
74 * - the register_done message from sessiond daemon
75 * (will let the sessiond daemon enable sessions before main
77 * - sessiond daemon is not reachable.
78 * - timeout (ensuring applications are resilient to session
81 static sem_t constructor_wait
;
83 * Doing this for both the global and local sessiond.
85 static int sem_count
= { 2 };
88 * Counting nesting within lttng-ust. Used to ensure that calling fork()
89 * from liblttng-ust does not execute the pre/post fork handlers.
91 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
94 * Info about socket and associated listener thread.
98 pthread_t ust_listener
; /* listener thread */
100 int constructor_sem_posted
;
105 char sock_path
[PATH_MAX
];
109 char wait_shm_path
[PATH_MAX
];
111 /* Keep track of lazy state dump not performed yet. */
112 int statedump_pending
;
115 /* Socket from app (connect) to session daemon (listen) for communication */
116 struct sock_info global_apps
= {
124 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
128 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
130 .statedump_pending
= 0,
133 /* TODO: allow global_apps_sock_path override */
135 struct sock_info local_apps
= {
139 .allowed
= 0, /* Check setuid bit first */
145 .statedump_pending
= 0,
148 static int wait_poll_fallback
;
150 static const char *cmd_name_mapping
[] = {
151 [ LTTNG_UST_RELEASE
] = "Release",
152 [ LTTNG_UST_SESSION
] = "Create Session",
153 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
155 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
156 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
157 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
158 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
160 /* Session FD commands */
161 [ LTTNG_UST_CHANNEL
] = "Create Channel",
162 [ LTTNG_UST_SESSION_START
] = "Start Session",
163 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
165 /* Channel FD commands */
166 [ LTTNG_UST_STREAM
] = "Create Stream",
167 [ LTTNG_UST_EVENT
] = "Create Event",
169 /* Event and Channel FD commands */
170 [ LTTNG_UST_CONTEXT
] = "Create Context",
171 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
173 /* Event, Channel and Session commands */
174 [ LTTNG_UST_ENABLE
] = "Enable",
175 [ LTTNG_UST_DISABLE
] = "Disable",
177 /* Tracepoint list commands */
178 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
179 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
181 /* Event FD commands */
182 [ LTTNG_UST_FILTER
] = "Create Filter",
183 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
186 static const char *str_timeout
;
187 static int got_timeout_env
;
189 extern void lttng_ring_buffer_client_overwrite_init(void);
190 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
191 extern void lttng_ring_buffer_client_discard_init(void);
192 extern void lttng_ring_buffer_client_discard_rt_init(void);
193 extern void lttng_ring_buffer_metadata_client_init(void);
194 extern void lttng_ring_buffer_client_overwrite_exit(void);
195 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
196 extern void lttng_ring_buffer_client_discard_exit(void);
197 extern void lttng_ring_buffer_client_discard_rt_exit(void);
198 extern void lttng_ring_buffer_metadata_client_exit(void);
201 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
205 const char *get_lttng_home_dir(void)
209 val
= (const char *) getenv("LTTNG_HOME");
213 return (const char *) getenv("HOME");
217 * Force a read (imply TLS fixup for dlopen) of TLS variables.
220 void lttng_fixup_nest_count_tls(void)
222 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
225 int lttng_get_notify_socket(void *owner
)
227 struct sock_info
*info
= owner
;
229 return info
->notify_socket
;
233 void print_cmd(int cmd
, int handle
)
235 const char *cmd_name
= "Unknown";
237 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
238 && cmd_name_mapping
[cmd
]) {
239 cmd_name
= cmd_name_mapping
[cmd
];
241 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
243 lttng_ust_obj_get_name(handle
), handle
);
247 int setup_local_apps(void)
249 const char *home_dir
;
254 * Disallow per-user tracing for setuid binaries.
256 if (uid
!= geteuid()) {
257 assert(local_apps
.allowed
== 0);
260 home_dir
= get_lttng_home_dir();
262 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
263 assert(local_apps
.allowed
== 0);
266 local_apps
.allowed
= 1;
267 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
269 LTTNG_DEFAULT_HOME_RUNDIR
,
270 LTTNG_UST_SOCK_FILENAME
);
271 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
272 LTTNG_UST_WAIT_FILENAME
,
278 * Get notify_sock timeout, in ms.
279 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
282 long get_timeout(void)
284 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
286 if (!got_timeout_env
) {
287 str_timeout
= getenv("LTTNG_UST_REGISTER_TIMEOUT");
291 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
292 return constructor_delay_ms
;
296 long get_notify_sock_timeout(void)
298 return get_timeout();
302 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
305 int get_constructor_timeout(struct timespec
*constructor_timeout
)
307 long constructor_delay_ms
;
310 constructor_delay_ms
= get_timeout();
312 switch (constructor_delay_ms
) {
313 case -1:/* fall-through */
315 return constructor_delay_ms
;
321 * If we are unable to find the current time, don't wait.
323 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
327 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
328 constructor_timeout
->tv_nsec
+=
329 (constructor_delay_ms
% 1000UL) * 1000000UL;
330 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
331 constructor_timeout
->tv_sec
++;
332 constructor_timeout
->tv_nsec
-= 1000000000UL;
338 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
340 return ustcomm_send_reg_msg(socket
,
343 lttng_alignof(uint8_t) * CHAR_BIT
,
344 lttng_alignof(uint16_t) * CHAR_BIT
,
345 lttng_alignof(uint32_t) * CHAR_BIT
,
346 lttng_alignof(uint64_t) * CHAR_BIT
,
347 lttng_alignof(unsigned long) * CHAR_BIT
);
351 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
355 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
358 DBG("message successfully sent");
361 if (len
== -ECONNRESET
) {
362 DBG("remote end closed connection");
367 DBG("incorrect message size: %zd", len
);
373 int handle_register_done(struct sock_info
*sock_info
)
377 if (sock_info
->constructor_sem_posted
)
379 sock_info
->constructor_sem_posted
= 1;
380 if (uatomic_read(&sem_count
) <= 0) {
383 ret
= uatomic_add_return(&sem_count
, -1);
385 ret
= sem_post(&constructor_wait
);
392 * Only execute pending statedump after the constructor semaphore has
393 * been posted by each listener thread. This means statedump will only
394 * be performed after the "registration done" command is received from
395 * each session daemon the application is connected to.
397 * This ensures we don't run into deadlock issues with the dynamic
398 * loader mutex, which is held while the constructor is called and
399 * waiting on the constructor semaphore. All operations requiring this
400 * dynamic loader lock need to be postponed using this mechanism.
403 void handle_pending_statedump(struct sock_info
*sock_info
)
405 int ctor_passed
= sock_info
->constructor_sem_posted
;
407 if (ctor_passed
&& sock_info
->statedump_pending
) {
408 sock_info
->statedump_pending
= 0;
409 lttng_handle_pending_statedump(sock_info
);
414 int handle_message(struct sock_info
*sock_info
,
415 int sock
, struct ustcomm_ust_msg
*lum
)
418 const struct lttng_ust_objd_ops
*ops
;
419 struct ustcomm_ust_reply lur
;
425 memset(&lur
, 0, sizeof(lur
));
427 if (lttng_ust_comm_should_quit
) {
428 ret
= -LTTNG_UST_ERR_EXITING
;
432 ops
= objd_ops(lum
->handle
);
439 case LTTNG_UST_REGISTER_DONE
:
440 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
441 ret
= handle_register_done(sock_info
);
445 case LTTNG_UST_RELEASE
:
446 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
449 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
451 case LTTNG_UST_FILTER
:
453 /* Receive filter data */
454 struct lttng_ust_filter_bytecode_node
*bytecode
;
456 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
457 ERR("Filter data size is too large: %u bytes",
458 lum
->u
.filter
.data_size
);
463 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
464 ERR("Filter reloc offset %u is not within data",
465 lum
->u
.filter
.reloc_offset
);
470 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
475 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
476 lum
->u
.filter
.data_size
);
478 case 0: /* orderly shutdown */
483 if (len
== lum
->u
.filter
.data_size
) {
484 DBG("filter data received");
486 } else if (len
< 0) {
487 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
488 if (len
== -ECONNRESET
) {
489 ERR("%s remote end closed connection", sock_info
->name
);
498 DBG("incorrect filter data message size: %zd", len
);
504 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
505 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
506 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
508 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
509 (unsigned long) bytecode
,
514 /* don't free bytecode if everything went fine. */
521 case LTTNG_UST_EXCLUSION
:
523 /* Receive exclusion names */
524 struct lttng_ust_excluder_node
*node
;
527 count
= lum
->u
.exclusion
.count
;
529 /* There are no names to read */
533 node
= zmalloc(sizeof(*node
) +
534 count
* LTTNG_UST_SYM_NAME_LEN
);
539 node
->excluder
.count
= count
;
540 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
541 count
* LTTNG_UST_SYM_NAME_LEN
);
543 case 0: /* orderly shutdown */
548 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
549 DBG("Exclusion data received");
551 } else if (len
< 0) {
552 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
553 if (len
== -ECONNRESET
) {
554 ERR("%s remote end closed connection", sock_info
->name
);
563 DBG("Incorrect exclusion data message size: %zd", len
);
570 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
571 (unsigned long) node
,
576 /* Don't free exclusion data if everything went fine. */
583 case LTTNG_UST_CHANNEL
:
588 len
= ustcomm_recv_channel_from_sessiond(sock
,
589 &chan_data
, lum
->u
.channel
.len
,
592 case 0: /* orderly shutdown */
596 if (len
== lum
->u
.channel
.len
) {
597 DBG("channel data received");
599 } else if (len
< 0) {
600 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
601 if (len
== -ECONNRESET
) {
602 ERR("%s remote end closed connection", sock_info
->name
);
609 DBG("incorrect channel data message size: %zd", len
);
614 args
.channel
.chan_data
= chan_data
;
615 args
.channel
.wakeup_fd
= wakeup_fd
;
617 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
618 (unsigned long) &lum
->u
,
624 case LTTNG_UST_STREAM
:
626 /* Receive shm_fd, wakeup_fd */
627 ret
= ustcomm_recv_stream_from_sessiond(sock
,
630 &args
.stream
.wakeup_fd
);
635 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
636 (unsigned long) &lum
->u
,
644 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
645 (unsigned long) &lum
->u
,
653 lur
.handle
= lum
->handle
;
657 lur
.ret_code
= LTTNG_UST_OK
;
660 * Use -LTTNG_UST_ERR as wildcard for UST internal
661 * error that are not caused by the transport, except if
662 * we already have a more precise error message to
665 if (ret
> -LTTNG_UST_ERR
) {
666 /* Translate code to UST error. */
669 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
672 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
675 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
678 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
681 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
684 lur
.ret_code
= -LTTNG_UST_ERR
;
693 case LTTNG_UST_TRACER_VERSION
:
694 lur
.u
.version
= lum
->u
.version
;
696 case LTTNG_UST_TRACEPOINT_LIST_GET
:
697 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
701 DBG("Return value: %d", lur
.ret_val
);
702 ret
= send_reply(sock
, &lur
);
704 DBG("error sending reply");
709 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
712 if (lur
.ret_code
== LTTNG_UST_OK
) {
714 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
715 len
= ustcomm_send_unix_sock(sock
,
716 &args
.field_list
.entry
,
717 sizeof(args
.field_list
.entry
));
722 if (len
!= sizeof(args
.field_list
.entry
)) {
733 * Performed delayed statedump operations outside of the UST
734 * lock. We need to take the dynamic loader lock before we take
735 * the UST lock internally within handle_pending_statedump().
737 handle_pending_statedump(sock_info
);
743 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
747 if (sock_info
->root_handle
!= -1) {
748 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
750 ERR("Error unref root handle");
752 sock_info
->root_handle
= -1;
754 sock_info
->constructor_sem_posted
= 0;
757 * wait_shm_mmap, socket and notify socket are used by listener
758 * threads outside of the ust lock, so we cannot tear them down
759 * ourselves, because we cannot join on these threads. Leave
760 * responsibility of cleaning up these resources to the OS
766 if (sock_info
->socket
!= -1) {
767 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
769 ERR("Error closing ust cmd socket");
771 sock_info
->socket
= -1;
773 if (sock_info
->notify_socket
!= -1) {
774 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
776 ERR("Error closing ust notify socket");
778 sock_info
->notify_socket
= -1;
780 if (sock_info
->wait_shm_mmap
) {
781 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
783 ERR("Error unmapping wait shm");
785 sock_info
->wait_shm_mmap
= NULL
;
790 * Using fork to set umask in the child process (not multi-thread safe).
791 * We deal with the shm_open vs ftruncate race (happening when the
792 * sessiond owns the shm and does not let everybody modify it, to ensure
793 * safety against shm_unlink) by simply letting the mmap fail and
794 * retrying after a few seconds.
795 * For global shm, everybody has rw access to it until the sessiond
799 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
801 int wait_shm_fd
, ret
;
805 * Try to open read-only.
807 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
808 if (wait_shm_fd
>= 0) {
811 size_t bytes_read
= 0;
814 * Try to read the fd. If unable to do so, try opening
818 len
= read(wait_shm_fd
,
819 &((char *) &tmp_read
)[bytes_read
],
820 sizeof(tmp_read
) - bytes_read
);
824 } while ((len
< 0 && errno
== EINTR
)
825 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
826 if (bytes_read
!= sizeof(tmp_read
)) {
827 ret
= close(wait_shm_fd
);
829 ERR("close wait_shm_fd");
834 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
836 * Real-only open did not work, and it's not because the
837 * entry was not present. It's a failure that prohibits
840 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
846 * If the open failed because the file did not exist, or because
847 * the file was not truncated yet, try creating it ourself.
849 URCU_TLS(lttng_ust_nest_count
)++;
851 URCU_TLS(lttng_ust_nest_count
)--;
856 * Parent: wait for child to return, in which case the
857 * shared memory map will have been created.
860 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
865 * Try to open read-only again after creation.
867 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
868 if (wait_shm_fd
< 0) {
870 * Real-only open did not work. It's a failure
871 * that prohibits using shm.
873 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
877 } else if (pid
== 0) {
881 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
882 if (sock_info
->global
)
883 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
885 * We're alone in a child process, so we can modify the
886 * process-wide umask.
890 * Try creating shm (or get rw access).
891 * We don't do an exclusive open, because we allow other
892 * processes to create+ftruncate it concurrently.
894 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
895 O_RDWR
| O_CREAT
, create_mode
);
896 if (wait_shm_fd
>= 0) {
897 ret
= ftruncate(wait_shm_fd
, mmap_size
);
905 * For local shm, we need to have rw access to accept
906 * opening it: this means the local sessiond will be
907 * able to wake us up. For global shm, we open it even
908 * if rw access is not granted, because the root.root
909 * sessiond will be able to override all rights and wake
912 if (!sock_info
->global
&& errno
!= EACCES
) {
913 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
917 * The shm exists, but we cannot open it RW. Report
925 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
929 * Ensure that our user is the owner of the shm file for
930 * local shm. If we do not own the file, it means our
931 * sessiond will not have access to wake us up (there is
932 * probably a rogue process trying to fake our
933 * sessiond). Fallback to polling method in this case.
935 ret
= fstat(wait_shm_fd
, &statbuf
);
940 if (statbuf
.st_uid
!= getuid())
946 ret
= close(wait_shm_fd
);
948 PERROR("Error closing fd");
954 char *get_map_shm(struct sock_info
*sock_info
)
956 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
957 int wait_shm_fd
, ret
;
960 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
961 if (wait_shm_fd
< 0) {
964 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
965 MAP_SHARED
, wait_shm_fd
, 0);
966 /* close shm fd immediately after taking the mmap reference */
967 ret
= close(wait_shm_fd
);
969 PERROR("Error closing fd");
971 if (wait_shm_mmap
== MAP_FAILED
) {
972 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
975 return wait_shm_mmap
;
982 void wait_for_sessiond(struct sock_info
*sock_info
)
987 if (lttng_ust_comm_should_quit
) {
990 if (wait_poll_fallback
) {
993 if (!sock_info
->wait_shm_mmap
) {
994 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
995 if (!sock_info
->wait_shm_mmap
)
1000 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1001 /* Wait for futex wakeup */
1002 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
1003 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1004 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
1006 if (errno
== EFAULT
) {
1007 wait_poll_fallback
= 1;
1009 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1010 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1011 "Please upgrade your kernel "
1012 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1013 "mainline). LTTng-UST will use polling mode fallback.");
1031 * This thread does not allocate any resource, except within
1032 * handle_message, within mutex protection. This mutex protects against
1034 * The other moment it allocates resources is at socket connection, which
1035 * is also protected by the mutex.
1038 void *ust_listener_thread(void *arg
)
1040 struct sock_info
*sock_info
= arg
;
1041 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0;
1044 /* Restart trying to connect to the session daemon */
1046 if (prev_connect_failed
) {
1047 /* Wait for sessiond availability with pipe */
1048 wait_for_sessiond(sock_info
);
1052 * Sleep for 5 seconds before retrying after a
1053 * sequence of failure / wait / failure. This
1054 * deals with a killed or broken session daemon.
1059 prev_connect_failed
= 0;
1062 if (sock_info
->socket
!= -1) {
1063 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1065 ERR("Error closing %s ust cmd socket",
1068 sock_info
->socket
= -1;
1070 if (sock_info
->notify_socket
!= -1) {
1071 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1073 ERR("Error closing %s ust notify socket",
1076 sock_info
->notify_socket
= -1;
1080 * Register. We need to perform both connect and sending
1081 * registration message before doing the next connect otherwise
1082 * we may reach unix socket connect queue max limits and block
1083 * on the 2nd connect while the session daemon is awaiting the
1084 * first connect registration message.
1086 /* Connect cmd socket */
1087 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
1089 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1090 prev_connect_failed
= 1;
1094 if (lttng_ust_comm_should_quit
) {
1099 * If we cannot find the sessiond daemon, don't delay
1100 * constructor execution.
1102 ret
= handle_register_done(sock_info
);
1107 sock_info
->socket
= ret
;
1111 if (lttng_ust_comm_should_quit
) {
1116 * Create only one root handle per listener thread for the whole
1117 * process lifetime, so we ensure we get ID which is statically
1118 * assigned to the root handle.
1120 if (sock_info
->root_handle
== -1) {
1121 ret
= lttng_abi_create_root_handle();
1123 ERR("Error creating root handle");
1126 sock_info
->root_handle
= ret
;
1129 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1131 ERR("Error registering to %s ust cmd socket",
1133 prev_connect_failed
= 1;
1135 * If we cannot register to the sessiond daemon, don't
1136 * delay constructor execution.
1138 ret
= handle_register_done(sock_info
);
1146 /* Connect notify socket */
1147 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
);
1149 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1150 prev_connect_failed
= 1;
1154 if (lttng_ust_comm_should_quit
) {
1159 * If we cannot find the sessiond daemon, don't delay
1160 * constructor execution.
1162 ret
= handle_register_done(sock_info
);
1167 sock_info
->notify_socket
= ret
;
1169 timeout
= get_notify_sock_timeout();
1172 * Give at least 10ms to sessiond to reply to
1177 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1180 WARN("Error setting socket receive timeout");
1182 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1185 WARN("Error setting socket send timeout");
1187 } else if (timeout
< -1) {
1188 WARN("Unsupported timeout value %ld", timeout
);
1193 if (lttng_ust_comm_should_quit
) {
1197 ret
= register_to_sessiond(sock_info
->notify_socket
,
1198 USTCTL_SOCKET_NOTIFY
);
1200 ERR("Error registering to %s ust notify socket",
1202 prev_connect_failed
= 1;
1204 * If we cannot register to the sessiond daemon, don't
1205 * delay constructor execution.
1207 ret
= handle_register_done(sock_info
);
1212 sock
= sock_info
->socket
;
1218 struct ustcomm_ust_msg lum
;
1220 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1222 case 0: /* orderly shutdown */
1223 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1225 if (lttng_ust_comm_should_quit
) {
1229 * Either sessiond has shutdown or refused us by closing the socket.
1230 * In either case, we don't want to delay construction execution,
1231 * and we need to wait before retry.
1233 prev_connect_failed
= 1;
1235 * If we cannot register to the sessiond daemon, don't
1236 * delay constructor execution.
1238 ret
= handle_register_done(sock_info
);
1243 print_cmd(lum
.cmd
, lum
.handle
);
1244 ret
= handle_message(sock_info
, sock
, &lum
);
1246 ERR("Error handling message for %s socket", sock_info
->name
);
1251 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1253 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1255 if (len
== -ECONNRESET
) {
1256 DBG("%s remote end closed connection", sock_info
->name
);
1265 if (lttng_ust_comm_should_quit
) {
1268 /* Cleanup socket handles before trying to reconnect */
1269 lttng_ust_objd_table_owner_cleanup(sock_info
);
1271 goto restart
; /* try to reconnect */
1274 sock_info
->thread_active
= 0;
1280 * sessiond monitoring thread: monitor presence of global and per-user
1281 * sessiond by polling the application common named pipe.
1283 void __attribute__((constructor
)) lttng_ust_init(void)
1285 struct timespec constructor_timeout
;
1286 sigset_t sig_all_blocked
, orig_parent_mask
;
1287 pthread_attr_t thread_attr
;
1291 if (uatomic_xchg(&initialized
, 1) == 1)
1295 * Fixup interdependency between TLS fixup mutex (which happens
1296 * to be the dynamic linker mutex) and ust_lock, taken within
1299 lttng_fixup_ringbuffer_tls();
1300 lttng_fixup_vtid_tls();
1301 lttng_fixup_nest_count_tls();
1302 lttng_fixup_procname_tls();
1305 * We want precise control over the order in which we construct
1306 * our sub-libraries vs starting to receive commands from
1307 * sessiond (otherwise leading to errors when trying to create
1308 * sessiond before the init functions are completed).
1312 lttng_ring_buffer_metadata_client_init();
1313 lttng_ring_buffer_client_overwrite_init();
1314 lttng_ring_buffer_client_overwrite_rt_init();
1315 lttng_ring_buffer_client_discard_init();
1316 lttng_ring_buffer_client_discard_rt_init();
1317 lttng_context_init();
1319 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1321 ret
= sem_init(&constructor_wait
, 0, 0);
1324 ret
= setup_local_apps();
1326 DBG("local apps setup returned %d", ret
);
1329 /* A new thread created by pthread_create inherits the signal mask
1330 * from the parent. To avoid any signal being received by the
1331 * listener thread, we block all signals temporarily in the parent,
1332 * while we create the listener thread.
1334 sigfillset(&sig_all_blocked
);
1335 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1337 ERR("pthread_sigmask: %s", strerror(ret
));
1340 ret
= pthread_attr_init(&thread_attr
);
1342 ERR("pthread_attr_init: %s", strerror(ret
));
1344 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1346 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1350 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1351 ust_listener_thread
, &global_apps
);
1353 ERR("pthread_create global: %s", strerror(ret
));
1355 global_apps
.thread_active
= 1;
1358 if (local_apps
.allowed
) {
1360 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1361 ust_listener_thread
, &local_apps
);
1363 ERR("pthread_create local: %s", strerror(ret
));
1365 local_apps
.thread_active
= 1;
1368 handle_register_done(&local_apps
);
1370 ret
= pthread_attr_destroy(&thread_attr
);
1372 ERR("pthread_attr_destroy: %s", strerror(ret
));
1375 /* Restore original signal mask in parent */
1376 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1378 ERR("pthread_sigmask: %s", strerror(ret
));
1381 switch (timeout_mode
) {
1382 case 1: /* timeout wait */
1384 ret
= sem_timedwait(&constructor_wait
,
1385 &constructor_timeout
);
1386 } while (ret
< 0 && errno
== EINTR
);
1387 if (ret
< 0 && errno
== ETIMEDOUT
) {
1388 ERR("Timed out waiting for lttng-sessiond");
1393 case -1:/* wait forever */
1395 ret
= sem_wait(&constructor_wait
);
1396 } while (ret
< 0 && errno
== EINTR
);
1399 case 0: /* no timeout */
1405 void lttng_ust_cleanup(int exiting
)
1407 cleanup_sock_info(&global_apps
, exiting
);
1408 if (local_apps
.allowed
) {
1409 cleanup_sock_info(&local_apps
, exiting
);
1412 * The teardown in this function all affect data structures
1413 * accessed under the UST lock by the listener thread. This
1414 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1415 * that none of these threads are accessing this data at this
1418 lttng_ust_abi_exit();
1419 lttng_ust_events_exit();
1420 lttng_context_exit();
1421 lttng_ring_buffer_client_discard_rt_exit();
1422 lttng_ring_buffer_client_discard_exit();
1423 lttng_ring_buffer_client_overwrite_rt_exit();
1424 lttng_ring_buffer_client_overwrite_exit();
1425 lttng_ring_buffer_metadata_client_exit();
1428 /* Reinitialize values for fork */
1430 lttng_ust_comm_should_quit
= 0;
1435 void __attribute__((destructor
)) lttng_ust_exit(void)
1440 * Using pthread_cancel here because:
1441 * A) we don't want to hang application teardown.
1442 * B) the thread is not allocating any resource.
1446 * Require the communication thread to quit. Synchronize with
1447 * mutexes to ensure it is not in a mutex critical section when
1448 * pthread_cancel is later called.
1451 lttng_ust_comm_should_quit
= 1;
1453 /* cancel threads */
1454 if (global_apps
.thread_active
) {
1455 ret
= pthread_cancel(global_apps
.ust_listener
);
1457 ERR("Error cancelling global ust listener thread: %s",
1460 global_apps
.thread_active
= 0;
1463 if (local_apps
.thread_active
) {
1464 ret
= pthread_cancel(local_apps
.ust_listener
);
1466 ERR("Error cancelling local ust listener thread: %s",
1469 local_apps
.thread_active
= 0;
1475 * Do NOT join threads: use of sys_futex makes it impossible to
1476 * join the threads without using async-cancel, but async-cancel
1477 * is delivered by a signal, which could hit the target thread
1478 * anywhere in its code path, including while the ust_lock() is
1479 * held, causing a deadlock for the other thread. Let the OS
1480 * cleanup the threads if there are stalled in a syscall.
1482 lttng_ust_cleanup(1);
1486 * We exclude the worker threads across fork and clone (except
1487 * CLONE_VM), because these system calls only keep the forking thread
1488 * running in the child. Therefore, we don't want to call fork or clone
1489 * in the middle of an tracepoint or ust tracing state modification.
1490 * Holding this mutex protects these structures across fork and clone.
1492 void ust_before_fork(sigset_t
*save_sigset
)
1495 * Disable signals. This is to avoid that the child intervenes
1496 * before it is properly setup for tracing. It is safer to
1497 * disable all signals, because then we know we are not breaking
1498 * anything by restoring the original mask.
1503 if (URCU_TLS(lttng_ust_nest_count
))
1505 /* Disable signals */
1506 sigfillset(&all_sigs
);
1507 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
1509 PERROR("sigprocmask");
1512 rcu_bp_before_fork();
1515 static void ust_after_fork_common(sigset_t
*restore_sigset
)
1519 DBG("process %d", getpid());
1521 /* Restore signals */
1522 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
1524 PERROR("sigprocmask");
1528 void ust_after_fork_parent(sigset_t
*restore_sigset
)
1530 if (URCU_TLS(lttng_ust_nest_count
))
1532 DBG("process %d", getpid());
1533 rcu_bp_after_fork_parent();
1534 /* Release mutexes and reenable signals */
1535 ust_after_fork_common(restore_sigset
);
1539 * After fork, in the child, we need to cleanup all the leftover state,
1540 * except the worker thread which already magically disappeared thanks
1541 * to the weird Linux fork semantics. After tyding up, we call
1542 * lttng_ust_init() again to start over as a new PID.
1544 * This is meant for forks() that have tracing in the child between the
1545 * fork and following exec call (if there is any).
1547 void ust_after_fork_child(sigset_t
*restore_sigset
)
1549 if (URCU_TLS(lttng_ust_nest_count
))
1551 DBG("process %d", getpid());
1552 /* Release urcu mutexes */
1553 rcu_bp_after_fork_child();
1554 lttng_ust_cleanup(0);
1555 lttng_context_vtid_reset();
1556 /* Release mutexes and reenable signals */
1557 ust_after_fork_common(restore_sigset
);
1561 void lttng_ust_sockinfo_session_enabled(void *owner
)
1563 struct sock_info
*sock_info
= owner
;
1564 sock_info
->statedump_pending
= 1;