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
)
128 char name
[16]; /* process name */
131 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
132 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
133 reg_msg
.pid
= getpid();
134 reg_msg
.ppid
= getppid();
135 reg_msg
.uid
= getuid();
136 reg_msg
.gid
= getgid();
137 prctl_ret
= prctl(PR_GET_NAME
, (unsigned long) reg_msg
.name
, 0, 0, 0);
139 ERR("Error executing prctl");
143 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
144 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
150 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
154 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
157 DBG("message successfully sent");
160 if (errno
== ECONNRESET
) {
161 printf("remote end closed connection\n");
166 printf("incorrect message size: %zd\n", len
);
172 int handle_register_done(struct sock_info
*sock_info
)
176 if (sock_info
->constructor_sem_posted
)
178 sock_info
->constructor_sem_posted
= 1;
179 ret
= uatomic_add_return(&sem_count
, -1);
181 ret
= sem_post(&constructor_wait
);
188 int handle_message(struct sock_info
*sock_info
,
189 int sock
, struct lttcomm_ust_msg
*lum
)
192 const struct objd_ops
*ops
;
193 struct lttcomm_ust_reply lur
;
195 pthread_mutex_lock(<tng_ust_comm_mutex
);
197 memset(&lur
, 0, sizeof(lur
));
199 if (lttng_ust_comm_should_quit
) {
204 ops
= objd_ops(lum
->handle
);
211 case LTTNG_UST_REGISTER_DONE
:
212 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
213 ret
= handle_register_done(sock_info
);
217 case LTTNG_UST_RELEASE
:
218 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
221 ret
= objd_unref(lum
->handle
);
225 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
226 (unsigned long) &lum
->u
);
233 lur
.handle
= lum
->handle
;
237 lur
.ret_code
= LTTCOMM_OK
;
239 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
241 ret
= send_reply(sock
, &lur
);
243 pthread_mutex_unlock(<tng_ust_comm_mutex
);
248 void cleanup_sock_info(struct sock_info
*sock_info
)
252 if (sock_info
->socket
!= -1) {
253 ret
= close(sock_info
->socket
);
255 ERR("Error closing local apps socket");
257 sock_info
->socket
= -1;
259 if (sock_info
->root_handle
!= -1) {
260 ret
= objd_unref(sock_info
->root_handle
);
262 ERR("Error unref root handle");
264 sock_info
->root_handle
= -1;
269 * This thread does not allocate any resource, except within
270 * handle_message, within mutex protection. This mutex protects against
272 * The other moment it allocates resources is at socket connexion, which
273 * is also protected by the mutex.
276 void *ust_listener_thread(void *arg
)
278 struct sock_info
*sock_info
= arg
;
281 /* Restart trying to connect to the session daemon */
283 pthread_mutex_lock(<tng_ust_comm_mutex
);
285 if (lttng_ust_comm_should_quit
) {
286 pthread_mutex_unlock(<tng_ust_comm_mutex
);
290 if (sock_info
->socket
!= -1) {
291 ret
= close(sock_info
->socket
);
293 ERR("Error closing %s apps socket", sock_info
->name
);
295 sock_info
->socket
= -1;
298 /* Check for sessiond availability with pipe TODO */
301 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
303 ERR("Error connecting to %s apps socket", sock_info
->name
);
305 * If we cannot find the sessiond daemon, don't delay
306 * constructor execution.
308 ret
= handle_register_done(sock_info
);
310 pthread_mutex_unlock(<tng_ust_comm_mutex
);
315 sock_info
->socket
= sock
= ret
;
318 * Create only one root handle per listener thread for the whole
321 if (sock_info
->root_handle
== -1) {
322 ret
= lttng_abi_create_root_handle();
324 ERR("Error creating root handle");
325 pthread_mutex_unlock(<tng_ust_comm_mutex
);
328 sock_info
->root_handle
= ret
;
331 ret
= register_app_to_sessiond(sock
);
333 ERR("Error registering to %s apps socket", sock_info
->name
);
335 * If we cannot register to the sessiond daemon, don't
336 * delay constructor execution.
338 ret
= handle_register_done(sock_info
);
340 pthread_mutex_unlock(<tng_ust_comm_mutex
);
344 pthread_mutex_unlock(<tng_ust_comm_mutex
);
348 struct lttcomm_ust_msg lum
;
350 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
352 case 0: /* orderly shutdown */
353 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
356 DBG("message received\n");
357 ret
= handle_message(sock_info
, sock
, &lum
);
359 ERR("Error handling message for %s socket", sock_info
->name
);
363 if (errno
== ECONNRESET
) {
364 ERR("%s remote end closed connection\n", sock_info
->name
);
369 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
375 goto restart
; /* try to reconnect */
381 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
384 int get_timeout(struct timespec
*constructor_timeout
)
386 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
390 str_delay
= getenv("UST_REGISTER_TIMEOUT");
392 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
395 switch (constructor_delay_ms
) {
396 case -1:/* fall-through */
398 return constructor_delay_ms
;
404 * If we are unable to find the current time, don't wait.
406 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
410 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
411 constructor_timeout
->tv_nsec
+=
412 (constructor_delay_ms
% 1000UL) * 1000000UL;
413 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
414 constructor_timeout
->tv_sec
++;
415 constructor_timeout
->tv_nsec
-= 1000000000UL;
421 * sessiond monitoring thread: monitor presence of global and per-user
422 * sessiond by polling the application common named pipe.
426 void __attribute__((constructor
)) lttng_ust_init(void)
428 struct timespec constructor_timeout
;
432 if (uatomic_xchg(&initialized
, 1) == 1)
436 * We want precise control over the order in which we construct
437 * our sub-libraries vs starting to receive commands from
438 * sessiond (otherwise leading to errors when trying to create
439 * sessiond before the init functions are completed).
443 ltt_ring_buffer_metadata_client_init();
444 ltt_ring_buffer_client_overwrite_init();
445 ltt_ring_buffer_client_discard_init();
447 timeout_mode
= get_timeout(&constructor_timeout
);
449 ret
= sem_init(&constructor_wait
, 0, 0);
452 ret
= setup_local_apps_socket();
454 ERR("Error setting up to local apps socket");
457 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
458 ust_listener_thread
, &global_apps
);
459 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
460 ust_listener_thread
, &local_apps
);
462 switch (timeout_mode
) {
463 case 1: /* timeout wait */
465 ret
= sem_timedwait(&constructor_wait
,
466 &constructor_timeout
);
467 } while (ret
< 0 && errno
== EINTR
);
468 if (ret
< 0 && errno
== ETIMEDOUT
) {
469 ERR("Timed out waiting for ltt-sessiond");
474 case -1:/* wait forever */
476 ret
= sem_wait(&constructor_wait
);
477 } while (ret
< 0 && errno
== EINTR
);
480 case 0: /* no timeout */
485 void __attribute__((destructor
)) lttng_ust_exit(void)
490 * Using pthread_cancel here because:
491 * A) we don't want to hang application teardown.
492 * B) the thread is not allocating any resource.
496 * Require the communication thread to quit. Synchronize with
497 * mutexes to ensure it is not in a mutex critical section when
498 * pthread_cancel is later called.
500 pthread_mutex_lock(<tng_ust_comm_mutex
);
501 lttng_ust_comm_should_quit
= 1;
502 pthread_mutex_unlock(<tng_ust_comm_mutex
);
505 ret
= pthread_cancel(global_apps
.ust_listener
);
507 ERR("Error cancelling global ust listener thread");
511 cleanup_sock_info(&global_apps
);
513 ret
= pthread_cancel(local_apps
.ust_listener
);
515 ERR("Error cancelling local ust listener thread");
518 cleanup_sock_info(&local_apps
);
520 lttng_ust_abi_exit();
522 ltt_ring_buffer_client_discard_exit();
523 ltt_ring_buffer_client_overwrite_exit();
524 ltt_ring_buffer_metadata_client_exit();