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