shm wakeup: update right management
[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 313/*
33bbeb90
MD
314 * Using fork to set umask in the child process (not multi-thread safe).
315 * We deal with the shm_open vs ftruncate race (happening when the
316 * sessiond owns the shm and does not let everybody modify it, to ensure
317 * safety against shm_unlink) by simply letting the mmap fail and
318 * retrying after a few seconds.
319 * For global shm, everybody has rw access to it until the sessiond
320 * starts.
58d4b2a2 321 */
7fc90dca 322static
58d4b2a2 323int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 324{
7fc90dca 325 int wait_shm_fd, ret;
58d4b2a2 326 pid_t pid;
44e073f5 327
58d4b2a2 328 /*
33bbeb90 329 * Try to open read-only.
58d4b2a2 330 */
33bbeb90 331 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
332 if (wait_shm_fd >= 0) {
333 goto end;
334 } else if (wait_shm_fd < 0 && errno != ENOENT) {
335 /*
33bbeb90
MD
336 * Real-only open did not work, and it's not because the
337 * entry was not present. It's a failure that prohibits
338 * using shm.
58d4b2a2 339 */
7fc90dca 340 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 341 goto end;
7fc90dca
MD
342 }
343 /*
58d4b2a2
MD
344 * If the open failed because the file did not exist, try
345 * creating it ourself.
7fc90dca 346 */
58d4b2a2
MD
347 pid = fork();
348 if (pid > 0) {
349 int status;
350
351 /*
352 * Parent: wait for child to return, in which case the
353 * shared memory map will have been created.
354 */
355 pid = wait(&status);
356 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
357 wait_shm_fd = -1;
358 goto end;
7fc90dca 359 }
58d4b2a2
MD
360 /*
361 * Try to open read-only again after creation.
362 */
33bbeb90 363 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
364 if (wait_shm_fd < 0) {
365 /*
366 * Real-only open did not work. It's a failure
367 * that prohibits using shm.
368 */
369 ERR("Error opening shm %s", sock_info->wait_shm_path);
370 goto end;
371 }
372 goto end;
373 } else if (pid == 0) {
374 int create_mode;
375
376 /* Child */
33bbeb90 377 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 378 if (sock_info->global)
33bbeb90 379 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
380 /*
381 * We're alone in a child process, so we can modify the
382 * process-wide umask.
383 */
33bbeb90 384 umask(~create_mode);
58d4b2a2 385 /*
33bbeb90
MD
386 * Try creating shm (or get rw access).
387 * We don't do an exclusive open, because we allow other
388 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
389 */
390 wait_shm_fd = shm_open(sock_info->wait_shm_path,
391 O_RDWR | O_CREAT, create_mode);
392 if (wait_shm_fd >= 0) {
393 ret = ftruncate(wait_shm_fd, mmap_size);
394 if (ret) {
395 PERROR("ftruncate");
396 exit(EXIT_FAILURE);
397 }
58d4b2a2
MD
398 exit(EXIT_SUCCESS);
399 }
33bbeb90
MD
400 /*
401 * For local shm, we need to have rw access to accept
402 * opening it: this means the local sessiond will be
403 * able to wake us up. For global shm, we open it even
404 * if rw access is not granted, because the root.root
405 * sessiond will be able to override all rights and wake
406 * us up.
407 */
408 if (!sock_info->global && errno != EACCES) {
58d4b2a2
MD
409 ERR("Error opening shm %s", sock_info->wait_shm_path);
410 exit(EXIT_FAILURE);
411 }
412 /*
33bbeb90
MD
413 * The shm exists, but we cannot open it RW. Report
414 * success.
58d4b2a2
MD
415 */
416 exit(EXIT_SUCCESS);
417 } else {
418 return -1;
7fc90dca 419 }
58d4b2a2 420end:
33bbeb90
MD
421 if (wait_shm_fd >= 0 && !sock_info->global) {
422 struct stat statbuf;
423
424 /*
425 * Ensure that our user is the owner of the shm file for
426 * local shm. If we do not own the file, it means our
427 * sessiond will not have access to wake us up (there is
428 * probably a rogue process trying to fake our
429 * sessiond). Fallback to polling method in this case.
430 */
431 ret = fstat(wait_shm_fd, &statbuf);
432 if (ret) {
433 PERROR("fstat");
434 goto error_close;
435 }
436 if (statbuf.st_uid != getuid())
437 goto error_close;
438 }
58d4b2a2 439 return wait_shm_fd;
33bbeb90
MD
440
441error_close:
442 ret = close(wait_shm_fd);
443 if (ret) {
444 PERROR("Error closing fd");
445 }
446 return -1;
58d4b2a2
MD
447}
448
449static
450char *get_map_shm(struct sock_info *sock_info)
451{
452 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
453 int wait_shm_fd, ret;
454 char *wait_shm_mmap;
455
456 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
457 if (wait_shm_fd < 0) {
458 goto error;
44e073f5 459 }
7fc90dca
MD
460 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
461 MAP_SHARED, wait_shm_fd, 0);
7fc90dca
MD
462 /* close shm fd immediately after taking the mmap reference */
463 ret = close(wait_shm_fd);
464 if (ret) {
33bbeb90
MD
465 PERROR("Error closing fd");
466 }
467 if (wait_shm_mmap == MAP_FAILED) {
468 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
469 goto error;
7fc90dca
MD
470 }
471 return wait_shm_mmap;
472
473error:
474 return NULL;
475}
476
477static
478void wait_for_sessiond(struct sock_info *sock_info)
479{
480 ust_lock();
481 if (lttng_ust_comm_should_quit) {
482 goto quit;
483 }
484 if (!sock_info->wait_shm_mmap) {
485 sock_info->wait_shm_mmap = get_map_shm(sock_info);
486 if (!sock_info->wait_shm_mmap)
487 goto error;
488 }
489 ust_unlock();
490
491 DBG("Waiting for %s apps sessiond", sock_info->name);
492 /* Wait for futex wakeup TODO */
493 sleep(5);
494
495 return;
496
497quit:
498 ust_unlock();
499 return;
500
501error:
502 ust_unlock();
503 /* Error handling: fallback on a 5 seconds sleep. */
504 sleep(5);
505 return;
46050b1a
MD
506}
507
1ea11eab
MD
508/*
509 * This thread does not allocate any resource, except within
510 * handle_message, within mutex protection. This mutex protects against
511 * fork and exit.
512 * The other moment it allocates resources is at socket connexion, which
513 * is also protected by the mutex.
514 */
d9e99d10
MD
515static
516void *ust_listener_thread(void *arg)
517{
1ea11eab
MD
518 struct sock_info *sock_info = arg;
519 int sock, ret;
d9e99d10 520
9eb62b9c
MD
521 /* Restart trying to connect to the session daemon */
522restart:
17dfb34b 523 ust_lock();
1ea11eab
MD
524
525 if (lttng_ust_comm_should_quit) {
17dfb34b 526 ust_unlock();
1ea11eab
MD
527 goto quit;
528 }
9eb62b9c 529
1ea11eab
MD
530 if (sock_info->socket != -1) {
531 ret = close(sock_info->socket);
532 if (ret) {
11ff9c7d 533 ERR("Error closing %s apps socket", sock_info->name);
1ea11eab
MD
534 }
535 sock_info->socket = -1;
536 }
46050b1a 537
9eb62b9c 538 /* Register */
1ea11eab 539 ret = lttcomm_connect_unix_sock(sock_info->sock_path);
9eb62b9c 540 if (ret < 0) {
11ff9c7d
MD
541 ERR("Error connecting to %s apps socket", sock_info->name);
542 /*
543 * If we cannot find the sessiond daemon, don't delay
544 * constructor execution.
545 */
edaa1431 546 ret = handle_register_done(sock_info);
11ff9c7d 547 assert(!ret);
17dfb34b 548 ust_unlock();
7fc90dca
MD
549
550 /* Wait for sessiond availability with pipe */
551 wait_for_sessiond(sock_info);
1ea11eab 552 goto restart;
46050b1a
MD
553 }
554
555 sock_info->socket = sock = ret;
556
557 /*
558 * Create only one root handle per listener thread for the whole
559 * process lifetime.
560 */
561 if (sock_info->root_handle == -1) {
562 ret = lttng_abi_create_root_handle();
563 if (ret) {
564 ERR("Error creating root handle");
17dfb34b 565 ust_unlock();
46050b1a
MD
566 goto quit;
567 }
568 sock_info->root_handle = ret;
9eb62b9c 569 }
1ea11eab 570
9eb62b9c
MD
571 ret = register_app_to_sessiond(sock);
572 if (ret < 0) {
11ff9c7d
MD
573 ERR("Error registering to %s apps socket", sock_info->name);
574 /*
575 * If we cannot register to the sessiond daemon, don't
576 * delay constructor execution.
577 */
edaa1431 578 ret = handle_register_done(sock_info);
11ff9c7d 579 assert(!ret);
17dfb34b 580 ust_unlock();
7fc90dca 581 wait_for_sessiond(sock_info);
9eb62b9c
MD
582 goto restart;
583 }
17dfb34b 584 ust_unlock();
46050b1a 585
d9e99d10
MD
586 for (;;) {
587 ssize_t len;
e7723462 588 struct lttcomm_ust_msg lum;
d9e99d10 589
e7723462 590 len = lttcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
591 switch (len) {
592 case 0: /* orderly shutdown */
11ff9c7d 593 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
d9e99d10 594 goto end;
e7723462 595 case sizeof(lum):
d9e99d10 596 DBG("message received\n");
11ff9c7d 597 ret = handle_message(sock_info, sock, &lum);
2a80c9d8 598 if (ret < 0) {
11ff9c7d 599 ERR("Error handling message for %s socket", sock_info->name);
d9e99d10
MD
600 }
601 continue;
602 case -1:
603 if (errno == ECONNRESET) {
11ff9c7d 604 ERR("%s remote end closed connection\n", sock_info->name);
d9e99d10
MD
605 goto end;
606 }
607 goto end;
608 default:
11ff9c7d 609 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
d9e99d10
MD
610 continue;
611 }
612
613 }
614end:
9eb62b9c 615 goto restart; /* try to reconnect */
1ea11eab 616quit:
d9e99d10
MD
617 return NULL;
618}
619
cf12a773
MD
620/*
621 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
622 */
11ff9c7d
MD
623static
624int get_timeout(struct timespec *constructor_timeout)
625{
cf12a773
MD
626 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
627 char *str_delay;
11ff9c7d
MD
628 int ret;
629
cf12a773
MD
630 str_delay = getenv("UST_REGISTER_TIMEOUT");
631 if (str_delay) {
632 constructor_delay_ms = strtol(str_delay, NULL, 10);
633 }
634
635 switch (constructor_delay_ms) {
636 case -1:/* fall-through */
637 case 0:
638 return constructor_delay_ms;
639 default:
640 break;
641 }
642
643 /*
644 * If we are unable to find the current time, don't wait.
645 */
646 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
647 if (ret) {
648 return -1;
649 }
95259bd0
MD
650 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
651 constructor_timeout->tv_nsec +=
652 (constructor_delay_ms % 1000UL) * 1000000UL;
11ff9c7d
MD
653 if (constructor_timeout->tv_nsec >= 1000000000UL) {
654 constructor_timeout->tv_sec++;
655 constructor_timeout->tv_nsec -= 1000000000UL;
656 }
cf12a773 657 return 1;
11ff9c7d 658}
d9e99d10 659
2691221a
MD
660/*
661 * sessiond monitoring thread: monitor presence of global and per-user
662 * sessiond by polling the application common named pipe.
663 */
664/* TODO */
665
edaa1431 666void __attribute__((constructor)) lttng_ust_init(void)
2691221a 667{
11ff9c7d 668 struct timespec constructor_timeout;
cf12a773 669 int timeout_mode;
2691221a
MD
670 int ret;
671
edaa1431
MD
672 if (uatomic_xchg(&initialized, 1) == 1)
673 return;
674
675 /*
676 * We want precise control over the order in which we construct
677 * our sub-libraries vs starting to receive commands from
678 * sessiond (otherwise leading to errors when trying to create
679 * sessiond before the init functions are completed).
680 */
2691221a 681 init_usterr();
edaa1431
MD
682 init_tracepoint();
683 ltt_ring_buffer_metadata_client_init();
684 ltt_ring_buffer_client_overwrite_init();
685 ltt_ring_buffer_client_discard_init();
2691221a 686
cf12a773 687 timeout_mode = get_timeout(&constructor_timeout);
11ff9c7d 688
95259bd0 689 ret = sem_init(&constructor_wait, 0, 0);
11ff9c7d
MD
690 assert(!ret);
691
8d20bf54 692 ret = setup_local_apps();
2691221a 693 if (ret) {
8d20bf54 694 ERR("Error setting up to local apps");
2691221a 695 }
1ea11eab
MD
696 ret = pthread_create(&local_apps.ust_listener, NULL,
697 ust_listener_thread, &local_apps);
11ff9c7d 698
8d20bf54
MD
699 if (local_apps.allowed) {
700 ret = pthread_create(&global_apps.ust_listener, NULL,
701 ust_listener_thread, &global_apps);
702 } else {
703 handle_register_done(&local_apps);
704 }
705
cf12a773
MD
706 switch (timeout_mode) {
707 case 1: /* timeout wait */
95259bd0
MD
708 do {
709 ret = sem_timedwait(&constructor_wait,
710 &constructor_timeout);
711 } while (ret < 0 && errno == EINTR);
cf12a773
MD
712 if (ret < 0 && errno == ETIMEDOUT) {
713 ERR("Timed out waiting for ltt-sessiond");
714 } else {
715 assert(!ret);
716 }
717 break;
7b766b16 718 case -1:/* wait forever */
95259bd0
MD
719 do {
720 ret = sem_wait(&constructor_wait);
721 } while (ret < 0 && errno == EINTR);
11ff9c7d 722 assert(!ret);
cf12a773 723 break;
7b766b16 724 case 0: /* no timeout */
cf12a773 725 break;
11ff9c7d 726 }
2691221a
MD
727}
728
17dfb34b
MD
729static
730void lttng_ust_cleanup(int exiting)
731{
732 cleanup_sock_info(&global_apps);
733 if (local_apps.allowed) {
734 cleanup_sock_info(&local_apps);
735 }
736 lttng_ust_abi_exit();
737 ltt_events_exit();
738 ltt_ring_buffer_client_discard_exit();
739 ltt_ring_buffer_client_overwrite_exit();
740 ltt_ring_buffer_metadata_client_exit();
741 exit_tracepoint();
742 if (!exiting) {
743 /* Reinitialize values for fork */
744 sem_count = 2;
745 lttng_ust_comm_should_quit = 0;
746 initialized = 0;
747 }
748}
749
edaa1431 750void __attribute__((destructor)) lttng_ust_exit(void)
2691221a
MD
751{
752 int ret;
753
9eb62b9c
MD
754 /*
755 * Using pthread_cancel here because:
756 * A) we don't want to hang application teardown.
757 * B) the thread is not allocating any resource.
758 */
1ea11eab
MD
759
760 /*
761 * Require the communication thread to quit. Synchronize with
762 * mutexes to ensure it is not in a mutex critical section when
763 * pthread_cancel is later called.
764 */
17dfb34b 765 ust_lock();
1ea11eab 766 lttng_ust_comm_should_quit = 1;
17dfb34b 767 ust_unlock();
1ea11eab 768
1ea11eab 769 ret = pthread_cancel(global_apps.ust_listener);
9eb62b9c
MD
770 if (ret) {
771 ERR("Error cancelling global ust listener thread");
2691221a 772 }
8d20bf54
MD
773 if (local_apps.allowed) {
774 ret = pthread_cancel(local_apps.ust_listener);
775 if (ret) {
776 ERR("Error cancelling local ust listener thread");
777 }
8d20bf54 778 }
17dfb34b 779 lttng_ust_cleanup(1);
2691221a 780}
e822f505
MD
781
782/*
783 * We exclude the worker threads across fork and clone (except
784 * CLONE_VM), because these system calls only keep the forking thread
785 * running in the child. Therefore, we don't want to call fork or clone
786 * in the middle of an tracepoint or ust tracing state modification.
787 * Holding this mutex protects these structures across fork and clone.
788 */
789void ust_before_fork(ust_fork_info_t *fork_info)
790{
791 /*
792 * Disable signals. This is to avoid that the child intervenes
793 * before it is properly setup for tracing. It is safer to
794 * disable all signals, because then we know we are not breaking
795 * anything by restoring the original mask.
796 */
797 sigset_t all_sigs;
798 int ret;
799
800 /* Disable signals */
801 sigfillset(&all_sigs);
802 ret = sigprocmask(SIG_BLOCK, &all_sigs, &fork_info->orig_sigs);
803 if (ret == -1) {
804 PERROR("sigprocmask");
805 }
17dfb34b 806 ust_lock();
e822f505
MD
807 rcu_bp_before_fork();
808}
809
810static void ust_after_fork_common(ust_fork_info_t *fork_info)
811{
812 int ret;
813
17dfb34b
MD
814 DBG("process %d", getpid());
815 ust_unlock();
e822f505
MD
816 /* Restore signals */
817 ret = sigprocmask(SIG_SETMASK, &fork_info->orig_sigs, NULL);
818 if (ret == -1) {
819 PERROR("sigprocmask");
820 }
821}
822
823void ust_after_fork_parent(ust_fork_info_t *fork_info)
824{
17dfb34b 825 DBG("process %d", getpid());
e822f505
MD
826 rcu_bp_after_fork_parent();
827 /* Release mutexes and reenable signals */
828 ust_after_fork_common(fork_info);
829}
830
17dfb34b
MD
831/*
832 * After fork, in the child, we need to cleanup all the leftover state,
833 * except the worker thread which already magically disappeared thanks
834 * to the weird Linux fork semantics. After tyding up, we call
835 * lttng_ust_init() again to start over as a new PID.
836 *
837 * This is meant for forks() that have tracing in the child between the
838 * fork and following exec call (if there is any).
839 */
e822f505
MD
840void ust_after_fork_child(ust_fork_info_t *fork_info)
841{
17dfb34b 842 DBG("process %d", getpid());
e822f505
MD
843 /* Release urcu mutexes */
844 rcu_bp_after_fork_child();
17dfb34b 845 lttng_ust_cleanup(0);
e822f505
MD
846 /* Release mutexes and reenable signals */
847 ust_after_fork_common(fork_info);
318dfea9 848 lttng_ust_init();
e822f505 849}
This page took 0.065122 seconds and 4 git commands to generate.