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>
27 #include <semaphore.h>
30 #include <urcu/uatomic.h>
32 #include <lttng-ust-comm.h>
33 #include <ust/usterr-signal-safe.h>
34 #include <ust/lttng-ust-abi.h>
35 #include <ust/tracepoint.h>
38 * Has lttng ust comm constructor been called ?
40 static int initialized
;
43 * communication thread mutex. Held when handling a command, also held
44 * by fork() to deal with removal of threads, and by exit path.
46 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
48 /* Should the ust comm thread quit ? */
49 static int lttng_ust_comm_should_quit
;
52 * Wait for either of these before continuing to the main
54 * - the register_done message from sessiond daemon
55 * (will let the sessiond daemon enable sessions before main
57 * - sessiond daemon is not reachable.
58 * - timeout (ensuring applications are resilient to session
61 static sem_t constructor_wait
;
63 * Doing this for both the global and local sessiond.
65 static int sem_count
= { 2 };
68 * Info about socket and associated listener thread.
72 char sock_path
[PATH_MAX
];
74 pthread_t ust_listener
; /* listener thread */
76 int constructor_sem_posted
;;
79 /* Socket from app (connect) to session daemon (listen) for communication */
80 struct sock_info global_apps
= {
82 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
87 /* TODO: allow global_apps_sock_path override */
89 struct sock_info local_apps
= {
95 extern void ltt_ring_buffer_client_overwrite_init(void);
96 extern void ltt_ring_buffer_client_discard_init(void);
97 extern void ltt_ring_buffer_metadata_client_init(void);
98 extern void ltt_ring_buffer_client_overwrite_exit(void);
99 extern void ltt_ring_buffer_client_discard_exit(void);
100 extern void ltt_ring_buffer_metadata_client_exit(void);
103 int setup_local_apps_socket(void)
105 const char *home_dir
;
107 home_dir
= (const char *) getenv("HOME");
110 snprintf(local_apps
.sock_path
, PATH_MAX
,
111 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
116 int register_app_to_sessiond(int socket
)
126 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
127 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
128 reg_msg
.pid
= getpid();
129 reg_msg
.uid
= getuid();
131 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
132 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
138 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
142 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
145 DBG("message successfully sent");
148 if (errno
== ECONNRESET
) {
149 printf("remote end closed connection\n");
154 printf("incorrect message size: %zd\n", len
);
160 int handle_register_done(struct sock_info
*sock_info
)
164 if (sock_info
->constructor_sem_posted
)
166 sock_info
->constructor_sem_posted
= 1;
167 ret
= uatomic_add_return(&sem_count
, -1);
168 fprintf(stderr
, "DEC ret %d\n", ret
);
170 fprintf(stderr
, "POST\n");
171 ret
= sem_post(&constructor_wait
);
178 int handle_message(struct sock_info
*sock_info
,
179 int sock
, struct lttcomm_ust_msg
*lum
)
182 const struct objd_ops
*ops
;
183 struct lttcomm_ust_reply lur
;
185 pthread_mutex_lock(<tng_ust_comm_mutex
);
187 memset(&lur
, 0, sizeof(lur
));
189 if (lttng_ust_comm_should_quit
) {
194 ops
= objd_ops(lum
->handle
);
201 case LTTNG_UST_REGISTER_DONE
:
202 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
203 ret
= handle_register_done(sock_info
);
207 case LTTNG_UST_RELEASE
:
208 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
211 ret
= objd_unref(lum
->handle
);
215 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
216 (unsigned long) &lum
->u
);
223 lur
.handle
= lum
->handle
;
227 lur
.ret_code
= LTTCOMM_OK
;
229 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
231 ret
= send_reply(sock
, &lur
);
233 pthread_mutex_unlock(<tng_ust_comm_mutex
);
238 void cleanup_sock_info(struct sock_info
*sock_info
)
242 if (sock_info
->socket
!= -1) {
243 ret
= close(sock_info
->socket
);
245 ERR("Error closing local apps socket");
247 sock_info
->socket
= -1;
249 if (sock_info
->root_handle
!= -1) {
250 ret
= objd_unref(sock_info
->root_handle
);
252 ERR("Error unref root handle");
254 sock_info
->root_handle
= -1;
259 * This thread does not allocate any resource, except within
260 * handle_message, within mutex protection. This mutex protects against
262 * The other moment it allocates resources is at socket connexion, which
263 * is also protected by the mutex.
266 void *ust_listener_thread(void *arg
)
268 struct sock_info
*sock_info
= arg
;
271 /* Restart trying to connect to the session daemon */
273 pthread_mutex_lock(<tng_ust_comm_mutex
);
275 if (lttng_ust_comm_should_quit
) {
276 pthread_mutex_unlock(<tng_ust_comm_mutex
);
280 if (sock_info
->socket
!= -1) {
281 ret
= close(sock_info
->socket
);
283 ERR("Error closing %s apps socket", sock_info
->name
);
285 sock_info
->socket
= -1;
288 /* Check for sessiond availability with pipe TODO */
291 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
293 ERR("Error connecting to %s apps socket", sock_info
->name
);
295 * If we cannot find the sessiond daemon, don't delay
296 * constructor execution.
298 ret
= handle_register_done(sock_info
);
300 pthread_mutex_unlock(<tng_ust_comm_mutex
);
305 sock_info
->socket
= sock
= ret
;
308 * Create only one root handle per listener thread for the whole
311 if (sock_info
->root_handle
== -1) {
312 ret
= lttng_abi_create_root_handle();
314 ERR("Error creating root handle");
315 pthread_mutex_unlock(<tng_ust_comm_mutex
);
318 sock_info
->root_handle
= ret
;
321 ret
= register_app_to_sessiond(sock
);
323 ERR("Error registering to %s apps socket", sock_info
->name
);
325 * If we cannot register to the sessiond daemon, don't
326 * delay constructor execution.
328 ret
= handle_register_done(sock_info
);
330 pthread_mutex_unlock(<tng_ust_comm_mutex
);
334 pthread_mutex_unlock(<tng_ust_comm_mutex
);
338 struct lttcomm_ust_msg lum
;
340 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
342 case 0: /* orderly shutdown */
343 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
346 DBG("message received\n");
347 ret
= handle_message(sock_info
, sock
, &lum
);
349 ERR("Error handling message for %s socket", sock_info
->name
);
353 if (errno
== ECONNRESET
) {
354 ERR("%s remote end closed connection\n", sock_info
->name
);
359 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
365 goto restart
; /* try to reconnect */
371 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
374 int get_timeout(struct timespec
*constructor_timeout
)
376 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
380 str_delay
= getenv("UST_REGISTER_TIMEOUT");
382 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
385 switch (constructor_delay_ms
) {
386 case -1:/* fall-through */
388 return constructor_delay_ms
;
394 * If we are unable to find the current time, don't wait.
396 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
400 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
401 constructor_timeout
->tv_nsec
+=
402 (constructor_delay_ms
% 1000UL) * 1000000UL;
403 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
404 constructor_timeout
->tv_sec
++;
405 constructor_timeout
->tv_nsec
-= 1000000000UL;
411 * sessiond monitoring thread: monitor presence of global and per-user
412 * sessiond by polling the application common named pipe.
416 void __attribute__((constructor
)) lttng_ust_init(void)
418 struct timespec constructor_timeout
;
422 if (uatomic_xchg(&initialized
, 1) == 1)
426 * We want precise control over the order in which we construct
427 * our sub-libraries vs starting to receive commands from
428 * sessiond (otherwise leading to errors when trying to create
429 * sessiond before the init functions are completed).
433 ltt_ring_buffer_metadata_client_init();
434 ltt_ring_buffer_client_overwrite_init();
435 ltt_ring_buffer_client_discard_init();
437 timeout_mode
= get_timeout(&constructor_timeout
);
439 ret
= sem_init(&constructor_wait
, 0, 0);
442 ret
= setup_local_apps_socket();
444 ERR("Error setting up to local apps socket");
447 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
448 ust_listener_thread
, &global_apps
);
449 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
450 ust_listener_thread
, &local_apps
);
452 switch (timeout_mode
) {
453 case 1: /* timeout wait */
455 ret
= sem_timedwait(&constructor_wait
,
456 &constructor_timeout
);
457 } while (ret
< 0 && errno
== EINTR
);
458 if (ret
< 0 && errno
== ETIMEDOUT
) {
459 ERR("Timed out waiting for ltt-sessiond");
464 case -1:/* wait forever */
466 ret
= sem_wait(&constructor_wait
);
467 } while (ret
< 0 && errno
== EINTR
);
470 case 0: /* no timeout */
475 void __attribute__((destructor
)) lttng_ust_exit(void)
480 * Using pthread_cancel here because:
481 * A) we don't want to hang application teardown.
482 * B) the thread is not allocating any resource.
486 * Require the communication thread to quit. Synchronize with
487 * mutexes to ensure it is not in a mutex critical section when
488 * pthread_cancel is later called.
490 pthread_mutex_lock(<tng_ust_comm_mutex
);
491 lttng_ust_comm_should_quit
= 1;
492 pthread_mutex_unlock(<tng_ust_comm_mutex
);
495 ret
= pthread_cancel(global_apps
.ust_listener
);
497 ERR("Error cancelling global ust listener thread");
501 cleanup_sock_info(&global_apps
);
503 ret
= pthread_cancel(local_apps
.ust_listener
);
505 ERR("Error cancelling local ust listener thread");
508 cleanup_sock_info(&local_apps
);
510 lttng_ust_abi_exit();
512 ltt_ring_buffer_client_discard_exit();
513 ltt_ring_buffer_client_overwrite_exit();
514 ltt_ring_buffer_metadata_client_exit();