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>
31 #include <semaphore.h>
35 #include <urcu/uatomic.h>
37 #include <lttng-ust-comm.h>
38 #include <ust/usterr-signal-safe.h>
39 #include <ust/lttng-ust-abi.h>
40 #include <ust/tracepoint.h>
41 #include <ust/tracepoint-internal.h>
43 #include "ltt-tracer-core.h"
46 * Has lttng ust comm constructor been called ?
48 static int initialized
;
51 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
52 * Held when handling a command, also held by fork() to deal with
53 * removal of threads, and by exit path.
56 /* Should the ust comm thread quit ? */
57 static int lttng_ust_comm_should_quit
;
60 * Wait for either of these before continuing to the main
62 * - the register_done message from sessiond daemon
63 * (will let the sessiond daemon enable sessions before main
65 * - sessiond daemon is not reachable.
66 * - timeout (ensuring applications are resilient to session
69 static sem_t constructor_wait
;
71 * Doing this for both the global and local sessiond.
73 static int sem_count
= { 2 };
76 * Info about socket and associated listener thread.
80 pthread_t ust_listener
; /* listener thread */
82 int constructor_sem_posted
;
86 char sock_path
[PATH_MAX
];
89 char wait_shm_path
[PATH_MAX
];
93 /* Socket from app (connect) to session daemon (listen) for communication */
94 struct sock_info global_apps
= {
101 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
104 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
107 /* TODO: allow global_apps_sock_path override */
109 struct sock_info local_apps
= {
113 .allowed
= 0, /* Check setuid bit first */
118 extern void ltt_ring_buffer_client_overwrite_init(void);
119 extern void ltt_ring_buffer_client_discard_init(void);
120 extern void ltt_ring_buffer_metadata_client_init(void);
121 extern void ltt_ring_buffer_client_overwrite_exit(void);
122 extern void ltt_ring_buffer_client_discard_exit(void);
123 extern void ltt_ring_buffer_metadata_client_exit(void);
126 int setup_local_apps(void)
128 const char *home_dir
;
133 * Disallow per-user tracing for setuid binaries.
135 if (uid
!= geteuid()) {
136 local_apps
.allowed
= 0;
139 local_apps
.allowed
= 1;
141 home_dir
= (const char *) getenv("HOME");
144 snprintf(local_apps
.sock_path
, PATH_MAX
,
145 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
146 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
147 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
152 int register_app_to_sessiond(int socket
)
163 char name
[16]; /* process name */
166 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
167 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
168 reg_msg
.pid
= getpid();
169 reg_msg
.ppid
= getppid();
170 reg_msg
.uid
= getuid();
171 reg_msg
.gid
= getgid();
172 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
174 ERR("Error executing prctl");
178 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
179 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
185 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
189 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
192 DBG("message successfully sent");
195 if (errno
== ECONNRESET
) {
196 printf("remote end closed connection\n");
201 printf("incorrect message size: %zd\n", len
);
207 int handle_register_done(struct sock_info
*sock_info
)
211 if (sock_info
->constructor_sem_posted
)
213 sock_info
->constructor_sem_posted
= 1;
214 ret
= uatomic_add_return(&sem_count
, -1);
216 ret
= sem_post(&constructor_wait
);
223 int handle_message(struct sock_info
*sock_info
,
224 int sock
, struct lttcomm_ust_msg
*lum
)
227 const struct objd_ops
*ops
;
228 struct lttcomm_ust_reply lur
;
232 memset(&lur
, 0, sizeof(lur
));
234 if (lttng_ust_comm_should_quit
) {
239 ops
= objd_ops(lum
->handle
);
246 case LTTNG_UST_REGISTER_DONE
:
247 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
248 ret
= handle_register_done(sock_info
);
252 case LTTNG_UST_RELEASE
:
253 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
256 ret
= objd_unref(lum
->handle
);
260 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
261 (unsigned long) &lum
->u
);
268 lur
.handle
= lum
->handle
;
272 lur
.ret_code
= LTTCOMM_OK
;
274 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
276 ret
= send_reply(sock
, &lur
);
283 void cleanup_sock_info(struct sock_info
*sock_info
)
287 if (sock_info
->socket
!= -1) {
288 ret
= close(sock_info
->socket
);
290 ERR("Error closing apps socket");
292 sock_info
->socket
= -1;
294 if (sock_info
->root_handle
!= -1) {
295 ret
= objd_unref(sock_info
->root_handle
);
297 ERR("Error unref root handle");
299 sock_info
->root_handle
= -1;
301 sock_info
->constructor_sem_posted
= 0;
302 if (sock_info
->wait_shm_mmap
) {
303 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
305 ERR("Error unmapping wait shm");
307 sock_info
->wait_shm_mmap
= NULL
;
312 char *get_map_shm(struct sock_info
*sock_info
)
314 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
315 int wait_shm_fd
, ret
;
319 mode
= S_IRUSR
| S_IRGRP
;
320 if (sock_info
->global
)
324 * Get existing (read-only) shm, or open new shm.
325 * First try to open read-only.
327 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
329 if (wait_shm_fd
>= 0)
332 * Real-only open did not work. If it is because it did
333 * not exist, try creating it. Else it's a failure that
334 * prohibits using shm.
336 if (errno
!= ENOENT
) {
337 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
340 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
341 O_RDWR
| O_CREAT
| O_EXCL
, mode
| S_IWUSR
);
342 if (wait_shm_fd
>= 0)
344 if (errno
!= EEXIST
) {
345 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
349 * If another process beat us to create the shm, we are
350 * pretty certain the shm is available for us in
353 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
354 O_RDWR
| O_CREAT
| O_EXCL
, mode
);
355 if (wait_shm_fd
>= 0)
361 ret
= ftruncate(wait_shm_fd
, mmap_size
);
364 ret
= close(wait_shm_fd
);
366 ERR("Error closing fd");
371 /* Drop write access ASAP */
372 ret
= chmod(sock_info
->wait_shm_path
, mode
);
377 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
378 MAP_SHARED
, wait_shm_fd
, 0);
379 if (wait_shm_mmap
== MAP_FAILED
) {
383 /* close shm fd immediately after taking the mmap reference */
384 ret
= close(wait_shm_fd
);
386 ERR("Error closing fd");
388 return wait_shm_mmap
;
395 void wait_for_sessiond(struct sock_info
*sock_info
)
398 if (lttng_ust_comm_should_quit
) {
401 if (!sock_info
->wait_shm_mmap
) {
402 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
403 if (!sock_info
->wait_shm_mmap
)
408 DBG("Waiting for %s apps sessiond", sock_info
->name
);
409 /* Wait for futex wakeup TODO */
420 /* Error handling: fallback on a 5 seconds sleep. */
426 * This thread does not allocate any resource, except within
427 * handle_message, within mutex protection. This mutex protects against
429 * The other moment it allocates resources is at socket connexion, which
430 * is also protected by the mutex.
433 void *ust_listener_thread(void *arg
)
435 struct sock_info
*sock_info
= arg
;
438 /* Restart trying to connect to the session daemon */
442 if (lttng_ust_comm_should_quit
) {
447 if (sock_info
->socket
!= -1) {
448 ret
= close(sock_info
->socket
);
450 ERR("Error closing %s apps socket", sock_info
->name
);
452 sock_info
->socket
= -1;
456 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
458 ERR("Error connecting to %s apps socket", sock_info
->name
);
460 * If we cannot find the sessiond daemon, don't delay
461 * constructor execution.
463 ret
= handle_register_done(sock_info
);
467 /* Wait for sessiond availability with pipe */
468 wait_for_sessiond(sock_info
);
472 sock_info
->socket
= sock
= ret
;
475 * Create only one root handle per listener thread for the whole
478 if (sock_info
->root_handle
== -1) {
479 ret
= lttng_abi_create_root_handle();
481 ERR("Error creating root handle");
485 sock_info
->root_handle
= ret
;
488 ret
= register_app_to_sessiond(sock
);
490 ERR("Error registering to %s apps socket", sock_info
->name
);
492 * If we cannot register to the sessiond daemon, don't
493 * delay constructor execution.
495 ret
= handle_register_done(sock_info
);
498 wait_for_sessiond(sock_info
);
505 struct lttcomm_ust_msg lum
;
507 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
509 case 0: /* orderly shutdown */
510 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
513 DBG("message received\n");
514 ret
= handle_message(sock_info
, sock
, &lum
);
516 ERR("Error handling message for %s socket", sock_info
->name
);
520 if (errno
== ECONNRESET
) {
521 ERR("%s remote end closed connection\n", sock_info
->name
);
526 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
532 goto restart
; /* try to reconnect */
538 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
541 int get_timeout(struct timespec
*constructor_timeout
)
543 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
547 str_delay
= getenv("UST_REGISTER_TIMEOUT");
549 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
552 switch (constructor_delay_ms
) {
553 case -1:/* fall-through */
555 return constructor_delay_ms
;
561 * If we are unable to find the current time, don't wait.
563 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
567 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
568 constructor_timeout
->tv_nsec
+=
569 (constructor_delay_ms
% 1000UL) * 1000000UL;
570 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
571 constructor_timeout
->tv_sec
++;
572 constructor_timeout
->tv_nsec
-= 1000000000UL;
578 * sessiond monitoring thread: monitor presence of global and per-user
579 * sessiond by polling the application common named pipe.
583 void __attribute__((constructor
)) lttng_ust_init(void)
585 struct timespec constructor_timeout
;
589 if (uatomic_xchg(&initialized
, 1) == 1)
593 * We want precise control over the order in which we construct
594 * our sub-libraries vs starting to receive commands from
595 * sessiond (otherwise leading to errors when trying to create
596 * sessiond before the init functions are completed).
600 ltt_ring_buffer_metadata_client_init();
601 ltt_ring_buffer_client_overwrite_init();
602 ltt_ring_buffer_client_discard_init();
604 timeout_mode
= get_timeout(&constructor_timeout
);
606 ret
= sem_init(&constructor_wait
, 0, 0);
609 ret
= setup_local_apps();
611 ERR("Error setting up to local apps");
613 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
614 ust_listener_thread
, &local_apps
);
616 if (local_apps
.allowed
) {
617 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
618 ust_listener_thread
, &global_apps
);
620 handle_register_done(&local_apps
);
623 switch (timeout_mode
) {
624 case 1: /* timeout wait */
626 ret
= sem_timedwait(&constructor_wait
,
627 &constructor_timeout
);
628 } while (ret
< 0 && errno
== EINTR
);
629 if (ret
< 0 && errno
== ETIMEDOUT
) {
630 ERR("Timed out waiting for ltt-sessiond");
635 case -1:/* wait forever */
637 ret
= sem_wait(&constructor_wait
);
638 } while (ret
< 0 && errno
== EINTR
);
641 case 0: /* no timeout */
647 void lttng_ust_cleanup(int exiting
)
649 cleanup_sock_info(&global_apps
);
650 if (local_apps
.allowed
) {
651 cleanup_sock_info(&local_apps
);
653 lttng_ust_abi_exit();
655 ltt_ring_buffer_client_discard_exit();
656 ltt_ring_buffer_client_overwrite_exit();
657 ltt_ring_buffer_metadata_client_exit();
660 /* Reinitialize values for fork */
662 lttng_ust_comm_should_quit
= 0;
667 void __attribute__((destructor
)) lttng_ust_exit(void)
672 * Using pthread_cancel here because:
673 * A) we don't want to hang application teardown.
674 * B) the thread is not allocating any resource.
678 * Require the communication thread to quit. Synchronize with
679 * mutexes to ensure it is not in a mutex critical section when
680 * pthread_cancel is later called.
683 lttng_ust_comm_should_quit
= 1;
686 ret
= pthread_cancel(global_apps
.ust_listener
);
688 ERR("Error cancelling global ust listener thread");
690 if (local_apps
.allowed
) {
691 ret
= pthread_cancel(local_apps
.ust_listener
);
693 ERR("Error cancelling local ust listener thread");
696 lttng_ust_cleanup(1);
700 * We exclude the worker threads across fork and clone (except
701 * CLONE_VM), because these system calls only keep the forking thread
702 * running in the child. Therefore, we don't want to call fork or clone
703 * in the middle of an tracepoint or ust tracing state modification.
704 * Holding this mutex protects these structures across fork and clone.
706 void ust_before_fork(ust_fork_info_t
*fork_info
)
709 * Disable signals. This is to avoid that the child intervenes
710 * before it is properly setup for tracing. It is safer to
711 * disable all signals, because then we know we are not breaking
712 * anything by restoring the original mask.
717 /* Disable signals */
718 sigfillset(&all_sigs
);
719 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
721 PERROR("sigprocmask");
724 rcu_bp_before_fork();
727 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
731 DBG("process %d", getpid());
733 /* Restore signals */
734 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
736 PERROR("sigprocmask");
740 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
742 DBG("process %d", getpid());
743 rcu_bp_after_fork_parent();
744 /* Release mutexes and reenable signals */
745 ust_after_fork_common(fork_info
);
749 * After fork, in the child, we need to cleanup all the leftover state,
750 * except the worker thread which already magically disappeared thanks
751 * to the weird Linux fork semantics. After tyding up, we call
752 * lttng_ust_init() again to start over as a new PID.
754 * This is meant for forks() that have tracing in the child between the
755 * fork and following exec call (if there is any).
757 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
759 DBG("process %d", getpid());
760 /* Release urcu mutexes */
761 rcu_bp_after_fork_child();
762 lttng_ust_cleanup(0);
763 /* Release mutexes and reenable signals */
764 ust_after_fork_common(fork_info
);