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