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