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
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/prctl.h>
27 #include <sys/types.h>
33 #include <semaphore.h>
37 #include <urcu/uatomic.h>
39 #include <lttng-ust-comm.h>
40 #include <ust/usterr-signal-safe.h>
41 #include <ust/lttng-ust-abi.h>
42 #include <ust/tracepoint.h>
43 #include <ust/tracepoint-internal.h>
45 #include "ltt-tracer-core.h"
48 * Has lttng ust comm constructor been called ?
50 static int initialized
;
53 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
54 * Held when handling a command, also held by fork() to deal with
55 * removal of threads, and by exit path.
58 /* Should the ust comm thread quit ? */
59 static int lttng_ust_comm_should_quit
;
62 * Wait for either of these before continuing to the main
64 * - the register_done message from sessiond daemon
65 * (will let the sessiond daemon enable sessions before main
67 * - sessiond daemon is not reachable.
68 * - timeout (ensuring applications are resilient to session
71 static sem_t constructor_wait
;
73 * Doing this for both the global and local sessiond.
75 static int sem_count
= { 2 };
78 * Info about socket and associated listener thread.
82 pthread_t ust_listener
; /* listener thread */
84 int constructor_sem_posted
;
88 char sock_path
[PATH_MAX
];
91 char wait_shm_path
[PATH_MAX
];
95 /* Socket from app (connect) to session daemon (listen) for communication */
96 struct sock_info global_apps
= {
103 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
106 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
109 /* TODO: allow global_apps_sock_path override */
111 struct sock_info local_apps
= {
115 .allowed
= 0, /* Check setuid bit first */
120 extern void ltt_ring_buffer_client_overwrite_init(void);
121 extern void ltt_ring_buffer_client_discard_init(void);
122 extern void ltt_ring_buffer_metadata_client_init(void);
123 extern void ltt_ring_buffer_client_overwrite_exit(void);
124 extern void ltt_ring_buffer_client_discard_exit(void);
125 extern void ltt_ring_buffer_metadata_client_exit(void);
128 int setup_local_apps(void)
130 const char *home_dir
;
135 * Disallow per-user tracing for setuid binaries.
137 if (uid
!= geteuid()) {
138 local_apps
.allowed
= 0;
141 local_apps
.allowed
= 1;
143 home_dir
= (const char *) getenv("HOME");
146 snprintf(local_apps
.sock_path
, PATH_MAX
,
147 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
148 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
149 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
154 int register_app_to_sessiond(int socket
)
165 char name
[16]; /* process name */
168 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
169 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
170 reg_msg
.pid
= getpid();
171 reg_msg
.ppid
= getppid();
172 reg_msg
.uid
= getuid();
173 reg_msg
.gid
= getgid();
174 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
176 ERR("Error executing prctl");
180 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
181 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
187 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
191 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
194 DBG("message successfully sent");
197 if (errno
== ECONNRESET
) {
198 printf("remote end closed connection\n");
203 printf("incorrect message size: %zd\n", len
);
209 int handle_register_done(struct sock_info
*sock_info
)
213 if (sock_info
->constructor_sem_posted
)
215 sock_info
->constructor_sem_posted
= 1;
216 ret
= uatomic_add_return(&sem_count
, -1);
218 ret
= sem_post(&constructor_wait
);
225 int handle_message(struct sock_info
*sock_info
,
226 int sock
, struct lttcomm_ust_msg
*lum
)
229 const struct objd_ops
*ops
;
230 struct lttcomm_ust_reply lur
;
234 memset(&lur
, 0, sizeof(lur
));
236 if (lttng_ust_comm_should_quit
) {
241 ops
= objd_ops(lum
->handle
);
248 case LTTNG_UST_REGISTER_DONE
:
249 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
250 ret
= handle_register_done(sock_info
);
254 case LTTNG_UST_RELEASE
:
255 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
258 ret
= objd_unref(lum
->handle
);
262 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
263 (unsigned long) &lum
->u
);
270 lur
.handle
= lum
->handle
;
274 lur
.ret_code
= LTTCOMM_OK
;
276 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
278 ret
= send_reply(sock
, &lur
);
285 void cleanup_sock_info(struct sock_info
*sock_info
)
289 if (sock_info
->socket
!= -1) {
290 ret
= close(sock_info
->socket
);
292 ERR("Error closing apps socket");
294 sock_info
->socket
= -1;
296 if (sock_info
->root_handle
!= -1) {
297 ret
= objd_unref(sock_info
->root_handle
);
299 ERR("Error unref root handle");
301 sock_info
->root_handle
= -1;
303 sock_info
->constructor_sem_posted
= 0;
304 if (sock_info
->wait_shm_mmap
) {
305 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
307 ERR("Error unmapping wait shm");
309 sock_info
->wait_shm_mmap
= NULL
;
314 * Using fork to set umask in the child process (not multi-thread safe).
315 * We deal with the shm_open vs ftruncate race (happening when the
316 * sessiond owns the shm and does not let everybody modify it, to ensure
317 * safety against shm_unlink) by simply letting the mmap fail and
318 * retrying after a few seconds.
319 * For global shm, everybody has rw access to it until the sessiond
323 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
325 int wait_shm_fd
, ret
;
329 * Try to open read-only.
331 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
332 if (wait_shm_fd
>= 0) {
334 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
336 * Real-only open did not work, and it's not because the
337 * entry was not present. It's a failure that prohibits
340 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
344 * If the open failed because the file did not exist, try
345 * creating it ourself.
352 * Parent: wait for child to return, in which case the
353 * shared memory map will have been created.
356 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
361 * Try to open read-only again after creation.
363 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
364 if (wait_shm_fd
< 0) {
366 * Real-only open did not work. It's a failure
367 * that prohibits using shm.
369 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
373 } else if (pid
== 0) {
377 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
378 if (sock_info
->global
)
379 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
381 * We're alone in a child process, so we can modify the
382 * process-wide umask.
386 * Try creating shm (or get rw access).
387 * We don't do an exclusive open, because we allow other
388 * processes to create+ftruncate it concurrently.
390 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
391 O_RDWR
| O_CREAT
, create_mode
);
392 if (wait_shm_fd
>= 0) {
393 ret
= ftruncate(wait_shm_fd
, mmap_size
);
401 * For local shm, we need to have rw access to accept
402 * opening it: this means the local sessiond will be
403 * able to wake us up. For global shm, we open it even
404 * if rw access is not granted, because the root.root
405 * sessiond will be able to override all rights and wake
408 if (!sock_info
->global
&& errno
!= EACCES
) {
409 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
413 * The shm exists, but we cannot open it RW. Report
421 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
425 * Ensure that our user is the owner of the shm file for
426 * local shm. If we do not own the file, it means our
427 * sessiond will not have access to wake us up (there is
428 * probably a rogue process trying to fake our
429 * sessiond). Fallback to polling method in this case.
431 ret
= fstat(wait_shm_fd
, &statbuf
);
436 if (statbuf
.st_uid
!= getuid())
442 ret
= close(wait_shm_fd
);
444 PERROR("Error closing fd");
450 char *get_map_shm(struct sock_info
*sock_info
)
452 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
453 int wait_shm_fd
, ret
;
456 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
457 if (wait_shm_fd
< 0) {
460 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
461 MAP_SHARED
, wait_shm_fd
, 0);
462 /* close shm fd immediately after taking the mmap reference */
463 ret
= close(wait_shm_fd
);
465 PERROR("Error closing fd");
467 if (wait_shm_mmap
== MAP_FAILED
) {
468 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
471 return wait_shm_mmap
;
478 void wait_for_sessiond(struct sock_info
*sock_info
)
481 if (lttng_ust_comm_should_quit
) {
484 if (!sock_info
->wait_shm_mmap
) {
485 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
486 if (!sock_info
->wait_shm_mmap
)
491 DBG("Waiting for %s apps sessiond", sock_info
->name
);
492 /* Wait for futex wakeup TODO */
503 /* Error handling: fallback on a 5 seconds sleep. */
509 * This thread does not allocate any resource, except within
510 * handle_message, within mutex protection. This mutex protects against
512 * The other moment it allocates resources is at socket connexion, which
513 * is also protected by the mutex.
516 void *ust_listener_thread(void *arg
)
518 struct sock_info
*sock_info
= arg
;
521 /* Restart trying to connect to the session daemon */
525 if (lttng_ust_comm_should_quit
) {
530 if (sock_info
->socket
!= -1) {
531 ret
= close(sock_info
->socket
);
533 ERR("Error closing %s apps socket", sock_info
->name
);
535 sock_info
->socket
= -1;
539 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
541 ERR("Error connecting to %s apps socket", sock_info
->name
);
543 * If we cannot find the sessiond daemon, don't delay
544 * constructor execution.
546 ret
= handle_register_done(sock_info
);
550 /* Wait for sessiond availability with pipe */
551 wait_for_sessiond(sock_info
);
555 sock_info
->socket
= sock
= ret
;
558 * Create only one root handle per listener thread for the whole
561 if (sock_info
->root_handle
== -1) {
562 ret
= lttng_abi_create_root_handle();
564 ERR("Error creating root handle");
568 sock_info
->root_handle
= ret
;
571 ret
= register_app_to_sessiond(sock
);
573 ERR("Error registering to %s apps socket", sock_info
->name
);
575 * If we cannot register to the sessiond daemon, don't
576 * delay constructor execution.
578 ret
= handle_register_done(sock_info
);
581 wait_for_sessiond(sock_info
);
588 struct lttcomm_ust_msg lum
;
590 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
592 case 0: /* orderly shutdown */
593 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
596 DBG("message received\n");
597 ret
= handle_message(sock_info
, sock
, &lum
);
599 ERR("Error handling message for %s socket", sock_info
->name
);
603 if (errno
== ECONNRESET
) {
604 ERR("%s remote end closed connection\n", sock_info
->name
);
609 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
615 goto restart
; /* try to reconnect */
621 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
624 int get_timeout(struct timespec
*constructor_timeout
)
626 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
630 str_delay
= getenv("UST_REGISTER_TIMEOUT");
632 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
635 switch (constructor_delay_ms
) {
636 case -1:/* fall-through */
638 return constructor_delay_ms
;
644 * If we are unable to find the current time, don't wait.
646 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
650 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
651 constructor_timeout
->tv_nsec
+=
652 (constructor_delay_ms
% 1000UL) * 1000000UL;
653 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
654 constructor_timeout
->tv_sec
++;
655 constructor_timeout
->tv_nsec
-= 1000000000UL;
661 * sessiond monitoring thread: monitor presence of global and per-user
662 * sessiond by polling the application common named pipe.
666 void __attribute__((constructor
)) lttng_ust_init(void)
668 struct timespec constructor_timeout
;
672 if (uatomic_xchg(&initialized
, 1) == 1)
676 * We want precise control over the order in which we construct
677 * our sub-libraries vs starting to receive commands from
678 * sessiond (otherwise leading to errors when trying to create
679 * sessiond before the init functions are completed).
683 ltt_ring_buffer_metadata_client_init();
684 ltt_ring_buffer_client_overwrite_init();
685 ltt_ring_buffer_client_discard_init();
687 timeout_mode
= get_timeout(&constructor_timeout
);
689 ret
= sem_init(&constructor_wait
, 0, 0);
692 ret
= setup_local_apps();
694 ERR("Error setting up to local apps");
696 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
697 ust_listener_thread
, &local_apps
);
699 if (local_apps
.allowed
) {
700 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
701 ust_listener_thread
, &global_apps
);
703 handle_register_done(&local_apps
);
706 switch (timeout_mode
) {
707 case 1: /* timeout wait */
709 ret
= sem_timedwait(&constructor_wait
,
710 &constructor_timeout
);
711 } while (ret
< 0 && errno
== EINTR
);
712 if (ret
< 0 && errno
== ETIMEDOUT
) {
713 ERR("Timed out waiting for ltt-sessiond");
718 case -1:/* wait forever */
720 ret
= sem_wait(&constructor_wait
);
721 } while (ret
< 0 && errno
== EINTR
);
724 case 0: /* no timeout */
730 void lttng_ust_cleanup(int exiting
)
732 cleanup_sock_info(&global_apps
);
733 if (local_apps
.allowed
) {
734 cleanup_sock_info(&local_apps
);
736 lttng_ust_abi_exit();
738 ltt_ring_buffer_client_discard_exit();
739 ltt_ring_buffer_client_overwrite_exit();
740 ltt_ring_buffer_metadata_client_exit();
743 /* Reinitialize values for fork */
745 lttng_ust_comm_should_quit
= 0;
750 void __attribute__((destructor
)) lttng_ust_exit(void)
755 * Using pthread_cancel here because:
756 * A) we don't want to hang application teardown.
757 * B) the thread is not allocating any resource.
761 * Require the communication thread to quit. Synchronize with
762 * mutexes to ensure it is not in a mutex critical section when
763 * pthread_cancel is later called.
766 lttng_ust_comm_should_quit
= 1;
769 ret
= pthread_cancel(global_apps
.ust_listener
);
771 ERR("Error cancelling global ust listener thread");
773 if (local_apps
.allowed
) {
774 ret
= pthread_cancel(local_apps
.ust_listener
);
776 ERR("Error cancelling local ust listener thread");
779 lttng_ust_cleanup(1);
783 * We exclude the worker threads across fork and clone (except
784 * CLONE_VM), because these system calls only keep the forking thread
785 * running in the child. Therefore, we don't want to call fork or clone
786 * in the middle of an tracepoint or ust tracing state modification.
787 * Holding this mutex protects these structures across fork and clone.
789 void ust_before_fork(ust_fork_info_t
*fork_info
)
792 * Disable signals. This is to avoid that the child intervenes
793 * before it is properly setup for tracing. It is safer to
794 * disable all signals, because then we know we are not breaking
795 * anything by restoring the original mask.
800 /* Disable signals */
801 sigfillset(&all_sigs
);
802 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
804 PERROR("sigprocmask");
807 rcu_bp_before_fork();
810 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
814 DBG("process %d", getpid());
816 /* Restore signals */
817 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
819 PERROR("sigprocmask");
823 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
825 DBG("process %d", getpid());
826 rcu_bp_after_fork_parent();
827 /* Release mutexes and reenable signals */
828 ust_after_fork_common(fork_info
);
832 * After fork, in the child, we need to cleanup all the leftover state,
833 * except the worker thread which already magically disappeared thanks
834 * to the weird Linux fork semantics. After tyding up, we call
835 * lttng_ust_init() again to start over as a new PID.
837 * This is meant for forks() that have tracing in the child between the
838 * fork and following exec call (if there is any).
840 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
842 DBG("process %d", getpid());
843 /* Release urcu mutexes */
844 rcu_bp_after_fork_child();
845 lttng_ust_cleanup(0);
846 /* Release mutexes and reenable signals */
847 ust_after_fork_common(fork_info
);