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>
35 * communication thread mutex. Held when handling a command, also held
36 * by fork() to deal with removal of threads, and by exit path.
38 static pthread_mutex_t lttng_ust_comm_mutex
= PTHREAD_MUTEX_INITIALIZER
;
40 /* Should the ust comm thread quit ? */
41 static int lttng_ust_comm_should_quit
;
44 * Wait for either of these before continuing to the main
46 * - the register_done message from sessiond daemon
47 * (will let the sessiond daemon enable sessions before main
49 * - sessiond daemon is not reachable.
50 * - timeout (ensuring applications are resilient to session
53 static sem_t constructor_wait
;
56 * Info about socket and associated listener thread.
60 char sock_path
[PATH_MAX
];
62 pthread_t ust_listener
; /* listener thread */
66 /* Socket from app (connect) to session daemon (listen) for communication */
67 struct sock_info global_apps
= {
69 .sock_path
= DEFAULT_GLOBAL_APPS_UNIX_SOCK
,
74 /* TODO: allow global_apps_sock_path override */
76 struct sock_info local_apps
= {
83 int setup_local_apps_socket(void)
87 home_dir
= (const char *) getenv("HOME");
90 snprintf(local_apps
.sock_path
, PATH_MAX
,
91 DEFAULT_HOME_APPS_UNIX_SOCK
, home_dir
);
96 int register_app_to_sessiond(int socket
)
106 reg_msg
.major
= LTTNG_UST_COMM_VERSION_MAJOR
;
107 reg_msg
.minor
= LTTNG_UST_COMM_VERSION_MINOR
;
108 reg_msg
.pid
= getpid();
109 reg_msg
.uid
= getuid();
111 ret
= lttcomm_send_unix_sock(socket
, ®_msg
, sizeof(reg_msg
));
112 if (ret
>= 0 && ret
!= sizeof(reg_msg
))
118 int send_reply(int sock
, struct lttcomm_ust_reply
*lur
)
122 len
= lttcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
125 DBG("message successfully sent");
128 if (errno
== ECONNRESET
) {
129 printf("remote end closed connection\n");
134 printf("incorrect message size: %zd\n", len
);
140 int handle_register_done(void)
144 ret
= sem_post(&constructor_wait
);
150 int handle_message(struct sock_info
*sock_info
,
151 int sock
, struct lttcomm_ust_msg
*lum
)
154 const struct objd_ops
*ops
;
155 struct lttcomm_ust_reply lur
;
157 pthread_mutex_lock(<tng_ust_comm_mutex
);
159 memset(&lur
, 0, sizeof(lur
));
161 if (lttng_ust_comm_should_quit
) {
166 ops
= objd_ops(lum
->handle
);
173 case LTTNG_UST_REGISTER_DONE
:
174 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
175 ret
= handle_register_done();
179 case LTTNG_UST_RELEASE
:
180 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
183 ret
= objd_unref(lum
->handle
);
187 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
188 (unsigned long) &lum
->u
);
195 lur
.handle
= lum
->handle
;
199 lur
.ret_code
= LTTCOMM_OK
;
201 lur
.ret_code
= LTTCOMM_SESSION_FAIL
;
203 ret
= send_reply(sock
, &lur
);
205 pthread_mutex_unlock(<tng_ust_comm_mutex
);
210 void cleanup_sock_info(struct sock_info
*sock_info
)
214 if (sock_info
->socket
!= -1) {
215 ret
= close(sock_info
->socket
);
217 ERR("Error closing local apps socket");
219 sock_info
->socket
= -1;
221 if (sock_info
->root_handle
!= -1) {
222 ret
= objd_unref(sock_info
->root_handle
);
224 ERR("Error unref root handle");
226 sock_info
->root_handle
= -1;
231 * This thread does not allocate any resource, except within
232 * handle_message, within mutex protection. This mutex protects against
234 * The other moment it allocates resources is at socket connexion, which
235 * is also protected by the mutex.
238 void *ust_listener_thread(void *arg
)
240 struct sock_info
*sock_info
= arg
;
243 /* Restart trying to connect to the session daemon */
245 pthread_mutex_lock(<tng_ust_comm_mutex
);
247 if (lttng_ust_comm_should_quit
) {
248 pthread_mutex_unlock(<tng_ust_comm_mutex
);
252 if (sock_info
->socket
!= -1) {
253 ret
= close(sock_info
->socket
);
255 ERR("Error closing %s apps socket", sock_info
->name
);
257 sock_info
->socket
= -1;
260 /* Check for sessiond availability with pipe TODO */
263 ret
= lttcomm_connect_unix_sock(sock_info
->sock_path
);
265 ERR("Error connecting to %s apps socket", sock_info
->name
);
267 * If we cannot find the sessiond daemon, don't delay
268 * constructor execution.
270 ret
= handle_register_done();
272 pthread_mutex_unlock(<tng_ust_comm_mutex
);
277 sock_info
->socket
= sock
= ret
;
280 * Create only one root handle per listener thread for the whole
283 if (sock_info
->root_handle
== -1) {
284 ret
= lttng_abi_create_root_handle();
286 ERR("Error creating root handle");
287 pthread_mutex_unlock(<tng_ust_comm_mutex
);
290 sock_info
->root_handle
= ret
;
293 ret
= register_app_to_sessiond(sock
);
295 ERR("Error registering to %s apps socket", sock_info
->name
);
297 * If we cannot register to the sessiond daemon, don't
298 * delay constructor execution.
300 ret
= handle_register_done();
302 pthread_mutex_unlock(<tng_ust_comm_mutex
);
306 pthread_mutex_unlock(<tng_ust_comm_mutex
);
310 struct lttcomm_ust_msg lum
;
312 len
= lttcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
314 case 0: /* orderly shutdown */
315 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info
->name
);
318 DBG("message received\n");
319 ret
= handle_message(sock_info
, sock
, &lum
);
321 ERR("Error handling message for %s socket", sock_info
->name
);
325 if (errno
== ECONNRESET
) {
326 ERR("%s remote end closed connection\n", sock_info
->name
);
331 ERR("incorrect message size (%s socket): %zd\n", sock_info
->name
, len
);
337 goto restart
; /* try to reconnect */
343 int get_timeout(struct timespec
*constructor_timeout
)
345 struct timespec constructor_delay
=
347 .tv_sec
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_S
,
348 .tv_nsec
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_NS
,
350 struct timespec realtime
;
353 ret
= clock_gettime(CLOCK_REALTIME
, &realtime
);
357 constructor_timeout
->tv_sec
=
358 realtime
.tv_sec
+ constructor_delay
.tv_sec
;
359 constructor_timeout
->tv_nsec
=
360 constructor_delay
.tv_nsec
+ realtime
.tv_nsec
;
361 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
362 constructor_timeout
->tv_sec
++;
363 constructor_timeout
->tv_nsec
-= 1000000000UL;
369 * sessiond monitoring thread: monitor presence of global and per-user
370 * sessiond by polling the application common named pipe.
374 void __attribute__((constructor
)) lttng_ust_comm_init(void)
376 struct timespec constructor_timeout
;
381 ret
= get_timeout(&constructor_timeout
);
384 ret
= sem_init(&constructor_wait
, 0, 2);
387 ret
= setup_local_apps_socket();
389 ERR("Error setting up to local apps socket");
393 * Wait for the pthread cond to let us continue to main program
394 * execution. Hold mutex across thread creation, so we start
395 * waiting for the condition before the threads can signal its
398 pthread_mutex_lock(<tng_ust_comm_mutex
);
399 ret
= pthread_create(&global_apps
.ust_listener
, NULL
,
400 ust_listener_thread
, &global_apps
);
401 ret
= pthread_create(&local_apps
.ust_listener
, NULL
,
402 ust_listener_thread
, &local_apps
);
404 ret
= sem_timedwait(&constructor_wait
, &constructor_timeout
);
405 if (ret
< 0 && errno
== ETIMEDOUT
) {
406 ERR("Timed out waiting for ltt-sessiond");
410 pthread_mutex_unlock(<tng_ust_comm_mutex
);
414 void __attribute__((destructor
)) lttng_ust_comm_exit(void)
419 * Using pthread_cancel here because:
420 * A) we don't want to hang application teardown.
421 * B) the thread is not allocating any resource.
425 * Require the communication thread to quit. Synchronize with
426 * mutexes to ensure it is not in a mutex critical section when
427 * pthread_cancel is later called.
429 pthread_mutex_lock(<tng_ust_comm_mutex
);
430 lttng_ust_comm_should_quit
= 1;
431 pthread_mutex_unlock(<tng_ust_comm_mutex
);
434 ret
= pthread_cancel(global_apps
.ust_listener
);
436 ERR("Error cancelling global ust listener thread");
440 cleanup_sock_info(&global_apps
);
442 ret
= pthread_cancel(local_apps
.ust_listener
);
444 ERR("Error cancelling local ust listener thread");
447 cleanup_sock_info(&local_apps
);
449 lttng_ust_abi_exit();