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>
26 #include <ust/lttng-ust-abi.h>
27 #include <lttng-ust-comm.h>
28 #include <ust/usterr-signal-safe.h>
30 #include <semaphore.h>
33 #include <urcu/uatomic.h>
36 * communication thread mutex. Held when handling a command, also held
37 * by fork() to deal with removal of threads, and by exit path.
39 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
41 /* Should the ust comm thread quit ? */
42 static int lttng_ust_comm_should_quit
;
45 * Wait for either of these before continuing to the main
47 * - the register_done message from sessiond daemon
48 * (will let the sessiond daemon enable sessions before main
50 * - sessiond daemon is not reachable.
51 * - timeout (ensuring applications are resilient to session
54 static sem_t constructor_wait
;
56 * Doing this for both the global and local sessiond.
58 static int sem_count
= { 2 };
61 * Info about socket and associated listener thread.
65 char sock_path
[PATH_MAX
];
67 pthread_t ust_listener
; /* listener thread */
71 /* Socket from app (connect) to session daemon (listen) for communication */
72 struct sock_info global_apps
= {
74 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
79 /* TODO: allow global_apps_sock_path override */
81 struct sock_info local_apps
= {
88 int setup_local_apps_socket(void)
92 home_dir
= (const char *) getenv("HOME");
95 snprintf(local_apps
.sock_path
, PATH_MAX
,
96 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
101 int register_app_to_sessiond(int socket
)
111 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
112 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
113 reg_msg
.pid
= getpid();
114 reg_msg
.uid
= getuid();
116 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
117 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
123 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
127 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
130 DBG("message successfully sent");
133 if (errno
== ECONNRESET
) {
134 printf("remote end closed connection\n");
139 printf("incorrect message size: %zd\n", len
);
145 int handle_register_done(void)
149 ret
= uatomic_add_return(&sem_count
, -1);
151 ret
= sem_post(&constructor_wait
);
158 int handle_message(struct sock_info
*sock_info
,
159 int sock
, struct lttcomm_ust_msg
*lum
)
162 const struct objd_ops
*ops
;
163 struct lttcomm_ust_reply lur
;
165 pthread_mutex_lock(<tng_ust_comm_mutex
);
167 memset(&lur
, 0, sizeof(lur
));
169 if (lttng_ust_comm_should_quit
) {
174 ops
= objd_ops(lum
->handle
);
181 case LTTNG_UST_REGISTER_DONE
:
182 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
183 ret
= handle_register_done();
187 case LTTNG_UST_RELEASE
:
188 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
191 ret
= objd_unref(lum
->handle
);
195 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
196 (unsigned long) &lum
->u
);
203 lur
.handle
= lum
->handle
;
207 lur
.ret_code
= LTTCOMM_OK
;
209 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
211 ret
= send_reply(sock
, &lur
);
213 pthread_mutex_unlock(<tng_ust_comm_mutex
);
218 void cleanup_sock_info(struct sock_info
*sock_info
)
222 if (sock_info
->socket
!= -1) {
223 ret
= close(sock_info
->socket
);
225 ERR("Error closing local apps socket");
227 sock_info
->socket
= -1;
229 if (sock_info
->root_handle
!= -1) {
230 ret
= objd_unref(sock_info
->root_handle
);
232 ERR("Error unref root handle");
234 sock_info
->root_handle
= -1;
239 * This thread does not allocate any resource, except within
240 * handle_message, within mutex protection. This mutex protects against
242 * The other moment it allocates resources is at socket connexion, which
243 * is also protected by the mutex.
246 void *ust_listener_thread(void *arg
)
248 struct sock_info
*sock_info
= arg
;
251 /* Restart trying to connect to the session daemon */
253 pthread_mutex_lock(<tng_ust_comm_mutex
);
255 if (lttng_ust_comm_should_quit
) {
256 pthread_mutex_unlock(<tng_ust_comm_mutex
);
260 if (sock_info
->socket
!= -1) {
261 ret
= close(sock_info
->socket
);
263 ERR("Error closing %s apps socket", sock_info
->name
);
265 sock_info
->socket
= -1;
268 /* Check for sessiond availability with pipe TODO */
271 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
273 ERR("Error connecting to %s apps socket", sock_info
->name
);
275 * If we cannot find the sessiond daemon, don't delay
276 * constructor execution.
278 ret
= handle_register_done();
280 pthread_mutex_unlock(<tng_ust_comm_mutex
);
285 sock_info
->socket
= sock
= ret
;
288 * Create only one root handle per listener thread for the whole
291 if (sock_info
->root_handle
== -1) {
292 ret
= lttng_abi_create_root_handle();
294 ERR("Error creating root handle");
295 pthread_mutex_unlock(<tng_ust_comm_mutex
);
298 sock_info
->root_handle
= ret
;
301 ret
= register_app_to_sessiond(sock
);
303 ERR("Error registering to %s apps socket", sock_info
->name
);
305 * If we cannot register to the sessiond daemon, don't
306 * delay constructor execution.
308 ret
= handle_register_done();
310 pthread_mutex_unlock(<tng_ust_comm_mutex
);
314 pthread_mutex_unlock(<tng_ust_comm_mutex
);
318 struct lttcomm_ust_msg lum
;
320 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
322 case 0: /* orderly shutdown */
323 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
326 DBG("message received\n");
327 ret
= handle_message(sock_info
, sock
, &lum
);
329 ERR("Error handling message for %s socket", sock_info
->name
);
333 if (errno
== ECONNRESET
) {
334 ERR("%s remote end closed connection\n", sock_info
->name
);
339 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
345 goto restart
; /* try to reconnect */
351 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
354 int get_timeout(struct timespec
*constructor_timeout
)
356 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
360 str_delay
= getenv("UST_REGISTER_TIMEOUT");
362 constructor_delay_ms
= strtol(str_delay
, NULL
, 10);
365 switch (constructor_delay_ms
) {
366 case -1:/* fall-through */
368 return constructor_delay_ms
;
374 * If we are unable to find the current time, don't wait.
376 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
380 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
381 constructor_timeout
->tv_nsec
+=
382 (constructor_delay_ms
% 1000UL) * 1000000UL;
383 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
384 constructor_timeout
->tv_sec
++;
385 constructor_timeout
->tv_nsec
-= 1000000000UL;
391 * sessiond monitoring thread: monitor presence of global and per-user
392 * sessiond by polling the application common named pipe.
396 void __attribute__((constructor
)) lttng_ust_comm_init(void)
398 struct timespec constructor_timeout
;
404 timeout_mode
= get_timeout(&constructor_timeout
);
406 ret
= sem_init(&constructor_wait
, 0, 0);
409 ret
= setup_local_apps_socket();
411 ERR("Error setting up to local apps socket");
414 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
415 ust_listener_thread
, &global_apps
);
416 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
417 ust_listener_thread
, &local_apps
);
419 switch (timeout_mode
) {
420 case 1: /* timeout wait */
422 ret
= sem_timedwait(&constructor_wait
,
423 &constructor_timeout
);
424 } while (ret
< 0 && errno
== EINTR
);
425 if (ret
< 0 && errno
== ETIMEDOUT
) {
426 ERR("Timed out waiting for ltt-sessiond");
431 case -1:/* wait forever */
433 ret
= sem_wait(&constructor_wait
);
434 } while (ret
< 0 && errno
== EINTR
);
437 case 0: /* no timeout */
442 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
447 * Using pthread_cancel here because:
448 * A) we don't want to hang application teardown.
449 * B) the thread is not allocating any resource.
453 * Require the communication thread to quit. Synchronize with
454 * mutexes to ensure it is not in a mutex critical section when
455 * pthread_cancel is later called.
457 pthread_mutex_lock(<tng_ust_comm_mutex
);
458 lttng_ust_comm_should_quit
= 1;
459 pthread_mutex_unlock(<tng_ust_comm_mutex
);
462 ret
= pthread_cancel(global_apps
.ust_listener
);
464 ERR("Error cancelling global ust listener thread");
468 cleanup_sock_info(&global_apps
);
470 ret
= pthread_cancel(local_apps
.ust_listener
);
472 ERR("Error cancelling local ust listener thread");
475 cleanup_sock_info(&local_apps
);
477 lttng_ust_abi_exit();