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>
42 * Has lttng ust comm constructor been called ?
44 static int initialized
;
47 * communication thread mutex. Held when handling a command, also held
48 * by fork() to deal with removal of threads, and by exit path.
50 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
52 /* Should the ust comm thread quit ? */
53 static int lttng_ust_comm_should_quit
;
56 * Wait for either of these before continuing to the main
58 * - the register_done message from sessiond daemon
59 * (will let the sessiond daemon enable sessions before main
61 * - sessiond daemon is not reachable.
62 * - timeout (ensuring applications are resilient to session
65 static sem_t constructor_wait
;
67 * Doing this for both the global and local sessiond.
69 static int sem_count
= { 2 };
72 * Info about socket and associated listener thread.
76 char sock_path
[PATH_MAX
];
78 pthread_t ust_listener
; /* listener thread */
80 int constructor_sem_posted
;
84 /* Socket from app (connect) to session daemon (listen) for communication */
85 struct sock_info global_apps
= {
87 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
93 /* TODO: allow global_apps_sock_path override */
95 struct sock_info local_apps
= {
99 .allowed
= 0, /* Check setuid bit first */
102 extern void ltt_ring_buffer_client_overwrite_init(void);
103 extern void ltt_ring_buffer_client_discard_init(void);
104 extern void ltt_ring_buffer_metadata_client_init(void);
105 extern void ltt_ring_buffer_client_overwrite_exit(void);
106 extern void ltt_ring_buffer_client_discard_exit(void);
107 extern void ltt_ring_buffer_metadata_client_exit(void);
110 int setup_local_apps(void)
112 const char *home_dir
;
115 * Disallow per-user tracing for setuid binaries.
117 if (getuid() != geteuid()) {
118 local_apps
.allowed
= 0;
121 local_apps
.allowed
= 1;
123 home_dir
= (const char *) getenv("HOME");
126 snprintf(local_apps
.sock_path
, PATH_MAX
,
127 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
132 int register_app_to_sessiond(int socket
)
143 char name
[16]; /* process name */
146 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
147 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
148 reg_msg
.pid
= getpid();
149 reg_msg
.ppid
= getppid();
150 reg_msg
.uid
= getuid();
151 reg_msg
.gid
= getgid();
152 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
154 ERR("Error executing prctl");
158 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
159 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
165 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
169 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
172 DBG("message successfully sent");
175 if (errno
== ECONNRESET
) {
176 printf("remote end closed connection\n");
181 printf("incorrect message size: %zd\n", len
);
187 int handle_register_done(struct sock_info
*sock_info
)
191 if (sock_info
->constructor_sem_posted
)
193 sock_info
->constructor_sem_posted
= 1;
194 ret
= uatomic_add_return(&sem_count
, -1);
196 ret
= sem_post(&constructor_wait
);
203 int handle_message(struct sock_info
*sock_info
,
204 int sock
, struct lttcomm_ust_msg
*lum
)
207 const struct objd_ops
*ops
;
208 struct lttcomm_ust_reply lur
;
210 pthread_mutex_lock(<tng_ust_comm_mutex
);
212 memset(&lur
, 0, sizeof(lur
));
214 if (lttng_ust_comm_should_quit
) {
219 ops
= objd_ops(lum
->handle
);
226 case LTTNG_UST_REGISTER_DONE
:
227 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
228 ret
= handle_register_done(sock_info
);
232 case LTTNG_UST_RELEASE
:
233 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
236 ret
= objd_unref(lum
->handle
);
240 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
241 (unsigned long) &lum
->u
);
248 lur
.handle
= lum
->handle
;
252 lur
.ret_code
= LTTCOMM_OK
;
254 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
256 ret
= send_reply(sock
, &lur
);
258 pthread_mutex_unlock(<tng_ust_comm_mutex
);
263 void cleanup_sock_info(struct sock_info
*sock_info
)
267 if (sock_info
->socket
!= -1) {
268 ret
= close(sock_info
->socket
);
270 ERR("Error closing local apps socket");
272 sock_info
->socket
= -1;
274 if (sock_info
->root_handle
!= -1) {
275 ret
= objd_unref(sock_info
->root_handle
);
277 ERR("Error unref root handle");
279 sock_info
->root_handle
= -1;
284 * This thread does not allocate any resource, except within
285 * handle_message, within mutex protection. This mutex protects against
287 * The other moment it allocates resources is at socket connexion, which
288 * is also protected by the mutex.
291 void *ust_listener_thread(void *arg
)
293 struct sock_info
*sock_info
= arg
;
296 /* Restart trying to connect to the session daemon */
298 pthread_mutex_lock(<tng_ust_comm_mutex
);
300 if (lttng_ust_comm_should_quit
) {
301 pthread_mutex_unlock(<tng_ust_comm_mutex
);
305 if (sock_info
->socket
!= -1) {
306 ret
= close(sock_info
->socket
);
308 ERR("Error closing %s apps socket", sock_info
->name
);
310 sock_info
->socket
= -1;
313 /* Check for sessiond availability with pipe TODO */
316 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
318 ERR("Error connecting to %s apps socket", sock_info
->name
);
320 * If we cannot find the sessiond daemon, don't delay
321 * constructor execution.
323 ret
= handle_register_done(sock_info
);
325 pthread_mutex_unlock(<tng_ust_comm_mutex
);
330 sock_info
->socket
= sock
= ret
;
333 * Create only one root handle per listener thread for the whole
336 if (sock_info
->root_handle
== -1) {
337 ret
= lttng_abi_create_root_handle();
339 ERR("Error creating root handle");
340 pthread_mutex_unlock(<tng_ust_comm_mutex
);
343 sock_info
->root_handle
= ret
;
346 ret
= register_app_to_sessiond(sock
);
348 ERR("Error registering to %s apps socket", sock_info
->name
);
350 * If we cannot register to the sessiond daemon, don't
351 * delay constructor execution.
353 ret
= handle_register_done(sock_info
);
355 pthread_mutex_unlock(<tng_ust_comm_mutex
);
359 pthread_mutex_unlock(<tng_ust_comm_mutex
);
363 struct lttcomm_ust_msg lum
;
365 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
367 case 0: /* orderly shutdown */
368 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
371 DBG("message received\n");
372 ret
= handle_message(sock_info
, sock
, &lum
);
374 ERR("Error handling message for %s socket", sock_info
->name
);
378 if (errno
== ECONNRESET
) {
379 ERR("%s remote end closed connection\n", sock_info
->name
);
384 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
390 goto restart
; /* try to reconnect */
396 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
399 int get_timeout(struct timespec
*constructor_timeout
)
401 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
405 str_delay
= getenv("UST_REGISTER_TIMEOUT");
407 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
410 switch (constructor_delay_ms
) {
411 case -1:/* fall-through */
413 return constructor_delay_ms
;
419 * If we are unable to find the current time, don't wait.
421 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
425 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
426 constructor_timeout
->tv_nsec
+=
427 (constructor_delay_ms
% 1000UL) * 1000000UL;
428 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
429 constructor_timeout
->tv_sec
++;
430 constructor_timeout
->tv_nsec
-= 1000000000UL;
436 * sessiond monitoring thread: monitor presence of global and per-user
437 * sessiond by polling the application common named pipe.
441 void __attribute__((constructor
)) lttng_ust_init(void)
443 struct timespec constructor_timeout
;
447 if (uatomic_xchg(&initialized
, 1) == 1)
451 * We want precise control over the order in which we construct
452 * our sub-libraries vs starting to receive commands from
453 * sessiond (otherwise leading to errors when trying to create
454 * sessiond before the init functions are completed).
458 ltt_ring_buffer_metadata_client_init();
459 ltt_ring_buffer_client_overwrite_init();
460 ltt_ring_buffer_client_discard_init();
462 timeout_mode
= get_timeout(&constructor_timeout
);
464 ret
= sem_init(&constructor_wait
, 0, 0);
467 ret
= setup_local_apps();
469 ERR("Error setting up to local apps");
471 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
472 ust_listener_thread
, &local_apps
);
474 if (local_apps
.allowed
) {
475 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
476 ust_listener_thread
, &global_apps
);
478 handle_register_done(&local_apps
);
481 switch (timeout_mode
) {
482 case 1: /* timeout wait */
484 ret
= sem_timedwait(&constructor_wait
,
485 &constructor_timeout
);
486 } while (ret
< 0 && errno
== EINTR
);
487 if (ret
< 0 && errno
== ETIMEDOUT
) {
488 ERR("Timed out waiting for ltt-sessiond");
493 case -1:/* wait forever */
495 ret
= sem_wait(&constructor_wait
);
496 } while (ret
< 0 && errno
== EINTR
);
499 case 0: /* no timeout */
504 void __attribute__((destructor
)) lttng_ust_exit(void)
509 * Using pthread_cancel here because:
510 * A) we don't want to hang application teardown.
511 * B) the thread is not allocating any resource.
515 * Require the communication thread to quit. Synchronize with
516 * mutexes to ensure it is not in a mutex critical section when
517 * pthread_cancel is later called.
519 pthread_mutex_lock(<tng_ust_comm_mutex
);
520 lttng_ust_comm_should_quit
= 1;
521 pthread_mutex_unlock(<tng_ust_comm_mutex
);
523 ret
= pthread_cancel(global_apps
.ust_listener
);
525 ERR("Error cancelling global ust listener thread");
528 cleanup_sock_info(&global_apps
);
530 if (local_apps
.allowed
) {
531 ret
= pthread_cancel(local_apps
.ust_listener
);
533 ERR("Error cancelling local ust listener thread");
536 cleanup_sock_info(&local_apps
);
539 lttng_ust_abi_exit();
541 ltt_ring_buffer_client_discard_exit();
542 ltt_ring_buffer_client_overwrite_exit();
543 ltt_ring_buffer_metadata_client_exit();
548 * We exclude the worker threads across fork and clone (except
549 * CLONE_VM), because these system calls only keep the forking thread
550 * running in the child. Therefore, we don't want to call fork or clone
551 * in the middle of an tracepoint or ust tracing state modification.
552 * Holding this mutex protects these structures across fork and clone.
554 void ust_before_fork(ust_fork_info_t
*fork_info
)
557 * Disable signals. This is to avoid that the child intervenes
558 * before it is properly setup for tracing. It is safer to
559 * disable all signals, because then we know we are not breaking
560 * anything by restoring the original mask.
565 /* Disable signals */
566 sigfillset(&all_sigs
);
567 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, &fork_info
->orig_sigs
);
569 PERROR("sigprocmask");
571 pthread_mutex_lock(<tng_ust_comm_mutex
);
572 rcu_bp_before_fork();
575 static void ust_after_fork_common(ust_fork_info_t
*fork_info
)
579 pthread_mutex_unlock(<tng_ust_comm_mutex
);
580 /* Restore signals */
581 ret
= sigprocmask(SIG_SETMASK
, &fork_info
->orig_sigs
, NULL
);
583 PERROR("sigprocmask");
587 void ust_after_fork_parent(ust_fork_info_t
*fork_info
)
589 rcu_bp_after_fork_parent();
590 /* Release mutexes and reenable signals */
591 ust_after_fork_common(fork_info
);
594 void ust_after_fork_child(ust_fork_info_t
*fork_info
)
596 /* Release urcu mutexes */
597 rcu_bp_after_fork_child();
598 /* Release mutexes and reenable signals */
599 ust_after_fork_common(fork_info
);