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
;;
80 /* Socket from app (connect) to session daemon (listen) for communication */
81 struct sock_info global_apps
= {
83 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
88 /* TODO: allow global_apps_sock_path override */
90 struct sock_info local_apps
= {
96 extern void ltt_ring_buffer_client_overwrite_init(void);
97 extern void ltt_ring_buffer_client_discard_init(void);
98 extern void ltt_ring_buffer_metadata_client_init(void);
99 extern void ltt_ring_buffer_client_overwrite_exit(void);
100 extern void ltt_ring_buffer_client_discard_exit(void);
101 extern void ltt_ring_buffer_metadata_client_exit(void);
104 int setup_local_apps_socket(void)
106 const char *home_dir
;
108 home_dir
= (const char *) getenv("HOME");
111 snprintf(local_apps
.sock_path
, PATH_MAX
,
112 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
117 int register_app_to_sessiond(int socket
)
126 char name
[16]; /* process name */
129 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
130 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
131 reg_msg
.pid
= getpid();
132 reg_msg
.uid
= getuid();
133 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
135 ERR("Error executing prctl");
139 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
140 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
146 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
150 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
153 DBG("message successfully sent");
156 if (errno
== ECONNRESET
) {
157 printf("remote end closed connection\n");
162 printf("incorrect message size: %zd\n", len
);
168 int handle_register_done(struct sock_info
*sock_info
)
172 if (sock_info
->constructor_sem_posted
)
174 sock_info
->constructor_sem_posted
= 1;
175 ret
= uatomic_add_return(&sem_count
, -1);
177 ret
= sem_post(&constructor_wait
);
184 int handle_message(struct sock_info
*sock_info
,
185 int sock
, struct lttcomm_ust_msg
*lum
)
188 const struct objd_ops
*ops
;
189 struct lttcomm_ust_reply lur
;
191 pthread_mutex_lock(<tng_ust_comm_mutex
);
193 memset(&lur
, 0, sizeof(lur
));
195 if (lttng_ust_comm_should_quit
) {
200 ops
= objd_ops(lum
->handle
);
207 case LTTNG_UST_REGISTER_DONE
:
208 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
209 ret
= handle_register_done(sock_info
);
213 case LTTNG_UST_RELEASE
:
214 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
217 ret
= objd_unref(lum
->handle
);
221 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
222 (unsigned long) &lum
->u
);
229 lur
.handle
= lum
->handle
;
233 lur
.ret_code
= LTTCOMM_OK
;
235 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
237 ret
= send_reply(sock
, &lur
);
239 pthread_mutex_unlock(<tng_ust_comm_mutex
);
244 void cleanup_sock_info(struct sock_info
*sock_info
)
248 if (sock_info
->socket
!= -1) {
249 ret
= close(sock_info
->socket
);
251 ERR("Error closing local apps socket");
253 sock_info
->socket
= -1;
255 if (sock_info
->root_handle
!= -1) {
256 ret
= objd_unref(sock_info
->root_handle
);
258 ERR("Error unref root handle");
260 sock_info
->root_handle
= -1;
265 * This thread does not allocate any resource, except within
266 * handle_message, within mutex protection. This mutex protects against
268 * The other moment it allocates resources is at socket connexion, which
269 * is also protected by the mutex.
272 void *ust_listener_thread(void *arg
)
274 struct sock_info
*sock_info
= arg
;
277 /* Restart trying to connect to the session daemon */
279 pthread_mutex_lock(<tng_ust_comm_mutex
);
281 if (lttng_ust_comm_should_quit
) {
282 pthread_mutex_unlock(<tng_ust_comm_mutex
);
286 if (sock_info
->socket
!= -1) {
287 ret
= close(sock_info
->socket
);
289 ERR("Error closing %s apps socket", sock_info
->name
);
291 sock_info
->socket
= -1;
294 /* Check for sessiond availability with pipe TODO */
297 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
299 ERR("Error connecting to %s apps socket", sock_info
->name
);
301 * If we cannot find the sessiond daemon, don't delay
302 * constructor execution.
304 ret
= handle_register_done(sock_info
);
306 pthread_mutex_unlock(<tng_ust_comm_mutex
);
311 sock_info
->socket
= sock
= ret
;
314 * Create only one root handle per listener thread for the whole
317 if (sock_info
->root_handle
== -1) {
318 ret
= lttng_abi_create_root_handle();
320 ERR("Error creating root handle");
321 pthread_mutex_unlock(<tng_ust_comm_mutex
);
324 sock_info
->root_handle
= ret
;
327 ret
= register_app_to_sessiond(sock
);
329 ERR("Error registering to %s apps socket", sock_info
->name
);
331 * If we cannot register to the sessiond daemon, don't
332 * delay constructor execution.
334 ret
= handle_register_done(sock_info
);
336 pthread_mutex_unlock(<tng_ust_comm_mutex
);
340 pthread_mutex_unlock(<tng_ust_comm_mutex
);
344 struct lttcomm_ust_msg lum
;
346 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
348 case 0: /* orderly shutdown */
349 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
352 DBG("message received\n");
353 ret
= handle_message(sock_info
, sock
, &lum
);
355 ERR("Error handling message for %s socket", sock_info
->name
);
359 if (errno
== ECONNRESET
) {
360 ERR("%s remote end closed connection\n", sock_info
->name
);
365 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
371 goto restart
; /* try to reconnect */
377 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
380 int get_timeout(struct timespec
*constructor_timeout
)
382 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
386 str_delay
= getenv("UST_REGISTER_TIMEOUT");
388 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
391 switch (constructor_delay_ms
) {
392 case -1:/* fall-through */
394 return constructor_delay_ms
;
400 * If we are unable to find the current time, don't wait.
402 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
406 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
407 constructor_timeout
->tv_nsec
+=
408 (constructor_delay_ms
% 1000UL) * 1000000UL;
409 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
410 constructor_timeout
->tv_sec
++;
411 constructor_timeout
->tv_nsec
-= 1000000000UL;
417 * sessiond monitoring thread: monitor presence of global and per-user
418 * sessiond by polling the application common named pipe.
422 void __attribute__((constructor
)) lttng_ust_init(void)
424 struct timespec constructor_timeout
;
428 if (uatomic_xchg(&initialized
, 1) == 1)
432 * We want precise control over the order in which we construct
433 * our sub-libraries vs starting to receive commands from
434 * sessiond (otherwise leading to errors when trying to create
435 * sessiond before the init functions are completed).
439 ltt_ring_buffer_metadata_client_init();
440 ltt_ring_buffer_client_overwrite_init();
441 ltt_ring_buffer_client_discard_init();
443 timeout_mode
= get_timeout(&constructor_timeout
);
445 ret
= sem_init(&constructor_wait
, 0, 0);
448 ret
= setup_local_apps_socket();
450 ERR("Error setting up to local apps socket");
453 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
454 ust_listener_thread
, &global_apps
);
455 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
456 ust_listener_thread
, &local_apps
);
458 switch (timeout_mode
) {
459 case 1: /* timeout wait */
461 ret
= sem_timedwait(&constructor_wait
,
462 &constructor_timeout
);
463 } while (ret
< 0 && errno
== EINTR
);
464 if (ret
< 0 && errno
== ETIMEDOUT
) {
465 ERR("Timed out waiting for ltt-sessiond");
470 case -1:/* wait forever */
472 ret
= sem_wait(&constructor_wait
);
473 } while (ret
< 0 && errno
== EINTR
);
476 case 0: /* no timeout */
481 void __attribute__((destructor
)) lttng_ust_exit(void)
486 * Using pthread_cancel here because:
487 * A) we don't want to hang application teardown.
488 * B) the thread is not allocating any resource.
492 * Require the communication thread to quit. Synchronize with
493 * mutexes to ensure it is not in a mutex critical section when
494 * pthread_cancel is later called.
496 pthread_mutex_lock(<tng_ust_comm_mutex
);
497 lttng_ust_comm_should_quit
= 1;
498 pthread_mutex_unlock(<tng_ust_comm_mutex
);
501 ret
= pthread_cancel(global_apps
.ust_listener
);
503 ERR("Error cancelling global ust listener thread");
507 cleanup_sock_info(&global_apps
);
509 ret
= pthread_cancel(local_apps
.ust_listener
);
511 ERR("Error cancelling local ust listener thread");
514 cleanup_sock_info(&local_apps
);
516 lttng_ust_abi_exit();
518 ltt_ring_buffer_client_discard_exit();
519 ltt_ring_buffer_client_overwrite_exit();
520 ltt_ring_buffer_metadata_client_exit();