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>
31 #include <urcu/uatomic.h>
33 #include <lttng-ust-comm.h>
34 #include <ust/usterr-signal-safe.h>
35 #include <ust/lttng-ust-abi.h>
36 #include <ust/tracepoint.h>
39 * Has lttng ust comm constructor been called ?
41 static int initialized
;
44 * communication thread mutex. Held when handling a command, also held
45 * by fork() to deal with removal of threads, and by exit path.
47 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
49 /* Should the ust comm thread quit ? */
50 static int lttng_ust_comm_should_quit
;
53 * Wait for either of these before continuing to the main
55 * - the register_done message from sessiond daemon
56 * (will let the sessiond daemon enable sessions before main
58 * - sessiond daemon is not reachable.
59 * - timeout (ensuring applications are resilient to session
62 static sem_t constructor_wait
;
64 * Doing this for both the global and local sessiond.
66 static int sem_count
= { 2 };
69 * Info about socket and associated listener thread.
73 char sock_path
[PATH_MAX
];
75 pthread_t ust_listener
; /* listener thread */
77 int constructor_sem_posted
;
81 /* Socket from app (connect) to session daemon (listen) for communication */
82 struct sock_info global_apps
= {
84 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
90 /* TODO: allow global_apps_sock_path override */
92 struct sock_info local_apps
= {
96 .allowed
= 0, /* Check setuid bit first */
99 extern void ltt_ring_buffer_client_overwrite_init(void);
100 extern void ltt_ring_buffer_client_discard_init(void);
101 extern void ltt_ring_buffer_metadata_client_init(void);
102 extern void ltt_ring_buffer_client_overwrite_exit(void);
103 extern void ltt_ring_buffer_client_discard_exit(void);
104 extern void ltt_ring_buffer_metadata_client_exit(void);
107 int setup_local_apps(void)
109 const char *home_dir
;
112 * Disallow per-user tracing for setuid binaries.
114 if (getuid() != geteuid()) {
115 local_apps
.allowed
= 0;
118 local_apps
.allowed
= 1;
120 home_dir
= (const char *) getenv("HOME");
123 snprintf(local_apps
.sock_path
, PATH_MAX
,
124 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
129 int register_app_to_sessiond(int socket
)
140 char name
[16]; /* process name */
143 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
144 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
145 reg_msg
.pid
= getpid();
146 reg_msg
.ppid
= getppid();
147 reg_msg
.uid
= getuid();
148 reg_msg
.gid
= getgid();
149 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
151 ERR("Error executing prctl");
155 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
156 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
162 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
166 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
169 DBG("message successfully sent");
172 if (errno
== ECONNRESET
) {
173 printf("remote end closed connection\n");
178 printf("incorrect message size: %zd\n", len
);
184 int handle_register_done(struct sock_info
*sock_info
)
188 if (sock_info
->constructor_sem_posted
)
190 sock_info
->constructor_sem_posted
= 1;
191 ret
= uatomic_add_return(&sem_count
, -1);
193 ret
= sem_post(&constructor_wait
);
200 int handle_message(struct sock_info
*sock_info
,
201 int sock
, struct lttcomm_ust_msg
*lum
)
204 const struct objd_ops
*ops
;
205 struct lttcomm_ust_reply lur
;
207 pthread_mutex_lock(<tng_ust_comm_mutex
);
209 memset(&lur
, 0, sizeof(lur
));
211 if (lttng_ust_comm_should_quit
) {
216 ops
= objd_ops(lum
->handle
);
223 case LTTNG_UST_REGISTER_DONE
:
224 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
225 ret
= handle_register_done(sock_info
);
229 case LTTNG_UST_RELEASE
:
230 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
233 ret
= objd_unref(lum
->handle
);
237 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
238 (unsigned long) &lum
->u
);
245 lur
.handle
= lum
->handle
;
249 lur
.ret_code
= LTTCOMM_OK
;
251 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
253 ret
= send_reply(sock
, &lur
);
255 pthread_mutex_unlock(<tng_ust_comm_mutex
);
260 void cleanup_sock_info(struct sock_info
*sock_info
)
264 if (sock_info
->socket
!= -1) {
265 ret
= close(sock_info
->socket
);
267 ERR("Error closing local apps socket");
269 sock_info
->socket
= -1;
271 if (sock_info
->root_handle
!= -1) {
272 ret
= objd_unref(sock_info
->root_handle
);
274 ERR("Error unref root handle");
276 sock_info
->root_handle
= -1;
281 * This thread does not allocate any resource, except within
282 * handle_message, within mutex protection. This mutex protects against
284 * The other moment it allocates resources is at socket connexion, which
285 * is also protected by the mutex.
288 void *ust_listener_thread(void *arg
)
290 struct sock_info
*sock_info
= arg
;
293 /* Restart trying to connect to the session daemon */
295 pthread_mutex_lock(<tng_ust_comm_mutex
);
297 if (lttng_ust_comm_should_quit
) {
298 pthread_mutex_unlock(<tng_ust_comm_mutex
);
302 if (sock_info
->socket
!= -1) {
303 ret
= close(sock_info
->socket
);
305 ERR("Error closing %s apps socket", sock_info
->name
);
307 sock_info
->socket
= -1;
310 /* Check for sessiond availability with pipe TODO */
313 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
315 ERR("Error connecting to %s apps socket", sock_info
->name
);
317 * If we cannot find the sessiond daemon, don't delay
318 * constructor execution.
320 ret
= handle_register_done(sock_info
);
322 pthread_mutex_unlock(<tng_ust_comm_mutex
);
327 sock_info
->socket
= sock
= ret
;
330 * Create only one root handle per listener thread for the whole
333 if (sock_info
->root_handle
== -1) {
334 ret
= lttng_abi_create_root_handle();
336 ERR("Error creating root handle");
337 pthread_mutex_unlock(<tng_ust_comm_mutex
);
340 sock_info
->root_handle
= ret
;
343 ret
= register_app_to_sessiond(sock
);
345 ERR("Error registering to %s apps socket", sock_info
->name
);
347 * If we cannot register to the sessiond daemon, don't
348 * delay constructor execution.
350 ret
= handle_register_done(sock_info
);
352 pthread_mutex_unlock(<tng_ust_comm_mutex
);
356 pthread_mutex_unlock(<tng_ust_comm_mutex
);
360 struct lttcomm_ust_msg lum
;
362 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
364 case 0: /* orderly shutdown */
365 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
368 DBG("message received\n");
369 ret
= handle_message(sock_info
, sock
, &lum
);
371 ERR("Error handling message for %s socket", sock_info
->name
);
375 if (errno
== ECONNRESET
) {
376 ERR("%s remote end closed connection\n", sock_info
->name
);
381 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
387 goto restart
; /* try to reconnect */
393 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
396 int get_timeout(struct timespec
*constructor_timeout
)
398 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
402 str_delay
= getenv("UST_REGISTER_TIMEOUT");
404 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
407 switch (constructor_delay_ms
) {
408 case -1:/* fall-through */
410 return constructor_delay_ms
;
416 * If we are unable to find the current time, don't wait.
418 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
422 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
423 constructor_timeout
->tv_nsec
+=
424 (constructor_delay_ms
% 1000UL) * 1000000UL;
425 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
426 constructor_timeout
->tv_sec
++;
427 constructor_timeout
->tv_nsec
-= 1000000000UL;
433 * sessiond monitoring thread: monitor presence of global and per-user
434 * sessiond by polling the application common named pipe.
438 void __attribute__((constructor
)) lttng_ust_init(void)
440 struct timespec constructor_timeout
;
444 if (uatomic_xchg(&initialized
, 1) == 1)
448 * We want precise control over the order in which we construct
449 * our sub-libraries vs starting to receive commands from
450 * sessiond (otherwise leading to errors when trying to create
451 * sessiond before the init functions are completed).
455 ltt_ring_buffer_metadata_client_init();
456 ltt_ring_buffer_client_overwrite_init();
457 ltt_ring_buffer_client_discard_init();
459 timeout_mode
= get_timeout(&constructor_timeout
);
461 ret
= sem_init(&constructor_wait
, 0, 0);
464 ret
= setup_local_apps();
466 ERR("Error setting up to local apps");
468 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
469 ust_listener_thread
, &local_apps
);
471 if (local_apps
.allowed
) {
472 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
473 ust_listener_thread
, &global_apps
);
475 handle_register_done(&local_apps
);
478 switch (timeout_mode
) {
479 case 1: /* timeout wait */
481 ret
= sem_timedwait(&constructor_wait
,
482 &constructor_timeout
);
483 } while (ret
< 0 && errno
== EINTR
);
484 if (ret
< 0 && errno
== ETIMEDOUT
) {
485 ERR("Timed out waiting for ltt-sessiond");
490 case -1:/* wait forever */
492 ret
= sem_wait(&constructor_wait
);
493 } while (ret
< 0 && errno
== EINTR
);
496 case 0: /* no timeout */
501 void __attribute__((destructor
)) lttng_ust_exit(void)
506 * Using pthread_cancel here because:
507 * A) we don't want to hang application teardown.
508 * B) the thread is not allocating any resource.
512 * Require the communication thread to quit. Synchronize with
513 * mutexes to ensure it is not in a mutex critical section when
514 * pthread_cancel is later called.
516 pthread_mutex_lock(<tng_ust_comm_mutex
);
517 lttng_ust_comm_should_quit
= 1;
518 pthread_mutex_unlock(<tng_ust_comm_mutex
);
520 ret
= pthread_cancel(global_apps
.ust_listener
);
522 ERR("Error cancelling global ust listener thread");
525 cleanup_sock_info(&global_apps
);
527 if (local_apps
.allowed
) {
528 ret
= pthread_cancel(local_apps
.ust_listener
);
530 ERR("Error cancelling local ust listener thread");
533 cleanup_sock_info(&local_apps
);
536 lttng_ust_abi_exit();
538 ltt_ring_buffer_client_discard_exit();
539 ltt_ring_buffer_client_overwrite_exit();
540 ltt_ring_buffer_metadata_client_exit();