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>
25 #include <sys/prctl.h>
28 #include <sys/types.h>
34 #include <semaphore.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/futex.h>
41 #include <lttng-ust-comm.h>
42 #include <ust/usterr-signal-safe.h>
43 #include <ust/lttng-ust-abi.h>
44 #include <ust/tracepoint.h>
45 #include <ust/tracepoint-internal.h>
47 #include "ltt-tracer-core.h"
50 * Has lttng ust comm constructor been called ?
52 static int initialized
;
55 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
56 * Held when handling a command, also held by fork() to deal with
57 * removal of threads, and by exit path.
60 /* Should the ust comm thread quit ? */
61 static int lttng_ust_comm_should_quit
;
64 * Wait for either of these before continuing to the main
66 * - the register_done message from sessiond daemon
67 * (will let the sessiond daemon enable sessions before main
69 * - sessiond daemon is not reachable.
70 * - timeout (ensuring applications are resilient to session
73 static sem_t constructor_wait
;
75 * Doing this for both the global and local sessiond.
77 static int sem_count
= { 2 };
80 * Info about socket and associated listener thread.
84 pthread_t ust_listener
; /* listener thread */
86 int constructor_sem_posted
;
90 char sock_path
[PATH_MAX
];
93 char wait_shm_path
[PATH_MAX
];
97 /* Socket from app (connect) to session daemon (listen) for communication */
98 struct sock_info global_apps
= {
105 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
108 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
111 /* TODO: allow global_apps_sock_path override */
113 struct sock_info local_apps
= {
117 .allowed
= 0, /* Check setuid bit first */
122 extern void ltt_ring_buffer_client_overwrite_init(void);
123 extern void ltt_ring_buffer_client_discard_init(void);
124 extern void ltt_ring_buffer_metadata_client_init(void);
125 extern void ltt_ring_buffer_client_overwrite_exit(void);
126 extern void ltt_ring_buffer_client_discard_exit(void);
127 extern void ltt_ring_buffer_metadata_client_exit(void);
130 int setup_local_apps(void)
132 const char *home_dir
;
137 * Disallow per-user tracing for setuid binaries.
139 if (uid
!= geteuid()) {
140 local_apps
.allowed
= 0;
143 local_apps
.allowed
= 1;
145 home_dir
= (const char *) getenv("HOME");
148 snprintf(local_apps
.sock_path
, PATH_MAX
,
149 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
150 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
151 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
156 int register_app_to_sessiond(int socket
)
167 char name
[16]; /* process name */
170 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
171 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
172 reg_msg
.pid
= getpid();
173 reg_msg
.ppid
= getppid();
174 reg_msg
.uid
= getuid();
175 reg_msg
.gid
= getgid();
176 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
178 ERR("Error executing prctl");
182 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
183 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
189 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
193 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
196 DBG("message successfully sent");
199 if (errno
== ECONNRESET
) {
200 printf("remote end closed connection\n");
205 printf("incorrect message size: %zd\n", len
);
211 int handle_register_done(struct sock_info
*sock_info
)
215 if (sock_info
->constructor_sem_posted
)
217 sock_info
->constructor_sem_posted
= 1;
218 ret
= uatomic_add_return(&sem_count
, -1);
220 ret
= sem_post(&constructor_wait
);
227 int handle_message(struct sock_info
*sock_info
,
228 int sock
, struct lttcomm_ust_msg
*lum
)
231 const struct objd_ops
*ops
;
232 struct lttcomm_ust_reply lur
;
236 memset(&lur
, 0, sizeof(lur
));
238 if (lttng_ust_comm_should_quit
) {
243 ops
= objd_ops(lum
->handle
);
250 case LTTNG_UST_REGISTER_DONE
:
251 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
252 ret
= handle_register_done(sock_info
);
256 case LTTNG_UST_RELEASE
:
257 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
260 ret
= objd_unref(lum
->handle
);
264 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
265 (unsigned long) &lum
->u
);
272 lur
.handle
= lum
->handle
;
276 lur
.ret_code
= LTTCOMM_OK
;
278 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
280 ret
= send_reply(sock
, &lur
);
287 void cleanup_sock_info(struct sock_info
*sock_info
)
291 if (sock_info
->socket
!= -1) {
292 ret
= close(sock_info
->socket
);
294 ERR("Error closing apps socket");
296 sock_info
->socket
= -1;
298 if (sock_info
->root_handle
!= -1) {
299 ret
= objd_unref(sock_info
->root_handle
);
301 ERR("Error unref root handle");
303 sock_info
->root_handle
= -1;
305 sock_info
->constructor_sem_posted
= 0;
306 if (sock_info
->wait_shm_mmap
) {
307 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
309 ERR("Error unmapping wait shm");
311 sock_info
->wait_shm_mmap
= NULL
;
316 * Using fork to set umask in the child process (not multi-thread safe).
317 * We deal with the shm_open vs ftruncate race (happening when the
318 * sessiond owns the shm and does not let everybody modify it, to ensure
319 * safety against shm_unlink) by simply letting the mmap fail and
320 * retrying after a few seconds.
321 * For global shm, everybody has rw access to it until the sessiond
325 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
327 int wait_shm_fd
, ret
;
331 * Try to open read-only.
333 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
334 if (wait_shm_fd
>= 0) {
336 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
338 * Real-only open did not work, and it's not because the
339 * entry was not present. It's a failure that prohibits
342 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
346 * If the open failed because the file did not exist, try
347 * creating it ourself.
354 * Parent: wait for child to return, in which case the
355 * shared memory map will have been created.
358 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
363 * Try to open read-only again after creation.
365 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
366 if (wait_shm_fd
< 0) {
368 * Real-only open did not work. It's a failure
369 * that prohibits using shm.
371 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
375 } else if (pid
== 0) {
379 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
380 if (sock_info
->global
)
381 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
383 * We're alone in a child process, so we can modify the
384 * process-wide umask.
388 * Try creating shm (or get rw access).
389 * We don't do an exclusive open, because we allow other
390 * processes to create+ftruncate it concurrently.
392 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
393 O_RDWR
| O_CREAT
, create_mode
);
394 if (wait_shm_fd
>= 0) {
395 ret
= ftruncate(wait_shm_fd
, mmap_size
);
403 * For local shm, we need to have rw access to accept
404 * opening it: this means the local sessiond will be
405 * able to wake us up. For global shm, we open it even
406 * if rw access is not granted, because the root.root
407 * sessiond will be able to override all rights and wake
410 if (!sock_info
->global
&& errno
!= EACCES
) {
411 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
415 * The shm exists, but we cannot open it RW. Report
423 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
427 * Ensure that our user is the owner of the shm file for
428 * local shm. If we do not own the file, it means our
429 * sessiond will not have access to wake us up (there is
430 * probably a rogue process trying to fake our
431 * sessiond). Fallback to polling method in this case.
433 ret
= fstat(wait_shm_fd
, &statbuf
);
438 if (statbuf
.st_uid
!= getuid())
444 ret
= close(wait_shm_fd
);
446 PERROR("Error closing fd");
452 char *get_map_shm(struct sock_info
*sock_info
)
454 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
455 int wait_shm_fd
, ret
;
458 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
459 if (wait_shm_fd
< 0) {
462 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
463 MAP_SHARED
, wait_shm_fd
, 0);
464 /* close shm fd immediately after taking the mmap reference */
465 ret
= close(wait_shm_fd
);
467 PERROR("Error closing fd");
469 if (wait_shm_mmap
== MAP_FAILED
) {
470 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
473 return wait_shm_mmap
;
480 void wait_for_sessiond(struct sock_info
*sock_info
)
485 if (lttng_ust_comm_should_quit
) {
488 if (!sock_info
->wait_shm_mmap
) {
489 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
490 if (!sock_info
->wait_shm_mmap
)
495 DBG("Waiting for %s apps sessiond", sock_info
->name
);
496 /* Wait for futex wakeup */
497 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
) == 0) {
498 ret
= futex_async((int32_t *) sock_info
->wait_shm_mmap
,
499 FUTEX_WAIT
, 0, NULL
, NULL
, 0);
501 * FIXME: Currently, futexes on read-only shm seems to
517 /* Error handling: fallback on a 5 seconds sleep. */
523 * This thread does not allocate any resource, except within
524 * handle_message, within mutex protection. This mutex protects against
526 * The other moment it allocates resources is at socket connexion, which
527 * is also protected by the mutex.
530 void *ust_listener_thread(void *arg
)
532 struct sock_info
*sock_info
= arg
;
535 /* Restart trying to connect to the session daemon */
539 if (lttng_ust_comm_should_quit
) {
544 if (sock_info
->socket
!= -1) {
545 ret
= close(sock_info
->socket
);
547 ERR("Error closing %s apps socket", sock_info
->name
);
549 sock_info
->socket
= -1;
553 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
555 ERR("Error connecting to %s apps socket", sock_info
->name
);
557 * If we cannot find the sessiond daemon, don't delay
558 * constructor execution.
560 ret
= handle_register_done(sock_info
);
564 /* Wait for sessiond availability with pipe */
565 wait_for_sessiond(sock_info
);
569 sock_info
->socket
= sock
= ret
;
572 * Create only one root handle per listener thread for the whole
575 if (sock_info
->root_handle
== -1) {
576 ret
= lttng_abi_create_root_handle();
578 ERR("Error creating root handle");
582 sock_info
->root_handle
= ret
;
585 ret
= register_app_to_sessiond(sock
);
587 ERR("Error registering to %s apps socket", sock_info
->name
);
589 * If we cannot register to the sessiond daemon, don't
590 * delay constructor execution.
592 ret
= handle_register_done(sock_info
);
595 wait_for_sessiond(sock_info
);
602 struct lttcomm_ust_msg lum
;
604 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
606 case 0: /* orderly shutdown */
607 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
610 DBG("message received\n");
611 ret
= handle_message(sock_info
, sock
, &lum
);
613 ERR("Error handling message for %s socket", sock_info
->name
);
617 if (errno
== ECONNRESET
) {
618 ERR("%s remote end closed connection\n", sock_info
->name
);
623 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
629 goto restart
; /* try to reconnect */
635 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
638 int get_timeout(struct timespec
*constructor_timeout
)
640 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
644 str_delay
= getenv("UST_REGISTER_TIMEOUT");
646 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
649 switch (constructor_delay_ms
) {
650 case -1:/* fall-through */
652 return constructor_delay_ms
;
658 * If we are unable to find the current time, don't wait.
660 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
664 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
665 constructor_timeout
->tv_nsec
+=
666 (constructor_delay_ms
% 1000UL) * 1000000UL;
667 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
668 constructor_timeout
->tv_sec
++;
669 constructor_timeout
->tv_nsec
-= 1000000000UL;
675 * sessiond monitoring thread: monitor presence of global and per-user
676 * sessiond by polling the application common named pipe.
680 void __attribute__((constructor
)) lttng_ust_init(void)
682 struct timespec constructor_timeout
;
686 if (uatomic_xchg(&initialized
, 1) == 1)
690 * We want precise control over the order in which we construct
691 * our sub-libraries vs starting to receive commands from
692 * sessiond (otherwise leading to errors when trying to create
693 * sessiond before the init functions are completed).
697 ltt_ring_buffer_metadata_client_init();
698 ltt_ring_buffer_client_overwrite_init();
699 ltt_ring_buffer_client_discard_init();
701 timeout_mode
= get_timeout(&constructor_timeout
);
703 ret
= sem_init(&constructor_wait
, 0, 0);
706 ret
= setup_local_apps();
708 ERR("Error setting up to local apps");
710 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
711 ust_listener_thread
, &local_apps
);
713 if (local_apps
.allowed
) {
714 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
715 ust_listener_thread
, &global_apps
);
717 handle_register_done(&local_apps
);
720 switch (timeout_mode
) {
721 case 1: /* timeout wait */
723 ret
= sem_timedwait(&constructor_wait
,
724 &constructor_timeout
);
725 } while (ret
< 0 && errno
== EINTR
);
726 if (ret
< 0 && errno
== ETIMEDOUT
) {
727 ERR("Timed out waiting for ltt-sessiond");
732 case -1:/* wait forever */
734 ret
= sem_wait(&constructor_wait
);
735 } while (ret
< 0 && errno
== EINTR
);
738 case 0: /* no timeout */
744 void lttng_ust_cleanup(int exiting
)
746 cleanup_sock_info(&global_apps
);
747 if (local_apps
.allowed
) {
748 cleanup_sock_info(&local_apps
);
750 lttng_ust_abi_exit();
752 ltt_ring_buffer_client_discard_exit();
753 ltt_ring_buffer_client_overwrite_exit();
754 ltt_ring_buffer_metadata_client_exit();
757 /* Reinitialize values for fork */
759 lttng_ust_comm_should_quit
= 0;
764 void __attribute__((destructor
)) lttng_ust_exit(void)
769 * Using pthread_cancel here because:
770 * A) we don't want to hang application teardown.
771 * B) the thread is not allocating any resource.
775 * Require the communication thread to quit. Synchronize with
776 * mutexes to ensure it is not in a mutex critical section when
777 * pthread_cancel is later called.
780 lttng_ust_comm_should_quit
= 1;
783 ret
= pthread_cancel(global_apps
.ust_listener
);
785 ERR("Error cancelling global ust listener thread");
787 if (local_apps
.allowed
) {
788 ret
= pthread_cancel(local_apps
.ust_listener
);
790 ERR("Error cancelling local ust listener thread");
793 lttng_ust_cleanup(1);
797 * We exclude the worker threads across fork and clone (except
798 * CLONE_VM), because these system calls only keep the forking thread
799 * running in the child. Therefore, we don't want to call fork or clone
800 * in the middle of an tracepoint or ust tracing state modification.
801 * Holding this mutex protects these structures across fork and clone.
803 void ust_before_fork(ust_fork_info_t
*fork_info
)
806 * Disable signals. This is to avoid that the child intervenes
807 * before it is properly setup for tracing. It is safer to
808 * disable all signals, because then we know we are not breaking
809 * anything by restoring the original mask.
814 /* Disable signals */
815 sigfillset(&all_sigs
);
816 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
818 PERROR("sigprocmask");
821 rcu_bp_before_fork();
824 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
828 DBG("process %d", getpid());
830 /* Restore signals */
831 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
833 PERROR("sigprocmask");
837 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
839 DBG("process %d", getpid());
840 rcu_bp_after_fork_parent();
841 /* Release mutexes and reenable signals */
842 ust_after_fork_common(fork_info
);
846 * After fork, in the child, we need to cleanup all the leftover state,
847 * except the worker thread which already magically disappeared thanks
848 * to the weird Linux fork semantics. After tyding up, we call
849 * lttng_ust_init() again to start over as a new PID.
851 * This is meant for forks() that have tracing in the child between the
852 * fork and following exec call (if there is any).
854 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
856 DBG("process %d", getpid());
857 /* Release urcu mutexes */
858 rcu_bp_after_fork_child();
859 lttng_ust_cleanup(0);
860 /* Release mutexes and reenable signals */
861 ust_after_fork_common(fork_info
);