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
;
85 char sock_path
[PATH_MAX
];
88 char wait_shm_path
[PATH_MAX
];
92 /* Socket from app (connect) to session daemon (listen) for communication */
93 struct sock_info global_apps
= {
99 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
102 .wait_shm_path
= DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH
,
105 /* TODO: allow global_apps_sock_path override */
107 struct sock_info local_apps
= {
110 .allowed
= 0, /* Check setuid bit first */
115 extern void ltt_ring_buffer_client_overwrite_init(void);
116 extern void ltt_ring_buffer_client_discard_init(void);
117 extern void ltt_ring_buffer_metadata_client_init(void);
118 extern void ltt_ring_buffer_client_overwrite_exit(void);
119 extern void ltt_ring_buffer_client_discard_exit(void);
120 extern void ltt_ring_buffer_metadata_client_exit(void);
123 int setup_local_apps(void)
125 const char *home_dir
;
130 * Disallow per-user tracing for setuid binaries.
132 if (uid
!= geteuid()) {
133 local_apps
.allowed
= 0;
136 local_apps
.allowed
= 1;
138 home_dir
= (const char *) getenv("HOME");
141 snprintf(local_apps
.sock_path
, PATH_MAX
,
142 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
143 snprintf(local_apps
.wait_shm_path
, PATH_MAX
,
144 DEFAULT_HOME_APPS_WAIT_SHM_PATH
, uid
);
149 int register_app_to_sessiond(int socket
)
160 char name
[16]; /* process name */
163 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
164 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
165 reg_msg
.pid
= getpid();
166 reg_msg
.ppid
= getppid();
167 reg_msg
.uid
= getuid();
168 reg_msg
.gid
= getgid();
169 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
171 ERR("Error executing prctl");
175 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
176 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
182 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
186 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
189 DBG("message successfully sent");
192 if (errno
== ECONNRESET
) {
193 printf("remote end closed connection\n");
198 printf("incorrect message size: %zd\n", len
);
204 int handle_register_done(struct sock_info
*sock_info
)
208 if (sock_info
->constructor_sem_posted
)
210 sock_info
->constructor_sem_posted
= 1;
211 ret
= uatomic_add_return(&sem_count
, -1);
213 ret
= sem_post(&constructor_wait
);
220 int handle_message(struct sock_info
*sock_info
,
221 int sock
, struct lttcomm_ust_msg
*lum
)
224 const struct objd_ops
*ops
;
225 struct lttcomm_ust_reply lur
;
229 memset(&lur
, 0, sizeof(lur
));
231 if (lttng_ust_comm_should_quit
) {
236 ops
= objd_ops(lum
->handle
);
243 case LTTNG_UST_REGISTER_DONE
:
244 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
245 ret
= handle_register_done(sock_info
);
249 case LTTNG_UST_RELEASE
:
250 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
253 ret
= objd_unref(lum
->handle
);
257 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
258 (unsigned long) &lum
->u
);
265 lur
.handle
= lum
->handle
;
269 lur
.ret_code
= LTTCOMM_OK
;
271 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
273 ret
= send_reply(sock
, &lur
);
280 void cleanup_sock_info(struct sock_info
*sock_info
)
284 if (sock_info
->socket
!= -1) {
285 ret
= close(sock_info
->socket
);
287 ERR("Error closing apps socket");
289 sock_info
->socket
= -1;
291 if (sock_info
->root_handle
!= -1) {
292 ret
= objd_unref(sock_info
->root_handle
);
294 ERR("Error unref root handle");
296 sock_info
->root_handle
= -1;
298 sock_info
->constructor_sem_posted
= 0;
299 if (sock_info
->wait_shm_mmap
) {
300 ret
= munmap(sock_info
->wait_shm_mmap
, sysconf(_SC_PAGE_SIZE
));
302 ERR("Error unmapping wait shm");
304 sock_info
->wait_shm_mmap
= NULL
;
309 char *get_map_shm(struct sock_info
*sock_info
)
311 size_t mmap_size
= sysconf(_SC_PAGE_SIZE
);
312 int wait_shm_fd
, ret
;
316 * Get existing (read-only) shm, or open new shm.
317 * First try to open read-only.
319 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
321 if (wait_shm_fd
>= 0)
324 * Real-only open did not work. If it is because it did
325 * not exist, try creating it. Else it's a failure that
326 * prohibits using shm.
328 if (errno
!= ENOENT
) {
329 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
332 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
333 O_RDWR
| O_CREAT
| O_EXCL
, 0700);
334 if (wait_shm_fd
>= 0)
336 if (errno
!= EEXIST
) {
337 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
341 * If another process beat us to create the shm, we are
342 * pretty certain the shm is available for us in
345 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
346 O_RDWR
| O_CREAT
| O_EXCL
, 0700);
347 if (wait_shm_fd
>= 0)
353 ret
= ftruncate(wait_shm_fd
, mmap_size
);
356 ret
= close(wait_shm_fd
);
358 ERR("Error closing fd");
364 wait_shm_mmap
= mmap(NULL
, mmap_size
, PROT_READ
,
365 MAP_SHARED
, wait_shm_fd
, 0);
366 if (wait_shm_mmap
== MAP_FAILED
) {
370 /* close shm fd immediately after taking the mmap reference */
371 ret
= close(wait_shm_fd
);
373 ERR("Error closing fd");
375 return wait_shm_mmap
;
382 void wait_for_sessiond(struct sock_info
*sock_info
)
385 if (lttng_ust_comm_should_quit
) {
388 if (!sock_info
->wait_shm_mmap
) {
389 sock_info
->wait_shm_mmap
= get_map_shm(sock_info
);
390 if (!sock_info
->wait_shm_mmap
)
395 DBG("Waiting for %s apps sessiond", sock_info
->name
);
396 /* Wait for futex wakeup TODO */
407 /* Error handling: fallback on a 5 seconds sleep. */
413 * This thread does not allocate any resource, except within
414 * handle_message, within mutex protection. This mutex protects against
416 * The other moment it allocates resources is at socket connexion, which
417 * is also protected by the mutex.
420 void *ust_listener_thread(void *arg
)
422 struct sock_info
*sock_info
= arg
;
425 /* Restart trying to connect to the session daemon */
429 if (lttng_ust_comm_should_quit
) {
434 if (sock_info
->socket
!= -1) {
435 ret
= close(sock_info
->socket
);
437 ERR("Error closing %s apps socket", sock_info
->name
);
439 sock_info
->socket
= -1;
443 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
445 ERR("Error connecting to %s apps socket", sock_info
->name
);
447 * If we cannot find the sessiond daemon, don't delay
448 * constructor execution.
450 ret
= handle_register_done(sock_info
);
454 /* Wait for sessiond availability with pipe */
455 wait_for_sessiond(sock_info
);
459 sock_info
->socket
= sock
= ret
;
462 * Create only one root handle per listener thread for the whole
465 if (sock_info
->root_handle
== -1) {
466 ret
= lttng_abi_create_root_handle();
468 ERR("Error creating root handle");
472 sock_info
->root_handle
= ret
;
475 ret
= register_app_to_sessiond(sock
);
477 ERR("Error registering to %s apps socket", sock_info
->name
);
479 * If we cannot register to the sessiond daemon, don't
480 * delay constructor execution.
482 ret
= handle_register_done(sock_info
);
485 wait_for_sessiond(sock_info
);
492 struct lttcomm_ust_msg lum
;
494 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
496 case 0: /* orderly shutdown */
497 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
500 DBG("message received\n");
501 ret
= handle_message(sock_info
, sock
, &lum
);
503 ERR("Error handling message for %s socket", sock_info
->name
);
507 if (errno
== ECONNRESET
) {
508 ERR("%s remote end closed connection\n", sock_info
->name
);
513 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
519 goto restart
; /* try to reconnect */
525 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
528 int get_timeout(struct timespec
*constructor_timeout
)
530 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
534 str_delay
= getenv("UST_REGISTER_TIMEOUT");
536 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
539 switch (constructor_delay_ms
) {
540 case -1:/* fall-through */
542 return constructor_delay_ms
;
548 * If we are unable to find the current time, don't wait.
550 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
554 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
555 constructor_timeout
->tv_nsec
+=
556 (constructor_delay_ms
% 1000UL) * 1000000UL;
557 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
558 constructor_timeout
->tv_sec
++;
559 constructor_timeout
->tv_nsec
-= 1000000000UL;
565 * sessiond monitoring thread: monitor presence of global and per-user
566 * sessiond by polling the application common named pipe.
570 void __attribute__((constructor
)) lttng_ust_init(void)
572 struct timespec constructor_timeout
;
576 if (uatomic_xchg(&initialized
, 1) == 1)
580 * We want precise control over the order in which we construct
581 * our sub-libraries vs starting to receive commands from
582 * sessiond (otherwise leading to errors when trying to create
583 * sessiond before the init functions are completed).
587 ltt_ring_buffer_metadata_client_init();
588 ltt_ring_buffer_client_overwrite_init();
589 ltt_ring_buffer_client_discard_init();
591 timeout_mode
= get_timeout(&constructor_timeout
);
593 ret
= sem_init(&constructor_wait
, 0, 0);
596 ret
= setup_local_apps();
598 ERR("Error setting up to local apps");
600 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
601 ust_listener_thread
, &local_apps
);
603 if (local_apps
.allowed
) {
604 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
605 ust_listener_thread
, &global_apps
);
607 handle_register_done(&local_apps
);
610 switch (timeout_mode
) {
611 case 1: /* timeout wait */
613 ret
= sem_timedwait(&constructor_wait
,
614 &constructor_timeout
);
615 } while (ret
< 0 && errno
== EINTR
);
616 if (ret
< 0 && errno
== ETIMEDOUT
) {
617 ERR("Timed out waiting for ltt-sessiond");
622 case -1:/* wait forever */
624 ret
= sem_wait(&constructor_wait
);
625 } while (ret
< 0 && errno
== EINTR
);
628 case 0: /* no timeout */
634 void lttng_ust_cleanup(int exiting
)
636 cleanup_sock_info(&global_apps
);
637 if (local_apps
.allowed
) {
638 cleanup_sock_info(&local_apps
);
640 lttng_ust_abi_exit();
642 ltt_ring_buffer_client_discard_exit();
643 ltt_ring_buffer_client_overwrite_exit();
644 ltt_ring_buffer_metadata_client_exit();
647 /* Reinitialize values for fork */
649 lttng_ust_comm_should_quit
= 0;
654 void __attribute__((destructor
)) lttng_ust_exit(void)
659 * Using pthread_cancel here because:
660 * A) we don't want to hang application teardown.
661 * B) the thread is not allocating any resource.
665 * Require the communication thread to quit. Synchronize with
666 * mutexes to ensure it is not in a mutex critical section when
667 * pthread_cancel is later called.
670 lttng_ust_comm_should_quit
= 1;
673 ret
= pthread_cancel(global_apps
.ust_listener
);
675 ERR("Error cancelling global ust listener thread");
677 if (local_apps
.allowed
) {
678 ret
= pthread_cancel(local_apps
.ust_listener
);
680 ERR("Error cancelling local ust listener thread");
683 lttng_ust_cleanup(1);
687 * We exclude the worker threads across fork and clone (except
688 * CLONE_VM), because these system calls only keep the forking thread
689 * running in the child. Therefore, we don't want to call fork or clone
690 * in the middle of an tracepoint or ust tracing state modification.
691 * Holding this mutex protects these structures across fork and clone.
693 void ust_before_fork(ust_fork_info_t
*fork_info
)
696 * Disable signals. This is to avoid that the child intervenes
697 * before it is properly setup for tracing. It is safer to
698 * disable all signals, because then we know we are not breaking
699 * anything by restoring the original mask.
704 /* Disable signals */
705 sigfillset(&all_sigs
);
706 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
708 PERROR("sigprocmask");
711 rcu_bp_before_fork();
714 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
718 DBG("process %d", getpid());
720 /* Restore signals */
721 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
723 PERROR("sigprocmask");
727 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
729 DBG("process %d", getpid());
730 rcu_bp_after_fork_parent();
731 /* Release mutexes and reenable signals */
732 ust_after_fork_common(fork_info
);
736 * After fork, in the child, we need to cleanup all the leftover state,
737 * except the worker thread which already magically disappeared thanks
738 * to the weird Linux fork semantics. After tyding up, we call
739 * lttng_ust_init() again to start over as a new PID.
741 * This is meant for forks() that have tracing in the child between the
742 * fork and following exec call (if there is any).
744 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
746 DBG("process %d", getpid());
747 /* Release urcu mutexes */
748 rcu_bp_after_fork_child();
749 lttng_ust_cleanup(0);
750 /* Release mutexes and reenable signals */
751 ust_after_fork_common(fork_info
);