Centralize control of contructors/destructors
[lttng-ust.git] / libust / lttng-ust-comm.c
CommitLineData
2691221a
MD
1/*
2 * lttng-ust-comm.c
3 *
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 *
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.
11 *
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.
16 *
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
20 */
21
22#include <sys/types.h>
23#include <sys/socket.h>
24#include <unistd.h>
25#include <errno.h>
d9e99d10 26#include <pthread.h>
11ff9c7d
MD
27#include <semaphore.h>
28#include <time.h>
1ea11eab 29#include <assert.h>
95259bd0 30#include <urcu/uatomic.h>
1ea11eab 31
edaa1431
MD
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>
36
37/*
38 * Has lttng ust comm constructor been called ?
39 */
40static int initialized;
41
1ea11eab
MD
42/*
43 * communication thread mutex. Held when handling a command, also held
44 * by fork() to deal with removal of threads, and by exit path.
45 */
46static pthread_mutex_t lttng_ust_comm_mutex = PTHREAD_MUTEX_INITIALIZER;
47
48/* Should the ust comm thread quit ? */
49static int lttng_ust_comm_should_quit;
50
11ff9c7d
MD
51/*
52 * Wait for either of these before continuing to the main
53 * program:
54 * - the register_done message from sessiond daemon
55 * (will let the sessiond daemon enable sessions before main
56 * starts.)
57 * - sessiond daemon is not reachable.
58 * - timeout (ensuring applications are resilient to session
59 * daemon problems).
60 */
61static sem_t constructor_wait;
950aab0c
MD
62/*
63 * Doing this for both the global and local sessiond.
64 */
95259bd0 65static int sem_count = { 2 };
11ff9c7d 66
1ea11eab
MD
67/*
68 * Info about socket and associated listener thread.
69 */
70struct sock_info {
11ff9c7d 71 const char *name;
1ea11eab
MD
72 char sock_path[PATH_MAX];
73 int socket;
74 pthread_t ust_listener; /* listener thread */
46050b1a 75 int root_handle;
edaa1431 76 int constructor_sem_posted;;
1ea11eab 77};
2691221a
MD
78
79/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 80struct sock_info global_apps = {
11ff9c7d 81 .name = "global",
1ea11eab
MD
82 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
83 .socket = -1,
46050b1a 84 .root_handle = -1,
1ea11eab 85};
2691221a
MD
86
87/* TODO: allow global_apps_sock_path override */
88
1ea11eab 89struct sock_info local_apps = {
11ff9c7d 90 .name = "local",
1ea11eab 91 .socket = -1,
46050b1a 92 .root_handle = -1,
1ea11eab 93};
2691221a 94
edaa1431
MD
95extern void ltt_ring_buffer_client_overwrite_init(void);
96extern void ltt_ring_buffer_client_discard_init(void);
97extern void ltt_ring_buffer_metadata_client_init(void);
98extern void ltt_ring_buffer_client_overwrite_exit(void);
99extern void ltt_ring_buffer_client_discard_exit(void);
100extern void ltt_ring_buffer_metadata_client_exit(void);
101
2691221a 102static
9eb62b9c 103int setup_local_apps_socket(void)
2691221a
MD
104{
105 const char *home_dir;
2691221a
MD
106
107 home_dir = (const char *) getenv("HOME");
108 if (!home_dir)
109 return -ENOENT;
1ea11eab 110 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 111 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
2691221a
MD
112 return 0;
113}
114
115static
116int register_app_to_sessiond(int socket)
117{
118 ssize_t ret;
119 struct {
e44418f3
MD
120 uint32_t major;
121 uint32_t minor;
2691221a
MD
122 pid_t pid;
123 uid_t uid;
124 } reg_msg;
125
e44418f3
MD
126 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
127 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a
MD
128 reg_msg.pid = getpid();
129 reg_msg.uid = getuid();
130
131 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
132 if (ret >= 0 && ret != sizeof(reg_msg))
133 return -EIO;
134 return ret;
135}
136
d9e99d10 137static
d3a492d1 138int send_reply(int sock, struct lttcomm_ust_reply *lur)
d9e99d10 139{
9eb62b9c 140 ssize_t len;
d3a492d1 141
a4be8962 142 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 143 switch (len) {
a4be8962 144 case sizeof(*lur):
d3a492d1
MD
145 DBG("message successfully sent");
146 return 0;
147 case -1:
148 if (errno == ECONNRESET) {
149 printf("remote end closed connection\n");
150 return 0;
151 }
152 return -1;
153 default:
154 printf("incorrect message size: %zd\n", len);
155 return -1;
156 }
157}
158
159static
edaa1431 160int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
161{
162 int ret;
163
edaa1431
MD
164 if (sock_info->constructor_sem_posted)
165 return 0;
166 sock_info->constructor_sem_posted = 1;
95259bd0 167 ret = uatomic_add_return(&sem_count, -1);
edaa1431 168 fprintf(stderr, "DEC ret %d\n", ret);
95259bd0 169 if (ret == 0) {
edaa1431 170 fprintf(stderr, "POST\n");
95259bd0
MD
171 ret = sem_post(&constructor_wait);
172 assert(!ret);
173 }
11ff9c7d
MD
174 return 0;
175}
176
177static
178int handle_message(struct sock_info *sock_info,
179 int sock, struct lttcomm_ust_msg *lum)
d3a492d1 180{
1ea11eab 181 int ret = 0;
46050b1a
MD
182 const struct objd_ops *ops;
183 struct lttcomm_ust_reply lur;
1ea11eab
MD
184
185 pthread_mutex_lock(&lttng_ust_comm_mutex);
186
46050b1a
MD
187 memset(&lur, 0, sizeof(lur));
188
1ea11eab 189 if (lttng_ust_comm_should_quit) {
46050b1a 190 ret = -EPERM;
1ea11eab
MD
191 goto end;
192 }
9eb62b9c 193
46050b1a
MD
194 ops = objd_ops(lum->handle);
195 if (!ops) {
196 ret = -ENOENT;
197 goto end;
1ea11eab 198 }
46050b1a
MD
199
200 switch (lum->cmd) {
11ff9c7d
MD
201 case LTTNG_UST_REGISTER_DONE:
202 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 203 ret = handle_register_done(sock_info);
11ff9c7d
MD
204 else
205 ret = -EINVAL;
206 break;
46050b1a
MD
207 case LTTNG_UST_RELEASE:
208 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
209 ret = -EPERM;
210 else
211 ret = objd_unref(lum->handle);
d9e99d10
MD
212 break;
213 default:
46050b1a
MD
214 if (ops->cmd)
215 ret = ops->cmd(lum->handle, lum->cmd,
216 (unsigned long) &lum->u);
217 else
218 ret = -ENOSYS;
219 break;
d9e99d10 220 }
46050b1a 221
1ea11eab 222end:
46050b1a
MD
223 lur.handle = lum->handle;
224 lur.cmd = lum->cmd;
225 lur.ret_val = ret;
226 if (ret >= 0) {
227 lur.ret_code = LTTCOMM_OK;
228 } else {
229 lur.ret_code = LTTCOMM_SESSION_FAIL;
230 }
231 ret = send_reply(sock, &lur);
232
1ea11eab
MD
233 pthread_mutex_unlock(&lttng_ust_comm_mutex);
234 return ret;
d9e99d10
MD
235}
236
46050b1a
MD
237static
238void cleanup_sock_info(struct sock_info *sock_info)
239{
240 int ret;
241
242 if (sock_info->socket != -1) {
243 ret = close(sock_info->socket);
244 if (ret) {
245 ERR("Error closing local apps socket");
246 }
247 sock_info->socket = -1;
248 }
249 if (sock_info->root_handle != -1) {
250 ret = objd_unref(sock_info->root_handle);
251 if (ret) {
252 ERR("Error unref root handle");
253 }
254 sock_info->root_handle = -1;
255 }
256}
257
1ea11eab
MD
258/*
259 * This thread does not allocate any resource, except within
260 * handle_message, within mutex protection. This mutex protects against
261 * fork and exit.
262 * The other moment it allocates resources is at socket connexion, which
263 * is also protected by the mutex.
264 */
d9e99d10
MD
265static
266void *ust_listener_thread(void *arg)
267{
1ea11eab
MD
268 struct sock_info *sock_info = arg;
269 int sock, ret;
d9e99d10 270
9eb62b9c
MD
271 /* Restart trying to connect to the session daemon */
272restart:
1ea11eab
MD
273 pthread_mutex_lock(&lttng_ust_comm_mutex);
274
275 if (lttng_ust_comm_should_quit) {
276 pthread_mutex_unlock(&lttng_ust_comm_mutex);
277 goto quit;
278 }
9eb62b9c 279
1ea11eab
MD
280 if (sock_info->socket != -1) {
281 ret = close(sock_info->socket);
282 if (ret) {
11ff9c7d 283 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
284 }
285 sock_info->socket = -1;
286 }
46050b1a 287
9eb62b9c
MD
288 /* Check for sessiond availability with pipe TODO */
289
290 /* Register */
1ea11eab 291 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 292 if (ret < 0) {
11ff9c7d
MD
293 ERR("Error connecting to %s apps socket", sock_info->name);
294 /*
295 * If we cannot find the sessiond daemon, don't delay
296 * constructor execution.
297 */
edaa1431 298 ret = handle_register_done(sock_info);
11ff9c7d 299 assert(!ret);
1ea11eab 300 pthread_mutex_unlock(&lttng_ust_comm_mutex);
4eef2998 301 sleep(5);
1ea11eab 302 goto restart;
46050b1a
MD
303 }
304
305 sock_info->socket = sock = ret;
306
307 /*
308 * Create only one root handle per listener thread for the whole
309 * process lifetime.
310 */
311 if (sock_info->root_handle == -1) {
312 ret = lttng_abi_create_root_handle();
313 if (ret) {
314 ERR("Error creating root handle");
315 pthread_mutex_unlock(&lttng_ust_comm_mutex);
316 goto quit;
317 }
318 sock_info->root_handle = ret;
9eb62b9c 319 }
1ea11eab 320
9eb62b9c
MD
321 ret = register_app_to_sessiond(sock);
322 if (ret < 0) {
11ff9c7d
MD
323 ERR("Error registering to %s apps socket", sock_info->name);
324 /*
325 * If we cannot register to the sessiond daemon, don't
326 * delay constructor execution.
327 */
edaa1431 328 ret = handle_register_done(sock_info);
11ff9c7d 329 assert(!ret);
46050b1a 330 pthread_mutex_unlock(&lttng_ust_comm_mutex);
9eb62b9c
MD
331 sleep(5);
332 goto restart;
333 }
46050b1a
MD
334 pthread_mutex_unlock(&lttng_ust_comm_mutex);
335
d9e99d10
MD
336 for (;;) {
337 ssize_t len;
e7723462 338 struct lttcomm_ust_msg lum;
d9e99d10 339
e7723462 340 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
341 switch (len) {
342 case 0: /* orderly shutdown */
11ff9c7d 343 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
d9e99d10 344 goto end;
e7723462 345 case sizeof(lum):
d9e99d10 346 DBG("message received\n");
11ff9c7d 347 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 348 if (ret < 0) {
11ff9c7d 349 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
350 }
351 continue;
352 case -1:
353 if (errno == ECONNRESET) {
11ff9c7d 354 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
355 goto end;
356 }
357 goto end;
358 default:
11ff9c7d 359 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
360 continue;
361 }
362
363 }
364end:
9eb62b9c 365 goto restart; /* try to reconnect */
1ea11eab 366quit:
d9e99d10
MD
367 return NULL;
368}
369
cf12a773
MD
370/*
371 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
372 */
11ff9c7d
MD
373static
374int get_timeout(struct timespec *constructor_timeout)
375{
cf12a773
MD
376 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
377 char *str_delay;
11ff9c7d
MD
378 int ret;
379
cf12a773
MD
380 str_delay = getenv("UST_REGISTER_TIMEOUT");
381 if (str_delay) {
382 constructor_delay_ms = strtol(str_delay, NULL, 10);
383 }
384
385 switch (constructor_delay_ms) {
386 case -1:/* fall-through */
387 case 0:
388 return constructor_delay_ms;
389 default:
390 break;
391 }
392
393 /*
394 * If we are unable to find the current time, don't wait.
395 */
396 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
397 if (ret) {
398 return -1;
399 }
95259bd0
MD
400 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
401 constructor_timeout->tv_nsec +=
402 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
403 if (constructor_timeout->tv_nsec >= 1000000000UL) {
404 constructor_timeout->tv_sec++;
405 constructor_timeout->tv_nsec -= 1000000000UL;
406 }
cf12a773 407 return 1;
11ff9c7d 408}
d9e99d10 409
2691221a
MD
410/*
411 * sessiond monitoring thread: monitor presence of global and per-user
412 * sessiond by polling the application common named pipe.
413 */
414/* TODO */
415
edaa1431 416void __attribute__((constructor)) lttng_ust_init(void)
2691221a 417{
11ff9c7d 418 struct timespec constructor_timeout;
cf12a773 419 int timeout_mode;
2691221a
MD
420 int ret;
421
edaa1431
MD
422 if (uatomic_xchg(&initialized, 1) == 1)
423 return;
424
425 /*
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).
430 */
2691221a 431 init_usterr();
edaa1431
MD
432 init_tracepoint();
433 ltt_ring_buffer_metadata_client_init();
434 ltt_ring_buffer_client_overwrite_init();
435 ltt_ring_buffer_client_discard_init();
2691221a 436
cf12a773 437 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 438
95259bd0 439 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
440 assert(!ret);
441
9eb62b9c 442 ret = setup_local_apps_socket();
2691221a 443 if (ret) {
9eb62b9c 444 ERR("Error setting up to local apps socket");
2691221a 445 }
11ff9c7d 446
1ea11eab
MD
447 ret = pthread_create(&global_apps.ust_listener, NULL,
448 ust_listener_thread, &global_apps);
1ea11eab
MD
449 ret = pthread_create(&local_apps.ust_listener, NULL,
450 ust_listener_thread, &local_apps);
11ff9c7d 451
cf12a773
MD
452 switch (timeout_mode) {
453 case 1: /* timeout wait */
95259bd0
MD
454 do {
455 ret = sem_timedwait(&constructor_wait,
456 &constructor_timeout);
457 } while (ret < 0 && errno == EINTR);
cf12a773
MD
458 if (ret < 0 && errno == ETIMEDOUT) {
459 ERR("Timed out waiting for ltt-sessiond");
460 } else {
461 assert(!ret);
462 }
463 break;
7b766b16 464 case -1:/* wait forever */
95259bd0
MD
465 do {
466 ret = sem_wait(&constructor_wait);
467 } while (ret < 0 && errno == EINTR);
11ff9c7d 468 assert(!ret);
cf12a773 469 break;
7b766b16 470 case 0: /* no timeout */
cf12a773 471 break;
11ff9c7d 472 }
2691221a
MD
473}
474
edaa1431 475void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
476{
477 int ret;
478
9eb62b9c
MD
479 /*
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.
483 */
1ea11eab
MD
484
485 /*
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.
489 */
490 pthread_mutex_lock(&lttng_ust_comm_mutex);
491 lttng_ust_comm_should_quit = 1;
492 pthread_mutex_unlock(&lttng_ust_comm_mutex);
493
494#if 0
495 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
496 if (ret) {
497 ERR("Error cancelling global ust listener thread");
2691221a 498 }
1ea11eab 499#endif //0
46050b1a
MD
500
501 cleanup_sock_info(&global_apps);
1ea11eab
MD
502
503 ret = pthread_cancel(local_apps.ust_listener);
9eb62b9c
MD
504 if (ret) {
505 ERR("Error cancelling local ust listener thread");
2691221a 506 }
1ea11eab 507
46050b1a 508 cleanup_sock_info(&local_apps);
1ea11eab 509
b35d179d 510 lttng_ust_abi_exit();
1ea11eab 511 ltt_events_exit();
edaa1431
MD
512 ltt_ring_buffer_client_discard_exit();
513 ltt_ring_buffer_client_overwrite_exit();
514 ltt_ring_buffer_metadata_client_exit();
515 exit_tracepoint();
2691221a 516}
This page took 0.047487 seconds and 4 git commands to generate.