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>
28 #include <semaphore.h>
32 #include <urcu/uatomic.h>
34 #include <lttng-ust-comm.h>
35 #include <ust/usterr-signal-safe.h>
36 #include <ust/lttng-ust-abi.h>
37 #include <ust/tracepoint.h>
38 #include <ust/tracepoint-internal.h>
40 #include "ltt-tracer-core.h"
43 * Has lttng ust comm constructor been called ?
45 static int initialized
;
48 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
49 * Held when handling a command, also held by fork() to deal with
50 * removal of threads, and by exit path.
53 /* Should the ust comm thread quit ? */
54 static int lttng_ust_comm_should_quit
;
57 * Wait for either of these before continuing to the main
59 * - the register_done message from sessiond daemon
60 * (will let the sessiond daemon enable sessions before main
62 * - sessiond daemon is not reachable.
63 * - timeout (ensuring applications are resilient to session
66 static sem_t constructor_wait
;
68 * Doing this for both the global and local sessiond.
70 static int sem_count
= { 2 };
73 * Info about socket and associated listener thread.
77 char sock_path
[PATH_MAX
];
79 pthread_t ust_listener
; /* listener thread */
81 int constructor_sem_posted
;
85 /* Socket from app (connect) to session daemon (listen) for communication */
86 struct sock_info global_apps
= {
88 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
94 /* TODO: allow global_apps_sock_path override */
96 struct sock_info local_apps
= {
100 .allowed
= 0, /* Check setuid bit first */
103 extern void ltt_ring_buffer_client_overwrite_init(void);
104 extern void ltt_ring_buffer_client_discard_init(void);
105 extern void ltt_ring_buffer_metadata_client_init(void);
106 extern void ltt_ring_buffer_client_overwrite_exit(void);
107 extern void ltt_ring_buffer_client_discard_exit(void);
108 extern void ltt_ring_buffer_metadata_client_exit(void);
111 int setup_local_apps(void)
113 const char *home_dir
;
116 * Disallow per-user tracing for setuid binaries.
118 if (getuid() != geteuid()) {
119 local_apps
.allowed
= 0;
122 local_apps
.allowed
= 1;
124 home_dir
= (const char *) getenv("HOME");
127 snprintf(local_apps
.sock_path
, PATH_MAX
,
128 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
133 int register_app_to_sessiond(int socket
)
144 char name
[16]; /* process name */
147 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
148 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
149 reg_msg
.pid
= getpid();
150 reg_msg
.ppid
= getppid();
151 reg_msg
.uid
= getuid();
152 reg_msg
.gid
= getgid();
153 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
155 ERR("Error executing prctl");
159 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
160 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
166 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
170 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
173 DBG("message successfully sent");
176 if (errno
== ECONNRESET
) {
177 printf("remote end closed connection\n");
182 printf("incorrect message size: %zd\n", len
);
188 int handle_register_done(struct sock_info
*sock_info
)
192 if (sock_info
->constructor_sem_posted
)
194 sock_info
->constructor_sem_posted
= 1;
195 ret
= uatomic_add_return(&sem_count
, -1);
197 ret
= sem_post(&constructor_wait
);
204 int handle_message(struct sock_info
*sock_info
,
205 int sock
, struct lttcomm_ust_msg
*lum
)
208 const struct objd_ops
*ops
;
209 struct lttcomm_ust_reply lur
;
213 memset(&lur
, 0, sizeof(lur
));
215 if (lttng_ust_comm_should_quit
) {
220 ops
= objd_ops(lum
->handle
);
227 case LTTNG_UST_REGISTER_DONE
:
228 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
229 ret
= handle_register_done(sock_info
);
233 case LTTNG_UST_RELEASE
:
234 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
237 ret
= objd_unref(lum
->handle
);
241 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
242 (unsigned long) &lum
->u
);
249 lur
.handle
= lum
->handle
;
253 lur
.ret_code
= LTTCOMM_OK
;
255 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
257 ret
= send_reply(sock
, &lur
);
264 void cleanup_sock_info(struct sock_info
*sock_info
)
268 if (sock_info
->socket
!= -1) {
269 ret
= close(sock_info
->socket
);
271 ERR("Error closing local apps socket");
273 sock_info
->socket
= -1;
275 if (sock_info
->root_handle
!= -1) {
276 ret
= objd_unref(sock_info
->root_handle
);
278 ERR("Error unref root handle");
280 sock_info
->root_handle
= -1;
285 * This thread does not allocate any resource, except within
286 * handle_message, within mutex protection. This mutex protects against
288 * The other moment it allocates resources is at socket connexion, which
289 * is also protected by the mutex.
292 void *ust_listener_thread(void *arg
)
294 struct sock_info
*sock_info
= arg
;
297 /* Restart trying to connect to the session daemon */
301 if (lttng_ust_comm_should_quit
) {
306 if (sock_info
->socket
!= -1) {
307 ret
= close(sock_info
->socket
);
309 ERR("Error closing %s apps socket", sock_info
->name
);
311 sock_info
->socket
= -1;
314 /* Check for sessiond availability with pipe TODO */
317 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
319 ERR("Error connecting to %s apps socket", sock_info
->name
);
321 * If we cannot find the sessiond daemon, don't delay
322 * constructor execution.
324 ret
= handle_register_done(sock_info
);
331 sock_info
->socket
= sock
= ret
;
334 * Create only one root handle per listener thread for the whole
337 if (sock_info
->root_handle
== -1) {
338 ret
= lttng_abi_create_root_handle();
340 ERR("Error creating root handle");
344 sock_info
->root_handle
= ret
;
347 ret
= register_app_to_sessiond(sock
);
349 ERR("Error registering to %s apps socket", sock_info
->name
);
351 * If we cannot register to the sessiond daemon, don't
352 * delay constructor execution.
354 ret
= handle_register_done(sock_info
);
364 struct lttcomm_ust_msg lum
;
366 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
368 case 0: /* orderly shutdown */
369 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
372 DBG("message received\n");
373 ret
= handle_message(sock_info
, sock
, &lum
);
375 ERR("Error handling message for %s socket", sock_info
->name
);
379 if (errno
== ECONNRESET
) {
380 ERR("%s remote end closed connection\n", sock_info
->name
);
385 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
391 goto restart
; /* try to reconnect */
397 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
400 int get_timeout(struct timespec
*constructor_timeout
)
402 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
406 str_delay
= getenv("UST_REGISTER_TIMEOUT");
408 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
411 switch (constructor_delay_ms
) {
412 case -1:/* fall-through */
414 return constructor_delay_ms
;
420 * If we are unable to find the current time, don't wait.
422 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
426 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
427 constructor_timeout
->tv_nsec
+=
428 (constructor_delay_ms
% 1000UL) * 1000000UL;
429 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
430 constructor_timeout
->tv_sec
++;
431 constructor_timeout
->tv_nsec
-= 1000000000UL;
437 * sessiond monitoring thread: monitor presence of global and per-user
438 * sessiond by polling the application common named pipe.
442 void __attribute__((constructor
)) lttng_ust_init(void)
444 struct timespec constructor_timeout
;
448 if (uatomic_xchg(&initialized
, 1) == 1)
452 * We want precise control over the order in which we construct
453 * our sub-libraries vs starting to receive commands from
454 * sessiond (otherwise leading to errors when trying to create
455 * sessiond before the init functions are completed).
459 ltt_ring_buffer_metadata_client_init();
460 ltt_ring_buffer_client_overwrite_init();
461 ltt_ring_buffer_client_discard_init();
463 timeout_mode
= get_timeout(&constructor_timeout
);
465 ret
= sem_init(&constructor_wait
, 0, 0);
468 ret
= setup_local_apps();
470 ERR("Error setting up to local apps");
472 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
473 ust_listener_thread
, &local_apps
);
475 if (local_apps
.allowed
) {
476 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
477 ust_listener_thread
, &global_apps
);
479 handle_register_done(&local_apps
);
482 switch (timeout_mode
) {
483 case 1: /* timeout wait */
485 ret
= sem_timedwait(&constructor_wait
,
486 &constructor_timeout
);
487 } while (ret
< 0 && errno
== EINTR
);
488 if (ret
< 0 && errno
== ETIMEDOUT
) {
489 ERR("Timed out waiting for ltt-sessiond");
494 case -1:/* wait forever */
496 ret
= sem_wait(&constructor_wait
);
497 } while (ret
< 0 && errno
== EINTR
);
500 case 0: /* no timeout */
506 void lttng_ust_cleanup(int exiting
)
508 cleanup_sock_info(&global_apps
);
509 if (local_apps
.allowed
) {
510 cleanup_sock_info(&local_apps
);
512 lttng_ust_abi_exit();
514 ltt_ring_buffer_client_discard_exit();
515 ltt_ring_buffer_client_overwrite_exit();
516 ltt_ring_buffer_metadata_client_exit();
519 /* Reinitialize values for fork */
521 lttng_ust_comm_should_quit
= 0;
526 void __attribute__((destructor
)) lttng_ust_exit(void)
531 * Using pthread_cancel here because:
532 * A) we don't want to hang application teardown.
533 * B) the thread is not allocating any resource.
537 * Require the communication thread to quit. Synchronize with
538 * mutexes to ensure it is not in a mutex critical section when
539 * pthread_cancel is later called.
542 lttng_ust_comm_should_quit
= 1;
545 ret
= pthread_cancel(global_apps
.ust_listener
);
547 ERR("Error cancelling global ust listener thread");
549 if (local_apps
.allowed
) {
550 ret
= pthread_cancel(local_apps
.ust_listener
);
552 ERR("Error cancelling local ust listener thread");
555 lttng_ust_cleanup(1);
559 * We exclude the worker threads across fork and clone (except
560 * CLONE_VM), because these system calls only keep the forking thread
561 * running in the child. Therefore, we don't want to call fork or clone
562 * in the middle of an tracepoint or ust tracing state modification.
563 * Holding this mutex protects these structures across fork and clone.
565 void ust_before_fork(ust_fork_info_t
*fork_info
)
568 * Disable signals. This is to avoid that the child intervenes
569 * before it is properly setup for tracing. It is safer to
570 * disable all signals, because then we know we are not breaking
571 * anything by restoring the original mask.
576 /* Disable signals */
577 sigfillset(&all_sigs
);
578 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
580 PERROR("sigprocmask");
583 rcu_bp_before_fork();
586 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
590 DBG("process %d", getpid());
592 /* Restore signals */
593 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
595 PERROR("sigprocmask");
599 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
601 DBG("process %d", getpid());
602 rcu_bp_after_fork_parent();
603 /* Release mutexes and reenable signals */
604 ust_after_fork_common(fork_info
);
608 * After fork, in the child, we need to cleanup all the leftover state,
609 * except the worker thread which already magically disappeared thanks
610 * to the weird Linux fork semantics. After tyding up, we call
611 * lttng_ust_init() again to start over as a new PID.
613 * This is meant for forks() that have tracing in the child between the
614 * fork and following exec call (if there is any).
616 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
618 DBG("process %d", getpid());
619 /* Release urcu mutexes */
620 rcu_bp_after_fork_child();
621 lttng_ust_cleanup(0);
623 /* Release mutexes and reenable signals */
624 ust_after_fork_common(fork_info
);