Implement base-address-state tracing
[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 <dlfcn.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/futex.h>
40 #include <urcu/compiler.h>
41
42 #include <lttng/ust-events.h>
43 #include <lttng/ust-abi.h>
44 #include <lttng/ust.h>
45 #include <lttng/ust-error.h>
46 #include <lttng/ust-ctl.h>
47 #include <urcu/tls-compat.h>
48 #include <ust-comm.h>
49 #include <usterr-signal-safe.h>
50 #include <helper.h>
51 #include "tracepoint-internal.h"
52 #include "lttng-tracer-core.h"
53 #include "compat.h"
54 #include "../libringbuffer/tlsfixup.h"
55
56 /*
57 * Has lttng ust comm constructor been called ?
58 */
59 static int initialized;
60
61 /*
62 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
63 * Held when handling a command, also held by fork() to deal with
64 * removal of threads, and by exit path.
65 */
66
67 /* Should the ust comm thread quit ? */
68 static int lttng_ust_comm_should_quit;
69
70 /*
71 * Wait for either of these before continuing to the main
72 * program:
73 * - the register_done message from sessiond daemon
74 * (will let the sessiond daemon enable sessions before main
75 * starts.)
76 * - sessiond daemon is not reachable.
77 * - timeout (ensuring applications are resilient to session
78 * daemon problems).
79 */
80 static sem_t constructor_wait;
81 /*
82 * Doing this for both the global and local sessiond.
83 */
84 static int sem_count = { 2 };
85
86 /*
87 * Counting nesting within lttng-ust. Used to ensure that calling fork()
88 * from liblttng-ust does not execute the pre/post fork handlers.
89 */
90 static DEFINE_URCU_TLS(int, lttng_ust_nest_count);
91
92 /*
93 * Info about socket and associated listener thread.
94 */
95 struct sock_info {
96 const char *name;
97 pthread_t ust_listener; /* listener thread */
98 int root_handle;
99 int constructor_sem_posted;
100 int allowed;
101 int global;
102 int thread_active;
103
104 char sock_path[PATH_MAX];
105 int socket;
106 int notify_socket;
107
108 char wait_shm_path[PATH_MAX];
109 char *wait_shm_mmap;
110 struct lttng_session *session_enabled;
111 };
112
113 /* Socket from app (connect) to session daemon (listen) for communication */
114 struct sock_info global_apps = {
115 .name = "global",
116 .global = 1,
117
118 .root_handle = -1,
119 .allowed = 1,
120 .thread_active = 0,
121
122 .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME,
123 .socket = -1,
124 .notify_socket = -1,
125
126 .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME,
127
128 .session_enabled = NULL,
129 };
130
131 /* TODO: allow global_apps_sock_path override */
132
133 struct sock_info local_apps = {
134 .name = "local",
135 .global = 0,
136 .root_handle = -1,
137 .allowed = 0, /* Check setuid bit first */
138 .thread_active = 0,
139
140 .socket = -1,
141 .notify_socket = -1,
142
143 .session_enabled = NULL,
144 };
145
146 static int wait_poll_fallback;
147
148 static const char *cmd_name_mapping[] = {
149 [ LTTNG_UST_RELEASE ] = "Release",
150 [ LTTNG_UST_SESSION ] = "Create Session",
151 [ LTTNG_UST_TRACER_VERSION ] = "Get Tracer Version",
152
153 [ LTTNG_UST_TRACEPOINT_LIST ] = "Create Tracepoint List",
154 [ LTTNG_UST_WAIT_QUIESCENT ] = "Wait for Quiescent State",
155 [ LTTNG_UST_REGISTER_DONE ] = "Registration Done",
156 [ LTTNG_UST_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List",
157
158 /* Session FD commands */
159 [ LTTNG_UST_CHANNEL ] = "Create Channel",
160 [ LTTNG_UST_SESSION_START ] = "Start Session",
161 [ LTTNG_UST_SESSION_STOP ] = "Stop Session",
162
163 /* Channel FD commands */
164 [ LTTNG_UST_STREAM ] = "Create Stream",
165 [ LTTNG_UST_EVENT ] = "Create Event",
166
167 /* Event and Channel FD commands */
168 [ LTTNG_UST_CONTEXT ] = "Create Context",
169 [ LTTNG_UST_FLUSH_BUFFER ] = "Flush Buffer",
170
171 /* Event, Channel and Session commands */
172 [ LTTNG_UST_ENABLE ] = "Enable",
173 [ LTTNG_UST_DISABLE ] = "Disable",
174
175 /* Tracepoint list commands */
176 [ LTTNG_UST_TRACEPOINT_LIST_GET ] = "List Next Tracepoint",
177 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field",
178
179 /* Event FD commands */
180 [ LTTNG_UST_FILTER ] = "Create Filter",
181 [ LTTNG_UST_EXCLUSION ] = "Add exclusions to event",
182 };
183
184 static const char *str_timeout;
185 static int got_timeout_env;
186 static void *ust_baddr_handle;
187
188 extern void lttng_ring_buffer_client_overwrite_init(void);
189 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
190 extern void lttng_ring_buffer_client_discard_init(void);
191 extern void lttng_ring_buffer_client_discard_rt_init(void);
192 extern void lttng_ring_buffer_metadata_client_init(void);
193 extern void lttng_ring_buffer_client_overwrite_exit(void);
194 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
195 extern void lttng_ring_buffer_client_discard_exit(void);
196 extern void lttng_ring_buffer_client_discard_rt_exit(void);
197 extern void lttng_ring_buffer_metadata_client_exit(void);
198
199 /*
200 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
201 * pointer.
202 */
203 static
204 const char *get_lttng_home_dir(void)
205 {
206 const char *val;
207
208 val = (const char *) getenv("LTTNG_HOME");
209 if (val != NULL) {
210 return val;
211 }
212 return (const char *) getenv("HOME");
213 }
214
215 /*
216 * Force a read (imply TLS fixup for dlopen) of TLS variables.
217 */
218 static
219 void lttng_fixup_nest_count_tls(void)
220 {
221 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count)));
222 }
223
224 int lttng_get_notify_socket(void *owner)
225 {
226 struct sock_info *info = owner;
227
228 return info->notify_socket;
229 }
230
231 static
232 void print_cmd(int cmd, int handle)
233 {
234 const char *cmd_name = "Unknown";
235
236 if (cmd >= 0 && cmd < LTTNG_ARRAY_SIZE(cmd_name_mapping)
237 && cmd_name_mapping[cmd]) {
238 cmd_name = cmd_name_mapping[cmd];
239 }
240 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
241 cmd_name, cmd,
242 lttng_ust_obj_get_name(handle), handle);
243 }
244
245 static
246 void *lttng_ust_baddr_handle(void)
247 {
248 if (!ust_baddr_handle) {
249 ust_baddr_handle = dlopen(
250 "liblttng-ust-baddr.so.0", RTLD_NOW | RTLD_GLOBAL);
251 if (ust_baddr_handle == NULL)
252 ERR("%s", dlerror());
253 }
254 return ust_baddr_handle;
255 }
256
257 static
258 int lttng_ust_baddr_statedump(struct lttng_session *session)
259 {
260 static
261 int (*lttng_ust_baddr_init_fn)(struct lttng_session *);
262
263 if (!lttng_ust_baddr_init_fn) {
264 void *baddr_handle = lttng_ust_baddr_handle();
265 if (baddr_handle) {
266 lttng_ust_baddr_init_fn = dlsym(baddr_handle,
267 "lttng_ust_baddr_statedump");
268 if (lttng_ust_baddr_init_fn == NULL)
269 ERR("%s", dlerror());
270 }
271 if (!lttng_ust_baddr_init_fn)
272 return -1;
273 }
274
275 return lttng_ust_baddr_init_fn(session);
276 }
277
278 static
279 int setup_local_apps(void)
280 {
281 const char *home_dir;
282 uid_t uid;
283
284 uid = getuid();
285 /*
286 * Disallow per-user tracing for setuid binaries.
287 */
288 if (uid != geteuid()) {
289 assert(local_apps.allowed == 0);
290 return 0;
291 }
292 home_dir = get_lttng_home_dir();
293 if (!home_dir) {
294 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
295 assert(local_apps.allowed == 0);
296 return -ENOENT;
297 }
298 local_apps.allowed = 1;
299 snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s",
300 home_dir,
301 LTTNG_DEFAULT_HOME_RUNDIR,
302 LTTNG_UST_SOCK_FILENAME);
303 snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u",
304 LTTNG_UST_WAIT_FILENAME,
305 uid);
306 return 0;
307 }
308
309 /*
310 * Get notify_sock timeout, in ms.
311 * -1: don't wait. 0: wait forever. >0: timeout, in ms.
312 */
313 static
314 long get_timeout(void)
315 {
316 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
317
318 if (!got_timeout_env) {
319 str_timeout = getenv("LTTNG_UST_REGISTER_TIMEOUT");
320 got_timeout_env = 1;
321 }
322 if (str_timeout)
323 constructor_delay_ms = strtol(str_timeout, NULL, 10);
324 return constructor_delay_ms;
325 }
326
327 static
328 long get_notify_sock_timeout(void)
329 {
330 return get_timeout();
331 }
332
333 /*
334 * Return values: -1: don't wait. 0: wait forever. 1: timeout wait.
335 */
336 static
337 int get_constructor_timeout(struct timespec *constructor_timeout)
338 {
339 long constructor_delay_ms;
340 int ret;
341
342 constructor_delay_ms = get_timeout();
343
344 switch (constructor_delay_ms) {
345 case -1:/* fall-through */
346 case 0:
347 return constructor_delay_ms;
348 default:
349 break;
350 }
351
352 /*
353 * If we are unable to find the current time, don't wait.
354 */
355 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
356 if (ret) {
357 return -1;
358 }
359 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
360 constructor_timeout->tv_nsec +=
361 (constructor_delay_ms % 1000UL) * 1000000UL;
362 if (constructor_timeout->tv_nsec >= 1000000000UL) {
363 constructor_timeout->tv_sec++;
364 constructor_timeout->tv_nsec -= 1000000000UL;
365 }
366 return 1;
367 }
368
369 static
370 int register_to_sessiond(int socket, enum ustctl_socket_type type)
371 {
372 return ustcomm_send_reg_msg(socket,
373 type,
374 CAA_BITS_PER_LONG,
375 lttng_alignof(uint8_t) * CHAR_BIT,
376 lttng_alignof(uint16_t) * CHAR_BIT,
377 lttng_alignof(uint32_t) * CHAR_BIT,
378 lttng_alignof(uint64_t) * CHAR_BIT,
379 lttng_alignof(unsigned long) * CHAR_BIT);
380 }
381
382 static
383 int send_reply(int sock, struct ustcomm_ust_reply *lur)
384 {
385 ssize_t len;
386
387 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
388 switch (len) {
389 case sizeof(*lur):
390 DBG("message successfully sent");
391 return 0;
392 default:
393 if (len == -ECONNRESET) {
394 DBG("remote end closed connection");
395 return 0;
396 }
397 if (len < 0)
398 return len;
399 DBG("incorrect message size: %zd", len);
400 return -EINVAL;
401 }
402 }
403
404 static
405 int handle_register_done(struct sock_info *sock_info)
406 {
407 int ret;
408
409 if (sock_info->constructor_sem_posted)
410 return 0;
411 sock_info->constructor_sem_posted = 1;
412 if (uatomic_read(&sem_count) <= 0) {
413 return 0;
414 }
415 ret = uatomic_add_return(&sem_count, -1);
416 if (ret == 0) {
417 ret = sem_post(&constructor_wait);
418 assert(!ret);
419 }
420 return 0;
421 }
422
423 static
424 int handle_message(struct sock_info *sock_info,
425 int sock, struct ustcomm_ust_msg *lum)
426 {
427 int ret = 0;
428 const struct lttng_ust_objd_ops *ops;
429 struct ustcomm_ust_reply lur;
430 union ust_args args;
431 ssize_t len;
432
433 ust_lock();
434
435 memset(&lur, 0, sizeof(lur));
436
437 if (lttng_ust_comm_should_quit) {
438 ret = -LTTNG_UST_ERR_EXITING;
439 goto end;
440 }
441
442 ops = objd_ops(lum->handle);
443 if (!ops) {
444 ret = -ENOENT;
445 goto end;
446 }
447
448 switch (lum->cmd) {
449 case LTTNG_UST_REGISTER_DONE:
450 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
451 ret = handle_register_done(sock_info);
452 else
453 ret = -EINVAL;
454 break;
455 case LTTNG_UST_RELEASE:
456 if (lum->handle == LTTNG_UST_ROOT_HANDLE)
457 ret = -EPERM;
458 else
459 ret = lttng_ust_objd_unref(lum->handle, 1);
460 break;
461 case LTTNG_UST_FILTER:
462 {
463 /* Receive filter data */
464 struct lttng_ust_filter_bytecode_node *bytecode;
465
466 if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) {
467 ERR("Filter data size is too large: %u bytes",
468 lum->u.filter.data_size);
469 ret = -EINVAL;
470 goto error;
471 }
472
473 if (lum->u.filter.reloc_offset > lum->u.filter.data_size) {
474 ERR("Filter reloc offset %u is not within data",
475 lum->u.filter.reloc_offset);
476 ret = -EINVAL;
477 goto error;
478 }
479
480 bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size);
481 if (!bytecode) {
482 ret = -ENOMEM;
483 goto error;
484 }
485 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data,
486 lum->u.filter.data_size);
487 switch (len) {
488 case 0: /* orderly shutdown */
489 ret = 0;
490 free(bytecode);
491 goto error;
492 default:
493 if (len == lum->u.filter.data_size) {
494 DBG("filter data received");
495 break;
496 } else if (len < 0) {
497 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
498 if (len == -ECONNRESET) {
499 ERR("%s remote end closed connection", sock_info->name);
500 ret = len;
501 free(bytecode);
502 goto error;
503 }
504 ret = len;
505 free(bytecode);
506 goto end;
507 } else {
508 DBG("incorrect filter data message size: %zd", len);
509 ret = -EINVAL;
510 free(bytecode);
511 goto end;
512 }
513 }
514 bytecode->bc.len = lum->u.filter.data_size;
515 bytecode->bc.reloc_offset = lum->u.filter.reloc_offset;
516 bytecode->bc.seqnum = lum->u.filter.seqnum;
517 if (ops->cmd) {
518 ret = ops->cmd(lum->handle, lum->cmd,
519 (unsigned long) bytecode,
520 &args, sock_info);
521 if (ret) {
522 free(bytecode);
523 }
524 /* don't free bytecode if everything went fine. */
525 } else {
526 ret = -ENOSYS;
527 free(bytecode);
528 }
529 break;
530 }
531 case LTTNG_UST_EXCLUSION:
532 {
533 /* Receive exclusion names */
534 struct lttng_ust_excluder_node *node;
535 unsigned int count;
536
537 count = lum->u.exclusion.count;
538 if (count == 0) {
539 /* There are no names to read */
540 ret = 0;
541 goto error;
542 }
543 node = zmalloc(sizeof(*node) +
544 count * LTTNG_UST_SYM_NAME_LEN);
545 if (!node) {
546 ret = -ENOMEM;
547 goto error;
548 }
549 node->excluder.count = count;
550 len = ustcomm_recv_unix_sock(sock, node->excluder.names,
551 count * LTTNG_UST_SYM_NAME_LEN);
552 switch (len) {
553 case 0: /* orderly shutdown */
554 ret = 0;
555 free(node);
556 goto error;
557 default:
558 if (len == count * LTTNG_UST_SYM_NAME_LEN) {
559 DBG("Exclusion data received");
560 break;
561 } else if (len < 0) {
562 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
563 if (len == -ECONNRESET) {
564 ERR("%s remote end closed connection", sock_info->name);
565 ret = len;
566 free(node);
567 goto error;
568 }
569 ret = len;
570 free(node);
571 goto end;
572 } else {
573 DBG("Incorrect exclusion data message size: %zd", len);
574 ret = -EINVAL;
575 free(node);
576 goto end;
577 }
578 }
579 if (ops->cmd) {
580 ret = ops->cmd(lum->handle, lum->cmd,
581 (unsigned long) node,
582 &args, sock_info);
583 if (ret) {
584 free(node);
585 }
586 /* Don't free exclusion data if everything went fine. */
587 } else {
588 ret = -ENOSYS;
589 free(node);
590 }
591 break;
592 }
593 case LTTNG_UST_CHANNEL:
594 {
595 void *chan_data;
596 int wakeup_fd;
597
598 len = ustcomm_recv_channel_from_sessiond(sock,
599 &chan_data, lum->u.channel.len,
600 &wakeup_fd);
601 switch (len) {
602 case 0: /* orderly shutdown */
603 ret = 0;
604 goto error;
605 default:
606 if (len == lum->u.channel.len) {
607 DBG("channel data received");
608 break;
609 } else if (len < 0) {
610 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
611 if (len == -ECONNRESET) {
612 ERR("%s remote end closed connection", sock_info->name);
613 ret = len;
614 goto error;
615 }
616 ret = len;
617 goto end;
618 } else {
619 DBG("incorrect channel data message size: %zd", len);
620 ret = -EINVAL;
621 goto end;
622 }
623 }
624 args.channel.chan_data = chan_data;
625 args.channel.wakeup_fd = wakeup_fd;
626 if (ops->cmd)
627 ret = ops->cmd(lum->handle, lum->cmd,
628 (unsigned long) &lum->u,
629 &args, sock_info);
630 else
631 ret = -ENOSYS;
632 break;
633 }
634 case LTTNG_UST_STREAM:
635 {
636 /* Receive shm_fd, wakeup_fd */
637 ret = ustcomm_recv_stream_from_sessiond(sock,
638 &lum->u.stream.len,
639 &args.stream.shm_fd,
640 &args.stream.wakeup_fd);
641 if (ret) {
642 goto end;
643 }
644 if (ops->cmd)
645 ret = ops->cmd(lum->handle, lum->cmd,
646 (unsigned long) &lum->u,
647 &args, sock_info);
648 else
649 ret = -ENOSYS;
650 break;
651 }
652 default:
653 if (ops->cmd)
654 ret = ops->cmd(lum->handle, lum->cmd,
655 (unsigned long) &lum->u,
656 &args, sock_info);
657 else
658 ret = -ENOSYS;
659 break;
660 }
661
662 end:
663 lur.handle = lum->handle;
664 lur.cmd = lum->cmd;
665 lur.ret_val = ret;
666 if (ret >= 0) {
667 lur.ret_code = LTTNG_UST_OK;
668 } else {
669 /*
670 * Use -LTTNG_UST_ERR as wildcard for UST internal
671 * error that are not caused by the transport, except if
672 * we already have a more precise error message to
673 * report.
674 */
675 if (ret > -LTTNG_UST_ERR) {
676 /* Translate code to UST error. */
677 switch (ret) {
678 case -EEXIST:
679 lur.ret_code = -LTTNG_UST_ERR_EXIST;
680 break;
681 case -EINVAL:
682 lur.ret_code = -LTTNG_UST_ERR_INVAL;
683 break;
684 case -ENOENT:
685 lur.ret_code = -LTTNG_UST_ERR_NOENT;
686 break;
687 case -EPERM:
688 lur.ret_code = -LTTNG_UST_ERR_PERM;
689 break;
690 case -ENOSYS:
691 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
692 break;
693 default:
694 lur.ret_code = -LTTNG_UST_ERR;
695 break;
696 }
697 } else {
698 lur.ret_code = ret;
699 }
700 }
701 if (ret >= 0) {
702 switch (lum->cmd) {
703 case LTTNG_UST_TRACER_VERSION:
704 lur.u.version = lum->u.version;
705 break;
706 case LTTNG_UST_TRACEPOINT_LIST_GET:
707 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
708 break;
709 }
710 }
711 DBG("Return value: %d", lur.ret_val);
712 ret = send_reply(sock, &lur);
713 if (ret < 0) {
714 DBG("error sending reply");
715 goto error;
716 }
717
718 /*
719 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
720 * after the reply.
721 */
722 if (lur.ret_code == LTTNG_UST_OK) {
723 switch (lum->cmd) {
724 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET:
725 len = ustcomm_send_unix_sock(sock,
726 &args.field_list.entry,
727 sizeof(args.field_list.entry));
728 if (len < 0) {
729 ret = len;
730 goto error;
731 }
732 if (len != sizeof(args.field_list.entry)) {
733 ret = -EINVAL;
734 goto error;
735 }
736 }
737 }
738
739 error:
740 ust_unlock();
741 return ret;
742 }
743
744 static
745 void cleanup_sock_info(struct sock_info *sock_info, int exiting)
746 {
747 int ret;
748
749 if (sock_info->root_handle != -1) {
750 ret = lttng_ust_objd_unref(sock_info->root_handle, 1);
751 if (ret) {
752 ERR("Error unref root handle");
753 }
754 sock_info->root_handle = -1;
755 }
756 sock_info->constructor_sem_posted = 0;
757
758 /*
759 * wait_shm_mmap, socket and notify socket are used by listener
760 * threads outside of the ust lock, so we cannot tear them down
761 * ourselves, because we cannot join on these threads. Leave
762 * responsibility of cleaning up these resources to the OS
763 * process exit.
764 */
765 if (exiting)
766 return;
767
768 if (sock_info->socket != -1) {
769 ret = ustcomm_close_unix_sock(sock_info->socket);
770 if (ret) {
771 ERR("Error closing ust cmd socket");
772 }
773 sock_info->socket = -1;
774 }
775 if (sock_info->notify_socket != -1) {
776 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
777 if (ret) {
778 ERR("Error closing ust notify socket");
779 }
780 sock_info->notify_socket = -1;
781 }
782 if (sock_info->wait_shm_mmap) {
783 ret = munmap(sock_info->wait_shm_mmap, sysconf(_SC_PAGE_SIZE));
784 if (ret) {
785 ERR("Error unmapping wait shm");
786 }
787 sock_info->wait_shm_mmap = NULL;
788 }
789 }
790
791 /*
792 * Using fork to set umask in the child process (not multi-thread safe).
793 * We deal with the shm_open vs ftruncate race (happening when the
794 * sessiond owns the shm and does not let everybody modify it, to ensure
795 * safety against shm_unlink) by simply letting the mmap fail and
796 * retrying after a few seconds.
797 * For global shm, everybody has rw access to it until the sessiond
798 * starts.
799 */
800 static
801 int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
802 {
803 int wait_shm_fd, ret;
804 pid_t pid;
805
806 /*
807 * Try to open read-only.
808 */
809 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
810 if (wait_shm_fd >= 0) {
811 int32_t tmp_read;
812 ssize_t len;
813 size_t bytes_read = 0;
814
815 /*
816 * Try to read the fd. If unable to do so, try opening
817 * it in write mode.
818 */
819 do {
820 len = read(wait_shm_fd,
821 &((char *) &tmp_read)[bytes_read],
822 sizeof(tmp_read) - bytes_read);
823 if (len > 0) {
824 bytes_read += len;
825 }
826 } while ((len < 0 && errno == EINTR)
827 || (len > 0 && bytes_read < sizeof(tmp_read)));
828 if (bytes_read != sizeof(tmp_read)) {
829 ret = close(wait_shm_fd);
830 if (ret) {
831 ERR("close wait_shm_fd");
832 }
833 goto open_write;
834 }
835 goto end;
836 } else if (wait_shm_fd < 0 && errno != ENOENT) {
837 /*
838 * Real-only open did not work, and it's not because the
839 * entry was not present. It's a failure that prohibits
840 * using shm.
841 */
842 ERR("Error opening shm %s", sock_info->wait_shm_path);
843 goto end;
844 }
845
846 open_write:
847 /*
848 * If the open failed because the file did not exist, or because
849 * the file was not truncated yet, try creating it ourself.
850 */
851 URCU_TLS(lttng_ust_nest_count)++;
852 pid = fork();
853 URCU_TLS(lttng_ust_nest_count)--;
854 if (pid > 0) {
855 int status;
856
857 /*
858 * Parent: wait for child to return, in which case the
859 * shared memory map will have been created.
860 */
861 pid = wait(&status);
862 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
863 wait_shm_fd = -1;
864 goto end;
865 }
866 /*
867 * Try to open read-only again after creation.
868 */
869 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
870 if (wait_shm_fd < 0) {
871 /*
872 * Real-only open did not work. It's a failure
873 * that prohibits using shm.
874 */
875 ERR("Error opening shm %s", sock_info->wait_shm_path);
876 goto end;
877 }
878 goto end;
879 } else if (pid == 0) {
880 int create_mode;
881
882 /* Child */
883 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
884 if (sock_info->global)
885 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
886 /*
887 * We're alone in a child process, so we can modify the
888 * process-wide umask.
889 */
890 umask(~create_mode);
891 /*
892 * Try creating shm (or get rw access).
893 * We don't do an exclusive open, because we allow other
894 * processes to create+ftruncate it concurrently.
895 */
896 wait_shm_fd = shm_open(sock_info->wait_shm_path,
897 O_RDWR | O_CREAT, create_mode);
898 if (wait_shm_fd >= 0) {
899 ret = ftruncate(wait_shm_fd, mmap_size);
900 if (ret) {
901 PERROR("ftruncate");
902 _exit(EXIT_FAILURE);
903 }
904 _exit(EXIT_SUCCESS);
905 }
906 /*
907 * For local shm, we need to have rw access to accept
908 * opening it: this means the local sessiond will be
909 * able to wake us up. For global shm, we open it even
910 * if rw access is not granted, because the root.root
911 * sessiond will be able to override all rights and wake
912 * us up.
913 */
914 if (!sock_info->global && errno != EACCES) {
915 ERR("Error opening shm %s", sock_info->wait_shm_path);
916 _exit(EXIT_FAILURE);
917 }
918 /*
919 * The shm exists, but we cannot open it RW. Report
920 * success.
921 */
922 _exit(EXIT_SUCCESS);
923 } else {
924 return -1;
925 }
926 end:
927 if (wait_shm_fd >= 0 && !sock_info->global) {
928 struct stat statbuf;
929
930 /*
931 * Ensure that our user is the owner of the shm file for
932 * local shm. If we do not own the file, it means our
933 * sessiond will not have access to wake us up (there is
934 * probably a rogue process trying to fake our
935 * sessiond). Fallback to polling method in this case.
936 */
937 ret = fstat(wait_shm_fd, &statbuf);
938 if (ret) {
939 PERROR("fstat");
940 goto error_close;
941 }
942 if (statbuf.st_uid != getuid())
943 goto error_close;
944 }
945 return wait_shm_fd;
946
947 error_close:
948 ret = close(wait_shm_fd);
949 if (ret) {
950 PERROR("Error closing fd");
951 }
952 return -1;
953 }
954
955 static
956 char *get_map_shm(struct sock_info *sock_info)
957 {
958 size_t mmap_size = sysconf(_SC_PAGE_SIZE);
959 int wait_shm_fd, ret;
960 char *wait_shm_mmap;
961
962 wait_shm_fd = get_wait_shm(sock_info, mmap_size);
963 if (wait_shm_fd < 0) {
964 goto error;
965 }
966 wait_shm_mmap = mmap(NULL, mmap_size, PROT_READ,
967 MAP_SHARED, wait_shm_fd, 0);
968 /* close shm fd immediately after taking the mmap reference */
969 ret = close(wait_shm_fd);
970 if (ret) {
971 PERROR("Error closing fd");
972 }
973 if (wait_shm_mmap == MAP_FAILED) {
974 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
975 goto error;
976 }
977 return wait_shm_mmap;
978
979 error:
980 return NULL;
981 }
982
983 static
984 void wait_for_sessiond(struct sock_info *sock_info)
985 {
986 int ret;
987
988 ust_lock();
989 if (lttng_ust_comm_should_quit) {
990 goto quit;
991 }
992 if (wait_poll_fallback) {
993 goto error;
994 }
995 if (!sock_info->wait_shm_mmap) {
996 sock_info->wait_shm_mmap = get_map_shm(sock_info);
997 if (!sock_info->wait_shm_mmap)
998 goto error;
999 }
1000 ust_unlock();
1001
1002 DBG("Waiting for %s apps sessiond", sock_info->name);
1003 /* Wait for futex wakeup */
1004 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap) == 0) {
1005 ret = futex_async((int32_t *) sock_info->wait_shm_mmap,
1006 FUTEX_WAIT, 0, NULL, NULL, 0);
1007 if (ret < 0) {
1008 if (errno == EFAULT) {
1009 wait_poll_fallback = 1;
1010 DBG(
1011 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1012 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1013 "Please upgrade your kernel "
1014 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1015 "mainline). LTTng-UST will use polling mode fallback.");
1016 if (ust_debug())
1017 PERROR("futex");
1018 }
1019 }
1020 }
1021 return;
1022
1023 quit:
1024 ust_unlock();
1025 return;
1026
1027 error:
1028 ust_unlock();
1029 return;
1030 }
1031
1032 /*
1033 * This thread does not allocate any resource, except within
1034 * handle_message, within mutex protection. This mutex protects against
1035 * fork and exit.
1036 * The other moment it allocates resources is at socket connection, which
1037 * is also protected by the mutex.
1038 */
1039 static
1040 void *ust_listener_thread(void *arg)
1041 {
1042 struct sock_info *sock_info = arg;
1043 int sock, ret, prev_connect_failed = 0, has_waited = 0;
1044 long timeout;
1045
1046 /* Restart trying to connect to the session daemon */
1047 restart:
1048 if (prev_connect_failed) {
1049 /* Wait for sessiond availability with pipe */
1050 wait_for_sessiond(sock_info);
1051 if (has_waited) {
1052 has_waited = 0;
1053 /*
1054 * Sleep for 5 seconds before retrying after a
1055 * sequence of failure / wait / failure. This
1056 * deals with a killed or broken session daemon.
1057 */
1058 sleep(5);
1059 }
1060 has_waited = 1;
1061 prev_connect_failed = 0;
1062 }
1063
1064 if (sock_info->socket != -1) {
1065 ret = ustcomm_close_unix_sock(sock_info->socket);
1066 if (ret) {
1067 ERR("Error closing %s ust cmd socket",
1068 sock_info->name);
1069 }
1070 sock_info->socket = -1;
1071 }
1072 if (sock_info->notify_socket != -1) {
1073 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1074 if (ret) {
1075 ERR("Error closing %s ust notify socket",
1076 sock_info->name);
1077 }
1078 sock_info->notify_socket = -1;
1079 }
1080
1081 /*
1082 * Register. We need to perform both connect and sending
1083 * registration message before doing the next connect otherwise
1084 * we may reach unix socket connect queue max limits and block
1085 * on the 2nd connect while the session daemon is awaiting the
1086 * first connect registration message.
1087 */
1088 /* Connect cmd socket */
1089 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
1090 if (ret < 0) {
1091 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1092 prev_connect_failed = 1;
1093
1094 ust_lock();
1095
1096 if (lttng_ust_comm_should_quit) {
1097 goto quit;
1098 }
1099
1100 /*
1101 * If we cannot find the sessiond daemon, don't delay
1102 * constructor execution.
1103 */
1104 ret = handle_register_done(sock_info);
1105 assert(!ret);
1106 ust_unlock();
1107 goto restart;
1108 }
1109 sock_info->socket = ret;
1110
1111 ust_lock();
1112
1113 if (lttng_ust_comm_should_quit) {
1114 goto quit;
1115 }
1116
1117 /*
1118 * Create only one root handle per listener thread for the whole
1119 * process lifetime, so we ensure we get ID which is statically
1120 * assigned to the root handle.
1121 */
1122 if (sock_info->root_handle == -1) {
1123 ret = lttng_abi_create_root_handle();
1124 if (ret < 0) {
1125 ERR("Error creating root handle");
1126 goto quit;
1127 }
1128 sock_info->root_handle = ret;
1129 }
1130
1131 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
1132 if (ret < 0) {
1133 ERR("Error registering to %s ust cmd socket",
1134 sock_info->name);
1135 prev_connect_failed = 1;
1136 /*
1137 * If we cannot register to the sessiond daemon, don't
1138 * delay constructor execution.
1139 */
1140 ret = handle_register_done(sock_info);
1141 assert(!ret);
1142 ust_unlock();
1143 goto restart;
1144 }
1145
1146 ust_unlock();
1147
1148 /* Connect notify socket */
1149 ret = ustcomm_connect_unix_sock(sock_info->sock_path);
1150 if (ret < 0) {
1151 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1152 prev_connect_failed = 1;
1153
1154 ust_lock();
1155
1156 if (lttng_ust_comm_should_quit) {
1157 goto quit;
1158 }
1159
1160 /*
1161 * If we cannot find the sessiond daemon, don't delay
1162 * constructor execution.
1163 */
1164 ret = handle_register_done(sock_info);
1165 assert(!ret);
1166 ust_unlock();
1167 goto restart;
1168 }
1169 sock_info->notify_socket = ret;
1170
1171 timeout = get_notify_sock_timeout();
1172 if (timeout >= 0) {
1173 /*
1174 * Give at least 10ms to sessiond to reply to
1175 * notifications.
1176 */
1177 if (timeout < 10)
1178 timeout = 10;
1179 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1180 timeout);
1181 if (ret < 0) {
1182 WARN("Error setting socket receive timeout");
1183 }
1184 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1185 timeout);
1186 if (ret < 0) {
1187 WARN("Error setting socket send timeout");
1188 }
1189 } else if (timeout < -1) {
1190 WARN("Unsupported timeout value %ld", timeout);
1191 }
1192
1193 ust_lock();
1194
1195 if (lttng_ust_comm_should_quit) {
1196 goto quit;
1197 }
1198
1199 ret = register_to_sessiond(sock_info->notify_socket,
1200 USTCTL_SOCKET_NOTIFY);
1201 if (ret < 0) {
1202 ERR("Error registering to %s ust notify socket",
1203 sock_info->name);
1204 prev_connect_failed = 1;
1205 /*
1206 * If we cannot register to the sessiond daemon, don't
1207 * delay constructor execution.
1208 */
1209 ret = handle_register_done(sock_info);
1210 assert(!ret);
1211 ust_unlock();
1212 goto restart;
1213 }
1214 sock = sock_info->socket;
1215
1216 ust_unlock();
1217
1218 for (;;) {
1219 ssize_t len;
1220 struct ustcomm_ust_msg lum;
1221
1222 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
1223 switch (len) {
1224 case 0: /* orderly shutdown */
1225 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
1226 ust_lock();
1227 if (lttng_ust_comm_should_quit) {
1228 goto quit;
1229 }
1230 /*
1231 * Either sessiond has shutdown or refused us by closing the socket.
1232 * In either case, we don't want to delay construction execution,
1233 * and we need to wait before retry.
1234 */
1235 prev_connect_failed = 1;
1236 /*
1237 * If we cannot register to the sessiond daemon, don't
1238 * delay constructor execution.
1239 */
1240 ret = handle_register_done(sock_info);
1241 assert(!ret);
1242 ust_unlock();
1243 goto end;
1244 case sizeof(lum):
1245 print_cmd(lum.cmd, lum.handle);
1246 ret = handle_message(sock_info, sock, &lum);
1247 if (ret) {
1248 ERR("Error handling message for %s socket", sock_info->name);
1249 } else {
1250 struct lttng_session *session =
1251 sock_info->session_enabled;
1252 if (session) {
1253 sock_info->session_enabled = NULL;
1254 lttng_ust_baddr_statedump(session);
1255 }
1256 }
1257 continue;
1258 default:
1259 if (len < 0) {
1260 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1261 } else {
1262 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
1263 }
1264 if (len == -ECONNRESET) {
1265 DBG("%s remote end closed connection", sock_info->name);
1266 goto end;
1267 }
1268 goto end;
1269 }
1270
1271 }
1272 end:
1273 ust_lock();
1274 if (lttng_ust_comm_should_quit) {
1275 goto quit;
1276 }
1277 /* Cleanup socket handles before trying to reconnect */
1278 lttng_ust_objd_table_owner_cleanup(sock_info);
1279 ust_unlock();
1280 goto restart; /* try to reconnect */
1281
1282 quit:
1283 sock_info->thread_active = 0;
1284 ust_unlock();
1285 return NULL;
1286 }
1287
1288 /*
1289 * sessiond monitoring thread: monitor presence of global and per-user
1290 * sessiond by polling the application common named pipe.
1291 */
1292 void __attribute__((constructor)) lttng_ust_init(void)
1293 {
1294 struct timespec constructor_timeout;
1295 sigset_t sig_all_blocked, orig_parent_mask;
1296 pthread_attr_t thread_attr;
1297 int timeout_mode;
1298 int ret;
1299
1300 if (uatomic_xchg(&initialized, 1) == 1)
1301 return;
1302
1303 /*
1304 * Fixup interdependency between TLS fixup mutex (which happens
1305 * to be the dynamic linker mutex) and ust_lock, taken within
1306 * the ust lock.
1307 */
1308 lttng_fixup_ringbuffer_tls();
1309 lttng_fixup_vtid_tls();
1310 lttng_fixup_nest_count_tls();
1311 lttng_fixup_procname_tls();
1312
1313 /*
1314 * We want precise control over the order in which we construct
1315 * our sub-libraries vs starting to receive commands from
1316 * sessiond (otherwise leading to errors when trying to create
1317 * sessiond before the init functions are completed).
1318 */
1319 init_usterr();
1320 init_tracepoint();
1321 lttng_ring_buffer_metadata_client_init();
1322 lttng_ring_buffer_client_overwrite_init();
1323 lttng_ring_buffer_client_overwrite_rt_init();
1324 lttng_ring_buffer_client_discard_init();
1325 lttng_ring_buffer_client_discard_rt_init();
1326 lttng_context_init();
1327
1328 timeout_mode = get_constructor_timeout(&constructor_timeout);
1329
1330 ret = sem_init(&constructor_wait, 0, 0);
1331 assert(!ret);
1332
1333 ret = setup_local_apps();
1334 if (ret) {
1335 DBG("local apps setup returned %d", ret);
1336 }
1337
1338 /* A new thread created by pthread_create inherits the signal mask
1339 * from the parent. To avoid any signal being received by the
1340 * listener thread, we block all signals temporarily in the parent,
1341 * while we create the listener thread.
1342 */
1343 sigfillset(&sig_all_blocked);
1344 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
1345 if (ret) {
1346 ERR("pthread_sigmask: %s", strerror(ret));
1347 }
1348
1349 ret = pthread_attr_init(&thread_attr);
1350 if (ret) {
1351 ERR("pthread_attr_init: %s", strerror(ret));
1352 }
1353 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
1354 if (ret) {
1355 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
1356 }
1357
1358 ust_lock();
1359 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
1360 ust_listener_thread, &global_apps);
1361 if (ret) {
1362 ERR("pthread_create global: %s", strerror(ret));
1363 }
1364 global_apps.thread_active = 1;
1365 ust_unlock();
1366
1367 if (local_apps.allowed) {
1368 ust_lock();
1369 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
1370 ust_listener_thread, &local_apps);
1371 if (ret) {
1372 ERR("pthread_create local: %s", strerror(ret));
1373 }
1374 local_apps.thread_active = 1;
1375 ust_unlock();
1376 } else {
1377 handle_register_done(&local_apps);
1378 }
1379 ret = pthread_attr_destroy(&thread_attr);
1380 if (ret) {
1381 ERR("pthread_attr_destroy: %s", strerror(ret));
1382 }
1383
1384 /* Restore original signal mask in parent */
1385 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
1386 if (ret) {
1387 ERR("pthread_sigmask: %s", strerror(ret));
1388 }
1389
1390 switch (timeout_mode) {
1391 case 1: /* timeout wait */
1392 do {
1393 ret = sem_timedwait(&constructor_wait,
1394 &constructor_timeout);
1395 } while (ret < 0 && errno == EINTR);
1396 if (ret < 0 && errno == ETIMEDOUT) {
1397 ERR("Timed out waiting for lttng-sessiond");
1398 } else {
1399 assert(!ret);
1400 }
1401 break;
1402 case -1:/* wait forever */
1403 do {
1404 ret = sem_wait(&constructor_wait);
1405 } while (ret < 0 && errno == EINTR);
1406 assert(!ret);
1407 break;
1408 case 0: /* no timeout */
1409 break;
1410 }
1411 }
1412
1413 static
1414 void lttng_ust_cleanup(int exiting)
1415 {
1416 cleanup_sock_info(&global_apps, exiting);
1417 if (local_apps.allowed) {
1418 cleanup_sock_info(&local_apps, exiting);
1419 }
1420 /*
1421 * The teardown in this function all affect data structures
1422 * accessed under the UST lock by the listener thread. This
1423 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1424 * that none of these threads are accessing this data at this
1425 * point.
1426 */
1427 lttng_ust_abi_exit();
1428 lttng_ust_events_exit();
1429 lttng_context_exit();
1430 lttng_ring_buffer_client_discard_rt_exit();
1431 lttng_ring_buffer_client_discard_exit();
1432 lttng_ring_buffer_client_overwrite_rt_exit();
1433 lttng_ring_buffer_client_overwrite_exit();
1434 lttng_ring_buffer_metadata_client_exit();
1435 exit_tracepoint();
1436 if (!exiting) {
1437 /* Reinitialize values for fork */
1438 sem_count = 2;
1439 lttng_ust_comm_should_quit = 0;
1440 initialized = 0;
1441 }
1442 }
1443
1444 void __attribute__((destructor)) lttng_ust_exit(void)
1445 {
1446 int ret;
1447
1448 /*
1449 * Using pthread_cancel here because:
1450 * A) we don't want to hang application teardown.
1451 * B) the thread is not allocating any resource.
1452 */
1453
1454 /*
1455 * Require the communication thread to quit. Synchronize with
1456 * mutexes to ensure it is not in a mutex critical section when
1457 * pthread_cancel is later called.
1458 */
1459 ust_lock();
1460 lttng_ust_comm_should_quit = 1;
1461
1462 /* cancel threads */
1463 if (global_apps.thread_active) {
1464 ret = pthread_cancel(global_apps.ust_listener);
1465 if (ret) {
1466 ERR("Error cancelling global ust listener thread: %s",
1467 strerror(ret));
1468 } else {
1469 global_apps.thread_active = 0;
1470 }
1471 }
1472 if (local_apps.thread_active) {
1473 ret = pthread_cancel(local_apps.ust_listener);
1474 if (ret) {
1475 ERR("Error cancelling local ust listener thread: %s",
1476 strerror(ret));
1477 } else {
1478 local_apps.thread_active = 0;
1479 }
1480 }
1481 ust_unlock();
1482
1483 /*
1484 * Do NOT join threads: use of sys_futex makes it impossible to
1485 * join the threads without using async-cancel, but async-cancel
1486 * is delivered by a signal, which could hit the target thread
1487 * anywhere in its code path, including while the ust_lock() is
1488 * held, causing a deadlock for the other thread. Let the OS
1489 * cleanup the threads if there are stalled in a syscall.
1490 */
1491 lttng_ust_cleanup(1);
1492
1493 if (ust_baddr_handle) {
1494 int ret = dlclose(ust_baddr_handle);
1495 if (ret)
1496 ERR("%s", dlerror());
1497 }
1498 }
1499
1500 /*
1501 * We exclude the worker threads across fork and clone (except
1502 * CLONE_VM), because these system calls only keep the forking thread
1503 * running in the child. Therefore, we don't want to call fork or clone
1504 * in the middle of an tracepoint or ust tracing state modification.
1505 * Holding this mutex protects these structures across fork and clone.
1506 */
1507 void ust_before_fork(sigset_t *save_sigset)
1508 {
1509 /*
1510 * Disable signals. This is to avoid that the child intervenes
1511 * before it is properly setup for tracing. It is safer to
1512 * disable all signals, because then we know we are not breaking
1513 * anything by restoring the original mask.
1514 */
1515 sigset_t all_sigs;
1516 int ret;
1517
1518 if (URCU_TLS(lttng_ust_nest_count))
1519 return;
1520 /* Disable signals */
1521 sigfillset(&all_sigs);
1522 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
1523 if (ret == -1) {
1524 PERROR("sigprocmask");
1525 }
1526 ust_lock();
1527 rcu_bp_before_fork();
1528 }
1529
1530 static void ust_after_fork_common(sigset_t *restore_sigset)
1531 {
1532 int ret;
1533
1534 DBG("process %d", getpid());
1535 ust_unlock();
1536 /* Restore signals */
1537 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
1538 if (ret == -1) {
1539 PERROR("sigprocmask");
1540 }
1541 }
1542
1543 void ust_after_fork_parent(sigset_t *restore_sigset)
1544 {
1545 if (URCU_TLS(lttng_ust_nest_count))
1546 return;
1547 DBG("process %d", getpid());
1548 rcu_bp_after_fork_parent();
1549 /* Release mutexes and reenable signals */
1550 ust_after_fork_common(restore_sigset);
1551 }
1552
1553 /*
1554 * After fork, in the child, we need to cleanup all the leftover state,
1555 * except the worker thread which already magically disappeared thanks
1556 * to the weird Linux fork semantics. After tyding up, we call
1557 * lttng_ust_init() again to start over as a new PID.
1558 *
1559 * This is meant for forks() that have tracing in the child between the
1560 * fork and following exec call (if there is any).
1561 */
1562 void ust_after_fork_child(sigset_t *restore_sigset)
1563 {
1564 if (URCU_TLS(lttng_ust_nest_count))
1565 return;
1566 DBG("process %d", getpid());
1567 /* Release urcu mutexes */
1568 rcu_bp_after_fork_child();
1569 lttng_ust_cleanup(0);
1570 lttng_context_vtid_reset();
1571 /* Release mutexes and reenable signals */
1572 ust_after_fork_common(restore_sigset);
1573 lttng_ust_init();
1574 }
1575
1576 void lttng_ust_sockinfo_session_enabled(void *owner,
1577 struct lttng_session *session_enabled)
1578 {
1579 struct sock_info *sock_info = owner;
1580 sock_info->session_enabled = session_enabled;
1581 }
This page took 0.092848 seconds and 5 git commands to generate.