Namespace remaining symbols in lttng/ringbuffer-context.h
[lttng-ust.git] / liblttng-ust / lttng-ust-comm.c
... / ...
CommitLineData
1/*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8#define _LGPL_SOURCE
9#include <stddef.h>
10#include <stdint.h>
11#include <sys/types.h>
12#include <sys/socket.h>
13#include <sys/mman.h>
14#include <sys/stat.h>
15#include <sys/types.h>
16#include <sys/wait.h>
17#include <dlfcn.h>
18#include <fcntl.h>
19#include <unistd.h>
20#include <errno.h>
21#include <pthread.h>
22#include <semaphore.h>
23#include <time.h>
24#include <assert.h>
25#include <signal.h>
26#include <limits.h>
27#include <urcu/uatomic.h>
28#include "futex.h"
29#include <urcu/compiler.h>
30#include <lttng/urcu/urcu-ust.h>
31
32#include <lttng/ust-utils.h>
33#include <lttng/ust-events.h>
34#include <lttng/ust-abi.h>
35#include <lttng/ust-fork.h>
36#include <lttng/ust-error.h>
37#include <lttng/ust-ctl.h>
38#include <lttng/ust-libc-wrapper.h>
39#include <lttng/ust-tracer.h>
40#include <urcu/tls-compat.h>
41#include <ust-comm.h>
42#include <ust-fd.h>
43#include <usterr-signal-safe.h>
44#include <ust-helper.h>
45#include "tracepoint-internal.h"
46#include "lttng-tracer-core.h"
47#include "compat.h"
48#include "../libringbuffer/rb-init.h"
49#include "lttng-ust-statedump.h"
50#include "clock.h"
51#include "../libringbuffer/getcpu.h"
52#include "getenv.h"
53#include "ust-events-internal.h"
54#include "context-internal.h"
55#include "ust-compat.h"
56
57/*
58 * Has lttng ust comm constructor been called ?
59 */
60static 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 * The UST lock is the centralized mutex across UST tracing control and
68 * probe registration.
69 *
70 * ust_exit_mutex must never nest in ust_mutex.
71 *
72 * ust_fork_mutex must never nest in ust_mutex.
73 *
74 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
75 * counter lazy initialization called by events within the statedump,
76 * which traces while the ust_mutex is held.
77 *
78 * ust_lock nests within the dynamic loader lock (within glibc) because
79 * it is taken within the library constructor.
80 *
81 * The ust fd tracker lock nests within the ust_mutex.
82 */
83static pthread_mutex_t ust_mutex = PTHREAD_MUTEX_INITIALIZER;
84
85/* Allow nesting the ust_mutex within the same thread. */
86static DEFINE_URCU_TLS(int, ust_mutex_nest);
87
88/*
89 * ust_exit_mutex protects thread_active variable wrt thread exit. It
90 * cannot be done by ust_mutex because pthread_cancel(), which takes an
91 * internal libc lock, cannot nest within ust_mutex.
92 *
93 * It never nests within a ust_mutex.
94 */
95static pthread_mutex_t ust_exit_mutex = PTHREAD_MUTEX_INITIALIZER;
96
97/*
98 * ust_fork_mutex protects base address statedump tracing against forks. It
99 * prevents the dynamic loader lock to be taken (by base address statedump
100 * tracing) while a fork is happening, thus preventing deadlock issues with
101 * the dynamic loader lock.
102 */
103static pthread_mutex_t ust_fork_mutex = PTHREAD_MUTEX_INITIALIZER;
104
105/* Should the ust comm thread quit ? */
106static int lttng_ust_comm_should_quit;
107
108/*
109 * This variable can be tested by applications to check whether
110 * lttng-ust is loaded. They simply have to define their own
111 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
112 * library constructor.
113 */
114int lttng_ust_loaded __attribute__((weak));
115
116/*
117 * Return 0 on success, -1 if should quit.
118 * The lock is taken in both cases.
119 * Signal-safe.
120 */
121int ust_lock(void)
122{
123 sigset_t sig_all_blocked, orig_mask;
124 int ret, oldstate;
125
126 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
127 if (ret) {
128 ERR("pthread_setcancelstate: %s", strerror(ret));
129 }
130 if (oldstate != PTHREAD_CANCEL_ENABLE) {
131 ERR("pthread_setcancelstate: unexpected oldstate");
132 }
133 sigfillset(&sig_all_blocked);
134 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
135 if (ret) {
136 ERR("pthread_sigmask: %s", strerror(ret));
137 }
138 if (!URCU_TLS(ust_mutex_nest)++)
139 pthread_mutex_lock(&ust_mutex);
140 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
141 if (ret) {
142 ERR("pthread_sigmask: %s", strerror(ret));
143 }
144 if (lttng_ust_comm_should_quit) {
145 return -1;
146 } else {
147 return 0;
148 }
149}
150
151/*
152 * ust_lock_nocheck() can be used in constructors/destructors, because
153 * they are already nested within the dynamic loader lock, and therefore
154 * have exclusive access against execution of liblttng-ust destructor.
155 * Signal-safe.
156 */
157void ust_lock_nocheck(void)
158{
159 sigset_t sig_all_blocked, orig_mask;
160 int ret, oldstate;
161
162 ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate);
163 if (ret) {
164 ERR("pthread_setcancelstate: %s", strerror(ret));
165 }
166 if (oldstate != PTHREAD_CANCEL_ENABLE) {
167 ERR("pthread_setcancelstate: unexpected oldstate");
168 }
169 sigfillset(&sig_all_blocked);
170 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
171 if (ret) {
172 ERR("pthread_sigmask: %s", strerror(ret));
173 }
174 if (!URCU_TLS(ust_mutex_nest)++)
175 pthread_mutex_lock(&ust_mutex);
176 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
177 if (ret) {
178 ERR("pthread_sigmask: %s", strerror(ret));
179 }
180}
181
182/*
183 * Signal-safe.
184 */
185void ust_unlock(void)
186{
187 sigset_t sig_all_blocked, orig_mask;
188 int ret, oldstate;
189
190 sigfillset(&sig_all_blocked);
191 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_mask);
192 if (ret) {
193 ERR("pthread_sigmask: %s", strerror(ret));
194 }
195 if (!--URCU_TLS(ust_mutex_nest))
196 pthread_mutex_unlock(&ust_mutex);
197 ret = pthread_sigmask(SIG_SETMASK, &orig_mask, NULL);
198 if (ret) {
199 ERR("pthread_sigmask: %s", strerror(ret));
200 }
201 ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &oldstate);
202 if (ret) {
203 ERR("pthread_setcancelstate: %s", strerror(ret));
204 }
205 if (oldstate != PTHREAD_CANCEL_DISABLE) {
206 ERR("pthread_setcancelstate: unexpected oldstate");
207 }
208}
209
210/*
211 * Wait for either of these before continuing to the main
212 * program:
213 * - the register_done message from sessiond daemon
214 * (will let the sessiond daemon enable sessions before main
215 * starts.)
216 * - sessiond daemon is not reachable.
217 * - timeout (ensuring applications are resilient to session
218 * daemon problems).
219 */
220static sem_t constructor_wait;
221/*
222 * Doing this for both the global and local sessiond.
223 */
224enum {
225 sem_count_initial_value = 4,
226};
227
228static int sem_count = sem_count_initial_value;
229
230/*
231 * Counting nesting within lttng-ust. Used to ensure that calling fork()
232 * from liblttng-ust does not execute the pre/post fork handlers.
233 */
234static DEFINE_URCU_TLS(int, lttng_ust_nest_count);
235
236/*
237 * Info about socket and associated listener thread.
238 */
239struct sock_info {
240 const char *name;
241 pthread_t ust_listener; /* listener thread */
242 int root_handle;
243 int registration_done;
244 int allowed;
245 int global;
246 int thread_active;
247
248 char sock_path[PATH_MAX];
249 int socket;
250 int notify_socket;
251
252 char wait_shm_path[PATH_MAX];
253 char *wait_shm_mmap;
254 /* Keep track of lazy state dump not performed yet. */
255 int statedump_pending;
256 int initial_statedump_done;
257 /* Keep procname for statedump */
258 char procname[LTTNG_UST_ABI_PROCNAME_LEN];
259};
260
261/* Socket from app (connect) to session daemon (listen) for communication */
262struct sock_info global_apps = {
263 .name = "global",
264 .global = 1,
265
266 .root_handle = -1,
267 .registration_done = 0,
268 .allowed = 0,
269 .thread_active = 0,
270
271 .sock_path = LTTNG_DEFAULT_RUNDIR "/" LTTNG_UST_SOCK_FILENAME,
272 .socket = -1,
273 .notify_socket = -1,
274
275 .wait_shm_path = "/" LTTNG_UST_WAIT_FILENAME,
276
277 .statedump_pending = 0,
278 .initial_statedump_done = 0,
279 .procname[0] = '\0'
280};
281
282/* TODO: allow global_apps_sock_path override */
283
284struct sock_info local_apps = {
285 .name = "local",
286 .global = 0,
287 .root_handle = -1,
288 .registration_done = 0,
289 .allowed = 0, /* Check setuid bit first */
290 .thread_active = 0,
291
292 .socket = -1,
293 .notify_socket = -1,
294
295 .statedump_pending = 0,
296 .initial_statedump_done = 0,
297 .procname[0] = '\0'
298};
299
300static int wait_poll_fallback;
301
302static const char *cmd_name_mapping[] = {
303 [ LTTNG_UST_ABI_RELEASE ] = "Release",
304 [ LTTNG_UST_ABI_SESSION ] = "Create Session",
305 [ LTTNG_UST_ABI_TRACER_VERSION ] = "Get Tracer Version",
306
307 [ LTTNG_UST_ABI_TRACEPOINT_LIST ] = "Create Tracepoint List",
308 [ LTTNG_UST_ABI_WAIT_QUIESCENT ] = "Wait for Quiescent State",
309 [ LTTNG_UST_ABI_REGISTER_DONE ] = "Registration Done",
310 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST ] = "Create Tracepoint Field List",
311
312 [ LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE ] = "Create event notifier group",
313
314 /* Session FD commands */
315 [ LTTNG_UST_ABI_CHANNEL ] = "Create Channel",
316 [ LTTNG_UST_ABI_SESSION_START ] = "Start Session",
317 [ LTTNG_UST_ABI_SESSION_STOP ] = "Stop Session",
318
319 /* Channel FD commands */
320 [ LTTNG_UST_ABI_STREAM ] = "Create Stream",
321 [ LTTNG_UST_ABI_EVENT ] = "Create Event",
322
323 /* Event and Channel FD commands */
324 [ LTTNG_UST_ABI_CONTEXT ] = "Create Context",
325 [ LTTNG_UST_ABI_FLUSH_BUFFER ] = "Flush Buffer",
326
327 /* Event, Channel and Session commands */
328 [ LTTNG_UST_ABI_ENABLE ] = "Enable",
329 [ LTTNG_UST_ABI_DISABLE ] = "Disable",
330
331 /* Tracepoint list commands */
332 [ LTTNG_UST_ABI_TRACEPOINT_LIST_GET ] = "List Next Tracepoint",
333 [ LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET ] = "List Next Tracepoint Field",
334
335 /* Event FD commands */
336 [ LTTNG_UST_ABI_FILTER ] = "Create Filter",
337 [ LTTNG_UST_ABI_EXCLUSION ] = "Add exclusions to event",
338
339 /* Event notifier group commands */
340 [ LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE ] = "Create event notifier",
341
342 /* Session and event notifier group commands */
343 [ LTTNG_UST_ABI_COUNTER ] = "Create Counter",
344
345 /* Counter commands */
346 [ LTTNG_UST_ABI_COUNTER_GLOBAL ] = "Create Counter Global",
347 [ LTTNG_UST_ABI_COUNTER_CPU ] = "Create Counter CPU",
348};
349
350static const char *str_timeout;
351static int got_timeout_env;
352
353extern void lttng_ring_buffer_client_overwrite_init(void);
354extern void lttng_ring_buffer_client_overwrite_rt_init(void);
355extern void lttng_ring_buffer_client_discard_init(void);
356extern void lttng_ring_buffer_client_discard_rt_init(void);
357extern void lttng_ring_buffer_metadata_client_init(void);
358extern void lttng_ring_buffer_client_overwrite_exit(void);
359extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
360extern void lttng_ring_buffer_client_discard_exit(void);
361extern void lttng_ring_buffer_client_discard_rt_exit(void);
362extern void lttng_ring_buffer_metadata_client_exit(void);
363
364__attribute__((visibility("hidden")))
365extern void lttng_counter_client_percpu_32_modular_init(void);
366
367__attribute__((visibility("hidden")))
368extern void lttng_counter_client_percpu_32_modular_exit(void);
369
370__attribute__((visibility("hidden")))
371extern void lttng_counter_client_percpu_64_modular_init(void);
372
373__attribute__((visibility("hidden")))
374extern void lttng_counter_client_percpu_64_modular_exit(void);
375
376static char *get_map_shm(struct sock_info *sock_info);
377
378ssize_t lttng_ust_read(int fd, void *buf, size_t len)
379{
380 ssize_t ret;
381 size_t copied = 0, to_copy = len;
382
383 do {
384 ret = read(fd, buf + copied, to_copy);
385 if (ret > 0) {
386 copied += ret;
387 to_copy -= ret;
388 }
389 } while ((ret > 0 && to_copy > 0)
390 || (ret < 0 && errno == EINTR));
391 if (ret > 0) {
392 ret = copied;
393 }
394 return ret;
395}
396/*
397 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
398 * pointer.
399 */
400static
401const char *get_lttng_home_dir(void)
402{
403 const char *val;
404
405 val = (const char *) lttng_ust_getenv("LTTNG_HOME");
406 if (val != NULL) {
407 return val;
408 }
409 return (const char *) lttng_ust_getenv("HOME");
410}
411
412/*
413 * Force a read (imply TLS fixup for dlopen) of TLS variables.
414 */
415static
416void lttng_fixup_nest_count_tls(void)
417{
418 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count)));
419}
420
421static
422void lttng_fixup_ust_mutex_nest_tls(void)
423{
424 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest)));
425}
426
427/*
428 * Fixup lttng-ust urcu TLS.
429 */
430static
431void lttng_fixup_lttng_ust_urcu_tls(void)
432{
433 (void) lttng_ust_urcu_read_ongoing();
434}
435
436void lttng_ust_fixup_tls(void)
437{
438 lttng_fixup_lttng_ust_urcu_tls();
439 lttng_fixup_ringbuffer_tls();
440 lttng_fixup_vtid_tls();
441 lttng_fixup_nest_count_tls();
442 lttng_fixup_procname_tls();
443 lttng_fixup_ust_mutex_nest_tls();
444 lttng_ust_fixup_perf_counter_tls();
445 lttng_ust_fixup_fd_tracker_tls();
446 lttng_fixup_cgroup_ns_tls();
447 lttng_fixup_ipc_ns_tls();
448 lttng_fixup_net_ns_tls();
449 lttng_fixup_time_ns_tls();
450 lttng_fixup_uts_ns_tls();
451}
452
453int lttng_get_notify_socket(void *owner)
454{
455 struct sock_info *info = owner;
456
457 return info->notify_socket;
458}
459
460
461char* lttng_ust_sockinfo_get_procname(void *owner)
462{
463 struct sock_info *info = owner;
464
465 return info->procname;
466}
467
468static
469void print_cmd(int cmd, int handle)
470{
471 const char *cmd_name = "Unknown";
472
473 if (cmd >= 0 && cmd < LTTNG_ARRAY_SIZE(cmd_name_mapping)
474 && cmd_name_mapping[cmd]) {
475 cmd_name = cmd_name_mapping[cmd];
476 }
477 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
478 cmd_name, cmd,
479 lttng_ust_obj_get_name(handle), handle);
480}
481
482static
483int setup_global_apps(void)
484{
485 int ret = 0;
486 assert(!global_apps.wait_shm_mmap);
487
488 global_apps.wait_shm_mmap = get_map_shm(&global_apps);
489 if (!global_apps.wait_shm_mmap) {
490 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
491 global_apps.allowed = 0;
492 ret = -EIO;
493 goto error;
494 }
495
496 global_apps.allowed = 1;
497 lttng_pthread_getname_np(global_apps.procname, LTTNG_UST_ABI_PROCNAME_LEN);
498error:
499 return ret;
500}
501static
502int setup_local_apps(void)
503{
504 int ret = 0;
505 const char *home_dir;
506 uid_t uid;
507
508 assert(!local_apps.wait_shm_mmap);
509
510 uid = getuid();
511 /*
512 * Disallow per-user tracing for setuid binaries.
513 */
514 if (uid != geteuid()) {
515 assert(local_apps.allowed == 0);
516 ret = 0;
517 goto end;
518 }
519 home_dir = get_lttng_home_dir();
520 if (!home_dir) {
521 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
522 assert(local_apps.allowed == 0);
523 ret = -ENOENT;
524 goto end;
525 }
526 local_apps.allowed = 1;
527 snprintf(local_apps.sock_path, PATH_MAX, "%s/%s/%s",
528 home_dir,
529 LTTNG_DEFAULT_HOME_RUNDIR,
530 LTTNG_UST_SOCK_FILENAME);
531 snprintf(local_apps.wait_shm_path, PATH_MAX, "/%s-%u",
532 LTTNG_UST_WAIT_FILENAME,
533 uid);
534
535 local_apps.wait_shm_mmap = get_map_shm(&local_apps);
536 if (!local_apps.wait_shm_mmap) {
537 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
538 local_apps.allowed = 0;
539 ret = -EIO;
540 goto end;
541 }
542
543 lttng_pthread_getname_np(local_apps.procname, LTTNG_UST_ABI_PROCNAME_LEN);
544end:
545 return ret;
546}
547
548/*
549 * Get socket timeout, in ms.
550 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
551 */
552static
553long get_timeout(void)
554{
555 long constructor_delay_ms = LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS;
556
557 if (!got_timeout_env) {
558 str_timeout = lttng_ust_getenv("LTTNG_UST_REGISTER_TIMEOUT");
559 got_timeout_env = 1;
560 }
561 if (str_timeout)
562 constructor_delay_ms = strtol(str_timeout, NULL, 10);
563 /* All negative values are considered as "-1". */
564 if (constructor_delay_ms < -1)
565 constructor_delay_ms = -1;
566 return constructor_delay_ms;
567}
568
569/* Timeout for notify socket send and recv. */
570static
571long get_notify_sock_timeout(void)
572{
573 return get_timeout();
574}
575
576/* Timeout for connecting to cmd and notify sockets. */
577static
578long get_connect_sock_timeout(void)
579{
580 return get_timeout();
581}
582
583/*
584 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
585 */
586static
587int get_constructor_timeout(struct timespec *constructor_timeout)
588{
589 long constructor_delay_ms;
590 int ret;
591
592 constructor_delay_ms = get_timeout();
593
594 switch (constructor_delay_ms) {
595 case -1:/* fall-through */
596 case 0:
597 return constructor_delay_ms;
598 default:
599 break;
600 }
601
602 /*
603 * If we are unable to find the current time, don't wait.
604 */
605 ret = clock_gettime(CLOCK_REALTIME, constructor_timeout);
606 if (ret) {
607 /* Don't wait. */
608 return 0;
609 }
610 constructor_timeout->tv_sec += constructor_delay_ms / 1000UL;
611 constructor_timeout->tv_nsec +=
612 (constructor_delay_ms % 1000UL) * 1000000UL;
613 if (constructor_timeout->tv_nsec >= 1000000000UL) {
614 constructor_timeout->tv_sec++;
615 constructor_timeout->tv_nsec -= 1000000000UL;
616 }
617 /* Timeout wait (constructor_delay_ms). */
618 return 1;
619}
620
621static
622void get_allow_blocking(void)
623{
624 const char *str_allow_blocking =
625 lttng_ust_getenv("LTTNG_UST_ALLOW_BLOCKING");
626
627 if (str_allow_blocking) {
628 DBG("%s environment variable is set",
629 "LTTNG_UST_ALLOW_BLOCKING");
630 lttng_ust_ringbuffer_set_allow_blocking();
631 }
632}
633
634static
635int register_to_sessiond(int socket, enum ustctl_socket_type type)
636{
637 return ustcomm_send_reg_msg(socket,
638 type,
639 CAA_BITS_PER_LONG,
640 lttng_alignof(uint8_t) * CHAR_BIT,
641 lttng_alignof(uint16_t) * CHAR_BIT,
642 lttng_alignof(uint32_t) * CHAR_BIT,
643 lttng_alignof(uint64_t) * CHAR_BIT,
644 lttng_alignof(unsigned long) * CHAR_BIT);
645}
646
647static
648int send_reply(int sock, struct ustcomm_ust_reply *lur)
649{
650 ssize_t len;
651
652 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
653 switch (len) {
654 case sizeof(*lur):
655 DBG("message successfully sent");
656 return 0;
657 default:
658 if (len == -ECONNRESET) {
659 DBG("remote end closed connection");
660 return 0;
661 }
662 if (len < 0)
663 return len;
664 DBG("incorrect message size: %zd", len);
665 return -EINVAL;
666 }
667}
668
669static
670void decrement_sem_count(unsigned int count)
671{
672 int ret;
673
674 assert(uatomic_read(&sem_count) >= count);
675
676 if (uatomic_read(&sem_count) <= 0) {
677 return;
678 }
679
680 ret = uatomic_add_return(&sem_count, -count);
681 if (ret == 0) {
682 ret = sem_post(&constructor_wait);
683 assert(!ret);
684 }
685}
686
687static
688int handle_register_done(struct sock_info *sock_info)
689{
690 if (sock_info->registration_done)
691 return 0;
692 sock_info->registration_done = 1;
693
694 decrement_sem_count(1);
695 if (!sock_info->statedump_pending) {
696 sock_info->initial_statedump_done = 1;
697 decrement_sem_count(1);
698 }
699
700 return 0;
701}
702
703static
704int handle_register_failed(struct sock_info *sock_info)
705{
706 if (sock_info->registration_done)
707 return 0;
708 sock_info->registration_done = 1;
709 sock_info->initial_statedump_done = 1;
710
711 decrement_sem_count(2);
712
713 return 0;
714}
715
716/*
717 * Only execute pending statedump after the constructor semaphore has
718 * been posted by the current listener thread. This means statedump will
719 * only be performed after the "registration done" command is received
720 * from this thread's session daemon.
721 *
722 * This ensures we don't run into deadlock issues with the dynamic
723 * loader mutex, which is held while the constructor is called and
724 * waiting on the constructor semaphore. All operations requiring this
725 * dynamic loader lock need to be postponed using this mechanism.
726 *
727 * In a scenario with two session daemons connected to the application,
728 * it is possible that the first listener thread which receives the
729 * registration done command issues its statedump while the dynamic
730 * loader lock is still held by the application constructor waiting on
731 * the semaphore. It will however be allowed to proceed when the
732 * second session daemon sends the registration done command to the
733 * second listener thread. This situation therefore does not produce
734 * a deadlock.
735 */
736static
737void handle_pending_statedump(struct sock_info *sock_info)
738{
739 if (sock_info->registration_done && sock_info->statedump_pending) {
740 sock_info->statedump_pending = 0;
741 pthread_mutex_lock(&ust_fork_mutex);
742 lttng_handle_pending_statedump(sock_info);
743 pthread_mutex_unlock(&ust_fork_mutex);
744
745 if (!sock_info->initial_statedump_done) {
746 sock_info->initial_statedump_done = 1;
747 decrement_sem_count(1);
748 }
749 }
750}
751
752static inline
753const char *bytecode_type_str(uint32_t cmd)
754{
755 switch (cmd) {
756 case LTTNG_UST_ABI_CAPTURE:
757 return "capture";
758 case LTTNG_UST_ABI_FILTER:
759 return "filter";
760 default:
761 abort();
762 }
763}
764
765static
766int handle_bytecode_recv(struct sock_info *sock_info,
767 int sock, struct ustcomm_ust_msg *lum)
768{
769 struct lttng_ust_bytecode_node *bytecode = NULL;
770 enum lttng_ust_bytecode_type type;
771 const struct lttng_ust_abi_objd_ops *ops;
772 uint32_t data_size, data_size_max, reloc_offset;
773 uint64_t seqnum;
774 ssize_t len;
775 int ret = 0;
776
777 switch (lum->cmd) {
778 case LTTNG_UST_ABI_FILTER:
779 type = LTTNG_UST_BYTECODE_TYPE_FILTER;
780 data_size = lum->u.filter.data_size;
781 data_size_max = LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN;
782 reloc_offset = lum->u.filter.reloc_offset;
783 seqnum = lum->u.filter.seqnum;
784 break;
785 case LTTNG_UST_ABI_CAPTURE:
786 type = LTTNG_UST_BYTECODE_TYPE_CAPTURE;
787 data_size = lum->u.capture.data_size;
788 data_size_max = LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN;
789 reloc_offset = lum->u.capture.reloc_offset;
790 seqnum = lum->u.capture.seqnum;
791 break;
792 default:
793 abort();
794 }
795
796 if (data_size > data_size_max) {
797 ERR("Bytecode %s data size is too large: %u bytes",
798 bytecode_type_str(lum->cmd), data_size);
799 ret = -EINVAL;
800 goto end;
801 }
802
803 if (reloc_offset > data_size) {
804 ERR("Bytecode %s reloc offset %u is not within data",
805 bytecode_type_str(lum->cmd), reloc_offset);
806 ret = -EINVAL;
807 goto end;
808 }
809
810 /* Allocate the structure AND the `data[]` field. */
811 bytecode = zmalloc(sizeof(*bytecode) + data_size);
812 if (!bytecode) {
813 ret = -ENOMEM;
814 goto end;
815 }
816
817 bytecode->bc.len = data_size;
818 bytecode->bc.reloc_offset = reloc_offset;
819 bytecode->bc.seqnum = seqnum;
820 bytecode->type = type;
821
822 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, bytecode->bc.len);
823 switch (len) {
824 case 0: /* orderly shutdown */
825 ret = 0;
826 goto end;
827 default:
828 if (len == bytecode->bc.len) {
829 DBG("Bytecode %s data received",
830 bytecode_type_str(lum->cmd));
831 break;
832 } else if (len < 0) {
833 DBG("Receive failed from lttng-sessiond with errno %d",
834 (int) -len);
835 if (len == -ECONNRESET) {
836 ERR("%s remote end closed connection",
837 sock_info->name);
838 ret = len;
839 goto end;
840 }
841 ret = len;
842 goto end;
843 } else {
844 DBG("Incorrect %s bytecode data message size: %zd",
845 bytecode_type_str(lum->cmd), len);
846 ret = -EINVAL;
847 goto end;
848 }
849 }
850
851 ops = lttng_ust_abi_objd_ops(lum->handle);
852 if (!ops) {
853 ret = -ENOENT;
854 goto end;
855 }
856
857 if (ops->cmd)
858 ret = ops->cmd(lum->handle, lum->cmd,
859 (unsigned long) &bytecode,
860 NULL, sock_info);
861 else
862 ret = -ENOSYS;
863
864end:
865 free(bytecode);
866 return ret;
867}
868
869static
870int handle_message(struct sock_info *sock_info,
871 int sock, struct ustcomm_ust_msg *lum)
872{
873 int ret = 0;
874 const struct lttng_ust_abi_objd_ops *ops;
875 struct ustcomm_ust_reply lur;
876 union lttng_ust_abi_args args;
877 char ctxstr[LTTNG_UST_ABI_SYM_NAME_LEN]; /* App context string. */
878 ssize_t len;
879
880 memset(&lur, 0, sizeof(lur));
881
882 if (ust_lock()) {
883 ret = -LTTNG_UST_ERR_EXITING;
884 goto error;
885 }
886
887 ops = lttng_ust_abi_objd_ops(lum->handle);
888 if (!ops) {
889 ret = -ENOENT;
890 goto error;
891 }
892
893 switch (lum->cmd) {
894 case LTTNG_UST_ABI_REGISTER_DONE:
895 if (lum->handle == LTTNG_UST_ABI_ROOT_HANDLE)
896 ret = handle_register_done(sock_info);
897 else
898 ret = -EINVAL;
899 break;
900 case LTTNG_UST_ABI_RELEASE:
901 if (lum->handle == LTTNG_UST_ABI_ROOT_HANDLE)
902 ret = -EPERM;
903 else
904 ret = lttng_ust_abi_objd_unref(lum->handle, 1);
905 break;
906 case LTTNG_UST_ABI_CAPTURE:
907 case LTTNG_UST_ABI_FILTER:
908 ret = handle_bytecode_recv(sock_info, sock, lum);
909 if (ret)
910 goto error;
911 break;
912 case LTTNG_UST_ABI_EXCLUSION:
913 {
914 /* Receive exclusion names */
915 struct lttng_ust_excluder_node *node;
916 unsigned int count;
917
918 count = lum->u.exclusion.count;
919 if (count == 0) {
920 /* There are no names to read */
921 ret = 0;
922 goto error;
923 }
924 node = zmalloc(sizeof(*node) +
925 count * LTTNG_UST_ABI_SYM_NAME_LEN);
926 if (!node) {
927 ret = -ENOMEM;
928 goto error;
929 }
930 node->excluder.count = count;
931 len = ustcomm_recv_unix_sock(sock, node->excluder.names,
932 count * LTTNG_UST_ABI_SYM_NAME_LEN);
933 switch (len) {
934 case 0: /* orderly shutdown */
935 ret = 0;
936 free(node);
937 goto error;
938 default:
939 if (len == count * LTTNG_UST_ABI_SYM_NAME_LEN) {
940 DBG("Exclusion data received");
941 break;
942 } else if (len < 0) {
943 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
944 if (len == -ECONNRESET) {
945 ERR("%s remote end closed connection", sock_info->name);
946 ret = len;
947 free(node);
948 goto error;
949 }
950 ret = len;
951 free(node);
952 goto error;
953 } else {
954 DBG("Incorrect exclusion data message size: %zd", len);
955 ret = -EINVAL;
956 free(node);
957 goto error;
958 }
959 }
960 if (ops->cmd)
961 ret = ops->cmd(lum->handle, lum->cmd,
962 (unsigned long) &node,
963 &args, sock_info);
964 else
965 ret = -ENOSYS;
966 free(node);
967 break;
968 }
969 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE:
970 {
971 int event_notifier_notif_fd, close_ret;
972
973 len = ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock,
974 &event_notifier_notif_fd);
975 switch (len) {
976 case 0: /* orderly shutdown */
977 ret = 0;
978 goto error;
979 case 1:
980 break;
981 default:
982 if (len < 0) {
983 DBG("Receive failed from lttng-sessiond with errno %d",
984 (int) -len);
985 if (len == -ECONNRESET) {
986 ERR("%s remote end closed connection",
987 sock_info->name);
988 ret = len;
989 goto error;
990 }
991 ret = len;
992 goto error;
993 } else {
994 DBG("Incorrect event notifier fd message size: %zd",
995 len);
996 ret = -EINVAL;
997 goto error;
998 }
999 }
1000 args.event_notifier_handle.event_notifier_notif_fd =
1001 event_notifier_notif_fd;
1002 if (ops->cmd)
1003 ret = ops->cmd(lum->handle, lum->cmd,
1004 (unsigned long) &lum->u,
1005 &args, sock_info);
1006 else
1007 ret = -ENOSYS;
1008 if (args.event_notifier_handle.event_notifier_notif_fd >= 0) {
1009 lttng_ust_lock_fd_tracker();
1010 close_ret = close(args.event_notifier_handle.event_notifier_notif_fd);
1011 lttng_ust_unlock_fd_tracker();
1012 if (close_ret)
1013 PERROR("close");
1014 }
1015 break;
1016 }
1017 case LTTNG_UST_ABI_CHANNEL:
1018 {
1019 void *chan_data;
1020 int wakeup_fd;
1021
1022 len = ustcomm_recv_channel_from_sessiond(sock,
1023 &chan_data, lum->u.channel.len,
1024 &wakeup_fd);
1025 switch (len) {
1026 case 0: /* orderly shutdown */
1027 ret = 0;
1028 goto error;
1029 default:
1030 if (len == lum->u.channel.len) {
1031 DBG("channel data received");
1032 break;
1033 } else if (len < 0) {
1034 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1035 if (len == -ECONNRESET) {
1036 ERR("%s remote end closed connection", sock_info->name);
1037 ret = len;
1038 goto error;
1039 }
1040 ret = len;
1041 goto error;
1042 } else {
1043 DBG("incorrect channel data message size: %zd", len);
1044 ret = -EINVAL;
1045 goto error;
1046 }
1047 }
1048 args.channel.chan_data = chan_data;
1049 args.channel.wakeup_fd = wakeup_fd;
1050 if (ops->cmd)
1051 ret = ops->cmd(lum->handle, lum->cmd,
1052 (unsigned long) &lum->u,
1053 &args, sock_info);
1054 else
1055 ret = -ENOSYS;
1056 if (args.channel.wakeup_fd >= 0) {
1057 int close_ret;
1058
1059 lttng_ust_lock_fd_tracker();
1060 close_ret = close(args.channel.wakeup_fd);
1061 lttng_ust_unlock_fd_tracker();
1062 args.channel.wakeup_fd = -1;
1063 if (close_ret)
1064 PERROR("close");
1065 }
1066 free(args.channel.chan_data);
1067 break;
1068 }
1069 case LTTNG_UST_ABI_STREAM:
1070 {
1071 int close_ret;
1072
1073 /* Receive shm_fd, wakeup_fd */
1074 ret = ustcomm_recv_stream_from_sessiond(sock,
1075 NULL,
1076 &args.stream.shm_fd,
1077 &args.stream.wakeup_fd);
1078 if (ret) {
1079 goto error;
1080 }
1081
1082 if (ops->cmd)
1083 ret = ops->cmd(lum->handle, lum->cmd,
1084 (unsigned long) &lum->u,
1085 &args, sock_info);
1086 else
1087 ret = -ENOSYS;
1088 if (args.stream.shm_fd >= 0) {
1089 lttng_ust_lock_fd_tracker();
1090 close_ret = close(args.stream.shm_fd);
1091 lttng_ust_unlock_fd_tracker();
1092 args.stream.shm_fd = -1;
1093 if (close_ret)
1094 PERROR("close");
1095 }
1096 if (args.stream.wakeup_fd >= 0) {
1097 lttng_ust_lock_fd_tracker();
1098 close_ret = close(args.stream.wakeup_fd);
1099 lttng_ust_unlock_fd_tracker();
1100 args.stream.wakeup_fd = -1;
1101 if (close_ret)
1102 PERROR("close");
1103 }
1104 break;
1105 }
1106 case LTTNG_UST_ABI_CONTEXT:
1107 switch (lum->u.context.ctx) {
1108 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
1109 {
1110 char *p;
1111 size_t ctxlen, recvlen;
1112
1113 ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1
1114 + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len;
1115 if (ctxlen >= LTTNG_UST_ABI_SYM_NAME_LEN) {
1116 ERR("Application context string length size is too large: %zu bytes",
1117 ctxlen);
1118 ret = -EINVAL;
1119 goto error;
1120 }
1121 strcpy(ctxstr, "$app.");
1122 p = &ctxstr[strlen("$app.")];
1123 recvlen = ctxlen - strlen("$app.");
1124 len = ustcomm_recv_unix_sock(sock, p, recvlen);
1125 switch (len) {
1126 case 0: /* orderly shutdown */
1127 ret = 0;
1128 goto error;
1129 default:
1130 if (len == recvlen) {
1131 DBG("app context data received");
1132 break;
1133 } else if (len < 0) {
1134 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1135 if (len == -ECONNRESET) {
1136 ERR("%s remote end closed connection", sock_info->name);
1137 ret = len;
1138 goto error;
1139 }
1140 ret = len;
1141 goto error;
1142 } else {
1143 DBG("incorrect app context data message size: %zd", len);
1144 ret = -EINVAL;
1145 goto error;
1146 }
1147 }
1148 /* Put : between provider and ctxname. */
1149 p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':';
1150 args.app_context.ctxname = ctxstr;
1151 break;
1152 }
1153 default:
1154 break;
1155 }
1156 if (ops->cmd) {
1157 ret = ops->cmd(lum->handle, lum->cmd,
1158 (unsigned long) &lum->u,
1159 &args, sock_info);
1160 } else {
1161 ret = -ENOSYS;
1162 }
1163 break;
1164 case LTTNG_UST_ABI_COUNTER:
1165 {
1166 void *counter_data;
1167
1168 len = ustcomm_recv_counter_from_sessiond(sock,
1169 &counter_data, lum->u.counter.len);
1170 switch (len) {
1171 case 0: /* orderly shutdown */
1172 ret = 0;
1173 goto error;
1174 default:
1175 if (len == lum->u.counter.len) {
1176 DBG("counter data received");
1177 break;
1178 } else if (len < 0) {
1179 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1180 if (len == -ECONNRESET) {
1181 ERR("%s remote end closed connection", sock_info->name);
1182 ret = len;
1183 goto error;
1184 }
1185 ret = len;
1186 goto error;
1187 } else {
1188 DBG("incorrect counter data message size: %zd", len);
1189 ret = -EINVAL;
1190 goto error;
1191 }
1192 }
1193 args.counter.counter_data = counter_data;
1194 if (ops->cmd)
1195 ret = ops->cmd(lum->handle, lum->cmd,
1196 (unsigned long) &lum->u,
1197 &args, sock_info);
1198 else
1199 ret = -ENOSYS;
1200 free(args.counter.counter_data);
1201 break;
1202 }
1203 case LTTNG_UST_ABI_COUNTER_GLOBAL:
1204 {
1205 /* Receive shm_fd */
1206 ret = ustcomm_recv_counter_shm_from_sessiond(sock,
1207 &args.counter_shm.shm_fd);
1208 if (ret) {
1209 goto error;
1210 }
1211
1212 if (ops->cmd)
1213 ret = ops->cmd(lum->handle, lum->cmd,
1214 (unsigned long) &lum->u,
1215 &args, sock_info);
1216 else
1217 ret = -ENOSYS;
1218 if (args.counter_shm.shm_fd >= 0) {
1219 int close_ret;
1220
1221 lttng_ust_lock_fd_tracker();
1222 close_ret = close(args.counter_shm.shm_fd);
1223 lttng_ust_unlock_fd_tracker();
1224 args.counter_shm.shm_fd = -1;
1225 if (close_ret)
1226 PERROR("close");
1227 }
1228 break;
1229 }
1230 case LTTNG_UST_ABI_COUNTER_CPU:
1231 {
1232 /* Receive shm_fd */
1233 ret = ustcomm_recv_counter_shm_from_sessiond(sock,
1234 &args.counter_shm.shm_fd);
1235 if (ret) {
1236 goto error;
1237 }
1238
1239 if (ops->cmd)
1240 ret = ops->cmd(lum->handle, lum->cmd,
1241 (unsigned long) &lum->u,
1242 &args, sock_info);
1243 else
1244 ret = -ENOSYS;
1245 if (args.counter_shm.shm_fd >= 0) {
1246 int close_ret;
1247
1248 lttng_ust_lock_fd_tracker();
1249 close_ret = close(args.counter_shm.shm_fd);
1250 lttng_ust_unlock_fd_tracker();
1251 args.counter_shm.shm_fd = -1;
1252 if (close_ret)
1253 PERROR("close");
1254 }
1255 break;
1256 }
1257 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE:
1258 {
1259 /* Receive struct lttng_ust_event_notifier */
1260 struct lttng_ust_abi_event_notifier event_notifier;
1261
1262 if (sizeof(event_notifier) != lum->u.event_notifier.len) {
1263 DBG("incorrect event notifier data message size: %u", lum->u.event_notifier.len);
1264 ret = -EINVAL;
1265 goto error;
1266 }
1267 len = ustcomm_recv_unix_sock(sock, &event_notifier, sizeof(event_notifier));
1268 switch (len) {
1269 case 0: /* orderly shutdown */
1270 ret = 0;
1271 goto error;
1272 default:
1273 if (len == sizeof(event_notifier)) {
1274 DBG("event notifier data received");
1275 break;
1276 } else if (len < 0) {
1277 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1278 if (len == -ECONNRESET) {
1279 ERR("%s remote end closed connection", sock_info->name);
1280 ret = len;
1281 goto error;
1282 }
1283 ret = len;
1284 goto error;
1285 } else {
1286 DBG("incorrect event notifier data message size: %zd", len);
1287 ret = -EINVAL;
1288 goto error;
1289 }
1290 }
1291 if (ops->cmd)
1292 ret = ops->cmd(lum->handle, lum->cmd,
1293 (unsigned long) &event_notifier,
1294 &args, sock_info);
1295 else
1296 ret = -ENOSYS;
1297 break;
1298 }
1299
1300 default:
1301 if (ops->cmd)
1302 ret = ops->cmd(lum->handle, lum->cmd,
1303 (unsigned long) &lum->u,
1304 &args, sock_info);
1305 else
1306 ret = -ENOSYS;
1307 break;
1308 }
1309
1310 lur.handle = lum->handle;
1311 lur.cmd = lum->cmd;
1312 lur.ret_val = ret;
1313 if (ret >= 0) {
1314 lur.ret_code = LTTNG_UST_OK;
1315 } else {
1316 /*
1317 * Use -LTTNG_UST_ERR as wildcard for UST internal
1318 * error that are not caused by the transport, except if
1319 * we already have a more precise error message to
1320 * report.
1321 */
1322 if (ret > -LTTNG_UST_ERR) {
1323 /* Translate code to UST error. */
1324 switch (ret) {
1325 case -EEXIST:
1326 lur.ret_code = -LTTNG_UST_ERR_EXIST;
1327 break;
1328 case -EINVAL:
1329 lur.ret_code = -LTTNG_UST_ERR_INVAL;
1330 break;
1331 case -ENOENT:
1332 lur.ret_code = -LTTNG_UST_ERR_NOENT;
1333 break;
1334 case -EPERM:
1335 lur.ret_code = -LTTNG_UST_ERR_PERM;
1336 break;
1337 case -ENOSYS:
1338 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
1339 break;
1340 default:
1341 lur.ret_code = -LTTNG_UST_ERR;
1342 break;
1343 }
1344 } else {
1345 lur.ret_code = ret;
1346 }
1347 }
1348 if (ret >= 0) {
1349 switch (lum->cmd) {
1350 case LTTNG_UST_ABI_TRACER_VERSION:
1351 lur.u.version = lum->u.version;
1352 break;
1353 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET:
1354 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
1355 break;
1356 }
1357 }
1358 DBG("Return value: %d", lur.ret_val);
1359
1360 ust_unlock();
1361
1362 /*
1363 * Performed delayed statedump operations outside of the UST
1364 * lock. We need to take the dynamic loader lock before we take
1365 * the UST lock internally within handle_pending_statedump().
1366 */
1367 handle_pending_statedump(sock_info);
1368
1369 if (ust_lock()) {
1370 ret = -LTTNG_UST_ERR_EXITING;
1371 goto error;
1372 }
1373
1374 ret = send_reply(sock, &lur);
1375 if (ret < 0) {
1376 DBG("error sending reply");
1377 goto error;
1378 }
1379
1380 /*
1381 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1382 * after the reply.
1383 */
1384 if (lur.ret_code == LTTNG_UST_OK) {
1385 switch (lum->cmd) {
1386 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET:
1387 len = ustcomm_send_unix_sock(sock,
1388 &args.field_list.entry,
1389 sizeof(args.field_list.entry));
1390 if (len < 0) {
1391 ret = len;
1392 goto error;
1393 }
1394 if (len != sizeof(args.field_list.entry)) {
1395 ret = -EINVAL;
1396 goto error;
1397 }
1398 }
1399 }
1400
1401error:
1402 ust_unlock();
1403
1404 return ret;
1405}
1406
1407static
1408void cleanup_sock_info(struct sock_info *sock_info, int exiting)
1409{
1410 int ret;
1411
1412 if (sock_info->root_handle != -1) {
1413 ret = lttng_ust_abi_objd_unref(sock_info->root_handle, 1);
1414 if (ret) {
1415 ERR("Error unref root handle");
1416 }
1417 sock_info->root_handle = -1;
1418 }
1419 sock_info->registration_done = 0;
1420 sock_info->initial_statedump_done = 0;
1421
1422 /*
1423 * wait_shm_mmap, socket and notify socket are used by listener
1424 * threads outside of the ust lock, so we cannot tear them down
1425 * ourselves, because we cannot join on these threads. Leave
1426 * responsibility of cleaning up these resources to the OS
1427 * process exit.
1428 */
1429 if (exiting)
1430 return;
1431
1432 if (sock_info->socket != -1) {
1433 ret = ustcomm_close_unix_sock(sock_info->socket);
1434 if (ret) {
1435 ERR("Error closing ust cmd socket");
1436 }
1437 sock_info->socket = -1;
1438 }
1439 if (sock_info->notify_socket != -1) {
1440 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1441 if (ret) {
1442 ERR("Error closing ust notify socket");
1443 }
1444 sock_info->notify_socket = -1;
1445 }
1446 if (sock_info->wait_shm_mmap) {
1447 long page_size;
1448
1449 page_size = LTTNG_UST_PAGE_SIZE;
1450 if (page_size <= 0) {
1451 if (!page_size) {
1452 errno = EINVAL;
1453 }
1454 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1455 } else {
1456 ret = munmap(sock_info->wait_shm_mmap, page_size);
1457 if (ret) {
1458 ERR("Error unmapping wait shm");
1459 }
1460 }
1461 sock_info->wait_shm_mmap = NULL;
1462 }
1463}
1464
1465/*
1466 * Using fork to set umask in the child process (not multi-thread safe).
1467 * We deal with the shm_open vs ftruncate race (happening when the
1468 * sessiond owns the shm and does not let everybody modify it, to ensure
1469 * safety against shm_unlink) by simply letting the mmap fail and
1470 * retrying after a few seconds.
1471 * For global shm, everybody has rw access to it until the sessiond
1472 * starts.
1473 */
1474static
1475int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
1476{
1477 int wait_shm_fd, ret;
1478 pid_t pid;
1479
1480 /*
1481 * Try to open read-only.
1482 */
1483 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
1484 if (wait_shm_fd >= 0) {
1485 int32_t tmp_read;
1486 ssize_t len;
1487 size_t bytes_read = 0;
1488
1489 /*
1490 * Try to read the fd. If unable to do so, try opening
1491 * it in write mode.
1492 */
1493 do {
1494 len = read(wait_shm_fd,
1495 &((char *) &tmp_read)[bytes_read],
1496 sizeof(tmp_read) - bytes_read);
1497 if (len > 0) {
1498 bytes_read += len;
1499 }
1500 } while ((len < 0 && errno == EINTR)
1501 || (len > 0 && bytes_read < sizeof(tmp_read)));
1502 if (bytes_read != sizeof(tmp_read)) {
1503 ret = close(wait_shm_fd);
1504 if (ret) {
1505 ERR("close wait_shm_fd");
1506 }
1507 goto open_write;
1508 }
1509 goto end;
1510 } else if (wait_shm_fd < 0 && errno != ENOENT) {
1511 /*
1512 * Real-only open did not work, and it's not because the
1513 * entry was not present. It's a failure that prohibits
1514 * using shm.
1515 */
1516 ERR("Error opening shm %s", sock_info->wait_shm_path);
1517 goto end;
1518 }
1519
1520open_write:
1521 /*
1522 * If the open failed because the file did not exist, or because
1523 * the file was not truncated yet, try creating it ourself.
1524 */
1525 URCU_TLS(lttng_ust_nest_count)++;
1526 pid = fork();
1527 URCU_TLS(lttng_ust_nest_count)--;
1528 if (pid > 0) {
1529 int status;
1530
1531 /*
1532 * Parent: wait for child to return, in which case the
1533 * shared memory map will have been created.
1534 */
1535 pid = wait(&status);
1536 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1537 wait_shm_fd = -1;
1538 goto end;
1539 }
1540 /*
1541 * Try to open read-only again after creation.
1542 */
1543 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
1544 if (wait_shm_fd < 0) {
1545 /*
1546 * Real-only open did not work. It's a failure
1547 * that prohibits using shm.
1548 */
1549 ERR("Error opening shm %s", sock_info->wait_shm_path);
1550 goto end;
1551 }
1552 goto end;
1553 } else if (pid == 0) {
1554 int create_mode;
1555
1556 /* Child */
1557 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
1558 if (sock_info->global)
1559 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
1560 /*
1561 * We're alone in a child process, so we can modify the
1562 * process-wide umask.
1563 */
1564 umask(~create_mode);
1565 /*
1566 * Try creating shm (or get rw access).
1567 * We don't do an exclusive open, because we allow other
1568 * processes to create+ftruncate it concurrently.
1569 */
1570 wait_shm_fd = shm_open(sock_info->wait_shm_path,
1571 O_RDWR | O_CREAT, create_mode);
1572 if (wait_shm_fd >= 0) {
1573 ret = ftruncate(wait_shm_fd, mmap_size);
1574 if (ret) {
1575 PERROR("ftruncate");
1576 _exit(EXIT_FAILURE);
1577 }
1578 _exit(EXIT_SUCCESS);
1579 }
1580 /*
1581 * For local shm, we need to have rw access to accept
1582 * opening it: this means the local sessiond will be
1583 * able to wake us up. For global shm, we open it even
1584 * if rw access is not granted, because the root.root
1585 * sessiond will be able to override all rights and wake
1586 * us up.
1587 */
1588 if (!sock_info->global && errno != EACCES) {
1589 ERR("Error opening shm %s", sock_info->wait_shm_path);
1590 _exit(EXIT_FAILURE);
1591 }
1592 /*
1593 * The shm exists, but we cannot open it RW. Report
1594 * success.
1595 */
1596 _exit(EXIT_SUCCESS);
1597 } else {
1598 return -1;
1599 }
1600end:
1601 if (wait_shm_fd >= 0 && !sock_info->global) {
1602 struct stat statbuf;
1603
1604 /*
1605 * Ensure that our user is the owner of the shm file for
1606 * local shm. If we do not own the file, it means our
1607 * sessiond will not have access to wake us up (there is
1608 * probably a rogue process trying to fake our
1609 * sessiond). Fallback to polling method in this case.
1610 */
1611 ret = fstat(wait_shm_fd, &statbuf);
1612 if (ret) {
1613 PERROR("fstat");
1614 goto error_close;
1615 }
1616 if (statbuf.st_uid != getuid())
1617 goto error_close;
1618 }
1619 return wait_shm_fd;
1620
1621error_close:
1622 ret = close(wait_shm_fd);
1623 if (ret) {
1624 PERROR("Error closing fd");
1625 }
1626 return -1;
1627}
1628
1629static
1630char *get_map_shm(struct sock_info *sock_info)
1631{
1632 long page_size;
1633 int wait_shm_fd, ret;
1634 char *wait_shm_mmap;
1635
1636 page_size = sysconf(_SC_PAGE_SIZE);
1637 if (page_size <= 0) {
1638 if (!page_size) {
1639 errno = EINVAL;
1640 }
1641 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1642 goto error;
1643 }
1644
1645 lttng_ust_lock_fd_tracker();
1646 wait_shm_fd = get_wait_shm(sock_info, page_size);
1647 if (wait_shm_fd < 0) {
1648 lttng_ust_unlock_fd_tracker();
1649 goto error;
1650 }
1651
1652 ret = lttng_ust_add_fd_to_tracker(wait_shm_fd);
1653 if (ret < 0) {
1654 ret = close(wait_shm_fd);
1655 if (!ret) {
1656 PERROR("Error closing fd");
1657 }
1658 lttng_ust_unlock_fd_tracker();
1659 goto error;
1660 }
1661
1662 wait_shm_fd = ret;
1663 lttng_ust_unlock_fd_tracker();
1664
1665 wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
1666 MAP_SHARED, wait_shm_fd, 0);
1667
1668 /* close shm fd immediately after taking the mmap reference */
1669 lttng_ust_lock_fd_tracker();
1670 ret = close(wait_shm_fd);
1671 if (!ret) {
1672 lttng_ust_delete_fd_from_tracker(wait_shm_fd);
1673 } else {
1674 PERROR("Error closing fd");
1675 }
1676 lttng_ust_unlock_fd_tracker();
1677
1678 if (wait_shm_mmap == MAP_FAILED) {
1679 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1680 goto error;
1681 }
1682 return wait_shm_mmap;
1683
1684error:
1685 return NULL;
1686}
1687
1688static
1689void wait_for_sessiond(struct sock_info *sock_info)
1690{
1691 /* Use ust_lock to check if we should quit. */
1692 if (ust_lock()) {
1693 goto quit;
1694 }
1695 if (wait_poll_fallback) {
1696 goto error;
1697 }
1698 ust_unlock();
1699
1700 assert(sock_info->wait_shm_mmap);
1701
1702 DBG("Waiting for %s apps sessiond", sock_info->name);
1703 /* Wait for futex wakeup */
1704 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap))
1705 goto end_wait;
1706
1707 while (lttng_ust_futex_async((int32_t *) sock_info->wait_shm_mmap,
1708 FUTEX_WAIT, 0, NULL, NULL, 0)) {
1709 switch (errno) {
1710 case EWOULDBLOCK:
1711 /* Value already changed. */
1712 goto end_wait;
1713 case EINTR:
1714 /* Retry if interrupted by signal. */
1715 break; /* Get out of switch. */
1716 case EFAULT:
1717 wait_poll_fallback = 1;
1718 DBG(
1719"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1720"do not support FUTEX_WAKE on read-only memory mappings correctly. "
1721"Please upgrade your kernel "
1722"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1723"mainline). LTTng-UST will use polling mode fallback.");
1724 if (ust_err_debug_enabled())
1725 PERROR("futex");
1726 goto end_wait;
1727 }
1728 }
1729end_wait:
1730 return;
1731
1732quit:
1733 ust_unlock();
1734 return;
1735
1736error:
1737 ust_unlock();
1738 return;
1739}
1740
1741/*
1742 * This thread does not allocate any resource, except within
1743 * handle_message, within mutex protection. This mutex protects against
1744 * fork and exit.
1745 * The other moment it allocates resources is at socket connection, which
1746 * is also protected by the mutex.
1747 */
1748static
1749void *ust_listener_thread(void *arg)
1750{
1751 struct sock_info *sock_info = arg;
1752 int sock, ret, prev_connect_failed = 0, has_waited = 0, fd;
1753 long timeout;
1754
1755 lttng_ust_fixup_tls();
1756 /*
1757 * If available, add '-ust' to the end of this thread's
1758 * process name
1759 */
1760 ret = lttng_ust_setustprocname();
1761 if (ret) {
1762 ERR("Unable to set UST process name");
1763 }
1764
1765 /* Restart trying to connect to the session daemon */
1766restart:
1767 if (prev_connect_failed) {
1768 /* Wait for sessiond availability with pipe */
1769 wait_for_sessiond(sock_info);
1770 if (has_waited) {
1771 has_waited = 0;
1772 /*
1773 * Sleep for 5 seconds before retrying after a
1774 * sequence of failure / wait / failure. This
1775 * deals with a killed or broken session daemon.
1776 */
1777 sleep(5);
1778 } else {
1779 has_waited = 1;
1780 }
1781 prev_connect_failed = 0;
1782 }
1783
1784 if (ust_lock()) {
1785 goto quit;
1786 }
1787
1788 if (sock_info->socket != -1) {
1789 /* FD tracker is updated by ustcomm_close_unix_sock() */
1790 ret = ustcomm_close_unix_sock(sock_info->socket);
1791 if (ret) {
1792 ERR("Error closing %s ust cmd socket",
1793 sock_info->name);
1794 }
1795 sock_info->socket = -1;
1796 }
1797 if (sock_info->notify_socket != -1) {
1798 /* FD tracker is updated by ustcomm_close_unix_sock() */
1799 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1800 if (ret) {
1801 ERR("Error closing %s ust notify socket",
1802 sock_info->name);
1803 }
1804 sock_info->notify_socket = -1;
1805 }
1806
1807
1808 /*
1809 * Register. We need to perform both connect and sending
1810 * registration message before doing the next connect otherwise
1811 * we may reach unix socket connect queue max limits and block
1812 * on the 2nd connect while the session daemon is awaiting the
1813 * first connect registration message.
1814 */
1815 /* Connect cmd socket */
1816 lttng_ust_lock_fd_tracker();
1817 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1818 get_connect_sock_timeout());
1819 if (ret < 0) {
1820 lttng_ust_unlock_fd_tracker();
1821 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1822 prev_connect_failed = 1;
1823
1824 /*
1825 * If we cannot find the sessiond daemon, don't delay
1826 * constructor execution.
1827 */
1828 ret = handle_register_failed(sock_info);
1829 assert(!ret);
1830 ust_unlock();
1831 goto restart;
1832 }
1833 fd = ret;
1834 ret = lttng_ust_add_fd_to_tracker(fd);
1835 if (ret < 0) {
1836 ret = close(fd);
1837 if (ret) {
1838 PERROR("close on sock_info->socket");
1839 }
1840 ret = -1;
1841 lttng_ust_unlock_fd_tracker();
1842 ust_unlock();
1843 goto quit;
1844 }
1845
1846 sock_info->socket = ret;
1847 lttng_ust_unlock_fd_tracker();
1848
1849 ust_unlock();
1850 /*
1851 * Unlock/relock ust lock because connect is blocking (with
1852 * timeout). Don't delay constructors on the ust lock for too
1853 * long.
1854 */
1855 if (ust_lock()) {
1856 goto quit;
1857 }
1858
1859 /*
1860 * Create only one root handle per listener thread for the whole
1861 * process lifetime, so we ensure we get ID which is statically
1862 * assigned to the root handle.
1863 */
1864 if (sock_info->root_handle == -1) {
1865 ret = lttng_abi_create_root_handle();
1866 if (ret < 0) {
1867 ERR("Error creating root handle");
1868 goto quit;
1869 }
1870 sock_info->root_handle = ret;
1871 }
1872
1873 ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD);
1874 if (ret < 0) {
1875 ERR("Error registering to %s ust cmd socket",
1876 sock_info->name);
1877 prev_connect_failed = 1;
1878 /*
1879 * If we cannot register to the sessiond daemon, don't
1880 * delay constructor execution.
1881 */
1882 ret = handle_register_failed(sock_info);
1883 assert(!ret);
1884 ust_unlock();
1885 goto restart;
1886 }
1887
1888 ust_unlock();
1889 /*
1890 * Unlock/relock ust lock because connect is blocking (with
1891 * timeout). Don't delay constructors on the ust lock for too
1892 * long.
1893 */
1894 if (ust_lock()) {
1895 goto quit;
1896 }
1897
1898 /* Connect notify socket */
1899 lttng_ust_lock_fd_tracker();
1900 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1901 get_connect_sock_timeout());
1902 if (ret < 0) {
1903 lttng_ust_unlock_fd_tracker();
1904 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1905 prev_connect_failed = 1;
1906
1907 /*
1908 * If we cannot find the sessiond daemon, don't delay
1909 * constructor execution.
1910 */
1911 ret = handle_register_failed(sock_info);
1912 assert(!ret);
1913 ust_unlock();
1914 goto restart;
1915 }
1916
1917 fd = ret;
1918 ret = lttng_ust_add_fd_to_tracker(fd);
1919 if (ret < 0) {
1920 ret = close(fd);
1921 if (ret) {
1922 PERROR("close on sock_info->notify_socket");
1923 }
1924 ret = -1;
1925 lttng_ust_unlock_fd_tracker();
1926 ust_unlock();
1927 goto quit;
1928 }
1929
1930 sock_info->notify_socket = ret;
1931 lttng_ust_unlock_fd_tracker();
1932
1933 ust_unlock();
1934 /*
1935 * Unlock/relock ust lock because connect is blocking (with
1936 * timeout). Don't delay constructors on the ust lock for too
1937 * long.
1938 */
1939 if (ust_lock()) {
1940 goto quit;
1941 }
1942
1943 timeout = get_notify_sock_timeout();
1944 if (timeout >= 0) {
1945 /*
1946 * Give at least 10ms to sessiond to reply to
1947 * notifications.
1948 */
1949 if (timeout < 10)
1950 timeout = 10;
1951 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1952 timeout);
1953 if (ret < 0) {
1954 WARN("Error setting socket receive timeout");
1955 }
1956 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1957 timeout);
1958 if (ret < 0) {
1959 WARN("Error setting socket send timeout");
1960 }
1961 } else if (timeout < -1) {
1962 WARN("Unsupported timeout value %ld", timeout);
1963 }
1964
1965 ret = register_to_sessiond(sock_info->notify_socket,
1966 USTCTL_SOCKET_NOTIFY);
1967 if (ret < 0) {
1968 ERR("Error registering to %s ust notify socket",
1969 sock_info->name);
1970 prev_connect_failed = 1;
1971 /*
1972 * If we cannot register to the sessiond daemon, don't
1973 * delay constructor execution.
1974 */
1975 ret = handle_register_failed(sock_info);
1976 assert(!ret);
1977 ust_unlock();
1978 goto restart;
1979 }
1980 sock = sock_info->socket;
1981
1982 ust_unlock();
1983
1984 for (;;) {
1985 ssize_t len;
1986 struct ustcomm_ust_msg lum;
1987
1988 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
1989 switch (len) {
1990 case 0: /* orderly shutdown */
1991 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
1992 if (ust_lock()) {
1993 goto quit;
1994 }
1995 /*
1996 * Either sessiond has shutdown or refused us by closing the socket.
1997 * In either case, we don't want to delay construction execution,
1998 * and we need to wait before retry.
1999 */
2000 prev_connect_failed = 1;
2001 /*
2002 * If we cannot register to the sessiond daemon, don't
2003 * delay constructor execution.
2004 */
2005 ret = handle_register_failed(sock_info);
2006 assert(!ret);
2007 ust_unlock();
2008 goto end;
2009 case sizeof(lum):
2010 print_cmd(lum.cmd, lum.handle);
2011 ret = handle_message(sock_info, sock, &lum);
2012 if (ret) {
2013 ERR("Error handling message for %s socket",
2014 sock_info->name);
2015 /*
2016 * Close socket if protocol error is
2017 * detected.
2018 */
2019 goto end;
2020 }
2021 continue;
2022 default:
2023 if (len < 0) {
2024 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
2025 } else {
2026 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
2027 }
2028 if (len == -ECONNRESET) {
2029 DBG("%s remote end closed connection", sock_info->name);
2030 goto end;
2031 }
2032 goto end;
2033 }
2034
2035 }
2036end:
2037 if (ust_lock()) {
2038 goto quit;
2039 }
2040 /* Cleanup socket handles before trying to reconnect */
2041 lttng_ust_abi_objd_table_owner_cleanup(sock_info);
2042 ust_unlock();
2043 goto restart; /* try to reconnect */
2044
2045quit:
2046 ust_unlock();
2047
2048 pthread_mutex_lock(&ust_exit_mutex);
2049 sock_info->thread_active = 0;
2050 pthread_mutex_unlock(&ust_exit_mutex);
2051 return NULL;
2052}
2053
2054/*
2055 * Weak symbol to call when the ust malloc wrapper is not loaded.
2056 */
2057__attribute__((weak))
2058void lttng_ust_libc_wrapper_malloc_init(void)
2059{
2060}
2061
2062/*
2063 * sessiond monitoring thread: monitor presence of global and per-user
2064 * sessiond by polling the application common named pipe.
2065 */
2066void __attribute__((constructor)) lttng_ust_init(void)
2067{
2068 struct timespec constructor_timeout;
2069 sigset_t sig_all_blocked, orig_parent_mask;
2070 pthread_attr_t thread_attr;
2071 int timeout_mode;
2072 int ret;
2073 void *handle;
2074
2075 if (uatomic_xchg(&initialized, 1) == 1)
2076 return;
2077
2078 /*
2079 * Fixup interdependency between TLS fixup mutex (which happens
2080 * to be the dynamic linker mutex) and ust_lock, taken within
2081 * the ust lock.
2082 */
2083 lttng_ust_fixup_tls();
2084
2085 lttng_ust_loaded = 1;
2086
2087 /*
2088 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2089 * the unloading of code used by the ust_listener_threads as we can not
2090 * reliably know when they exited. To do that, manually load
2091 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2092 * this library so it never becomes zero, thus never gets unloaded from the
2093 * address space of the process. Since we are already running in the
2094 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
2095 * simply increment the refcount and no additionnal work is needed by the
2096 * dynamic loader as the shared library is already loaded in the address
2097 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2098 * unloading of the UST library if its refcount becomes zero (which should
2099 * never happen). Do the return value check but discard the handle at the
2100 * end of the function as it's not needed.
2101 */
2102 handle = dlopen(LTTNG_UST_LIB_SONAME, RTLD_LAZY | RTLD_NODELETE);
2103 if (!handle) {
2104 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME);
2105 }
2106
2107 /*
2108 * We want precise control over the order in which we construct
2109 * our sub-libraries vs starting to receive commands from
2110 * sessiond (otherwise leading to errors when trying to create
2111 * sessiond before the init functions are completed).
2112 */
2113 ust_err_init();
2114 lttng_ust_getenv_init(); /* Needs ust_err_init() to be completed. */
2115 lttng_ust_tp_init();
2116 lttng_ust_init_fd_tracker();
2117 lttng_ust_clock_init();
2118 lttng_ust_getcpu_init();
2119 lttng_ust_statedump_init();
2120 lttng_ring_buffer_metadata_client_init();
2121 lttng_ring_buffer_client_overwrite_init();
2122 lttng_ring_buffer_client_overwrite_rt_init();
2123 lttng_ring_buffer_client_discard_init();
2124 lttng_ring_buffer_client_discard_rt_init();
2125 lttng_counter_client_percpu_32_modular_init();
2126 lttng_counter_client_percpu_64_modular_init();
2127 lttng_perf_counter_init();
2128 /*
2129 * Invoke ust malloc wrapper init before starting other threads.
2130 */
2131 lttng_ust_libc_wrapper_malloc_init();
2132
2133 timeout_mode = get_constructor_timeout(&constructor_timeout);
2134
2135 get_allow_blocking();
2136
2137 ret = sem_init(&constructor_wait, 0, 0);
2138 if (ret) {
2139 PERROR("sem_init");
2140 }
2141
2142 ret = setup_global_apps();
2143 if (ret) {
2144 assert(global_apps.allowed == 0);
2145 DBG("global apps setup returned %d", ret);
2146 }
2147
2148 ret = setup_local_apps();
2149 if (ret) {
2150 assert(local_apps.allowed == 0);
2151 DBG("local apps setup returned %d", ret);
2152 }
2153
2154 /* A new thread created by pthread_create inherits the signal mask
2155 * from the parent. To avoid any signal being received by the
2156 * listener thread, we block all signals temporarily in the parent,
2157 * while we create the listener thread.
2158 */
2159 sigfillset(&sig_all_blocked);
2160 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
2161 if (ret) {
2162 ERR("pthread_sigmask: %s", strerror(ret));
2163 }
2164
2165 ret = pthread_attr_init(&thread_attr);
2166 if (ret) {
2167 ERR("pthread_attr_init: %s", strerror(ret));
2168 }
2169 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
2170 if (ret) {
2171 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
2172 }
2173
2174 if (global_apps.allowed) {
2175 pthread_mutex_lock(&ust_exit_mutex);
2176 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
2177 ust_listener_thread, &global_apps);
2178 if (ret) {
2179 ERR("pthread_create global: %s", strerror(ret));
2180 }
2181 global_apps.thread_active = 1;
2182 pthread_mutex_unlock(&ust_exit_mutex);
2183 } else {
2184 handle_register_done(&global_apps);
2185 }
2186
2187 if (local_apps.allowed) {
2188 pthread_mutex_lock(&ust_exit_mutex);
2189 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
2190 ust_listener_thread, &local_apps);
2191 if (ret) {
2192 ERR("pthread_create local: %s", strerror(ret));
2193 }
2194 local_apps.thread_active = 1;
2195 pthread_mutex_unlock(&ust_exit_mutex);
2196 } else {
2197 handle_register_done(&local_apps);
2198 }
2199 ret = pthread_attr_destroy(&thread_attr);
2200 if (ret) {
2201 ERR("pthread_attr_destroy: %s", strerror(ret));
2202 }
2203
2204 /* Restore original signal mask in parent */
2205 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
2206 if (ret) {
2207 ERR("pthread_sigmask: %s", strerror(ret));
2208 }
2209
2210 switch (timeout_mode) {
2211 case 1: /* timeout wait */
2212 do {
2213 ret = sem_timedwait(&constructor_wait,
2214 &constructor_timeout);
2215 } while (ret < 0 && errno == EINTR);
2216 if (ret < 0) {
2217 switch (errno) {
2218 case ETIMEDOUT:
2219 ERR("Timed out waiting for lttng-sessiond");
2220 break;
2221 case EINVAL:
2222 PERROR("sem_timedwait");
2223 break;
2224 default:
2225 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2226 strerror(errno));
2227 }
2228 }
2229 break;
2230 case -1:/* wait forever */
2231 do {
2232 ret = sem_wait(&constructor_wait);
2233 } while (ret < 0 && errno == EINTR);
2234 if (ret < 0) {
2235 switch (errno) {
2236 case EINVAL:
2237 PERROR("sem_wait");
2238 break;
2239 default:
2240 ERR("Unexpected error \"%s\" returned by sem_wait",
2241 strerror(errno));
2242 }
2243 }
2244 break;
2245 case 0: /* no timeout */
2246 break;
2247 }
2248}
2249
2250static
2251void lttng_ust_cleanup(int exiting)
2252{
2253 cleanup_sock_info(&global_apps, exiting);
2254 cleanup_sock_info(&local_apps, exiting);
2255 local_apps.allowed = 0;
2256 global_apps.allowed = 0;
2257 /*
2258 * The teardown in this function all affect data structures
2259 * accessed under the UST lock by the listener thread. This
2260 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2261 * that none of these threads are accessing this data at this
2262 * point.
2263 */
2264 lttng_ust_abi_exit();
2265 lttng_ust_abi_events_exit();
2266 lttng_perf_counter_exit();
2267 lttng_ring_buffer_client_discard_rt_exit();
2268 lttng_ring_buffer_client_discard_exit();
2269 lttng_ring_buffer_client_overwrite_rt_exit();
2270 lttng_ring_buffer_client_overwrite_exit();
2271 lttng_ring_buffer_metadata_client_exit();
2272 lttng_counter_client_percpu_32_modular_exit();
2273 lttng_counter_client_percpu_64_modular_exit();
2274 lttng_ust_statedump_destroy();
2275 lttng_ust_tp_exit();
2276 if (!exiting) {
2277 /* Reinitialize values for fork */
2278 sem_count = sem_count_initial_value;
2279 lttng_ust_comm_should_quit = 0;
2280 initialized = 0;
2281 }
2282}
2283
2284void __attribute__((destructor)) lttng_ust_exit(void)
2285{
2286 int ret;
2287
2288 /*
2289 * Using pthread_cancel here because:
2290 * A) we don't want to hang application teardown.
2291 * B) the thread is not allocating any resource.
2292 */
2293
2294 /*
2295 * Require the communication thread to quit. Synchronize with
2296 * mutexes to ensure it is not in a mutex critical section when
2297 * pthread_cancel is later called.
2298 */
2299 ust_lock_nocheck();
2300 lttng_ust_comm_should_quit = 1;
2301 ust_unlock();
2302
2303 pthread_mutex_lock(&ust_exit_mutex);
2304 /* cancel threads */
2305 if (global_apps.thread_active) {
2306 ret = pthread_cancel(global_apps.ust_listener);
2307 if (ret) {
2308 ERR("Error cancelling global ust listener thread: %s",
2309 strerror(ret));
2310 } else {
2311 global_apps.thread_active = 0;
2312 }
2313 }
2314 if (local_apps.thread_active) {
2315 ret = pthread_cancel(local_apps.ust_listener);
2316 if (ret) {
2317 ERR("Error cancelling local ust listener thread: %s",
2318 strerror(ret));
2319 } else {
2320 local_apps.thread_active = 0;
2321 }
2322 }
2323 pthread_mutex_unlock(&ust_exit_mutex);
2324
2325 /*
2326 * Do NOT join threads: use of sys_futex makes it impossible to
2327 * join the threads without using async-cancel, but async-cancel
2328 * is delivered by a signal, which could hit the target thread
2329 * anywhere in its code path, including while the ust_lock() is
2330 * held, causing a deadlock for the other thread. Let the OS
2331 * cleanup the threads if there are stalled in a syscall.
2332 */
2333 lttng_ust_cleanup(1);
2334}
2335
2336static
2337void ust_context_ns_reset(void)
2338{
2339 lttng_context_pid_ns_reset();
2340 lttng_context_cgroup_ns_reset();
2341 lttng_context_ipc_ns_reset();
2342 lttng_context_mnt_ns_reset();
2343 lttng_context_net_ns_reset();
2344 lttng_context_user_ns_reset();
2345 lttng_context_time_ns_reset();
2346 lttng_context_uts_ns_reset();
2347}
2348
2349static
2350void ust_context_vuids_reset(void)
2351{
2352 lttng_context_vuid_reset();
2353 lttng_context_veuid_reset();
2354 lttng_context_vsuid_reset();
2355}
2356
2357static
2358void ust_context_vgids_reset(void)
2359{
2360 lttng_context_vgid_reset();
2361 lttng_context_vegid_reset();
2362 lttng_context_vsgid_reset();
2363}
2364
2365/*
2366 * We exclude the worker threads across fork and clone (except
2367 * CLONE_VM), because these system calls only keep the forking thread
2368 * running in the child. Therefore, we don't want to call fork or clone
2369 * in the middle of an tracepoint or ust tracing state modification.
2370 * Holding this mutex protects these structures across fork and clone.
2371 */
2372void lttng_ust_before_fork(sigset_t *save_sigset)
2373{
2374 /*
2375 * Disable signals. This is to avoid that the child intervenes
2376 * before it is properly setup for tracing. It is safer to
2377 * disable all signals, because then we know we are not breaking
2378 * anything by restoring the original mask.
2379 */
2380 sigset_t all_sigs;
2381 int ret;
2382
2383 /* Fixup lttng-ust TLS. */
2384 lttng_ust_fixup_tls();
2385
2386 if (URCU_TLS(lttng_ust_nest_count))
2387 return;
2388 /* Disable signals */
2389 sigfillset(&all_sigs);
2390 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
2391 if (ret == -1) {
2392 PERROR("sigprocmask");
2393 }
2394
2395 pthread_mutex_lock(&ust_fork_mutex);
2396
2397 ust_lock_nocheck();
2398 lttng_ust_urcu_before_fork();
2399 lttng_ust_lock_fd_tracker();
2400 lttng_perf_lock();
2401}
2402
2403static void ust_after_fork_common(sigset_t *restore_sigset)
2404{
2405 int ret;
2406
2407 DBG("process %d", getpid());
2408 lttng_perf_unlock();
2409 lttng_ust_unlock_fd_tracker();
2410 ust_unlock();
2411
2412 pthread_mutex_unlock(&ust_fork_mutex);
2413
2414 /* Restore signals */
2415 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
2416 if (ret == -1) {
2417 PERROR("sigprocmask");
2418 }
2419}
2420
2421void lttng_ust_after_fork_parent(sigset_t *restore_sigset)
2422{
2423 if (URCU_TLS(lttng_ust_nest_count))
2424 return;
2425 DBG("process %d", getpid());
2426 lttng_ust_urcu_after_fork_parent();
2427 /* Release mutexes and reenable signals */
2428 ust_after_fork_common(restore_sigset);
2429}
2430
2431/*
2432 * After fork, in the child, we need to cleanup all the leftover state,
2433 * except the worker thread which already magically disappeared thanks
2434 * to the weird Linux fork semantics. After tyding up, we call
2435 * lttng_ust_init() again to start over as a new PID.
2436 *
2437 * This is meant for forks() that have tracing in the child between the
2438 * fork and following exec call (if there is any).
2439 */
2440void lttng_ust_after_fork_child(sigset_t *restore_sigset)
2441{
2442 if (URCU_TLS(lttng_ust_nest_count))
2443 return;
2444 lttng_context_vpid_reset();
2445 lttng_context_vtid_reset();
2446 lttng_ust_context_procname_reset();
2447 ust_context_ns_reset();
2448 ust_context_vuids_reset();
2449 ust_context_vgids_reset();
2450 DBG("process %d", getpid());
2451 /* Release urcu mutexes */
2452 lttng_ust_urcu_after_fork_child();
2453 lttng_ust_cleanup(0);
2454 /* Release mutexes and reenable signals */
2455 ust_after_fork_common(restore_sigset);
2456 lttng_ust_init();
2457}
2458
2459void lttng_ust_after_setns(void)
2460{
2461 ust_context_ns_reset();
2462 ust_context_vuids_reset();
2463 ust_context_vgids_reset();
2464}
2465
2466void lttng_ust_after_unshare(void)
2467{
2468 ust_context_ns_reset();
2469 ust_context_vuids_reset();
2470 ust_context_vgids_reset();
2471}
2472
2473void lttng_ust_after_setuid(void)
2474{
2475 ust_context_vuids_reset();
2476}
2477
2478void lttng_ust_after_seteuid(void)
2479{
2480 ust_context_vuids_reset();
2481}
2482
2483void lttng_ust_after_setreuid(void)
2484{
2485 ust_context_vuids_reset();
2486}
2487
2488void lttng_ust_after_setresuid(void)
2489{
2490 ust_context_vuids_reset();
2491}
2492
2493void lttng_ust_after_setgid(void)
2494{
2495 ust_context_vgids_reset();
2496}
2497
2498void lttng_ust_after_setegid(void)
2499{
2500 ust_context_vgids_reset();
2501}
2502
2503void lttng_ust_after_setregid(void)
2504{
2505 ust_context_vgids_reset();
2506}
2507
2508void lttng_ust_after_setresgid(void)
2509{
2510 ust_context_vgids_reset();
2511}
2512
2513void lttng_ust_sockinfo_session_enabled(void *owner)
2514{
2515 struct sock_info *sock_info = owner;
2516 sock_info->statedump_pending = 1;
2517}
This page took 0.031832 seconds and 4 git commands to generate.