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 to 0777 in the child process (not
315 * multi-thread safe).
318 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
320 int wait_shm_fd
, ret
;
325 * At this point, we should be able to open it for
326 * reading. If it fails, then it's because there is
327 * something wrong: bail out in that case.
329 read_mode
= S_IRUSR
| S_IRGRP
;
330 if (sock_info
->global
)
331 read_mode
|= S_IROTH
;
334 * Try to open read-only. If it is set read-only, it
335 * means the shm size has been already set with
336 * ftruncate. Note: all processes creating shm need to
337 * call ftruncate on the shm before restricting its
338 * access rights to read-only. The shm should never be
339 * unlinked. It a rogue process try to create a non-accessible
340 * shm or to unlink it, the worse-case scenario is that we don't
341 * use the shm wakeup method and sleep/retry instead.
343 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
344 O_RDONLY
, read_mode
);
345 if (wait_shm_fd
>= 0) {
347 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
349 * Real-only open did not work. It's a failure
350 * that prohibits using shm.
352 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
357 * If the open failed because the file did not exist, try
358 * creating it ourself.
365 * Parent: wait for child to return, in which case the
366 * shared memory map will have been created.
369 if (!WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
374 * Try to open read-only again after creation.
376 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
377 O_RDONLY
, read_mode
);
378 if (wait_shm_fd
< 0) {
380 * Real-only open did not work. It's a failure
381 * that prohibits using shm.
383 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
387 } else if (pid
== 0) {
391 create_mode
= S_IRUSR
| S_IRGRP
| S_IWUSR
| S_IWGRP
;
392 if (sock_info
->global
)
393 create_mode
|= S_IROTH
| S_IWOTH
;
395 * We're alone in a child process, so we can modify the
396 * process-wide umask.
400 * First try creating shm (or get rw access). We need to start
401 * by this because of the ftruncate vs concurrent map race.
402 * We need to give write access to everyone because of the
403 * ftruncate vs mmap race too. We don't do an exclusive
404 * open, because we allow other processes to
405 * create+ftruncate it concurrently.
407 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
408 O_RDWR
| O_CREAT
, create_mode
);
409 if (wait_shm_fd
>= 0) {
410 ret
= ftruncate(wait_shm_fd
, mmap_size
);
415 ret
= fchmod(wait_shm_fd
, read_mode
);
422 if (errno
!= EACCES
) {
423 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
427 * The shm exists, but we cannot open it RW. It means it
428 * has already been setup and ftruncated, so we can
429 * let the child exit.
440 char *get_map_shm(struct sock_info
*sock_info
)
442 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
443 int wait_shm_fd
, ret
;
446 wait_shm_fd
= get_wait_shm(sock_info
, mmap_size
);
447 if (wait_shm_fd
< 0) {
450 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
451 MAP_SHARED
, wait_shm_fd
, 0);
452 if (wait_shm_mmap
== MAP_FAILED
) {
456 /* close shm fd immediately after taking the mmap reference */
457 ret
= close(wait_shm_fd
);
459 ERR("Error closing fd");
461 return wait_shm_mmap
;
468 void wait_for_sessiond(struct sock_info
*sock_info
)
471 if (lttng_ust_comm_should_quit
) {
474 if (!sock_info
->wait_shm_mmap
) {
475 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
476 if (!sock_info
->wait_shm_mmap
)
481 DBG("Waiting for %s apps sessiond", sock_info
->name
);
482 /* Wait for futex wakeup TODO */
493 /* Error handling: fallback on a 5 seconds sleep. */
499 * This thread does not allocate any resource, except within
500 * handle_message, within mutex protection. This mutex protects against
502 * The other moment it allocates resources is at socket connexion, which
503 * is also protected by the mutex.
506 void *ust_listener_thread(void *arg
)
508 struct sock_info
*sock_info
= arg
;
511 /* Restart trying to connect to the session daemon */
515 if (lttng_ust_comm_should_quit
) {
520 if (sock_info
->socket
!= -1) {
521 ret
= close(sock_info
->socket
);
523 ERR("Error closing %s apps socket", sock_info
->name
);
525 sock_info
->socket
= -1;
529 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
531 ERR("Error connecting to %s apps socket", sock_info
->name
);
533 * If we cannot find the sessiond daemon, don't delay
534 * constructor execution.
536 ret
= handle_register_done(sock_info
);
540 /* Wait for sessiond availability with pipe */
541 wait_for_sessiond(sock_info
);
545 sock_info
->socket
= sock
= ret
;
548 * Create only one root handle per listener thread for the whole
551 if (sock_info
->root_handle
== -1) {
552 ret
= lttng_abi_create_root_handle();
554 ERR("Error creating root handle");
558 sock_info
->root_handle
= ret
;
561 ret
= register_app_to_sessiond(sock
);
563 ERR("Error registering to %s apps socket", sock_info
->name
);
565 * If we cannot register to the sessiond daemon, don't
566 * delay constructor execution.
568 ret
= handle_register_done(sock_info
);
571 wait_for_sessiond(sock_info
);
578 struct lttcomm_ust_msg lum
;
580 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
582 case 0: /* orderly shutdown */
583 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
586 DBG("message received\n");
587 ret
= handle_message(sock_info
, sock
, &lum
);
589 ERR("Error handling message for %s socket", sock_info
->name
);
593 if (errno
== ECONNRESET
) {
594 ERR("%s remote end closed connection\n", sock_info
->name
);
599 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
605 goto restart
; /* try to reconnect */
611 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
614 int get_timeout(struct timespec
*constructor_timeout
)
616 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
620 str_delay
= getenv("UST_REGISTER_TIMEOUT");
622 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
625 switch (constructor_delay_ms
) {
626 case -1:/* fall-through */
628 return constructor_delay_ms
;
634 * If we are unable to find the current time, don't wait.
636 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
640 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
641 constructor_timeout
->tv_nsec
+=
642 (constructor_delay_ms
% 1000UL) * 1000000UL;
643 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
644 constructor_timeout
->tv_sec
++;
645 constructor_timeout
->tv_nsec
-= 1000000000UL;
651 * sessiond monitoring thread: monitor presence of global and per-user
652 * sessiond by polling the application common named pipe.
656 void __attribute__((constructor
)) lttng_ust_init(void)
658 struct timespec constructor_timeout
;
662 if (uatomic_xchg(&initialized
, 1) == 1)
666 * We want precise control over the order in which we construct
667 * our sub-libraries vs starting to receive commands from
668 * sessiond (otherwise leading to errors when trying to create
669 * sessiond before the init functions are completed).
673 ltt_ring_buffer_metadata_client_init();
674 ltt_ring_buffer_client_overwrite_init();
675 ltt_ring_buffer_client_discard_init();
677 timeout_mode
= get_timeout(&constructor_timeout
);
679 ret
= sem_init(&constructor_wait
, 0, 0);
682 ret
= setup_local_apps();
684 ERR("Error setting up to local apps");
686 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
687 ust_listener_thread
, &local_apps
);
689 if (local_apps
.allowed
) {
690 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
691 ust_listener_thread
, &global_apps
);
693 handle_register_done(&local_apps
);
696 switch (timeout_mode
) {
697 case 1: /* timeout wait */
699 ret
= sem_timedwait(&constructor_wait
,
700 &constructor_timeout
);
701 } while (ret
< 0 && errno
== EINTR
);
702 if (ret
< 0 && errno
== ETIMEDOUT
) {
703 ERR("Timed out waiting for ltt-sessiond");
708 case -1:/* wait forever */
710 ret
= sem_wait(&constructor_wait
);
711 } while (ret
< 0 && errno
== EINTR
);
714 case 0: /* no timeout */
720 void lttng_ust_cleanup(int exiting
)
722 cleanup_sock_info(&global_apps
);
723 if (local_apps
.allowed
) {
724 cleanup_sock_info(&local_apps
);
726 lttng_ust_abi_exit();
728 ltt_ring_buffer_client_discard_exit();
729 ltt_ring_buffer_client_overwrite_exit();
730 ltt_ring_buffer_metadata_client_exit();
733 /* Reinitialize values for fork */
735 lttng_ust_comm_should_quit
= 0;
740 void __attribute__((destructor
)) lttng_ust_exit(void)
745 * Using pthread_cancel here because:
746 * A) we don't want to hang application teardown.
747 * B) the thread is not allocating any resource.
751 * Require the communication thread to quit. Synchronize with
752 * mutexes to ensure it is not in a mutex critical section when
753 * pthread_cancel is later called.
756 lttng_ust_comm_should_quit
= 1;
759 ret
= pthread_cancel(global_apps
.ust_listener
);
761 ERR("Error cancelling global ust listener thread");
763 if (local_apps
.allowed
) {
764 ret
= pthread_cancel(local_apps
.ust_listener
);
766 ERR("Error cancelling local ust listener thread");
769 lttng_ust_cleanup(1);
773 * We exclude the worker threads across fork and clone (except
774 * CLONE_VM), because these system calls only keep the forking thread
775 * running in the child. Therefore, we don't want to call fork or clone
776 * in the middle of an tracepoint or ust tracing state modification.
777 * Holding this mutex protects these structures across fork and clone.
779 void ust_before_fork(ust_fork_info_t
*fork_info
)
782 * Disable signals. This is to avoid that the child intervenes
783 * before it is properly setup for tracing. It is safer to
784 * disable all signals, because then we know we are not breaking
785 * anything by restoring the original mask.
790 /* Disable signals */
791 sigfillset(&all_sigs
);
792 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
794 PERROR("sigprocmask");
797 rcu_bp_before_fork();
800 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
804 DBG("process %d", getpid());
806 /* Restore signals */
807 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
809 PERROR("sigprocmask");
813 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
815 DBG("process %d", getpid());
816 rcu_bp_after_fork_parent();
817 /* Release mutexes and reenable signals */
818 ust_after_fork_common(fork_info
);
822 * After fork, in the child, we need to cleanup all the leftover state,
823 * except the worker thread which already magically disappeared thanks
824 * to the weird Linux fork semantics. After tyding up, we call
825 * lttng_ust_init() again to start over as a new PID.
827 * This is meant for forks() that have tracing in the child between the
828 * fork and following exec call (if there is any).
830 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
832 DBG("process %d", getpid());
833 /* Release urcu mutexes */
834 rcu_bp_after_fork_child();
835 lttng_ust_cleanup(0);
836 /* Release mutexes and reenable signals */
837 ust_after_fork_common(fork_info
);