Fix: liblttng-ust-fork deadlock
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
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 #define _LGPL_SOURCE
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/mman.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <pthread.h>
33 #include <semaphore.h>
34 #include <time.h>
35 #include <assert.h>
36 #include <signal.h>
37 #include <urcu/uatomic.h>
38 #include <urcu/futex.h>
39 #include <urcu/compiler.h>
40
41 #include <lttng/ust-events.h>
42 #include <lttng/ust-abi.h>
43 #include <lttng/ust.h>
44 #include <ust-comm.h>
45 #include <usterr-signal-safe.h>
46 #include "tracepoint-internal.h"
47 #include "ltt-tracer-core.h"
48 #include "compat.h"
49 #include "../libringbuffer/tlsfixup.h"
50
51 /*
52 * Has lttng ust comm constructor been called ?
53 */
54 static int initialized;
55
56 /*
57 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
58 * Held when handling a command, also held by fork() to deal with
59 * removal of threads, and by exit path.
60 */
61
62 /* Should the ust comm thread quit ? */
63 static int lttng_ust_comm_should_quit;
64
65 /*
66 * Wait for either of these before continuing to the main
67 * program:
68 * - the register_done message from sessiond daemon
69 * (will let the sessiond daemon enable sessions before main
70 * starts.)
71 * - sessiond daemon is not reachable.
72 * - timeout (ensuring applications are resilient to session
73 * daemon problems).
74 */
75 static sem_t constructor_wait;
76 /*
77 * Doing this for both the global and local sessiond.
78 */
79 static int sem_count = { 2 };
80
81 /*
82 * Counting nesting within lttng-ust. Used to ensure that calling fork()
83 * from liblttng-ust does not execute the pre/post fork handlers.
84 */
85 static int __thread lttng_ust_nest_count;
86
87 /*
88 * Info about socket and associated listener thread.
89 */
90 struct sock_info {
91 const char *name;
92 pthread_t ust_listener; /* listener thread */
93 int root_handle;
94 int constructor_sem_posted;
95 int allowed;
96 int global;
97
98 char sock_path[PATH_MAX];
99 int socket;
100
101 char wait_shm_path[PATH_MAX];
102 char *wait_shm_mmap;
103 };
104
105 /* Socket from app (connect) to session daemon (listen) for communication */
106 struct sock_info global_apps = {
107 .name = "global",
108 .global = 1,
109
110 .root_handle = -1,
111 .allowed = 1,
112
113 .sock_path = DEFAULT_GLOBAL_APPS_UNIX_SOCK,
114 .socket = -1,
115
116 .wait_shm_path = DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH,
117 };
118
119 /* TODO: allow global_apps_sock_path override */
120
121 struct sock_info local_apps = {
122 .name = "local",
123 .global = 0,
124 .root_handle = -1,
125 .allowed = 0, /* Check setuid bit first */
126
127 .socket = -1,
128 };
129
130 static int wait_poll_fallback;
131
132 extern void ltt_ring_buffer_client_overwrite_init(void);
133 extern void ltt_ring_buffer_client_discard_init(void);
134 extern void ltt_ring_buffer_metadata_client_init(void);
135 extern void ltt_ring_buffer_client_overwrite_exit(void);
136 extern void ltt_ring_buffer_client_discard_exit(void);
137 extern void ltt_ring_buffer_metadata_client_exit(void);
138
139 static
140 int setup_local_apps(void)
141 {
142 const char *home_dir;
143 uid_t uid;
144
145 uid = getuid();
146 /*
147 * Disallow per-user tracing for setuid binaries.
148 */
149 if (uid != geteuid()) {
150 local_apps.allowed = 0;
151 return 0;
152 } else {
153 local_apps.allowed = 1;
154 }
155 home_dir = (const char *) getenv("HOME");
156 if (!home_dir)
157 return -ENOENT;
158 snprintf(local_apps.sock_path, PATH_MAX,
159 DEFAULT_HOME_APPS_UNIX_SOCK, home_dir);
160 snprintf(local_apps.wait_shm_path, PATH_MAX,
161 DEFAULT_HOME_APPS_WAIT_SHM_PATH, uid);
162 return 0;
163 }
164
165 static
166 int register_app_to_sessiond(int socket)
167 {
168 ssize_t ret;
169 struct {
170 uint32_t major;
171 uint32_t minor;
172 pid_t pid;
173 pid_t ppid;
174 uid_t uid;
175 gid_t gid;
176 uint32_t bits_per_long;
177 char name[16]; /* process name */
178 } reg_msg;
179
180 reg_msg.major = LTTNG_UST_COMM_VERSION_MAJOR;
181 reg_msg.minor = LTTNG_UST_COMM_VERSION_MINOR;
182 reg_msg.pid = getpid();
183 reg_msg.ppid = getppid();
184 reg_msg.uid = getuid();
185 reg_msg.gid = getgid();
186 reg_msg.bits_per_long = CAA_BITS_PER_LONG;
187 lttng_ust_getprocname(reg_msg.name);
188
189 ret = ustcomm_send_unix_sock(socket, &reg_msg, sizeof(reg_msg));
190 if (ret >= 0 && ret != sizeof(reg_msg))
191 return -EIO;
192 return ret;
193 }
194
195 static
196 int send_reply(int sock, struct ustcomm_ust_reply *lur)
197 {
198 ssize_t len;
199
200 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
201 switch (len) {
202 case sizeof(*lur):
203 DBG("message successfully sent");
204 return 0;
205 case -1:
206 if (errno == ECONNRESET) {
207 printf("remote end closed connection\n");
208 return 0;
209 }
210 return -1;
211 default:
212 printf("incorrect message size: %zd\n", len);
213 return -1;
214 }
215 }
216
217 static
218 int handle_register_done(struct sock_info *sock_info)
219 {
220 int ret;
221
222 if (sock_info->constructor_sem_posted)
223 return 0;
224 sock_info->constructor_sem_posted = 1;
225 if (uatomic_read(&sem_count) <= 0) {
226 return 0;
227 }
228 ret = uatomic_add_return(&sem_count, -1);
229 if (ret == 0) {
230 ret = sem_post(&constructor_wait);
231 assert(!ret);
232 }
233 return 0;
234 }
235
236 static
237 int handle_message(struct sock_info *sock_info,
238 int sock, struct ustcomm_ust_msg *lum)
239 {
240 int ret = 0;
241 const struct lttng_ust_objd_ops *ops;
242 struct ustcomm_ust_reply lur;
243 int shm_fd, wait_fd;
244 union ust_args args;
245 ssize_t len;
246
247 ust_lock();
248
249 memset(&lur, 0, sizeof(lur));
250
251 if (lttng_ust_comm_should_quit) {
252 ret = -EPERM;
253 goto end;
254 }
255
256 ops = objd_ops(lum->handle);
257 if (!ops) {
258 ret = -ENOENT;
259 goto end;
260 }
261
262 switch (lum->cmd) {
263 case LTTNG_UST_REGISTER_DONE:
264 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
265 ret = handle_register_done(sock_info);
266 else
267 ret = -EINVAL;
268 break;
269 case LTTNG_UST_RELEASE:
270 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
271 ret = -EPERM;
272 else
273 ret = lttng_ust_objd_unref(lum->handle);
274 break;
275 default:
276 if (ops->cmd)
277 ret = ops->cmd(lum->handle, lum->cmd,
278 (unsigned long) &lum->u,
279 &args);
280 else
281 ret = -ENOSYS;
282 break;
283 }
284
285 end:
286 lur.handle = lum->handle;
287 lur.cmd = lum->cmd;
288 lur.ret_val = ret;
289 if (ret >= 0) {
290 lur.ret_code = USTCOMM_OK;
291 } else {
292 //lur.ret_code = USTCOMM_SESSION_FAIL;
293 lur.ret_code = ret;
294 }
295 if (ret >= 0) {
296 switch (lum->cmd) {
297 case LTTNG_UST_STREAM:
298 /*
299 * Special-case reply to send stream info.
300 * Use lum.u output.
301 */
302 lur.u.stream.memory_map_size = *args.stream.memory_map_size;
303 shm_fd = *args.stream.shm_fd;
304 wait_fd = *args.stream.wait_fd;
305 break;
306 case LTTNG_UST_METADATA:
307 case LTTNG_UST_CHANNEL:
308 lur.u.channel.memory_map_size = *args.channel.memory_map_size;
309 shm_fd = *args.channel.shm_fd;
310 wait_fd = *args.channel.wait_fd;
311 break;
312 case LTTNG_UST_TRACER_VERSION:
313 lur.u.version = lum->u.version;
314 break;
315 case LTTNG_UST_TRACEPOINT_LIST_GET:
316 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
317 break;
318 }
319 }
320 ret = send_reply(sock, &lur);
321 if (ret < 0) {
322 perror("error sending reply");
323 goto error;
324 }
325
326 if ((lum->cmd == LTTNG_UST_STREAM
327 || lum->cmd == LTTNG_UST_CHANNEL
328 || lum->cmd == LTTNG_UST_METADATA)
329 && lur.ret_code == USTCOMM_OK) {
330 int sendret = 0;
331
332 /* we also need to send the file descriptors. */
333 ret = ustcomm_send_fds_unix_sock(sock,
334 &shm_fd, &shm_fd,
335 1, sizeof(int));
336 if (ret < 0) {
337 perror("send shm_fd");
338 sendret = ret;
339 }
340 /*
341 * The sessiond expects 2 file descriptors, even upon
342 * error.
343 */
344 ret = ustcomm_send_fds_unix_sock(sock,
345 &wait_fd, &wait_fd,
346 1, sizeof(int));
347 if (ret < 0) {
348 perror("send wait_fd");
349 goto error;
350 }
351 if (sendret) {
352 ret = sendret;
353 goto error;
354 }
355 }
356 /*
357 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
358 * after the reply.
359 */
360 if (lur.ret_code == USTCOMM_OK) {
361 switch (lum->cmd) {
362 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
363 len = ustcomm_send_unix_sock(sock,
364 &args.field_list.entry,
365 sizeof(args.field_list.entry));
366 if (len != sizeof(args.field_list.entry)) {
367 ret = -1;
368 goto error;
369 }
370 }
371 }
372 /*
373 * We still have the memory map reference, and the fds have been
374 * sent to the sessiond. We can therefore close those fds. Note
375 * that we keep the write side of the wait_fd open, but close
376 * the read side.
377 */
378 if (lur.ret_code == USTCOMM_OK) {
379 switch (lum->cmd) {
380 case LTTNG_UST_STREAM:
381 if (shm_fd >= 0) {
382 ret = close(shm_fd);
383 if (ret) {
384 PERROR("Error closing stream shm_fd");
385 }
386 *args.stream.shm_fd = -1;
387 }
388 if (wait_fd >= 0) {
389 ret = close(wait_fd);
390 if (ret) {
391 PERROR("Error closing stream wait_fd");
392 }
393 *args.stream.wait_fd = -1;
394 }
395 break;
396 case LTTNG_UST_METADATA:
397 case LTTNG_UST_CHANNEL:
398 if (shm_fd >= 0) {
399 ret = close(shm_fd);
400 if (ret) {
401 PERROR("Error closing channel shm_fd");
402 }
403 *args.channel.shm_fd = -1;
404 }
405 if (wait_fd >= 0) {
406 ret = close(wait_fd);
407 if (ret) {
408 PERROR("Error closing channel wait_fd");
409 }
410 *args.channel.wait_fd = -1;
411 }
412 break;
413 }
414 }
415
416 error:
417 ust_unlock();
418 return ret;
419 }
420
421 static
422 void cleanup_sock_info(struct sock_info *sock_info, int exiting)
423 {
424 int ret;
425
426 if (sock_info->socket != -1) {
427 ret = ustcomm_close_unix_sock(sock_info->socket);
428 if (ret) {
429 ERR("Error closing apps socket");
430 }
431 sock_info->socket = -1;
432 }
433 if (sock_info->root_handle != -1) {
434 ret = lttng_ust_objd_unref(sock_info->root_handle);
435 if (ret) {
436 ERR("Error unref root handle");
437 }
438 sock_info->root_handle = -1;
439 }
440 sock_info->constructor_sem_posted = 0;
441 /*
442 * wait_shm_mmap is used by listener threads outside of the
443 * ust lock, so we cannot tear it down ourselves, because we
444 * cannot join on these threads. Leave this task to the OS
445 * process exit.
446 */
447 if (!exiting && sock_info->wait_shm_mmap) {
448 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
449 if (ret) {
450 ERR("Error unmapping wait shm");
451 }
452 sock_info->wait_shm_mmap = NULL;
453 }
454 }
455
456 /*
457 * Using fork to set umask in the child process (not multi-thread safe).
458 * We deal with the shm_open vs ftruncate race (happening when the
459 * sessiond owns the shm and does not let everybody modify it, to ensure
460 * safety against shm_unlink) by simply letting the mmap fail and
461 * retrying after a few seconds.
462 * For global shm, everybody has rw access to it until the sessiond
463 * starts.
464 */
465 static
466 int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
467 {
468 int wait_shm_fd, ret;
469 pid_t pid;
470
471 /*
472 * Try to open read-only.
473 */
474 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
475 if (wait_shm_fd >= 0) {
476 goto end;
477 } else if (wait_shm_fd < 0 && errno != ENOENT) {
478 /*
479 * Real-only open did not work, and it's not because the
480 * entry was not present. It's a failure that prohibits
481 * using shm.
482 */
483 ERR("Error opening shm %s", sock_info->wait_shm_path);
484 goto end;
485 }
486 /*
487 * If the open failed because the file did not exist, try
488 * creating it ourself.
489 */
490 lttng_ust_nest_count++;
491 pid = fork();
492 lttng_ust_nest_count--;
493 if (pid > 0) {
494 int status;
495
496 /*
497 * Parent: wait for child to return, in which case the
498 * shared memory map will have been created.
499 */
500 pid = wait(&status);
501 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
502 wait_shm_fd = -1;
503 goto end;
504 }
505 /*
506 * Try to open read-only again after creation.
507 */
508 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
509 if (wait_shm_fd < 0) {
510 /*
511 * Real-only open did not work. It's a failure
512 * that prohibits using shm.
513 */
514 ERR("Error opening shm %s", sock_info->wait_shm_path);
515 goto end;
516 }
517 goto end;
518 } else if (pid == 0) {
519 int create_mode;
520
521 /* Child */
522 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
523 if (sock_info->global)
524 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
525 /*
526 * We're alone in a child process, so we can modify the
527 * process-wide umask.
528 */
529 umask(~create_mode);
530 /*
531 * Try creating shm (or get rw access).
532 * We don't do an exclusive open, because we allow other
533 * processes to create+ftruncate it concurrently.
534 */
535 wait_shm_fd = shm_open(sock_info->wait_shm_path,
536 O_RDWR | O_CREAT, create_mode);
537 if (wait_shm_fd >= 0) {
538 ret = ftruncate(wait_shm_fd, mmap_size);
539 if (ret) {
540 PERROR("ftruncate");
541 exit(EXIT_FAILURE);
542 }
543 exit(EXIT_SUCCESS);
544 }
545 /*
546 * For local shm, we need to have rw access to accept
547 * opening it: this means the local sessiond will be
548 * able to wake us up. For global shm, we open it even
549 * if rw access is not granted, because the root.root
550 * sessiond will be able to override all rights and wake
551 * us up.
552 */
553 if (!sock_info->global && errno != EACCES) {
554 ERR("Error opening shm %s", sock_info->wait_shm_path);
555 exit(EXIT_FAILURE);
556 }
557 /*
558 * The shm exists, but we cannot open it RW. Report
559 * success.
560 */
561 exit(EXIT_SUCCESS);
562 } else {
563 return -1;
564 }
565 end:
566 if (wait_shm_fd >= 0 && !sock_info->global) {
567 struct stat statbuf;
568
569 /*
570 * Ensure that our user is the owner of the shm file for
571 * local shm. If we do not own the file, it means our
572 * sessiond will not have access to wake us up (there is
573 * probably a rogue process trying to fake our
574 * sessiond). Fallback to polling method in this case.
575 */
576 ret = fstat(wait_shm_fd, &statbuf);
577 if (ret) {
578 PERROR("fstat");
579 goto error_close;
580 }
581 if (statbuf.st_uid != getuid())
582 goto error_close;
583 }
584 return wait_shm_fd;
585
586 error_close:
587 ret = close(wait_shm_fd);
588 if (ret) {
589 PERROR("Error closing fd");
590 }
591 return -1;
592 }
593
594 static
595 char *get_map_shm(struct sock_info *sock_info)
596 {
597 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
598 int wait_shm_fd, ret;
599 char *wait_shm_mmap;
600
601 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
602 if (wait_shm_fd < 0) {
603 goto error;
604 }
605 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
606 MAP_SHARED, wait_shm_fd, 0);
607 /* close shm fd immediately after taking the mmap reference */
608 ret = close(wait_shm_fd);
609 if (ret) {
610 PERROR("Error closing fd");
611 }
612 if (wait_shm_mmap == MAP_FAILED) {
613 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
614 goto error;
615 }
616 return wait_shm_mmap;
617
618 error:
619 return NULL;
620 }
621
622 static
623 void wait_for_sessiond(struct sock_info *sock_info)
624 {
625 int ret;
626
627 ust_lock();
628 if (lttng_ust_comm_should_quit) {
629 goto quit;
630 }
631 if (wait_poll_fallback) {
632 goto error;
633 }
634 if (!sock_info->wait_shm_mmap) {
635 sock_info->wait_shm_mmap = get_map_shm(sock_info);
636 if (!sock_info->wait_shm_mmap)
637 goto error;
638 }
639 ust_unlock();
640
641 DBG("Waiting for %s apps sessiond", sock_info->name);
642 /* Wait for futex wakeup */
643 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
644 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
645 FUTEX_WAIT, 0, NULL, NULL, 0);
646 if (ret < 0) {
647 if (errno == EFAULT) {
648 wait_poll_fallback = 1;
649 DBG(
650 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
651 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
652 "Please upgrade your kernel "
653 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
654 "mainline). LTTng-UST will use polling mode fallback.");
655 if (ust_debug())
656 PERROR("futex");
657 }
658 }
659 }
660 return;
661
662 quit:
663 ust_unlock();
664 return;
665
666 error:
667 ust_unlock();
668 return;
669 }
670
671 /*
672 * This thread does not allocate any resource, except within
673 * handle_message, within mutex protection. This mutex protects against
674 * fork and exit.
675 * The other moment it allocates resources is at socket connexion, which
676 * is also protected by the mutex.
677 */
678 static
679 void *ust_listener_thread(void *arg)
680 {
681 struct sock_info *sock_info = arg;
682 int sock, ret, prev_connect_failed = 0, has_waited = 0;
683
684 /* Restart trying to connect to the session daemon */
685 restart:
686 if (prev_connect_failed) {
687 /* Wait for sessiond availability with pipe */
688 wait_for_sessiond(sock_info);
689 if (has_waited) {
690 has_waited = 0;
691 /*
692 * Sleep for 5 seconds before retrying after a
693 * sequence of failure / wait / failure. This
694 * deals with a killed or broken session daemon.
695 */
696 sleep(5);
697 }
698 has_waited = 1;
699 prev_connect_failed = 0;
700 }
701 ust_lock();
702
703 if (lttng_ust_comm_should_quit) {
704 ust_unlock();
705 goto quit;
706 }
707
708 if (sock_info->socket != -1) {
709 ret = ustcomm_close_unix_sock(sock_info->socket);
710 if (ret) {
711 ERR("Error closing %s apps socket", sock_info->name);
712 }
713 sock_info->socket = -1;
714 }
715
716 /* Register */
717 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
718 if (ret < 0) {
719 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
720 prev_connect_failed = 1;
721 /*
722 * If we cannot find the sessiond daemon, don't delay
723 * constructor execution.
724 */
725 ret = handle_register_done(sock_info);
726 assert(!ret);
727 ust_unlock();
728 goto restart;
729 }
730
731 sock_info->socket = sock = ret;
732
733 /*
734 * Create only one root handle per listener thread for the whole
735 * process lifetime.
736 */
737 if (sock_info->root_handle == -1) {
738 ret = lttng_abi_create_root_handle();
739 if (ret < 0) {
740 ERR("Error creating root handle");
741 ust_unlock();
742 goto quit;
743 }
744 sock_info->root_handle = ret;
745 }
746
747 ret = register_app_to_sessiond(sock);
748 if (ret < 0) {
749 ERR("Error registering to %s apps socket", sock_info->name);
750 prev_connect_failed = 1;
751 /*
752 * If we cannot register to the sessiond daemon, don't
753 * delay constructor execution.
754 */
755 ret = handle_register_done(sock_info);
756 assert(!ret);
757 ust_unlock();
758 goto restart;
759 }
760 ust_unlock();
761
762 for (;;) {
763 ssize_t len;
764 struct ustcomm_ust_msg lum;
765
766 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
767 switch (len) {
768 case 0: /* orderly shutdown */
769 DBG("%s ltt-sessiond has performed an orderly shutdown\n", sock_info->name);
770 ust_lock();
771 /*
772 * Either sessiond has shutdown or refused us by closing the socket.
773 * In either case, we don't want to delay construction execution,
774 * and we need to wait before retry.
775 */
776 prev_connect_failed = 1;
777 /*
778 * If we cannot register to the sessiond daemon, don't
779 * delay constructor execution.
780 */
781 ret = handle_register_done(sock_info);
782 assert(!ret);
783 ust_unlock();
784 goto end;
785 case sizeof(lum):
786 DBG("message received\n");
787 ret = handle_message(sock_info, sock, &lum);
788 if (ret < 0) {
789 ERR("Error handling message for %s socket", sock_info->name);
790 }
791 continue;
792 case -1:
793 DBG("Receive failed from lttng-sessiond with errno %d", errno);
794 if (errno == ECONNRESET) {
795 ERR("%s remote end closed connection\n", sock_info->name);
796 goto end;
797 }
798 goto end;
799 default:
800 ERR("incorrect message size (%s socket): %zd\n", sock_info->name, len);
801 continue;
802 }
803
804 }
805 end:
806 goto restart; /* try to reconnect */
807 quit:
808 return NULL;
809 }
810
811 /*
812 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
813 */
814 static
815 int get_timeout(struct timespec *constructor_timeout)
816 {
817 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
818 char *str_delay;
819 int ret;
820
821 str_delay = getenv("LTTNG_UST_REGISTER_TIMEOUT");
822 if (str_delay) {
823 constructor_delay_ms = strtol(str_delay, NULL, 10);
824 }
825
826 switch (constructor_delay_ms) {
827 case -1:/* fall-through */
828 case 0:
829 return constructor_delay_ms;
830 default:
831 break;
832 }
833
834 /*
835 * If we are unable to find the current time, don't wait.
836 */
837 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
838 if (ret) {
839 return -1;
840 }
841 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
842 constructor_timeout->tv_nsec +=
843 (constructor_delay_ms % 1000UL) * 1000000UL;
844 if (constructor_timeout->tv_nsec >= 1000000000UL) {
845 constructor_timeout->tv_sec++;
846 constructor_timeout->tv_nsec -= 1000000000UL;
847 }
848 return 1;
849 }
850
851 /*
852 * sessiond monitoring thread: monitor presence of global and per-user
853 * sessiond by polling the application common named pipe.
854 */
855 /* TODO */
856
857 void __attribute__((constructor)) lttng_ust_init(void)
858 {
859 struct timespec constructor_timeout;
860 sigset_t sig_all_blocked, orig_parent_mask;
861 int timeout_mode;
862 int ret;
863
864 if (uatomic_xchg(&initialized, 1) == 1)
865 return;
866
867 /*
868 * Fixup interdependency between TLS fixup mutex (which happens
869 * to be the dynamic linker mutex) and ust_lock, taken within
870 * the ust lock.
871 */
872 lttng_fixup_event_tls();
873 lttng_fixup_ringbuffer_tls();
874 lttng_fixup_vtid_tls();
875
876 /*
877 * We want precise control over the order in which we construct
878 * our sub-libraries vs starting to receive commands from
879 * sessiond (otherwise leading to errors when trying to create
880 * sessiond before the init functions are completed).
881 */
882 init_usterr();
883 init_tracepoint();
884 ltt_ring_buffer_metadata_client_init();
885 ltt_ring_buffer_client_overwrite_init();
886 ltt_ring_buffer_client_discard_init();
887
888 timeout_mode = get_timeout(&constructor_timeout);
889
890 ret = sem_init(&constructor_wait, 0, 0);
891 assert(!ret);
892
893 ret = setup_local_apps();
894 if (ret) {
895 ERR("Error setting up to local apps");
896 }
897
898 /* A new thread created by pthread_create inherits the signal mask
899 * from the parent. To avoid any signal being received by the
900 * listener thread, we block all signals temporarily in the parent,
901 * while we create the listener thread.
902 */
903 sigfillset(&sig_all_blocked);
904 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
905 if (ret) {
906 ERR("pthread_sigmask: %s", strerror(ret));
907 }
908
909 ret = pthread_create(&global_apps.ust_listener, NULL,
910 ust_listener_thread, &global_apps);
911 if (ret) {
912 ERR("pthread_create global: %s", strerror(ret));
913 }
914 if (local_apps.allowed) {
915 ret = pthread_create(&local_apps.ust_listener, NULL,
916 ust_listener_thread, &local_apps);
917 if (ret) {
918 ERR("pthread_create local: %s", strerror(ret));
919 }
920 } else {
921 handle_register_done(&local_apps);
922 }
923
924 /* Restore original signal mask in parent */
925 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
926 if (ret) {
927 ERR("pthread_sigmask: %s", strerror(ret));
928 }
929
930 switch (timeout_mode) {
931 case 1: /* timeout wait */
932 do {
933 ret = sem_timedwait(&constructor_wait,
934 &constructor_timeout);
935 } while (ret < 0 && errno == EINTR);
936 if (ret < 0 && errno == ETIMEDOUT) {
937 ERR("Timed out waiting for ltt-sessiond");
938 } else {
939 assert(!ret);
940 }
941 break;
942 case -1:/* wait forever */
943 do {
944 ret = sem_wait(&constructor_wait);
945 } while (ret < 0 && errno == EINTR);
946 assert(!ret);
947 break;
948 case 0: /* no timeout */
949 break;
950 }
951 }
952
953 static
954 void lttng_ust_cleanup(int exiting)
955 {
956 cleanup_sock_info(&global_apps, exiting);
957 if (local_apps.allowed) {
958 cleanup_sock_info(&local_apps, exiting);
959 }
960 /*
961 * The teardown in this function all affect data structures
962 * accessed under the UST lock by the listener thread. This
963 * lock, along with the lttng_ust_comm_should_quit flag, ensure
964 * that none of these threads are accessing this data at this
965 * point.
966 */
967 lttng_ust_abi_exit();
968 lttng_ust_events_exit();
969 ltt_ring_buffer_client_discard_exit();
970 ltt_ring_buffer_client_overwrite_exit();
971 ltt_ring_buffer_metadata_client_exit();
972 exit_tracepoint();
973 if (!exiting) {
974 /* Reinitialize values for fork */
975 sem_count = 2;
976 lttng_ust_comm_should_quit = 0;
977 initialized = 0;
978 }
979 }
980
981 void __attribute__((destructor)) lttng_ust_exit(void)
982 {
983 int ret;
984
985 /*
986 * Using pthread_cancel here because:
987 * A) we don't want to hang application teardown.
988 * B) the thread is not allocating any resource.
989 */
990
991 /*
992 * Require the communication thread to quit. Synchronize with
993 * mutexes to ensure it is not in a mutex critical section when
994 * pthread_cancel is later called.
995 */
996 ust_lock();
997 lttng_ust_comm_should_quit = 1;
998 ust_unlock();
999
1000 /* cancel threads */
1001 ret = pthread_cancel(global_apps.ust_listener);
1002 if (ret) {
1003 ERR("Error cancelling global ust listener thread: %s",
1004 strerror(ret));
1005 }
1006 if (local_apps.allowed) {
1007 ret = pthread_cancel(local_apps.ust_listener);
1008 if (ret) {
1009 ERR("Error cancelling local ust listener thread: %s",
1010 strerror(ret));
1011 }
1012 }
1013 /*
1014 * Do NOT join threads: use of sys_futex makes it impossible to
1015 * join the threads without using async-cancel, but async-cancel
1016 * is delivered by a signal, which could hit the target thread
1017 * anywhere in its code path, including while the ust_lock() is
1018 * held, causing a deadlock for the other thread. Let the OS
1019 * cleanup the threads if there are stalled in a syscall.
1020 */
1021 lttng_ust_cleanup(1);
1022 }
1023
1024 /*
1025 * We exclude the worker threads across fork and clone (except
1026 * CLONE_VM), because these system calls only keep the forking thread
1027 * running in the child. Therefore, we don't want to call fork or clone
1028 * in the middle of an tracepoint or ust tracing state modification.
1029 * Holding this mutex protects these structures across fork and clone.
1030 */
1031 void ust_before_fork(sigset_t *save_sigset)
1032 {
1033 /*
1034 * Disable signals. This is to avoid that the child intervenes
1035 * before it is properly setup for tracing. It is safer to
1036 * disable all signals, because then we know we are not breaking
1037 * anything by restoring the original mask.
1038 */
1039 sigset_t all_sigs;
1040 int ret;
1041
1042 if (lttng_ust_nest_count)
1043 return;
1044 /* Disable signals */
1045 sigfillset(&all_sigs);
1046 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
1047 if (ret == -1) {
1048 PERROR("sigprocmask");
1049 }
1050 ust_lock();
1051 rcu_bp_before_fork();
1052 }
1053
1054 static void ust_after_fork_common(sigset_t *restore_sigset)
1055 {
1056 int ret;
1057
1058 DBG("process %d", getpid());
1059 ust_unlock();
1060 /* Restore signals */
1061 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
1062 if (ret == -1) {
1063 PERROR("sigprocmask");
1064 }
1065 }
1066
1067 void ust_after_fork_parent(sigset_t *restore_sigset)
1068 {
1069 if (lttng_ust_nest_count)
1070 return;
1071 DBG("process %d", getpid());
1072 rcu_bp_after_fork_parent();
1073 /* Release mutexes and reenable signals */
1074 ust_after_fork_common(restore_sigset);
1075 }
1076
1077 /*
1078 * After fork, in the child, we need to cleanup all the leftover state,
1079 * except the worker thread which already magically disappeared thanks
1080 * to the weird Linux fork semantics. After tyding up, we call
1081 * lttng_ust_init() again to start over as a new PID.
1082 *
1083 * This is meant for forks() that have tracing in the child between the
1084 * fork and following exec call (if there is any).
1085 */
1086 void ust_after_fork_child(sigset_t *restore_sigset)
1087 {
1088 if (lttng_ust_nest_count)
1089 return;
1090 DBG("process %d", getpid());
1091 /* Release urcu mutexes */
1092 rcu_bp_after_fork_child();
1093 lttng_ust_cleanup(0);
1094 lttng_context_vtid_reset();
1095 /* Release mutexes and reenable signals */
1096 ust_after_fork_common(restore_sigset);
1097 lttng_ust_init();
1098 }
This page took 0.064984 seconds and 5 git commands to generate.