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;
282 sock_info
->constructor_sem_posted
= 0;
286 * This thread does not allocate any resource, except within
287 * handle_message, within mutex protection. This mutex protects against
289 * The other moment it allocates resources is at socket connexion, which
290 * is also protected by the mutex.
293 void *ust_listener_thread(void *arg
)
295 struct sock_info
*sock_info
= arg
;
298 /* Restart trying to connect to the session daemon */
302 if (lttng_ust_comm_should_quit
) {
307 if (sock_info
->socket
!= -1) {
308 ret
= close(sock_info
->socket
);
310 ERR("Error closing %s apps socket", sock_info
->name
);
312 sock_info
->socket
= -1;
315 /* Check for sessiond availability with pipe TODO */
318 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
320 ERR("Error connecting to %s apps socket", sock_info
->name
);
322 * If we cannot find the sessiond daemon, don't delay
323 * constructor execution.
325 ret
= handle_register_done(sock_info
);
332 sock_info
->socket
= sock
= ret
;
335 * Create only one root handle per listener thread for the whole
338 if (sock_info
->root_handle
== -1) {
339 ret
= lttng_abi_create_root_handle();
341 ERR("Error creating root handle");
345 sock_info
->root_handle
= ret
;
348 ret
= register_app_to_sessiond(sock
);
350 ERR("Error registering to %s apps socket", sock_info
->name
);
352 * If we cannot register to the sessiond daemon, don't
353 * delay constructor execution.
355 ret
= handle_register_done(sock_info
);
365 struct lttcomm_ust_msg lum
;
367 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
369 case 0: /* orderly shutdown */
370 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
373 DBG("message received\n");
374 ret
= handle_message(sock_info
, sock
, &lum
);
376 ERR("Error handling message for %s socket", sock_info
->name
);
380 if (errno
== ECONNRESET
) {
381 ERR("%s remote end closed connection\n", sock_info
->name
);
386 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
392 goto restart
; /* try to reconnect */
398 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
401 int get_timeout(struct timespec
*constructor_timeout
)
403 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
407 str_delay
= getenv("UST_REGISTER_TIMEOUT");
409 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
412 switch (constructor_delay_ms
) {
413 case -1:/* fall-through */
415 return constructor_delay_ms
;
421 * If we are unable to find the current time, don't wait.
423 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
427 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
428 constructor_timeout
->tv_nsec
+=
429 (constructor_delay_ms
% 1000UL) * 1000000UL;
430 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
431 constructor_timeout
->tv_sec
++;
432 constructor_timeout
->tv_nsec
-= 1000000000UL;
438 * sessiond monitoring thread: monitor presence of global and per-user
439 * sessiond by polling the application common named pipe.
443 void __attribute__((constructor
)) lttng_ust_init(void)
445 struct timespec constructor_timeout
;
449 if (uatomic_xchg(&initialized
, 1) == 1)
453 * We want precise control over the order in which we construct
454 * our sub-libraries vs starting to receive commands from
455 * sessiond (otherwise leading to errors when trying to create
456 * sessiond before the init functions are completed).
460 ltt_ring_buffer_metadata_client_init();
461 ltt_ring_buffer_client_overwrite_init();
462 ltt_ring_buffer_client_discard_init();
464 timeout_mode
= get_timeout(&constructor_timeout
);
466 ret
= sem_init(&constructor_wait
, 0, 0);
469 ret
= setup_local_apps();
471 ERR("Error setting up to local apps");
473 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
474 ust_listener_thread
, &local_apps
);
476 if (local_apps
.allowed
) {
477 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
478 ust_listener_thread
, &global_apps
);
480 handle_register_done(&local_apps
);
483 switch (timeout_mode
) {
484 case 1: /* timeout wait */
486 ret
= sem_timedwait(&constructor_wait
,
487 &constructor_timeout
);
488 } while (ret
< 0 && errno
== EINTR
);
489 if (ret
< 0 && errno
== ETIMEDOUT
) {
490 ERR("Timed out waiting for ltt-sessiond");
495 case -1:/* wait forever */
497 ret
= sem_wait(&constructor_wait
);
498 } while (ret
< 0 && errno
== EINTR
);
501 case 0: /* no timeout */
507 void lttng_ust_cleanup(int exiting
)
509 cleanup_sock_info(&global_apps
);
510 if (local_apps
.allowed
) {
511 cleanup_sock_info(&local_apps
);
513 lttng_ust_abi_exit();
515 ltt_ring_buffer_client_discard_exit();
516 ltt_ring_buffer_client_overwrite_exit();
517 ltt_ring_buffer_metadata_client_exit();
520 /* Reinitialize values for fork */
522 lttng_ust_comm_should_quit
= 0;
527 void __attribute__((destructor
)) lttng_ust_exit(void)
532 * Using pthread_cancel here because:
533 * A) we don't want to hang application teardown.
534 * B) the thread is not allocating any resource.
538 * Require the communication thread to quit. Synchronize with
539 * mutexes to ensure it is not in a mutex critical section when
540 * pthread_cancel is later called.
543 lttng_ust_comm_should_quit
= 1;
546 ret
= pthread_cancel(global_apps
.ust_listener
);
548 ERR("Error cancelling global ust listener thread");
550 if (local_apps
.allowed
) {
551 ret
= pthread_cancel(local_apps
.ust_listener
);
553 ERR("Error cancelling local ust listener thread");
556 lttng_ust_cleanup(1);
560 * We exclude the worker threads across fork and clone (except
561 * CLONE_VM), because these system calls only keep the forking thread
562 * running in the child. Therefore, we don't want to call fork or clone
563 * in the middle of an tracepoint or ust tracing state modification.
564 * Holding this mutex protects these structures across fork and clone.
566 void ust_before_fork(ust_fork_info_t
*fork_info
)
569 * Disable signals. This is to avoid that the child intervenes
570 * before it is properly setup for tracing. It is safer to
571 * disable all signals, because then we know we are not breaking
572 * anything by restoring the original mask.
577 /* Disable signals */
578 sigfillset(&all_sigs
);
579 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
581 PERROR("sigprocmask");
584 rcu_bp_before_fork();
587 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
591 DBG("process %d", getpid());
593 /* Restore signals */
594 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
596 PERROR("sigprocmask");
600 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
602 DBG("process %d", getpid());
603 rcu_bp_after_fork_parent();
604 /* Release mutexes and reenable signals */
605 ust_after_fork_common(fork_info
);
609 * After fork, in the child, we need to cleanup all the leftover state,
610 * except the worker thread which already magically disappeared thanks
611 * to the weird Linux fork semantics. After tyding up, we call
612 * lttng_ust_init() again to start over as a new PID.
614 * This is meant for forks() that have tracing in the child between the
615 * fork and following exec call (if there is any).
617 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
619 DBG("process %d", getpid());
620 /* Release urcu mutexes */
621 rcu_bp_after_fork_child();
622 lttng_ust_cleanup(0);
623 /* Release mutexes and reenable signals */
624 ust_after_fork_common(fork_info
);