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