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