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