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