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