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>
37 #include <ust/tracepoint-internal.h>
40 * Has lttng ust comm constructor been called ?
42 static int initialized
;
45 * communication thread mutex. Held when handling a command, also held
46 * by fork() to deal with removal of threads, and by exit path.
48 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
50 /* Should the ust comm thread quit ? */
51 static int lttng_ust_comm_should_quit
;
54 * Wait for either of these before continuing to the main
56 * - the register_done message from sessiond daemon
57 * (will let the sessiond daemon enable sessions before main
59 * - sessiond daemon is not reachable.
60 * - timeout (ensuring applications are resilient to session
63 static sem_t constructor_wait
;
65 * Doing this for both the global and local sessiond.
67 static int sem_count
= { 2 };
70 * Info about socket and associated listener thread.
74 char sock_path
[PATH_MAX
];
76 pthread_t ust_listener
; /* listener thread */
78 int constructor_sem_posted
;
82 /* Socket from app (connect) to session daemon (listen) for communication */
83 struct sock_info global_apps
= {
85 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
91 /* TODO: allow global_apps_sock_path override */
93 struct sock_info local_apps
= {
97 .allowed
= 0, /* Check setuid bit first */
100 extern void ltt_ring_buffer_client_overwrite_init(void);
101 extern void ltt_ring_buffer_client_discard_init(void);
102 extern void ltt_ring_buffer_metadata_client_init(void);
103 extern void ltt_ring_buffer_client_overwrite_exit(void);
104 extern void ltt_ring_buffer_client_discard_exit(void);
105 extern void ltt_ring_buffer_metadata_client_exit(void);
108 int setup_local_apps(void)
110 const char *home_dir
;
113 * Disallow per-user tracing for setuid binaries.
115 if (getuid() != geteuid()) {
116 local_apps
.allowed
= 0;
119 local_apps
.allowed
= 1;
121 home_dir
= (const char *) getenv("HOME");
124 snprintf(local_apps
.sock_path
, PATH_MAX
,
125 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
130 int register_app_to_sessiond(int socket
)
141 char name
[16]; /* process name */
144 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
145 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
146 reg_msg
.pid
= getpid();
147 reg_msg
.ppid
= getppid();
148 reg_msg
.uid
= getuid();
149 reg_msg
.gid
= getgid();
150 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
152 ERR("Error executing prctl");
156 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
157 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
163 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
167 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
170 DBG("message successfully sent");
173 if (errno
== ECONNRESET
) {
174 printf("remote end closed connection\n");
179 printf("incorrect message size: %zd\n", len
);
185 int handle_register_done(struct sock_info
*sock_info
)
189 if (sock_info
->constructor_sem_posted
)
191 sock_info
->constructor_sem_posted
= 1;
192 ret
= uatomic_add_return(&sem_count
, -1);
194 ret
= sem_post(&constructor_wait
);
201 int handle_message(struct sock_info
*sock_info
,
202 int sock
, struct lttcomm_ust_msg
*lum
)
205 const struct objd_ops
*ops
;
206 struct lttcomm_ust_reply lur
;
208 pthread_mutex_lock(<tng_ust_comm_mutex
);
210 memset(&lur
, 0, sizeof(lur
));
212 if (lttng_ust_comm_should_quit
) {
217 ops
= objd_ops(lum
->handle
);
224 case LTTNG_UST_REGISTER_DONE
:
225 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
226 ret
= handle_register_done(sock_info
);
230 case LTTNG_UST_RELEASE
:
231 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
234 ret
= objd_unref(lum
->handle
);
238 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
239 (unsigned long) &lum
->u
);
246 lur
.handle
= lum
->handle
;
250 lur
.ret_code
= LTTCOMM_OK
;
252 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
254 ret
= send_reply(sock
, &lur
);
256 pthread_mutex_unlock(<tng_ust_comm_mutex
);
261 void cleanup_sock_info(struct sock_info
*sock_info
)
265 if (sock_info
->socket
!= -1) {
266 ret
= close(sock_info
->socket
);
268 ERR("Error closing local apps socket");
270 sock_info
->socket
= -1;
272 if (sock_info
->root_handle
!= -1) {
273 ret
= objd_unref(sock_info
->root_handle
);
275 ERR("Error unref root handle");
277 sock_info
->root_handle
= -1;
282 * This thread does not allocate any resource, except within
283 * handle_message, within mutex protection. This mutex protects against
285 * The other moment it allocates resources is at socket connexion, which
286 * is also protected by the mutex.
289 void *ust_listener_thread(void *arg
)
291 struct sock_info
*sock_info
= arg
;
294 /* Restart trying to connect to the session daemon */
296 pthread_mutex_lock(<tng_ust_comm_mutex
);
298 if (lttng_ust_comm_should_quit
) {
299 pthread_mutex_unlock(<tng_ust_comm_mutex
);
303 if (sock_info
->socket
!= -1) {
304 ret
= close(sock_info
->socket
);
306 ERR("Error closing %s apps socket", sock_info
->name
);
308 sock_info
->socket
= -1;
311 /* Check for sessiond availability with pipe TODO */
314 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
316 ERR("Error connecting to %s apps socket", sock_info
->name
);
318 * If we cannot find the sessiond daemon, don't delay
319 * constructor execution.
321 ret
= handle_register_done(sock_info
);
323 pthread_mutex_unlock(<tng_ust_comm_mutex
);
328 sock_info
->socket
= sock
= ret
;
331 * Create only one root handle per listener thread for the whole
334 if (sock_info
->root_handle
== -1) {
335 ret
= lttng_abi_create_root_handle();
337 ERR("Error creating root handle");
338 pthread_mutex_unlock(<tng_ust_comm_mutex
);
341 sock_info
->root_handle
= ret
;
344 ret
= register_app_to_sessiond(sock
);
346 ERR("Error registering to %s apps socket", sock_info
->name
);
348 * If we cannot register to the sessiond daemon, don't
349 * delay constructor execution.
351 ret
= handle_register_done(sock_info
);
353 pthread_mutex_unlock(<tng_ust_comm_mutex
);
357 pthread_mutex_unlock(<tng_ust_comm_mutex
);
361 struct lttcomm_ust_msg lum
;
363 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
365 case 0: /* orderly shutdown */
366 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
369 DBG("message received\n");
370 ret
= handle_message(sock_info
, sock
, &lum
);
372 ERR("Error handling message for %s socket", sock_info
->name
);
376 if (errno
== ECONNRESET
) {
377 ERR("%s remote end closed connection\n", sock_info
->name
);
382 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
388 goto restart
; /* try to reconnect */
394 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
397 int get_timeout(struct timespec
*constructor_timeout
)
399 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
403 str_delay
= getenv("UST_REGISTER_TIMEOUT");
405 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
408 switch (constructor_delay_ms
) {
409 case -1:/* fall-through */
411 return constructor_delay_ms
;
417 * If we are unable to find the current time, don't wait.
419 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
423 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
424 constructor_timeout
->tv_nsec
+=
425 (constructor_delay_ms
% 1000UL) * 1000000UL;
426 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
427 constructor_timeout
->tv_sec
++;
428 constructor_timeout
->tv_nsec
-= 1000000000UL;
434 * sessiond monitoring thread: monitor presence of global and per-user
435 * sessiond by polling the application common named pipe.
439 void __attribute__((constructor
)) lttng_ust_init(void)
441 struct timespec constructor_timeout
;
445 if (uatomic_xchg(&initialized
, 1) == 1)
449 * We want precise control over the order in which we construct
450 * our sub-libraries vs starting to receive commands from
451 * sessiond (otherwise leading to errors when trying to create
452 * sessiond before the init functions are completed).
456 ltt_ring_buffer_metadata_client_init();
457 ltt_ring_buffer_client_overwrite_init();
458 ltt_ring_buffer_client_discard_init();
460 timeout_mode
= get_timeout(&constructor_timeout
);
462 ret
= sem_init(&constructor_wait
, 0, 0);
465 ret
= setup_local_apps();
467 ERR("Error setting up to local apps");
469 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
470 ust_listener_thread
, &local_apps
);
472 if (local_apps
.allowed
) {
473 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
474 ust_listener_thread
, &global_apps
);
476 handle_register_done(&local_apps
);
479 switch (timeout_mode
) {
480 case 1: /* timeout wait */
482 ret
= sem_timedwait(&constructor_wait
,
483 &constructor_timeout
);
484 } while (ret
< 0 && errno
== EINTR
);
485 if (ret
< 0 && errno
== ETIMEDOUT
) {
486 ERR("Timed out waiting for ltt-sessiond");
491 case -1:/* wait forever */
493 ret
= sem_wait(&constructor_wait
);
494 } while (ret
< 0 && errno
== EINTR
);
497 case 0: /* no timeout */
502 void __attribute__((destructor
)) lttng_ust_exit(void)
507 * Using pthread_cancel here because:
508 * A) we don't want to hang application teardown.
509 * B) the thread is not allocating any resource.
513 * Require the communication thread to quit. Synchronize with
514 * mutexes to ensure it is not in a mutex critical section when
515 * pthread_cancel is later called.
517 pthread_mutex_lock(<tng_ust_comm_mutex
);
518 lttng_ust_comm_should_quit
= 1;
519 pthread_mutex_unlock(<tng_ust_comm_mutex
);
521 ret
= pthread_cancel(global_apps
.ust_listener
);
523 ERR("Error cancelling global ust listener thread");
526 cleanup_sock_info(&global_apps
);
528 if (local_apps
.allowed
) {
529 ret
= pthread_cancel(local_apps
.ust_listener
);
531 ERR("Error cancelling local ust listener thread");
534 cleanup_sock_info(&local_apps
);
537 lttng_ust_abi_exit();
539 ltt_ring_buffer_client_discard_exit();
540 ltt_ring_buffer_client_overwrite_exit();
541 ltt_ring_buffer_metadata_client_exit();