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