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