Implement futex wait scheme
[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
80e2814b 22#define _LGPL_SOURCE
2691221a
MD
23#include <sys/types.h>
24#include <sys/socket.h>
2629549e 25#include <sys/prctl.h>
7fc90dca
MD
26#include <sys/mman.h>
27#include <sys/stat.h>
58d4b2a2
MD
28#include <sys/types.h>
29#include <sys/wait.h>
7fc90dca 30#include <fcntl.h>
2691221a
MD
31#include <unistd.h>
32#include <errno.h>
d9e99d10 33#include <pthread.h>
11ff9c7d
MD
34#include <semaphore.h>
35#include <time.h>
1ea11eab 36#include <assert.h>
e822f505 37#include <signal.h>
95259bd0 38#include <urcu/uatomic.h>
80e2814b 39#include <urcu/futex.h>
1ea11eab 40
edaa1431
MD
41#include <lttng-ust-comm.h>
42#include <ust/usterr-signal-safe.h>
43#include <ust/lttng-ust-abi.h>
44#include <ust/tracepoint.h>
d0a1ae63 45#include <ust/tracepoint-internal.h>
e822f505 46#include <ust/ust.h>
b751f722 47#include "ltt-tracer-core.h"
edaa1431
MD
48
49/*
50 * Has lttng ust comm constructor been called ?
51 */
52static int initialized;
53
1ea11eab 54/*
17dfb34b
MD
55 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
56 * Held when handling a command, also held by fork() to deal with
57 * removal of threads, and by exit path.
1ea11eab 58 */
1ea11eab
MD
59
60/* Should the ust comm thread quit ? */
61static int lttng_ust_comm_should_quit;
62
11ff9c7d
MD
63/*
64 * Wait for either of these before continuing to the main
65 * program:
66 * - the register_done message from sessiond daemon
67 * (will let the sessiond daemon enable sessions before main
68 * starts.)
69 * - sessiond daemon is not reachable.
70 * - timeout (ensuring applications are resilient to session
71 * daemon problems).
72 */
73static sem_t constructor_wait;
950aab0c
MD
74/*
75 * Doing this for both the global and local sessiond.
76 */
95259bd0 77static int sem_count = { 2 };
11ff9c7d 78
1ea11eab
MD
79/*
80 * Info about socket and associated listener thread.
81 */
82struct sock_info {
11ff9c7d 83 const char *name;
1ea11eab 84 pthread_t ust_listener; /* listener thread */
46050b1a 85 int root_handle;
8d20bf54
MD
86 int constructor_sem_posted;
87 int allowed;
44e073f5 88 int global;
7fc90dca
MD
89
90 char sock_path[PATH_MAX];
91 int socket;
92
93 char wait_shm_path[PATH_MAX];
94 char *wait_shm_mmap;
1ea11eab 95};
2691221a
MD
96
97/* Socket from app (connect) to session daemon (listen) for communication */
1ea11eab 98struct sock_info global_apps = {
11ff9c7d 99 .name = "global",
44e073f5 100 .global = 1,
7fc90dca 101
46050b1a 102 .root_handle = -1,
8d20bf54 103 .allowed = 1,
7fc90dca
MD
104
105 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
106 .socket = -1,
107
108 .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
1ea11eab 109};
2691221a
MD
110
111/* TODO: allow global_apps_sock_path override */
112
1ea11eab 113struct sock_info local_apps = {
11ff9c7d 114 .name = "local",
44e073f5 115 .global = 0,
46050b1a 116 .root_handle = -1,
8d20bf54 117 .allowed = 0, /* Check setuid bit first */
7fc90dca
MD
118
119 .socket = -1,
1ea11eab 120};
2691221a 121
edaa1431
MD
122extern void ltt_ring_buffer_client_overwrite_init(void);
123extern void ltt_ring_buffer_client_discard_init(void);
124extern void ltt_ring_buffer_metadata_client_init(void);
125extern void ltt_ring_buffer_client_overwrite_exit(void);
126extern void ltt_ring_buffer_client_discard_exit(void);
127extern void ltt_ring_buffer_metadata_client_exit(void);
128
2691221a 129static
8d20bf54 130int setup_local_apps(void)
2691221a
MD
131{
132 const char *home_dir;
7fc90dca 133 uid_t uid;
2691221a 134
7fc90dca 135 uid = getuid();
8d20bf54
MD
136 /*
137 * Disallow per-user tracing for setuid binaries.
138 */
7fc90dca 139 if (uid != geteuid()) {
8d20bf54 140 local_apps.allowed = 0;
d0a1ae63 141 return 0;
8d20bf54
MD
142 } else {
143 local_apps.allowed = 1;
144 }
2691221a
MD
145 home_dir = (const char *) getenv("HOME");
146 if (!home_dir)
147 return -ENOENT;
1ea11eab 148 snprintf(local_apps.sock_path, PATH_MAX,
2691221a 149 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
7fc90dca
MD
150 snprintf(local_apps.wait_shm_path, PATH_MAX,
151 DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid);
2691221a
MD
152 return 0;
153}
154
155static
156int register_app_to_sessiond(int socket)
157{
158 ssize_t ret;
2629549e 159 int prctl_ret;
2691221a 160 struct {
e44418f3
MD
161 uint32_t major;
162 uint32_t minor;
2691221a 163 pid_t pid;
5c33bde8 164 pid_t ppid;
2691221a 165 uid_t uid;
83610856 166 gid_t gid;
2629549e 167 char name[16]; /* process name */
2691221a
MD
168 } reg_msg;
169
e44418f3
MD
170 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
171 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
2691221a 172 reg_msg.pid = getpid();
5c33bde8 173 reg_msg.ppid = getppid();
2691221a 174 reg_msg.uid = getuid();
83610856 175 reg_msg.gid = getgid();
2629549e
MD
176 prctl_ret = prctl(PR_GET_NAME, (unsigned long) reg_msg.name, 0, 0, 0);
177 if (prctl_ret) {
178 ERR("Error executing prctl");
179 return -errno;
180 }
2691221a
MD
181
182 ret = lttcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
183 if (ret >= 0 && ret != sizeof(reg_msg))
184 return -EIO;
185 return ret;
186}
187
d9e99d10 188static
d3a492d1 189int send_reply(int sock, struct lttcomm_ust_reply *lur)
d9e99d10 190{
9eb62b9c 191 ssize_t len;
d3a492d1 192
a4be8962 193 len = lttcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 194 switch (len) {
a4be8962 195 case sizeof(*lur):
d3a492d1
MD
196 DBG("message successfully sent");
197 return 0;
198 case -1:
199 if (errno == ECONNRESET) {
200 printf("remote end closed connection\n");
201 return 0;
202 }
203 return -1;
204 default:
205 printf("incorrect message size: %zd\n", len);
206 return -1;
207 }
208}
209
210static
edaa1431 211int handle_register_done(struct sock_info *sock_info)
11ff9c7d
MD
212{
213 int ret;
214
edaa1431
MD
215 if (sock_info->constructor_sem_posted)
216 return 0;
217 sock_info->constructor_sem_posted = 1;
95259bd0
MD
218 ret = uatomic_add_return(&sem_count, -1);
219 if (ret == 0) {
220 ret = sem_post(&constructor_wait);
221 assert(!ret);
222 }
11ff9c7d
MD
223 return 0;
224}
225
226static
227int handle_message(struct sock_info *sock_info,
228 int sock, struct lttcomm_ust_msg *lum)
d3a492d1 229{
1ea11eab 230 int ret = 0;
46050b1a
MD
231 const struct objd_ops *ops;
232 struct lttcomm_ust_reply lur;
1ea11eab 233
17dfb34b 234 ust_lock();
1ea11eab 235
46050b1a
MD
236 memset(&lur, 0, sizeof(lur));
237
1ea11eab 238 if (lttng_ust_comm_should_quit) {
46050b1a 239 ret = -EPERM;
1ea11eab
MD
240 goto end;
241 }
9eb62b9c 242
46050b1a
MD
243 ops = objd_ops(lum->handle);
244 if (!ops) {
245 ret = -ENOENT;
246 goto end;
1ea11eab 247 }
46050b1a
MD
248
249 switch (lum->cmd) {
11ff9c7d
MD
250 case LTTNG_UST_REGISTER_DONE:
251 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
edaa1431 252 ret = handle_register_done(sock_info);
11ff9c7d
MD
253 else
254 ret = -EINVAL;
255 break;
46050b1a
MD
256 case LTTNG_UST_RELEASE:
257 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
258 ret = -EPERM;
259 else
260 ret = objd_unref(lum->handle);
d9e99d10
MD
261 break;
262 default:
46050b1a
MD
263 if (ops->cmd)
264 ret = ops->cmd(lum->handle, lum->cmd,
265 (unsigned long) &lum->u);
266 else
267 ret = -ENOSYS;
268 break;
d9e99d10 269 }
46050b1a 270
1ea11eab 271end:
46050b1a
MD
272 lur.handle = lum->handle;
273 lur.cmd = lum->cmd;
274 lur.ret_val = ret;
275 if (ret >= 0) {
276 lur.ret_code = LTTCOMM_OK;
277 } else {
278 lur.ret_code = LTTCOMM_SESSION_FAIL;
279 }
280 ret = send_reply(sock, &lur);
281
17dfb34b 282 ust_unlock();
1ea11eab 283 return ret;
d9e99d10
MD
284}
285
46050b1a
MD
286static
287void cleanup_sock_info(struct sock_info *sock_info)
288{
289 int ret;
290
291 if (sock_info->socket != -1) {
292 ret = close(sock_info->socket);
293 if (ret) {
7fc90dca 294 ERR("Error closing apps socket");
46050b1a
MD
295 }
296 sock_info->socket = -1;
297 }
298 if (sock_info->root_handle != -1) {
299 ret = objd_unref(sock_info->root_handle);
300 if (ret) {
301 ERR("Error unref root handle");
302 }
303 sock_info->root_handle = -1;
304 }
318dfea9 305 sock_info->constructor_sem_posted = 0;
7fc90dca
MD
306 if (sock_info->wait_shm_mmap) {
307 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
308 if (ret) {
309 ERR("Error unmapping wait shm");
310 }
311 sock_info->wait_shm_mmap = NULL;
312 }
313}
314
58d4b2a2 315/*
33bbeb90
MD
316 * Using fork to set umask in the child process (not multi-thread safe).
317 * We deal with the shm_open vs ftruncate race (happening when the
318 * sessiond owns the shm and does not let everybody modify it, to ensure
319 * safety against shm_unlink) by simply letting the mmap fail and
320 * retrying after a few seconds.
321 * For global shm, everybody has rw access to it until the sessiond
322 * starts.
58d4b2a2 323 */
7fc90dca 324static
58d4b2a2 325int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 326{
7fc90dca 327 int wait_shm_fd, ret;
58d4b2a2 328 pid_t pid;
44e073f5 329
58d4b2a2 330 /*
33bbeb90 331 * Try to open read-only.
58d4b2a2 332 */
33bbeb90 333 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
334 if (wait_shm_fd >= 0) {
335 goto end;
336 } else if (wait_shm_fd < 0 && errno != ENOENT) {
337 /*
33bbeb90
MD
338 * Real-only open did not work, and it's not because the
339 * entry was not present. It's a failure that prohibits
340 * using shm.
58d4b2a2 341 */
7fc90dca 342 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 343 goto end;
7fc90dca
MD
344 }
345 /*
58d4b2a2
MD
346 * If the open failed because the file did not exist, try
347 * creating it ourself.
7fc90dca 348 */
58d4b2a2
MD
349 pid = fork();
350 if (pid > 0) {
351 int status;
352
353 /*
354 * Parent: wait for child to return, in which case the
355 * shared memory map will have been created.
356 */
357 pid = wait(&status);
358 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
359 wait_shm_fd = -1;
360 goto end;
7fc90dca 361 }
58d4b2a2
MD
362 /*
363 * Try to open read-only again after creation.
364 */
33bbeb90 365 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
366 if (wait_shm_fd < 0) {
367 /*
368 * Real-only open did not work. It's a failure
369 * that prohibits using shm.
370 */
371 ERR("Error opening shm %s", sock_info->wait_shm_path);
372 goto end;
373 }
374 goto end;
375 } else if (pid == 0) {
376 int create_mode;
377
378 /* Child */
33bbeb90 379 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 380 if (sock_info->global)
33bbeb90 381 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
382 /*
383 * We're alone in a child process, so we can modify the
384 * process-wide umask.
385 */
33bbeb90 386 umask(~create_mode);
58d4b2a2 387 /*
33bbeb90
MD
388 * Try creating shm (or get rw access).
389 * We don't do an exclusive open, because we allow other
390 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
391 */
392 wait_shm_fd = shm_open(sock_info->wait_shm_path,
393 O_RDWR | O_CREAT, create_mode);
394 if (wait_shm_fd >= 0) {
395 ret = ftruncate(wait_shm_fd, mmap_size);
396 if (ret) {
397 PERROR("ftruncate");
398 exit(EXIT_FAILURE);
399 }
58d4b2a2
MD
400 exit(EXIT_SUCCESS);
401 }
33bbeb90
MD
402 /*
403 * For local shm, we need to have rw access to accept
404 * opening it: this means the local sessiond will be
405 * able to wake us up. For global shm, we open it even
406 * if rw access is not granted, because the root.root
407 * sessiond will be able to override all rights and wake
408 * us up.
409 */
410 if (!sock_info->global && errno != EACCES) {
58d4b2a2
MD
411 ERR("Error opening shm %s", sock_info->wait_shm_path);
412 exit(EXIT_FAILURE);
413 }
414 /*
33bbeb90
MD
415 * The shm exists, but we cannot open it RW. Report
416 * success.
58d4b2a2
MD
417 */
418 exit(EXIT_SUCCESS);
419 } else {
420 return -1;
7fc90dca 421 }
58d4b2a2 422end:
33bbeb90
MD
423 if (wait_shm_fd >= 0 && !sock_info->global) {
424 struct stat statbuf;
425
426 /*
427 * Ensure that our user is the owner of the shm file for
428 * local shm. If we do not own the file, it means our
429 * sessiond will not have access to wake us up (there is
430 * probably a rogue process trying to fake our
431 * sessiond). Fallback to polling method in this case.
432 */
433 ret = fstat(wait_shm_fd, &statbuf);
434 if (ret) {
435 PERROR("fstat");
436 goto error_close;
437 }
438 if (statbuf.st_uid != getuid())
439 goto error_close;
440 }
58d4b2a2 441 return wait_shm_fd;
33bbeb90
MD
442
443error_close:
444 ret = close(wait_shm_fd);
445 if (ret) {
446 PERROR("Error closing fd");
447 }
448 return -1;
58d4b2a2
MD
449}
450
451static
452char *get_map_shm(struct sock_info *sock_info)
453{
454 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
455 int wait_shm_fd, ret;
456 char *wait_shm_mmap;
457
458 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
459 if (wait_shm_fd < 0) {
460 goto error;
44e073f5 461 }
7fc90dca
MD
462 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
463 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
464 /* close shm fd immediately after taking the mmap reference */
465 ret = close(wait_shm_fd);
466 if (ret) {
33bbeb90
MD
467 PERROR("Error closing fd");
468 }
469 if (wait_shm_mmap == MAP_FAILED) {
470 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
471 goto error;
7fc90dca
MD
472 }
473 return wait_shm_mmap;
474
475error:
476 return NULL;
477}
478
479static
480void wait_for_sessiond(struct sock_info *sock_info)
481{
80e2814b
MD
482 int ret;
483
7fc90dca
MD
484 ust_lock();
485 if (lttng_ust_comm_should_quit) {
486 goto quit;
487 }
488 if (!sock_info->wait_shm_mmap) {
489 sock_info->wait_shm_mmap = get_map_shm(sock_info);
490 if (!sock_info->wait_shm_mmap)
491 goto error;
492 }
493 ust_unlock();
494
495 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b
MD
496 /* Wait for futex wakeup */
497 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
498 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
499 FUTEX_WAIT, 0, NULL, NULL, 0);
500 /*
501 * FIXME: Currently, futexes on read-only shm seems to
502 * EFAULT.
503 */
504 if (ret < 0) {
505 PERROR("futex");
506 sleep(5);
507 }
508 }
7fc90dca
MD
509 return;
510
511quit:
512 ust_unlock();
513 return;
514
515error:
516 ust_unlock();
517 /* Error handling: fallback on a 5 seconds sleep. */
518 sleep(5);
519 return;
46050b1a
MD
520}
521
1ea11eab
MD
522/*
523 * This thread does not allocate any resource, except within
524 * handle_message, within mutex protection. This mutex protects against
525 * fork and exit.
526 * The other moment it allocates resources is at socket connexion, which
527 * is also protected by the mutex.
528 */
d9e99d10
MD
529static
530void *ust_listener_thread(void *arg)
531{
1ea11eab
MD
532 struct sock_info *sock_info = arg;
533 int sock, ret;
d9e99d10 534
9eb62b9c
MD
535 /* Restart trying to connect to the session daemon */
536restart:
17dfb34b 537 ust_lock();
1ea11eab
MD
538
539 if (lttng_ust_comm_should_quit) {
17dfb34b 540 ust_unlock();
1ea11eab
MD
541 goto quit;
542 }
9eb62b9c 543
1ea11eab
MD
544 if (sock_info->socket != -1) {
545 ret = close(sock_info->socket);
546 if (ret) {
11ff9c7d 547 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
548 }
549 sock_info->socket = -1;
550 }
46050b1a 551
9eb62b9c 552 /* Register */
1ea11eab 553 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 554 if (ret < 0) {
11ff9c7d
MD
555 ERR("Error connecting to %s apps socket", sock_info->name);
556 /*
557 * If we cannot find the sessiond daemon, don't delay
558 * constructor execution.
559 */
edaa1431 560 ret = handle_register_done(sock_info);
11ff9c7d 561 assert(!ret);
17dfb34b 562 ust_unlock();
7fc90dca
MD
563
564 /* Wait for sessiond availability with pipe */
565 wait_for_sessiond(sock_info);
1ea11eab 566 goto restart;
46050b1a
MD
567 }
568
569 sock_info->socket = sock = ret;
570
571 /*
572 * Create only one root handle per listener thread for the whole
573 * process lifetime.
574 */
575 if (sock_info->root_handle == -1) {
576 ret = lttng_abi_create_root_handle();
577 if (ret) {
578 ERR("Error creating root handle");
17dfb34b 579 ust_unlock();
46050b1a
MD
580 goto quit;
581 }
582 sock_info->root_handle = ret;
9eb62b9c 583 }
1ea11eab 584
9eb62b9c
MD
585 ret = register_app_to_sessiond(sock);
586 if (ret < 0) {
11ff9c7d
MD
587 ERR("Error registering to %s apps socket", sock_info->name);
588 /*
589 * If we cannot register to the sessiond daemon, don't
590 * delay constructor execution.
591 */
edaa1431 592 ret = handle_register_done(sock_info);
11ff9c7d 593 assert(!ret);
17dfb34b 594 ust_unlock();
7fc90dca 595 wait_for_sessiond(sock_info);
9eb62b9c
MD
596 goto restart;
597 }
17dfb34b 598 ust_unlock();
46050b1a 599
d9e99d10
MD
600 for (;;) {
601 ssize_t len;
e7723462 602 struct lttcomm_ust_msg lum;
d9e99d10 603
e7723462 604 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
605 switch (len) {
606 case 0: /* orderly shutdown */
11ff9c7d 607 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
d9e99d10 608 goto end;
e7723462 609 case sizeof(lum):
d9e99d10 610 DBG("message received\n");
11ff9c7d 611 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 612 if (ret < 0) {
11ff9c7d 613 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
614 }
615 continue;
616 case -1:
617 if (errno == ECONNRESET) {
11ff9c7d 618 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
619 goto end;
620 }
621 goto end;
622 default:
11ff9c7d 623 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
624 continue;
625 }
626
627 }
628end:
9eb62b9c 629 goto restart; /* try to reconnect */
1ea11eab 630quit:
d9e99d10
MD
631 return NULL;
632}
633
cf12a773
MD
634/*
635 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
636 */
11ff9c7d
MD
637static
638int get_timeout(struct timespec *constructor_timeout)
639{
cf12a773
MD
640 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
641 char *str_delay;
11ff9c7d
MD
642 int ret;
643
cf12a773
MD
644 str_delay = getenv("UST_REGISTER_TIMEOUT");
645 if (str_delay) {
646 constructor_delay_ms = strtol(str_delay, NULL, 10);
647 }
648
649 switch (constructor_delay_ms) {
650 case -1:/* fall-through */
651 case 0:
652 return constructor_delay_ms;
653 default:
654 break;
655 }
656
657 /*
658 * If we are unable to find the current time, don't wait.
659 */
660 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
661 if (ret) {
662 return -1;
663 }
95259bd0
MD
664 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
665 constructor_timeout->tv_nsec +=
666 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
667 if (constructor_timeout->tv_nsec >= 1000000000UL) {
668 constructor_timeout->tv_sec++;
669 constructor_timeout->tv_nsec -= 1000000000UL;
670 }
cf12a773 671 return 1;
11ff9c7d 672}
d9e99d10 673
2691221a
MD
674/*
675 * sessiond monitoring thread: monitor presence of global and per-user
676 * sessiond by polling the application common named pipe.
677 */
678/* TODO */
679
edaa1431 680void __attribute__((constructor)) lttng_ust_init(void)
2691221a 681{
11ff9c7d 682 struct timespec constructor_timeout;
cf12a773 683 int timeout_mode;
2691221a
MD
684 int ret;
685
edaa1431
MD
686 if (uatomic_xchg(&initialized, 1) == 1)
687 return;
688
689 /*
690 * We want precise control over the order in which we construct
691 * our sub-libraries vs starting to receive commands from
692 * sessiond (otherwise leading to errors when trying to create
693 * sessiond before the init functions are completed).
694 */
2691221a 695 init_usterr();
edaa1431
MD
696 init_tracepoint();
697 ltt_ring_buffer_metadata_client_init();
698 ltt_ring_buffer_client_overwrite_init();
699 ltt_ring_buffer_client_discard_init();
2691221a 700
cf12a773 701 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 702
95259bd0 703 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
704 assert(!ret);
705
8d20bf54 706 ret = setup_local_apps();
2691221a 707 if (ret) {
8d20bf54 708 ERR("Error setting up to local apps");
2691221a 709 }
1ea11eab
MD
710 ret = pthread_create(&local_apps.ust_listener, NULL,
711 ust_listener_thread, &local_apps);
11ff9c7d 712
8d20bf54
MD
713 if (local_apps.allowed) {
714 ret = pthread_create(&global_apps.ust_listener, NULL,
715 ust_listener_thread, &global_apps);
716 } else {
717 handle_register_done(&local_apps);
718 }
719
cf12a773
MD
720 switch (timeout_mode) {
721 case 1: /* timeout wait */
95259bd0
MD
722 do {
723 ret = sem_timedwait(&constructor_wait,
724 &constructor_timeout);
725 } while (ret < 0 && errno == EINTR);
cf12a773
MD
726 if (ret < 0 && errno == ETIMEDOUT) {
727 ERR("Timed out waiting for ltt-sessiond");
728 } else {
729 assert(!ret);
730 }
731 break;
7b766b16 732 case -1:/* wait forever */
95259bd0
MD
733 do {
734 ret = sem_wait(&constructor_wait);
735 } while (ret < 0 && errno == EINTR);
11ff9c7d 736 assert(!ret);
cf12a773 737 break;
7b766b16 738 case 0: /* no timeout */
cf12a773 739 break;
11ff9c7d 740 }
2691221a
MD
741}
742
17dfb34b
MD
743static
744void lttng_ust_cleanup(int exiting)
745{
746 cleanup_sock_info(&global_apps);
747 if (local_apps.allowed) {
748 cleanup_sock_info(&local_apps);
749 }
750 lttng_ust_abi_exit();
751 ltt_events_exit();
752 ltt_ring_buffer_client_discard_exit();
753 ltt_ring_buffer_client_overwrite_exit();
754 ltt_ring_buffer_metadata_client_exit();
755 exit_tracepoint();
756 if (!exiting) {
757 /* Reinitialize values for fork */
758 sem_count = 2;
759 lttng_ust_comm_should_quit = 0;
760 initialized = 0;
761 }
762}
763
edaa1431 764void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
765{
766 int ret;
767
9eb62b9c
MD
768 /*
769 * Using pthread_cancel here because:
770 * A) we don't want to hang application teardown.
771 * B) the thread is not allocating any resource.
772 */
1ea11eab
MD
773
774 /*
775 * Require the communication thread to quit. Synchronize with
776 * mutexes to ensure it is not in a mutex critical section when
777 * pthread_cancel is later called.
778 */
17dfb34b 779 ust_lock();
1ea11eab 780 lttng_ust_comm_should_quit = 1;
17dfb34b 781 ust_unlock();
1ea11eab 782
1ea11eab 783 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
784 if (ret) {
785 ERR("Error cancelling global ust listener thread");
2691221a 786 }
8d20bf54
MD
787 if (local_apps.allowed) {
788 ret = pthread_cancel(local_apps.ust_listener);
789 if (ret) {
790 ERR("Error cancelling local ust listener thread");
791 }
8d20bf54 792 }
17dfb34b 793 lttng_ust_cleanup(1);
2691221a 794}
e822f505
MD
795
796/*
797 * We exclude the worker threads across fork and clone (except
798 * CLONE_VM), because these system calls only keep the forking thread
799 * running in the child. Therefore, we don't want to call fork or clone
800 * in the middle of an tracepoint or ust tracing state modification.
801 * Holding this mutex protects these structures across fork and clone.
802 */
803void ust_before_fork(ust_fork_info_t *fork_info)
804{
805 /*
806 * Disable signals. This is to avoid that the child intervenes
807 * before it is properly setup for tracing. It is safer to
808 * disable all signals, because then we know we are not breaking
809 * anything by restoring the original mask.
810 */
811 sigset_t all_sigs;
812 int ret;
813
814 /* Disable signals */
815 sigfillset(&all_sigs);
816 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
817 if (ret == -1) {
818 PERROR("sigprocmask");
819 }
17dfb34b 820 ust_lock();
e822f505
MD
821 rcu_bp_before_fork();
822}
823
824static void ust_after_fork_common(ust_fork_info_t *fork_info)
825{
826 int ret;
827
17dfb34b
MD
828 DBG("process %d", getpid());
829 ust_unlock();
e822f505
MD
830 /* Restore signals */
831 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
832 if (ret == -1) {
833 PERROR("sigprocmask");
834 }
835}
836
837void ust_after_fork_parent(ust_fork_info_t *fork_info)
838{
17dfb34b 839 DBG("process %d", getpid());
e822f505
MD
840 rcu_bp_after_fork_parent();
841 /* Release mutexes and reenable signals */
842 ust_after_fork_common(fork_info);
843}
844
17dfb34b
MD
845/*
846 * After fork, in the child, we need to cleanup all the leftover state,
847 * except the worker thread which already magically disappeared thanks
848 * to the weird Linux fork semantics. After tyding up, we call
849 * lttng_ust_init() again to start over as a new PID.
850 *
851 * This is meant for forks() that have tracing in the child between the
852 * fork and following exec call (if there is any).
853 */
e822f505
MD
854void ust_after_fork_child(ust_fork_info_t *fork_info)
855{
17dfb34b 856 DBG("process %d", getpid());
e822f505
MD
857 /* Release urcu mutexes */
858 rcu_bp_after_fork_child();
17dfb34b 859 lttng_ust_cleanup(0);
e822f505
MD
860 /* Release mutexes and reenable signals */
861 ust_after_fork_common(fork_info);
318dfea9 862 lttng_ust_init();
e822f505 863}
This page took 0.064904 seconds and 4 git commands to generate.