Move the getcpu plugin implementation to liblttn-ust-common
[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"
b62f8205 58#include "lttng-counter-client.h"
f27f1026 59#include "lttng-rb-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 */
0db3d6ee 262 char procname[LTTNG_UST_ABI_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;
0db3d6ee 485 lttng_pthread_getname_np(global_apps.procname, LTTNG_UST_ABI_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
0db3d6ee 531 lttng_pthread_getname_np(local_apps.procname, LTTNG_UST_ABI_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
249cffb5 623int register_to_sessiond(int socket, enum lttng_ust_ctl_socket_type type)
2691221a 624{
32ce8569
MD
625 return ustcomm_send_reg_msg(socket,
626 type,
627 CAA_BITS_PER_LONG,
dc325c1d
MJ
628 lttng_ust_rb_alignof(uint8_t) * CHAR_BIT,
629 lttng_ust_rb_alignof(uint16_t) * CHAR_BIT,
630 lttng_ust_rb_alignof(uint32_t) * CHAR_BIT,
631 lttng_ust_rb_alignof(uint64_t) * CHAR_BIT,
632 lttng_ust_rb_alignof(unsigned long) * CHAR_BIT);
2691221a
MD
633}
634
d9e99d10 635static
57773204 636int send_reply(int sock, struct ustcomm_ust_reply *lur)
d9e99d10 637{
9eb62b9c 638 ssize_t len;
d3a492d1 639
57773204 640 len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur));
d3a492d1 641 switch (len) {
a4be8962 642 case sizeof(*lur):
d3a492d1
MD
643 DBG("message successfully sent");
644 return 0;
7bc53e94
MD
645 default:
646 if (len == -ECONNRESET) {
647 DBG("remote end closed connection");
d3a492d1
MD
648 return 0;
649 }
7bc53e94
MD
650 if (len < 0)
651 return len;
652 DBG("incorrect message size: %zd", len);
653 return -EINVAL;
d3a492d1
MD
654 }
655}
656
657static
eb0e6022 658void decrement_sem_count(unsigned int count)
11ff9c7d
MD
659{
660 int ret;
661
eb0e6022
GAPG
662 assert(uatomic_read(&sem_count) >= count);
663
56cd7e2f 664 if (uatomic_read(&sem_count) <= 0) {
eb0e6022 665 return;
56cd7e2f 666 }
eb0e6022
GAPG
667
668 ret = uatomic_add_return(&sem_count, -count);
95259bd0
MD
669 if (ret == 0) {
670 ret = sem_post(&constructor_wait);
671 assert(!ret);
672 }
eb0e6022
GAPG
673}
674
675static
676int handle_register_done(struct sock_info *sock_info)
677{
678 if (sock_info->registration_done)
679 return 0;
680 sock_info->registration_done = 1;
681
682 decrement_sem_count(1);
04682184
MD
683 if (!sock_info->statedump_pending) {
684 sock_info->initial_statedump_done = 1;
685 decrement_sem_count(1);
686 }
eb0e6022
GAPG
687
688 return 0;
689}
690
691static
692int handle_register_failed(struct sock_info *sock_info)
693{
694 if (sock_info->registration_done)
695 return 0;
696 sock_info->registration_done = 1;
697 sock_info->initial_statedump_done = 1;
698
699 decrement_sem_count(2);
700
11ff9c7d
MD
701 return 0;
702}
703
37dddb65
MD
704/*
705 * Only execute pending statedump after the constructor semaphore has
eb0e6022
GAPG
706 * been posted by the current listener thread. This means statedump will
707 * only be performed after the "registration done" command is received
708 * from this thread's session daemon.
37dddb65
MD
709 *
710 * This ensures we don't run into deadlock issues with the dynamic
711 * loader mutex, which is held while the constructor is called and
712 * waiting on the constructor semaphore. All operations requiring this
713 * dynamic loader lock need to be postponed using this mechanism.
eb0e6022
GAPG
714 *
715 * In a scenario with two session daemons connected to the application,
716 * it is possible that the first listener thread which receives the
717 * registration done command issues its statedump while the dynamic
718 * loader lock is still held by the application constructor waiting on
719 * the semaphore. It will however be allowed to proceed when the
720 * second session daemon sends the registration done command to the
721 * second listener thread. This situation therefore does not produce
722 * a deadlock.
37dddb65
MD
723 */
724static
725void handle_pending_statedump(struct sock_info *sock_info)
726{
eb0e6022 727 if (sock_info->registration_done && sock_info->statedump_pending) {
37dddb65 728 sock_info->statedump_pending = 0;
2932a87f 729 pthread_mutex_lock(&ust_fork_mutex);
37dddb65 730 lttng_handle_pending_statedump(sock_info);
458d678c 731 pthread_mutex_unlock(&ust_fork_mutex);
eb0e6022
GAPG
732
733 if (!sock_info->initial_statedump_done) {
734 sock_info->initial_statedump_done = 1;
735 decrement_sem_count(1);
736 }
37dddb65
MD
737 }
738}
739
6a6ef048
FD
740static inline
741const char *bytecode_type_str(uint32_t cmd)
742{
743 switch (cmd) {
fd17d7ce 744 case LTTNG_UST_ABI_CAPTURE:
d37ecb3f 745 return "capture";
fd17d7ce 746 case LTTNG_UST_ABI_FILTER:
6a6ef048
FD
747 return "filter";
748 default:
749 abort();
750 }
751}
752
753static
754int handle_bytecode_recv(struct sock_info *sock_info,
755 int sock, struct ustcomm_ust_msg *lum)
756{
ab89263e 757 struct lttng_ust_bytecode_node *bytecode = NULL;
22c30e27 758 enum lttng_ust_bytecode_type type;
fd17d7ce 759 const struct lttng_ust_abi_objd_ops *ops;
6a6ef048
FD
760 uint32_t data_size, data_size_max, reloc_offset;
761 uint64_t seqnum;
762 ssize_t len;
763 int ret = 0;
764
765 switch (lum->cmd) {
fd17d7ce 766 case LTTNG_UST_ABI_FILTER:
22c30e27 767 type = LTTNG_UST_BYTECODE_TYPE_FILTER;
6a6ef048 768 data_size = lum->u.filter.data_size;
fd17d7ce 769 data_size_max = LTTNG_UST_ABI_FILTER_BYTECODE_MAX_LEN;
6a6ef048
FD
770 reloc_offset = lum->u.filter.reloc_offset;
771 seqnum = lum->u.filter.seqnum;
772 break;
fd17d7ce 773 case LTTNG_UST_ABI_CAPTURE:
22c30e27 774 type = LTTNG_UST_BYTECODE_TYPE_CAPTURE;
d37ecb3f 775 data_size = lum->u.capture.data_size;
fd17d7ce 776 data_size_max = LTTNG_UST_ABI_CAPTURE_BYTECODE_MAX_LEN;
d37ecb3f
FD
777 reloc_offset = lum->u.capture.reloc_offset;
778 seqnum = lum->u.capture.seqnum;
779 break;
6a6ef048
FD
780 default:
781 abort();
782 }
783
784 if (data_size > data_size_max) {
785 ERR("Bytecode %s data size is too large: %u bytes",
786 bytecode_type_str(lum->cmd), data_size);
787 ret = -EINVAL;
788 goto end;
789 }
790
791 if (reloc_offset > data_size) {
792 ERR("Bytecode %s reloc offset %u is not within data",
793 bytecode_type_str(lum->cmd), reloc_offset);
794 ret = -EINVAL;
795 goto end;
796 }
797
798 /* Allocate the structure AND the `data[]` field. */
799 bytecode = zmalloc(sizeof(*bytecode) + data_size);
800 if (!bytecode) {
801 ret = -ENOMEM;
802 goto end;
803 }
804
805 bytecode->bc.len = data_size;
806 bytecode->bc.reloc_offset = reloc_offset;
807 bytecode->bc.seqnum = seqnum;
808 bytecode->type = type;
809
810 len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, bytecode->bc.len);
811 switch (len) {
812 case 0: /* orderly shutdown */
813 ret = 0;
ab89263e 814 goto end;
6a6ef048
FD
815 default:
816 if (len == bytecode->bc.len) {
817 DBG("Bytecode %s data received",
818 bytecode_type_str(lum->cmd));
819 break;
820 } else if (len < 0) {
821 DBG("Receive failed from lttng-sessiond with errno %d",
822 (int) -len);
823 if (len == -ECONNRESET) {
824 ERR("%s remote end closed connection",
825 sock_info->name);
826 ret = len;
ab89263e 827 goto end;
6a6ef048
FD
828 }
829 ret = len;
ab89263e 830 goto end;
6a6ef048
FD
831 } else {
832 DBG("Incorrect %s bytecode data message size: %zd",
833 bytecode_type_str(lum->cmd), len);
834 ret = -EINVAL;
ab89263e 835 goto end;
6a6ef048
FD
836 }
837 }
838
fd17d7ce 839 ops = lttng_ust_abi_objd_ops(lum->handle);
6a6ef048
FD
840 if (!ops) {
841 ret = -ENOENT;
ab89263e 842 goto end;
6a6ef048
FD
843 }
844
ab89263e 845 if (ops->cmd)
6a6ef048 846 ret = ops->cmd(lum->handle, lum->cmd,
ab89263e 847 (unsigned long) &bytecode,
6a6ef048 848 NULL, sock_info);
ab89263e 849 else
6a6ef048 850 ret = -ENOSYS;
6a6ef048 851
6a6ef048 852end:
ab89263e 853 free(bytecode);
6a6ef048
FD
854 return ret;
855}
856
11ff9c7d
MD
857static
858int handle_message(struct sock_info *sock_info,
57773204 859 int sock, struct ustcomm_ust_msg *lum)
d3a492d1 860{
1ea11eab 861 int ret = 0;
fd17d7ce 862 const struct lttng_ust_abi_objd_ops *ops;
57773204 863 struct ustcomm_ust_reply lur;
fd17d7ce
MD
864 union lttng_ust_abi_args args;
865 char ctxstr[LTTNG_UST_ABI_SYM_NAME_LEN]; /* App context string. */
40003310 866 ssize_t len;
1ea11eab 867
46050b1a
MD
868 memset(&lur, 0, sizeof(lur));
869
3327ac33 870 if (ust_lock()) {
74d81a6c 871 ret = -LTTNG_UST_ERR_EXITING;
0dafcd63 872 goto error;
1ea11eab 873 }
9eb62b9c 874
fd17d7ce 875 ops = lttng_ust_abi_objd_ops(lum->handle);
46050b1a
MD
876 if (!ops) {
877 ret = -ENOENT;
0dafcd63 878 goto error;
1ea11eab 879 }
46050b1a
MD
880
881 switch (lum->cmd) {
fd17d7ce
MD
882 case LTTNG_UST_ABI_REGISTER_DONE:
883 if (lum->handle == LTTNG_UST_ABI_ROOT_HANDLE)
edaa1431 884 ret = handle_register_done(sock_info);
11ff9c7d
MD
885 else
886 ret = -EINVAL;
887 break;
fd17d7ce
MD
888 case LTTNG_UST_ABI_RELEASE:
889 if (lum->handle == LTTNG_UST_ABI_ROOT_HANDLE)
46050b1a
MD
890 ret = -EPERM;
891 else
fd17d7ce 892 ret = lttng_ust_abi_objd_unref(lum->handle, 1);
d9e99d10 893 break;
fd17d7ce
MD
894 case LTTNG_UST_ABI_CAPTURE:
895 case LTTNG_UST_ABI_FILTER:
6a6ef048
FD
896 ret = handle_bytecode_recv(sock_info, sock, lum);
897 if (ret)
2734ca65 898 goto error;
2d78951a 899 break;
fd17d7ce 900 case LTTNG_UST_ABI_EXCLUSION:
86e36163
JI
901 {
902 /* Receive exclusion names */
903 struct lttng_ust_excluder_node *node;
904 unsigned int count;
905
906 count = lum->u.exclusion.count;
907 if (count == 0) {
908 /* There are no names to read */
909 ret = 0;
910 goto error;
911 }
912 node = zmalloc(sizeof(*node) +
fd17d7ce 913 count * LTTNG_UST_ABI_SYM_NAME_LEN);
86e36163
JI
914 if (!node) {
915 ret = -ENOMEM;
916 goto error;
917 }
918 node->excluder.count = count;
919 len = ustcomm_recv_unix_sock(sock, node->excluder.names,
fd17d7ce 920 count * LTTNG_UST_ABI_SYM_NAME_LEN);
86e36163
JI
921 switch (len) {
922 case 0: /* orderly shutdown */
923 ret = 0;
924 free(node);
925 goto error;
926 default:
fd17d7ce 927 if (len == count * LTTNG_UST_ABI_SYM_NAME_LEN) {
86e36163
JI
928 DBG("Exclusion data received");
929 break;
930 } else if (len < 0) {
931 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
932 if (len == -ECONNRESET) {
933 ERR("%s remote end closed connection", sock_info->name);
934 ret = len;
935 free(node);
936 goto error;
937 }
938 ret = len;
939 free(node);
0dafcd63 940 goto error;
86e36163
JI
941 } else {
942 DBG("Incorrect exclusion data message size: %zd", len);
943 ret = -EINVAL;
944 free(node);
0dafcd63 945 goto error;
86e36163
JI
946 }
947 }
e9fe6aad 948 if (ops->cmd)
86e36163 949 ret = ops->cmd(lum->handle, lum->cmd,
e9fe6aad 950 (unsigned long) &node,
86e36163 951 &args, sock_info);
e9fe6aad 952 else
86e36163 953 ret = -ENOSYS;
e9fe6aad 954 free(node);
86e36163
JI
955 break;
956 }
fd17d7ce 957 case LTTNG_UST_ABI_EVENT_NOTIFIER_GROUP_CREATE:
d8d2416d 958 {
f52e5902 959 int event_notifier_notif_fd, close_ret;
d8d2416d
FD
960
961 len = ustcomm_recv_event_notifier_notif_fd_from_sessiond(sock,
962 &event_notifier_notif_fd);
963 switch (len) {
964 case 0: /* orderly shutdown */
965 ret = 0;
966 goto error;
967 case 1:
968 break;
969 default:
970 if (len < 0) {
971 DBG("Receive failed from lttng-sessiond with errno %d",
972 (int) -len);
973 if (len == -ECONNRESET) {
974 ERR("%s remote end closed connection",
975 sock_info->name);
976 ret = len;
977 goto error;
978 }
979 ret = len;
980 goto error;
981 } else {
982 DBG("Incorrect event notifier fd message size: %zd",
983 len);
984 ret = -EINVAL;
985 goto error;
986 }
987 }
988 args.event_notifier_handle.event_notifier_notif_fd =
989 event_notifier_notif_fd;
990 if (ops->cmd)
991 ret = ops->cmd(lum->handle, lum->cmd,
992 (unsigned long) &lum->u,
993 &args, sock_info);
994 else
995 ret = -ENOSYS;
f52e5902
MD
996 if (args.event_notifier_handle.event_notifier_notif_fd >= 0) {
997 lttng_ust_lock_fd_tracker();
998 close_ret = close(args.event_notifier_handle.event_notifier_notif_fd);
999 lttng_ust_unlock_fd_tracker();
1000 if (close_ret)
1001 PERROR("close");
1002 }
d8d2416d
FD
1003 break;
1004 }
fd17d7ce 1005 case LTTNG_UST_ABI_CHANNEL:
74d81a6c
MD
1006 {
1007 void *chan_data;
ff0f5728 1008 int wakeup_fd;
74d81a6c
MD
1009
1010 len = ustcomm_recv_channel_from_sessiond(sock,
ff0f5728
MD
1011 &chan_data, lum->u.channel.len,
1012 &wakeup_fd);
74d81a6c
MD
1013 switch (len) {
1014 case 0: /* orderly shutdown */
1015 ret = 0;
1016 goto error;
1017 default:
1018 if (len == lum->u.channel.len) {
1019 DBG("channel 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 goto error;
1027 }
1028 ret = len;
0dafcd63 1029 goto error;
74d81a6c
MD
1030 } else {
1031 DBG("incorrect channel data message size: %zd", len);
1032 ret = -EINVAL;
0dafcd63 1033 goto error;
74d81a6c
MD
1034 }
1035 }
1036 args.channel.chan_data = chan_data;
ff0f5728 1037 args.channel.wakeup_fd = wakeup_fd;
74d81a6c
MD
1038 if (ops->cmd)
1039 ret = ops->cmd(lum->handle, lum->cmd,
1040 (unsigned long) &lum->u,
1041 &args, sock_info);
1042 else
1043 ret = -ENOSYS;
867942fd
MD
1044 if (args.channel.wakeup_fd >= 0) {
1045 int close_ret;
1046
1047 lttng_ust_lock_fd_tracker();
1048 close_ret = close(args.channel.wakeup_fd);
1049 lttng_ust_unlock_fd_tracker();
1050 args.channel.wakeup_fd = -1;
1051 if (close_ret)
1052 PERROR("close");
1053 }
1054 free(args.channel.chan_data);
74d81a6c
MD
1055 break;
1056 }
fd17d7ce 1057 case LTTNG_UST_ABI_STREAM:
74d81a6c 1058 {
118c051e
MD
1059 int close_ret;
1060
74d81a6c
MD
1061 /* Receive shm_fd, wakeup_fd */
1062 ret = ustcomm_recv_stream_from_sessiond(sock,
61e520fb 1063 NULL,
74d81a6c
MD
1064 &args.stream.shm_fd,
1065 &args.stream.wakeup_fd);
1066 if (ret) {
0dafcd63 1067 goto error;
74d81a6c 1068 }
973eac63 1069
74d81a6c
MD
1070 if (ops->cmd)
1071 ret = ops->cmd(lum->handle, lum->cmd,
1072 (unsigned long) &lum->u,
1073 &args, sock_info);
1074 else
1075 ret = -ENOSYS;
118c051e
MD
1076 if (args.stream.shm_fd >= 0) {
1077 lttng_ust_lock_fd_tracker();
1078 close_ret = close(args.stream.shm_fd);
1079 lttng_ust_unlock_fd_tracker();
1080 args.stream.shm_fd = -1;
1081 if (close_ret)
1082 PERROR("close");
1083 }
1084 if (args.stream.wakeup_fd >= 0) {
1085 lttng_ust_lock_fd_tracker();
1086 close_ret = close(args.stream.wakeup_fd);
1087 lttng_ust_unlock_fd_tracker();
1088 args.stream.wakeup_fd = -1;
1089 if (close_ret)
1090 PERROR("close");
1091 }
74d81a6c
MD
1092 break;
1093 }
fd17d7ce 1094 case LTTNG_UST_ABI_CONTEXT:
8e696cfa 1095 switch (lum->u.context.ctx) {
fd17d7ce 1096 case LTTNG_UST_ABI_CONTEXT_APP_CONTEXT:
8e696cfa
MD
1097 {
1098 char *p;
1099 size_t ctxlen, recvlen;
1100
1101 ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1
1102 + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len;
fd17d7ce 1103 if (ctxlen >= LTTNG_UST_ABI_SYM_NAME_LEN) {
8e696cfa
MD
1104 ERR("Application context string length size is too large: %zu bytes",
1105 ctxlen);
1106 ret = -EINVAL;
1107 goto error;
1108 }
1109 strcpy(ctxstr, "$app.");
1110 p = &ctxstr[strlen("$app.")];
1111 recvlen = ctxlen - strlen("$app.");
1112 len = ustcomm_recv_unix_sock(sock, p, recvlen);
1113 switch (len) {
1114 case 0: /* orderly shutdown */
1115 ret = 0;
1116 goto error;
1117 default:
1118 if (len == recvlen) {
1119 DBG("app context data received");
1120 break;
1121 } else if (len < 0) {
1122 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1123 if (len == -ECONNRESET) {
1124 ERR("%s remote end closed connection", sock_info->name);
1125 ret = len;
1126 goto error;
1127 }
1128 ret = len;
1129 goto error;
1130 } else {
1131 DBG("incorrect app context data message size: %zd", len);
1132 ret = -EINVAL;
1133 goto error;
1134 }
1135 }
1136 /* Put : between provider and ctxname. */
1137 p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':';
1138 args.app_context.ctxname = ctxstr;
1139 break;
1140 }
1141 default:
1142 break;
1143 }
1144 if (ops->cmd) {
1145 ret = ops->cmd(lum->handle, lum->cmd,
1146 (unsigned long) &lum->u,
1147 &args, sock_info);
1148 } else {
1149 ret = -ENOSYS;
1150 }
1151 break;
fd17d7ce 1152 case LTTNG_UST_ABI_COUNTER:
ebabbf58
MD
1153 {
1154 void *counter_data;
1155
1156 len = ustcomm_recv_counter_from_sessiond(sock,
1157 &counter_data, lum->u.counter.len);
1158 switch (len) {
1159 case 0: /* orderly shutdown */
1160 ret = 0;
1161 goto error;
1162 default:
1163 if (len == lum->u.counter.len) {
1164 DBG("counter data received");
1165 break;
1166 } else if (len < 0) {
1167 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1168 if (len == -ECONNRESET) {
1169 ERR("%s remote end closed connection", sock_info->name);
1170 ret = len;
1171 goto error;
1172 }
1173 ret = len;
1174 goto error;
1175 } else {
1176 DBG("incorrect counter data message size: %zd", len);
1177 ret = -EINVAL;
1178 goto error;
1179 }
1180 }
1181 args.counter.counter_data = counter_data;
1182 if (ops->cmd)
1183 ret = ops->cmd(lum->handle, lum->cmd,
1184 (unsigned long) &lum->u,
1185 &args, sock_info);
1186 else
1187 ret = -ENOSYS;
d61ad9ef 1188 free(args.counter.counter_data);
ebabbf58
MD
1189 break;
1190 }
fd17d7ce 1191 case LTTNG_UST_ABI_COUNTER_GLOBAL:
ebabbf58
MD
1192 {
1193 /* Receive shm_fd */
1194 ret = ustcomm_recv_counter_shm_from_sessiond(sock,
1195 &args.counter_shm.shm_fd);
1196 if (ret) {
1197 goto error;
1198 }
1199
1200 if (ops->cmd)
1201 ret = ops->cmd(lum->handle, lum->cmd,
1202 (unsigned long) &lum->u,
1203 &args, sock_info);
1204 else
1205 ret = -ENOSYS;
d61ad9ef
MD
1206 if (args.counter_shm.shm_fd >= 0) {
1207 int close_ret;
1208
1209 lttng_ust_lock_fd_tracker();
1210 close_ret = close(args.counter_shm.shm_fd);
1211 lttng_ust_unlock_fd_tracker();
1212 args.counter_shm.shm_fd = -1;
1213 if (close_ret)
1214 PERROR("close");
1215 }
ebabbf58
MD
1216 break;
1217 }
fd17d7ce 1218 case LTTNG_UST_ABI_COUNTER_CPU:
ebabbf58
MD
1219 {
1220 /* Receive shm_fd */
1221 ret = ustcomm_recv_counter_shm_from_sessiond(sock,
1222 &args.counter_shm.shm_fd);
1223 if (ret) {
1224 goto error;
1225 }
1226
1227 if (ops->cmd)
1228 ret = ops->cmd(lum->handle, lum->cmd,
1229 (unsigned long) &lum->u,
1230 &args, sock_info);
1231 else
1232 ret = -ENOSYS;
d61ad9ef
MD
1233 if (args.counter_shm.shm_fd >= 0) {
1234 int close_ret;
1235
1236 lttng_ust_lock_fd_tracker();
1237 close_ret = close(args.counter_shm.shm_fd);
1238 lttng_ust_unlock_fd_tracker();
1239 args.counter_shm.shm_fd = -1;
1240 if (close_ret)
1241 PERROR("close");
1242 }
ebabbf58
MD
1243 break;
1244 }
fd17d7ce 1245 case LTTNG_UST_ABI_EVENT_NOTIFIER_CREATE:
8406222c
MD
1246 {
1247 /* Receive struct lttng_ust_event_notifier */
fd17d7ce 1248 struct lttng_ust_abi_event_notifier event_notifier;
8406222c
MD
1249
1250 if (sizeof(event_notifier) != lum->u.event_notifier.len) {
1251 DBG("incorrect event notifier data message size: %u", lum->u.event_notifier.len);
1252 ret = -EINVAL;
1253 goto error;
1254 }
1255 len = ustcomm_recv_unix_sock(sock, &event_notifier, sizeof(event_notifier));
1256 switch (len) {
1257 case 0: /* orderly shutdown */
1258 ret = 0;
1259 goto error;
1260 default:
1261 if (len == sizeof(event_notifier)) {
1262 DBG("event notifier data received");
1263 break;
1264 } else if (len < 0) {
1265 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
1266 if (len == -ECONNRESET) {
1267 ERR("%s remote end closed connection", sock_info->name);
1268 ret = len;
1269 goto error;
1270 }
1271 ret = len;
1272 goto error;
1273 } else {
1274 DBG("incorrect event notifier data message size: %zd", len);
1275 ret = -EINVAL;
1276 goto error;
1277 }
1278 }
1279 if (ops->cmd)
1280 ret = ops->cmd(lum->handle, lum->cmd,
1281 (unsigned long) &event_notifier,
1282 &args, sock_info);
1283 else
1284 ret = -ENOSYS;
1285 break;
1286 }
ebabbf58 1287
d9e99d10 1288 default:
46050b1a
MD
1289 if (ops->cmd)
1290 ret = ops->cmd(lum->handle, lum->cmd,
ef9ff354 1291 (unsigned long) &lum->u,
f59ed768 1292 &args, sock_info);
46050b1a
MD
1293 else
1294 ret = -ENOSYS;
1295 break;
d9e99d10 1296 }
46050b1a 1297
46050b1a
MD
1298 lur.handle = lum->handle;
1299 lur.cmd = lum->cmd;
1300 lur.ret_val = ret;
1301 if (ret >= 0) {
7bc53e94 1302 lur.ret_code = LTTNG_UST_OK;
46050b1a 1303 } else {
7bc53e94
MD
1304 /*
1305 * Use -LTTNG_UST_ERR as wildcard for UST internal
1306 * error that are not caused by the transport, except if
1307 * we already have a more precise error message to
1308 * report.
1309 */
64b2564e
DG
1310 if (ret > -LTTNG_UST_ERR) {
1311 /* Translate code to UST error. */
1312 switch (ret) {
1313 case -EEXIST:
1314 lur.ret_code = -LTTNG_UST_ERR_EXIST;
1315 break;
1316 case -EINVAL:
1317 lur.ret_code = -LTTNG_UST_ERR_INVAL;
1318 break;
1319 case -ENOENT:
1320 lur.ret_code = -LTTNG_UST_ERR_NOENT;
1321 break;
1322 case -EPERM:
1323 lur.ret_code = -LTTNG_UST_ERR_PERM;
1324 break;
1325 case -ENOSYS:
1326 lur.ret_code = -LTTNG_UST_ERR_NOSYS;
1327 break;
1328 default:
1329 lur.ret_code = -LTTNG_UST_ERR;
1330 break;
1331 }
1332 } else {
7bc53e94 1333 lur.ret_code = ret;
64b2564e 1334 }
46050b1a 1335 }
e6ea14c5
MD
1336 if (ret >= 0) {
1337 switch (lum->cmd) {
fd17d7ce 1338 case LTTNG_UST_ABI_TRACER_VERSION:
e6ea14c5
MD
1339 lur.u.version = lum->u.version;
1340 break;
fd17d7ce 1341 case LTTNG_UST_ABI_TRACEPOINT_LIST_GET:
e6ea14c5
MD
1342 memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint));
1343 break;
1344 }
381c0f1e 1345 }
74d81a6c 1346 DBG("Return value: %d", lur.ret_val);
4c62d8d1
MD
1347
1348 ust_unlock();
1349
1350 /*
1351 * Performed delayed statedump operations outside of the UST
1352 * lock. We need to take the dynamic loader lock before we take
1353 * the UST lock internally within handle_pending_statedump().
1354 */
1355 handle_pending_statedump(sock_info);
1356
1357 if (ust_lock()) {
1358 ret = -LTTNG_UST_ERR_EXITING;
1359 goto error;
1360 }
1361
46050b1a 1362 ret = send_reply(sock, &lur);
193183fb 1363 if (ret < 0) {
7bc53e94 1364 DBG("error sending reply");
193183fb
MD
1365 goto error;
1366 }
46050b1a 1367
40003310
MD
1368 /*
1369 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1370 * after the reply.
1371 */
7bc53e94 1372 if (lur.ret_code == LTTNG_UST_OK) {
40003310 1373 switch (lum->cmd) {
fd17d7ce 1374 case LTTNG_UST_ABI_TRACEPOINT_FIELD_LIST_GET:
40003310
MD
1375 len = ustcomm_send_unix_sock(sock,
1376 &args.field_list.entry,
1377 sizeof(args.field_list.entry));
7bc53e94
MD
1378 if (len < 0) {
1379 ret = len;
1380 goto error;
1381 }
40003310 1382 if (len != sizeof(args.field_list.entry)) {
7bc53e94 1383 ret = -EINVAL;
40003310
MD
1384 goto error;
1385 }
1386 }
1387 }
ef9ff354 1388
381c0f1e 1389error:
17dfb34b 1390 ust_unlock();
d9e99d10 1391
37dddb65 1392 return ret;
246be17e
PW
1393}
1394
46050b1a 1395static
efe0de09 1396void cleanup_sock_info(struct sock_info *sock_info, int exiting)
46050b1a
MD
1397{
1398 int ret;
1399
5b14aab3 1400 if (sock_info->root_handle != -1) {
fd17d7ce 1401 ret = lttng_ust_abi_objd_unref(sock_info->root_handle, 1);
5b14aab3
MD
1402 if (ret) {
1403 ERR("Error unref root handle");
1404 }
1405 sock_info->root_handle = -1;
1406 }
eb0e6022
GAPG
1407 sock_info->registration_done = 0;
1408 sock_info->initial_statedump_done = 0;
5b14aab3
MD
1409
1410 /*
1411 * wait_shm_mmap, socket and notify socket are used by listener
1412 * threads outside of the ust lock, so we cannot tear them down
1413 * ourselves, because we cannot join on these threads. Leave
1414 * responsibility of cleaning up these resources to the OS
1415 * process exit.
1416 */
1417 if (exiting)
1418 return;
1419
46050b1a 1420 if (sock_info->socket != -1) {
e6973a89 1421 ret = ustcomm_close_unix_sock(sock_info->socket);
46050b1a 1422 if (ret) {
32ce8569 1423 ERR("Error closing ust cmd socket");
46050b1a
MD
1424 }
1425 sock_info->socket = -1;
1426 }
32ce8569
MD
1427 if (sock_info->notify_socket != -1) {
1428 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1429 if (ret) {
1430 ERR("Error closing ust notify socket");
1431 }
1432 sock_info->notify_socket = -1;
1433 }
5b14aab3 1434 if (sock_info->wait_shm_mmap) {
172d6b68
MD
1435 long page_size;
1436
b72687b8 1437 page_size = LTTNG_UST_PAGE_SIZE;
2657d1ba
MD
1438 if (page_size <= 0) {
1439 if (!page_size) {
1440 errno = EINVAL;
1441 }
1442 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1443 } else {
172d6b68
MD
1444 ret = munmap(sock_info->wait_shm_mmap, page_size);
1445 if (ret) {
1446 ERR("Error unmapping wait shm");
1447 }
7fc90dca
MD
1448 }
1449 sock_info->wait_shm_mmap = NULL;
1450 }
1451}
1452
58d4b2a2 1453/*
33bbeb90
MD
1454 * Using fork to set umask in the child process (not multi-thread safe).
1455 * We deal with the shm_open vs ftruncate race (happening when the
1456 * sessiond owns the shm and does not let everybody modify it, to ensure
1457 * safety against shm_unlink) by simply letting the mmap fail and
1458 * retrying after a few seconds.
1459 * For global shm, everybody has rw access to it until the sessiond
1460 * starts.
58d4b2a2 1461 */
7fc90dca 1462static
58d4b2a2 1463int get_wait_shm(struct sock_info *sock_info, size_t mmap_size)
7fc90dca 1464{
7fc90dca 1465 int wait_shm_fd, ret;
58d4b2a2 1466 pid_t pid;
44e073f5 1467
58d4b2a2 1468 /*
33bbeb90 1469 * Try to open read-only.
58d4b2a2 1470 */
33bbeb90 1471 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2 1472 if (wait_shm_fd >= 0) {
7aa76730
MD
1473 int32_t tmp_read;
1474 ssize_t len;
1475 size_t bytes_read = 0;
1476
1477 /*
1478 * Try to read the fd. If unable to do so, try opening
1479 * it in write mode.
1480 */
1481 do {
1482 len = read(wait_shm_fd,
1483 &((char *) &tmp_read)[bytes_read],
1484 sizeof(tmp_read) - bytes_read);
1485 if (len > 0) {
1486 bytes_read += len;
1487 }
1488 } while ((len < 0 && errno == EINTR)
1489 || (len > 0 && bytes_read < sizeof(tmp_read)));
1490 if (bytes_read != sizeof(tmp_read)) {
1491 ret = close(wait_shm_fd);
1492 if (ret) {
1493 ERR("close wait_shm_fd");
1494 }
1495 goto open_write;
1496 }
58d4b2a2
MD
1497 goto end;
1498 } else if (wait_shm_fd < 0 && errno != ENOENT) {
1499 /*
33bbeb90
MD
1500 * Real-only open did not work, and it's not because the
1501 * entry was not present. It's a failure that prohibits
1502 * using shm.
58d4b2a2 1503 */
7fc90dca 1504 ERR("Error opening shm %s", sock_info->wait_shm_path);
58d4b2a2 1505 goto end;
7fc90dca 1506 }
7aa76730
MD
1507
1508open_write:
7fc90dca 1509 /*
7aa76730
MD
1510 * If the open failed because the file did not exist, or because
1511 * the file was not truncated yet, try creating it ourself.
7fc90dca 1512 */
8c90a710 1513 URCU_TLS(lttng_ust_nest_count)++;
58d4b2a2 1514 pid = fork();
8c90a710 1515 URCU_TLS(lttng_ust_nest_count)--;
58d4b2a2
MD
1516 if (pid > 0) {
1517 int status;
1518
1519 /*
1520 * Parent: wait for child to return, in which case the
1521 * shared memory map will have been created.
1522 */
1523 pid = wait(&status);
b7d3cb32 1524 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
58d4b2a2
MD
1525 wait_shm_fd = -1;
1526 goto end;
7fc90dca 1527 }
58d4b2a2
MD
1528 /*
1529 * Try to open read-only again after creation.
1530 */
33bbeb90 1531 wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0);
58d4b2a2
MD
1532 if (wait_shm_fd < 0) {
1533 /*
1534 * Real-only open did not work. It's a failure
1535 * that prohibits using shm.
1536 */
1537 ERR("Error opening shm %s", sock_info->wait_shm_path);
1538 goto end;
1539 }
1540 goto end;
1541 } else if (pid == 0) {
1542 int create_mode;
1543
1544 /* Child */
33bbeb90 1545 create_mode = S_IRUSR | S_IWUSR | S_IRGRP;
58d4b2a2 1546 if (sock_info->global)
33bbeb90 1547 create_mode |= S_IROTH | S_IWGRP | S_IWOTH;
58d4b2a2
MD
1548 /*
1549 * We're alone in a child process, so we can modify the
1550 * process-wide umask.
1551 */
33bbeb90 1552 umask(~create_mode);
58d4b2a2 1553 /*
33bbeb90
MD
1554 * Try creating shm (or get rw access).
1555 * We don't do an exclusive open, because we allow other
1556 * processes to create+ftruncate it concurrently.
58d4b2a2
MD
1557 */
1558 wait_shm_fd = shm_open(sock_info->wait_shm_path,
1559 O_RDWR | O_CREAT, create_mode);
1560 if (wait_shm_fd >= 0) {
1561 ret = ftruncate(wait_shm_fd, mmap_size);
1562 if (ret) {
1563 PERROR("ftruncate");
b0c1425d 1564 _exit(EXIT_FAILURE);
58d4b2a2 1565 }
b0c1425d 1566 _exit(EXIT_SUCCESS);
58d4b2a2 1567 }
33bbeb90
MD
1568 /*
1569 * For local shm, we need to have rw access to accept
1570 * opening it: this means the local sessiond will be
1571 * able to wake us up. For global shm, we open it even
1572 * if rw access is not granted, because the root.root
1573 * sessiond will be able to override all rights and wake
1574 * us up.
1575 */
1576 if (!sock_info->global && errno != EACCES) {
58d4b2a2 1577 ERR("Error opening shm %s", sock_info->wait_shm_path);
5d3bc5ed 1578 _exit(EXIT_FAILURE);
58d4b2a2
MD
1579 }
1580 /*
33bbeb90
MD
1581 * The shm exists, but we cannot open it RW. Report
1582 * success.
58d4b2a2 1583 */
5d3bc5ed 1584 _exit(EXIT_SUCCESS);
58d4b2a2
MD
1585 } else {
1586 return -1;
7fc90dca 1587 }
58d4b2a2 1588end:
33bbeb90
MD
1589 if (wait_shm_fd >= 0 && !sock_info->global) {
1590 struct stat statbuf;
1591
1592 /*
1593 * Ensure that our user is the owner of the shm file for
1594 * local shm. If we do not own the file, it means our
1595 * sessiond will not have access to wake us up (there is
1596 * probably a rogue process trying to fake our
1597 * sessiond). Fallback to polling method in this case.
1598 */
1599 ret = fstat(wait_shm_fd, &statbuf);
1600 if (ret) {
1601 PERROR("fstat");
1602 goto error_close;
1603 }
1604 if (statbuf.st_uid != getuid())
1605 goto error_close;
1606 }
58d4b2a2 1607 return wait_shm_fd;
33bbeb90
MD
1608
1609error_close:
1610 ret = close(wait_shm_fd);
1611 if (ret) {
1612 PERROR("Error closing fd");
1613 }
1614 return -1;
58d4b2a2
MD
1615}
1616
1617static
1618char *get_map_shm(struct sock_info *sock_info)
1619{
172d6b68 1620 long page_size;
58d4b2a2
MD
1621 int wait_shm_fd, ret;
1622 char *wait_shm_mmap;
1623
172d6b68 1624 page_size = sysconf(_SC_PAGE_SIZE);
2657d1ba
MD
1625 if (page_size <= 0) {
1626 if (!page_size) {
1627 errno = EINVAL;
1628 }
1629 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
172d6b68
MD
1630 goto error;
1631 }
1632
6548fca4 1633 lttng_ust_lock_fd_tracker();
172d6b68 1634 wait_shm_fd = get_wait_shm(sock_info, page_size);
58d4b2a2 1635 if (wait_shm_fd < 0) {
6548fca4 1636 lttng_ust_unlock_fd_tracker();
58d4b2a2 1637 goto error;
44e073f5 1638 }
f5c453e9
JR
1639
1640 ret = lttng_ust_add_fd_to_tracker(wait_shm_fd);
1641 if (ret < 0) {
1642 ret = close(wait_shm_fd);
1643 if (!ret) {
1644 PERROR("Error closing fd");
1645 }
1646 lttng_ust_unlock_fd_tracker();
1647 goto error;
1648 }
1649
1650 wait_shm_fd = ret;
6548fca4
MD
1651 lttng_ust_unlock_fd_tracker();
1652
172d6b68 1653 wait_shm_mmap = mmap(NULL, page_size, PROT_READ,
7fc90dca 1654 MAP_SHARED, wait_shm_fd, 0);
6548fca4 1655
7fc90dca 1656 /* close shm fd immediately after taking the mmap reference */
6548fca4 1657 lttng_ust_lock_fd_tracker();
7fc90dca 1658 ret = close(wait_shm_fd);
6548fca4
MD
1659 if (!ret) {
1660 lttng_ust_delete_fd_from_tracker(wait_shm_fd);
1661 } else {
33bbeb90
MD
1662 PERROR("Error closing fd");
1663 }
6548fca4
MD
1664 lttng_ust_unlock_fd_tracker();
1665
33bbeb90
MD
1666 if (wait_shm_mmap == MAP_FAILED) {
1667 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1668 goto error;
7fc90dca
MD
1669 }
1670 return wait_shm_mmap;
1671
1672error:
1673 return NULL;
1674}
1675
1676static
1677void wait_for_sessiond(struct sock_info *sock_info)
1678{
060577e3 1679 /* Use ust_lock to check if we should quit. */
3327ac33 1680 if (ust_lock()) {
7fc90dca
MD
1681 goto quit;
1682 }
37ed587a
MD
1683 if (wait_poll_fallback) {
1684 goto error;
1685 }
7fc90dca
MD
1686 ust_unlock();
1687
060577e3
JR
1688 assert(sock_info->wait_shm_mmap);
1689
7fc90dca 1690 DBG("Waiting for %s apps sessiond", sock_info->name);
80e2814b 1691 /* Wait for futex wakeup */
ee7fcec8
MD
1692 if (uatomic_read((int32_t *) sock_info->wait_shm_mmap))
1693 goto end_wait;
1694
10544ee8 1695 while (lttng_ust_futex_async((int32_t *) sock_info->wait_shm_mmap,
ee7fcec8
MD
1696 FUTEX_WAIT, 0, NULL, NULL, 0)) {
1697 switch (errno) {
1698 case EWOULDBLOCK:
1699 /* Value already changed. */
1700 goto end_wait;
1701 case EINTR:
1702 /* Retry if interrupted by signal. */
1703 break; /* Get out of switch. */
1704 case EFAULT:
1705 wait_poll_fallback = 1;
1706 DBG(
37ed587a
MD
1707"Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1708"do not support FUTEX_WAKE on read-only memory mappings correctly. "
1709"Please upgrade your kernel "
1710"(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1711"mainline). LTTng-UST will use polling mode fallback.");
c6e6343c 1712 if (lttng_ust_logging_debug_enabled())
ee7fcec8
MD
1713 PERROR("futex");
1714 goto end_wait;
80e2814b
MD
1715 }
1716 }
ee7fcec8 1717end_wait:
7fc90dca
MD
1718 return;
1719
1720quit:
1721 ust_unlock();
1722 return;
1723
1724error:
1725 ust_unlock();
7fc90dca 1726 return;
46050b1a
MD
1727}
1728
1ea11eab
MD
1729/*
1730 * This thread does not allocate any resource, except within
1731 * handle_message, within mutex protection. This mutex protects against
1732 * fork and exit.
98bf993f 1733 * The other moment it allocates resources is at socket connection, which
1ea11eab
MD
1734 * is also protected by the mutex.
1735 */
d9e99d10
MD
1736static
1737void *ust_listener_thread(void *arg)
1738{
1ea11eab 1739 struct sock_info *sock_info = arg;
f5c453e9 1740 int sock, ret, prev_connect_failed = 0, has_waited = 0, fd;
ff517991 1741 long timeout;
d9e99d10 1742
a9fd951a 1743 lttng_ust_alloc_tls();
01f0e40c
RB
1744 /*
1745 * If available, add '-ust' to the end of this thread's
1746 * process name
1747 */
1748 ret = lttng_ust_setustprocname();
1749 if (ret) {
1750 ERR("Unable to set UST process name");
1751 }
1752
9eb62b9c
MD
1753 /* Restart trying to connect to the session daemon */
1754restart:
c0eedf81
MD
1755 if (prev_connect_failed) {
1756 /* Wait for sessiond availability with pipe */
1757 wait_for_sessiond(sock_info);
1758 if (has_waited) {
1759 has_waited = 0;
1760 /*
1761 * Sleep for 5 seconds before retrying after a
1762 * sequence of failure / wait / failure. This
1763 * deals with a killed or broken session daemon.
1764 */
1765 sleep(5);
eacc4aa4
MD
1766 } else {
1767 has_waited = 1;
c0eedf81 1768 }
c0eedf81
MD
1769 prev_connect_failed = 0;
1770 }
9eb62b9c 1771
101dace0
JR
1772 if (ust_lock()) {
1773 goto quit;
1774 }
1775
1ea11eab 1776 if (sock_info->socket != -1) {
6548fca4 1777 /* FD tracker is updated by ustcomm_close_unix_sock() */
e6973a89 1778 ret = ustcomm_close_unix_sock(sock_info->socket);
1ea11eab 1779 if (ret) {
32ce8569
MD
1780 ERR("Error closing %s ust cmd socket",
1781 sock_info->name);
1ea11eab
MD
1782 }
1783 sock_info->socket = -1;
1784 }
32ce8569 1785 if (sock_info->notify_socket != -1) {
6548fca4 1786 /* FD tracker is updated by ustcomm_close_unix_sock() */
32ce8569
MD
1787 ret = ustcomm_close_unix_sock(sock_info->notify_socket);
1788 if (ret) {
1789 ERR("Error closing %s ust notify socket",
1790 sock_info->name);
1791 }
1792 sock_info->notify_socket = -1;
1793 }
46050b1a 1794
6548fca4 1795
321f2351
MD
1796 /*
1797 * Register. We need to perform both connect and sending
1798 * registration message before doing the next connect otherwise
1799 * we may reach unix socket connect queue max limits and block
1800 * on the 2nd connect while the session daemon is awaiting the
1801 * first connect registration message.
1802 */
1803 /* Connect cmd socket */
6548fca4 1804 lttng_ust_lock_fd_tracker();
451d66b2
MD
1805 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1806 get_connect_sock_timeout());
321f2351 1807 if (ret < 0) {
6548fca4 1808 lttng_ust_unlock_fd_tracker();
321f2351
MD
1809 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1810 prev_connect_failed = 1;
5b14aab3 1811
e3426ddc 1812 /*
321f2351
MD
1813 * If we cannot find the sessiond daemon, don't delay
1814 * constructor execution.
e3426ddc 1815 */
eb0e6022 1816 ret = handle_register_failed(sock_info);
321f2351
MD
1817 assert(!ret);
1818 ust_unlock();
1819 goto restart;
27fe9f21 1820 }
f5c453e9
JR
1821 fd = ret;
1822 ret = lttng_ust_add_fd_to_tracker(fd);
1823 if (ret < 0) {
1824 ret = close(fd);
1825 if (ret) {
1826 PERROR("close on sock_info->socket");
1827 }
1828 ret = -1;
1829 lttng_ust_unlock_fd_tracker();
1830 ust_unlock();
1831 goto quit;
1832 }
1833
321f2351 1834 sock_info->socket = ret;
f5c453e9 1835 lttng_ust_unlock_fd_tracker();
27fe9f21 1836
6548fca4
MD
1837 ust_unlock();
1838 /*
1839 * Unlock/relock ust lock because connect is blocking (with
1840 * timeout). Don't delay constructors on the ust lock for too
1841 * long.
1842 */
3327ac33 1843 if (ust_lock()) {
5b14aab3
MD
1844 goto quit;
1845 }
1846
46050b1a
MD
1847 /*
1848 * Create only one root handle per listener thread for the whole
f59ed768
MD
1849 * process lifetime, so we ensure we get ID which is statically
1850 * assigned to the root handle.
46050b1a
MD
1851 */
1852 if (sock_info->root_handle == -1) {
1853 ret = lttng_abi_create_root_handle();
a51070bb 1854 if (ret < 0) {
46050b1a 1855 ERR("Error creating root handle");
46050b1a
MD
1856 goto quit;
1857 }
1858 sock_info->root_handle = ret;
9eb62b9c 1859 }
1ea11eab 1860
249cffb5 1861 ret = register_to_sessiond(sock_info->socket, LTTNG_UST_CTL_SOCKET_CMD);
9eb62b9c 1862 if (ret < 0) {
32ce8569
MD
1863 ERR("Error registering to %s ust cmd socket",
1864 sock_info->name);
c0eedf81 1865 prev_connect_failed = 1;
11ff9c7d
MD
1866 /*
1867 * If we cannot register to the sessiond daemon, don't
1868 * delay constructor execution.
1869 */
eb0e6022 1870 ret = handle_register_failed(sock_info);
11ff9c7d 1871 assert(!ret);
17dfb34b 1872 ust_unlock();
9eb62b9c
MD
1873 goto restart;
1874 }
321f2351
MD
1875
1876 ust_unlock();
6548fca4
MD
1877 /*
1878 * Unlock/relock ust lock because connect is blocking (with
1879 * timeout). Don't delay constructors on the ust lock for too
1880 * long.
1881 */
1882 if (ust_lock()) {
1883 goto quit;
1884 }
321f2351
MD
1885
1886 /* Connect notify socket */
6548fca4 1887 lttng_ust_lock_fd_tracker();
451d66b2
MD
1888 ret = ustcomm_connect_unix_sock(sock_info->sock_path,
1889 get_connect_sock_timeout());
321f2351 1890 if (ret < 0) {
6548fca4 1891 lttng_ust_unlock_fd_tracker();
321f2351
MD
1892 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name);
1893 prev_connect_failed = 1;
1894
321f2351
MD
1895 /*
1896 * If we cannot find the sessiond daemon, don't delay
1897 * constructor execution.
1898 */
eb0e6022 1899 ret = handle_register_failed(sock_info);
321f2351
MD
1900 assert(!ret);
1901 ust_unlock();
1902 goto restart;
1903 }
f5c453e9
JR
1904
1905 fd = ret;
1906 ret = lttng_ust_add_fd_to_tracker(fd);
1907 if (ret < 0) {
1908 ret = close(fd);
1909 if (ret) {
1910 PERROR("close on sock_info->notify_socket");
1911 }
1912 ret = -1;
1913 lttng_ust_unlock_fd_tracker();
1914 ust_unlock();
1915 goto quit;
1916 }
1917
321f2351 1918 sock_info->notify_socket = ret;
f5c453e9 1919 lttng_ust_unlock_fd_tracker();
321f2351 1920
6548fca4
MD
1921 ust_unlock();
1922 /*
1923 * Unlock/relock ust lock because connect is blocking (with
1924 * timeout). Don't delay constructors on the ust lock for too
1925 * long.
1926 */
1927 if (ust_lock()) {
1928 goto quit;
1929 }
1930
321f2351
MD
1931 timeout = get_notify_sock_timeout();
1932 if (timeout >= 0) {
1933 /*
1934 * Give at least 10ms to sessiond to reply to
1935 * notifications.
1936 */
1937 if (timeout < 10)
1938 timeout = 10;
1939 ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket,
1940 timeout);
1941 if (ret < 0) {
1942 WARN("Error setting socket receive timeout");
1943 }
1944 ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket,
1945 timeout);
1946 if (ret < 0) {
1947 WARN("Error setting socket send timeout");
1948 }
1949 } else if (timeout < -1) {
1950 WARN("Unsupported timeout value %ld", timeout);
1951 }
1952
32ce8569 1953 ret = register_to_sessiond(sock_info->notify_socket,
249cffb5 1954 LTTNG_UST_CTL_SOCKET_NOTIFY);
32ce8569
MD
1955 if (ret < 0) {
1956 ERR("Error registering to %s ust notify socket",
1957 sock_info->name);
1958 prev_connect_failed = 1;
1959 /*
1960 * If we cannot register to the sessiond daemon, don't
1961 * delay constructor execution.
1962 */
eb0e6022 1963 ret = handle_register_failed(sock_info);
32ce8569
MD
1964 assert(!ret);
1965 ust_unlock();
1966 goto restart;
1967 }
1968 sock = sock_info->socket;
1969
17dfb34b 1970 ust_unlock();
46050b1a 1971
d9e99d10
MD
1972 for (;;) {
1973 ssize_t len;
57773204 1974 struct ustcomm_ust_msg lum;
d9e99d10 1975
57773204 1976 len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum));
d9e99d10
MD
1977 switch (len) {
1978 case 0: /* orderly shutdown */
7dd08bec 1979 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name);
3327ac33 1980 if (ust_lock()) {
d5e1fea6
MD
1981 goto quit;
1982 }
8236ba10
MD
1983 /*
1984 * Either sessiond has shutdown or refused us by closing the socket.
1985 * In either case, we don't want to delay construction execution,
1986 * and we need to wait before retry.
1987 */
1988 prev_connect_failed = 1;
1989 /*
1990 * If we cannot register to the sessiond daemon, don't
1991 * delay constructor execution.
1992 */
eb0e6022 1993 ret = handle_register_failed(sock_info);
8236ba10
MD
1994 assert(!ret);
1995 ust_unlock();
d9e99d10 1996 goto end;
e7723462 1997 case sizeof(lum):
74d81a6c 1998 print_cmd(lum.cmd, lum.handle);
11ff9c7d 1999 ret = handle_message(sock_info, sock, &lum);
7bc53e94 2000 if (ret) {
0dafcd63
MD
2001 ERR("Error handling message for %s socket",
2002 sock_info->name);
2003 /*
2004 * Close socket if protocol error is
2005 * detected.
2006 */
2007 goto end;
d9e99d10
MD
2008 }
2009 continue;
7bc53e94
MD
2010 default:
2011 if (len < 0) {
2012 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len);
2013 } else {
2014 DBG("incorrect message size (%s socket): %zd", sock_info->name, len);
2015 }
2016 if (len == -ECONNRESET) {
2017 DBG("%s remote end closed connection", sock_info->name);
d9e99d10
MD
2018 goto end;
2019 }
2020 goto end;
d9e99d10
MD
2021 }
2022
2023 }
2024end:
3327ac33 2025 if (ust_lock()) {
d5e1fea6
MD
2026 goto quit;
2027 }
f59ed768 2028 /* Cleanup socket handles before trying to reconnect */
fd17d7ce 2029 lttng_ust_abi_objd_table_owner_cleanup(sock_info);
f59ed768 2030 ust_unlock();
9eb62b9c 2031 goto restart; /* try to reconnect */
e33f3265 2032
1ea11eab 2033quit:
e33f3265 2034 ust_unlock();
3327ac33
MD
2035
2036 pthread_mutex_lock(&ust_exit_mutex);
2037 sock_info->thread_active = 0;
2038 pthread_mutex_unlock(&ust_exit_mutex);
d9e99d10
MD
2039 return NULL;
2040}
2041
2594a5b4
MD
2042/*
2043 * Weak symbol to call when the ust malloc wrapper is not loaded.
2044 */
2045__attribute__((weak))
fca97dfd 2046void lttng_ust_libc_wrapper_malloc_ctor(void)
2594a5b4
MD
2047{
2048}
2049
2691221a
MD
2050/*
2051 * sessiond monitoring thread: monitor presence of global and per-user
2052 * sessiond by polling the application common named pipe.
2053 */
465a0d04 2054static
fca97dfd 2055void lttng_ust_ctor(void)
465a0d04
MJ
2056 __attribute__((constructor));
2057static
fca97dfd 2058void lttng_ust_ctor(void)
2691221a 2059{
11ff9c7d 2060 struct timespec constructor_timeout;
ae6a58bf 2061 sigset_t sig_all_blocked, orig_parent_mask;
1879f67f 2062 pthread_attr_t thread_attr;
cf12a773 2063 int timeout_mode;
2691221a 2064 int ret;
b2292d85 2065 void *handle;
2691221a 2066
edaa1431
MD
2067 if (uatomic_xchg(&initialized, 1) == 1)
2068 return;
2069
eddd8d5d 2070 /*
a9fd951a 2071 * Fixup interdependency between TLS allocation mutex (which happens
eddd8d5d
MD
2072 * to be the dynamic linker mutex) and ust_lock, taken within
2073 * the ust lock.
2074 */
a9fd951a 2075 lttng_ust_alloc_tls();
eddd8d5d 2076
07b57e5e
MD
2077 lttng_ust_loaded = 1;
2078
b2292d85
FD
2079 /*
2080 * We need to ensure that the liblttng-ust library is not unloaded to avoid
2081 * the unloading of code used by the ust_listener_threads as we can not
2082 * reliably know when they exited. To do that, manually load
2083 * liblttng-ust.so to increment the dynamic loader's internal refcount for
2084 * this library so it never becomes zero, thus never gets unloaded from the
2085 * address space of the process. Since we are already running in the
3d3dc207 2086 * constructor of the LTTNG_UST_LIB_SONAME library, calling dlopen will
b2292d85
FD
2087 * simply increment the refcount and no additionnal work is needed by the
2088 * dynamic loader as the shared library is already loaded in the address
2089 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
2090 * unloading of the UST library if its refcount becomes zero (which should
2091 * never happen). Do the return value check but discard the handle at the
2092 * end of the function as it's not needed.
2093 */
3d3dc207 2094 handle = dlopen(LTTNG_UST_LIB_SONAME, RTLD_LAZY | RTLD_NODELETE);
b2292d85 2095 if (!handle) {
3d3dc207 2096 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SONAME);
b2292d85
FD
2097 }
2098
edaa1431
MD
2099 /*
2100 * We want precise control over the order in which we construct
2101 * our sub-libraries vs starting to receive commands from
2102 * sessiond (otherwise leading to errors when trying to create
2103 * sessiond before the init functions are completed).
2104 */
39313bf3
MJ
2105
2106 /*
2107 * Both the logging and getenv lazy-initialization uses getenv()
2108 * internally and thus needs to be explicitly initialized in
2109 * liblttng-ust before we start any threads as an unsuspecting normally
2110 * single threaded application using liblttng-ust could be using
2111 * setenv() which is not thread-safe.
2112 */
c6e6343c 2113 lttng_ust_logging_init();
39313bf3
MJ
2114 lttng_ust_getenv_init();
2115
fca97dfd
MJ
2116 /* Call the liblttng-ust-common constructor. */
2117 lttng_ust_common_ctor();
2118
25afb7ac 2119 lttng_ust_tp_init();
cf73e0fe 2120 lttng_ust_statedump_init();
14e0a135
MD
2121 lttng_ust_ring_buffer_clients_init();
2122 lttng_ust_counter_clients_init();
d58d1454 2123 lttng_perf_counter_init();
2594a5b4
MD
2124 /*
2125 * Invoke ust malloc wrapper init before starting other threads.
2126 */
fca97dfd 2127 lttng_ust_libc_wrapper_malloc_ctor();
2691221a 2128
ff517991 2129 timeout_mode = get_constructor_timeout(&constructor_timeout);
11ff9c7d 2130
b2c5f61a 2131 get_allow_blocking();
6f97f9c2 2132
95259bd0 2133 ret = sem_init(&constructor_wait, 0, 0);
8aadb54a
MD
2134 if (ret) {
2135 PERROR("sem_init");
2136 }
11ff9c7d 2137
060577e3
JR
2138 ret = setup_global_apps();
2139 if (ret) {
2140 assert(global_apps.allowed == 0);
2141 DBG("global apps setup returned %d", ret);
2142 }
2143
8d20bf54 2144 ret = setup_local_apps();
2691221a 2145 if (ret) {
060577e3 2146 assert(local_apps.allowed == 0);
9ec6895c 2147 DBG("local apps setup returned %d", ret);
2691221a 2148 }
ae6a58bf
WP
2149
2150 /* A new thread created by pthread_create inherits the signal mask
2151 * from the parent. To avoid any signal being received by the
2152 * listener thread, we block all signals temporarily in the parent,
2153 * while we create the listener thread.
2154 */
2155 sigfillset(&sig_all_blocked);
2156 ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask);
2157 if (ret) {
d94d802c 2158 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
2159 }
2160
1879f67f
MG
2161 ret = pthread_attr_init(&thread_attr);
2162 if (ret) {
2163 ERR("pthread_attr_init: %s", strerror(ret));
2164 }
2165 ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
2166 if (ret) {
2167 ERR("pthread_attr_setdetachstate: %s", strerror(ret));
2168 }
2169
060577e3
JR
2170 if (global_apps.allowed) {
2171 pthread_mutex_lock(&ust_exit_mutex);
2172 ret = pthread_create(&global_apps.ust_listener, &thread_attr,
2173 ust_listener_thread, &global_apps);
2174 if (ret) {
2175 ERR("pthread_create global: %s", strerror(ret));
2176 }
2177 global_apps.thread_active = 1;
2178 pthread_mutex_unlock(&ust_exit_mutex);
2179 } else {
2180 handle_register_done(&global_apps);
d94d802c 2181 }
e33f3265 2182
8d20bf54 2183 if (local_apps.allowed) {
c0bbbd5a 2184 pthread_mutex_lock(&ust_exit_mutex);
1879f67f 2185 ret = pthread_create(&local_apps.ust_listener, &thread_attr,
dde70ea0 2186 ust_listener_thread, &local_apps);
d94d802c
MD
2187 if (ret) {
2188 ERR("pthread_create local: %s", strerror(ret));
2189 }
e33f3265 2190 local_apps.thread_active = 1;
c0bbbd5a 2191 pthread_mutex_unlock(&ust_exit_mutex);
8d20bf54
MD
2192 } else {
2193 handle_register_done(&local_apps);
2194 }
1879f67f
MG
2195 ret = pthread_attr_destroy(&thread_attr);
2196 if (ret) {
2197 ERR("pthread_attr_destroy: %s", strerror(ret));
2198 }
8d20bf54 2199
ae6a58bf
WP
2200 /* Restore original signal mask in parent */
2201 ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL);
2202 if (ret) {
d94d802c 2203 ERR("pthread_sigmask: %s", strerror(ret));
ae6a58bf
WP
2204 }
2205
cf12a773
MD
2206 switch (timeout_mode) {
2207 case 1: /* timeout wait */
95259bd0
MD
2208 do {
2209 ret = sem_timedwait(&constructor_wait,
2210 &constructor_timeout);
2211 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
2212 if (ret < 0) {
2213 switch (errno) {
2214 case ETIMEDOUT:
2215 ERR("Timed out waiting for lttng-sessiond");
2216 break;
2217 case EINVAL:
2218 PERROR("sem_timedwait");
2219 break;
2220 default:
2221 ERR("Unexpected error \"%s\" returned by sem_timedwait",
2222 strerror(errno));
2223 }
cf12a773
MD
2224 }
2225 break;
7b766b16 2226 case -1:/* wait forever */
95259bd0
MD
2227 do {
2228 ret = sem_wait(&constructor_wait);
2229 } while (ret < 0 && errno == EINTR);
8aadb54a
MD
2230 if (ret < 0) {
2231 switch (errno) {
2232 case EINVAL:
2233 PERROR("sem_wait");
2234 break;
2235 default:
2236 ERR("Unexpected error \"%s\" returned by sem_wait",
2237 strerror(errno));
2238 }
2239 }
cf12a773 2240 break;
7b766b16 2241 case 0: /* no timeout */
cf12a773 2242 break;
11ff9c7d 2243 }
2691221a
MD
2244}
2245
17dfb34b
MD
2246static
2247void lttng_ust_cleanup(int exiting)
2248{
efe0de09 2249 cleanup_sock_info(&global_apps, exiting);
932cfadb 2250 cleanup_sock_info(&local_apps, exiting);
74f98bc9 2251 local_apps.allowed = 0;
060577e3 2252 global_apps.allowed = 0;
efe0de09
MD
2253 /*
2254 * The teardown in this function all affect data structures
2255 * accessed under the UST lock by the listener thread. This
2256 * lock, along with the lttng_ust_comm_should_quit flag, ensure
2257 * that none of these threads are accessing this data at this
2258 * point.
2259 */
17dfb34b 2260 lttng_ust_abi_exit();
fd17d7ce 2261 lttng_ust_abi_events_exit();
d58d1454 2262 lttng_perf_counter_exit();
14e0a135
MD
2263 lttng_ust_ring_buffer_clients_exit();
2264 lttng_ust_counter_clients_exit();
cf73e0fe 2265 lttng_ust_statedump_destroy();
25afb7ac 2266 lttng_ust_tp_exit();
17dfb34b
MD
2267 if (!exiting) {
2268 /* Reinitialize values for fork */
eb0e6022 2269 sem_count = sem_count_initial_value;
17dfb34b
MD
2270 lttng_ust_comm_should_quit = 0;
2271 initialized = 0;
2272 }
2273}
2274
c589eca2
MJ
2275static
2276void lttng_ust_exit(void)
2277 __attribute__((destructor));
2278static
2279void lttng_ust_exit(void)
2691221a
MD
2280{
2281 int ret;
2282
9eb62b9c
MD
2283 /*
2284 * Using pthread_cancel here because:
2285 * A) we don't want to hang application teardown.
2286 * B) the thread is not allocating any resource.
2287 */
1ea11eab
MD
2288
2289 /*
2290 * Require the communication thread to quit. Synchronize with
2291 * mutexes to ensure it is not in a mutex critical section when
2292 * pthread_cancel is later called.
2293 */
3327ac33 2294 ust_lock_nocheck();
1ea11eab 2295 lttng_ust_comm_should_quit = 1;
3327ac33 2296 ust_unlock();
1ea11eab 2297
3327ac33 2298 pthread_mutex_lock(&ust_exit_mutex);
f5f94532 2299 /* cancel threads */
e33f3265
MD
2300 if (global_apps.thread_active) {
2301 ret = pthread_cancel(global_apps.ust_listener);
2302 if (ret) {
2303 ERR("Error cancelling global ust listener thread: %s",
2304 strerror(ret));
2305 } else {
2306 global_apps.thread_active = 0;
2307 }
2691221a 2308 }
e33f3265 2309 if (local_apps.thread_active) {
8d20bf54
MD
2310 ret = pthread_cancel(local_apps.ust_listener);
2311 if (ret) {
d94d802c
MD
2312 ERR("Error cancelling local ust listener thread: %s",
2313 strerror(ret));
e33f3265
MD
2314 } else {
2315 local_apps.thread_active = 0;
8d20bf54 2316 }
8d20bf54 2317 }
3327ac33 2318 pthread_mutex_unlock(&ust_exit_mutex);
e33f3265 2319
efe0de09
MD
2320 /*
2321 * Do NOT join threads: use of sys_futex makes it impossible to
2322 * join the threads without using async-cancel, but async-cancel
2323 * is delivered by a signal, which could hit the target thread
2324 * anywhere in its code path, including while the ust_lock() is
2325 * held, causing a deadlock for the other thread. Let the OS
2326 * cleanup the threads if there are stalled in a syscall.
2327 */
17dfb34b 2328 lttng_ust_cleanup(1);
2691221a 2329}
e822f505 2330
735bef47
MJ
2331static
2332void ust_context_ns_reset(void)
2333{
2334 lttng_context_pid_ns_reset();
2335 lttng_context_cgroup_ns_reset();
2336 lttng_context_ipc_ns_reset();
2337 lttng_context_mnt_ns_reset();
2338 lttng_context_net_ns_reset();
2339 lttng_context_user_ns_reset();
cefef7a7 2340 lttng_context_time_ns_reset();
735bef47
MJ
2341 lttng_context_uts_ns_reset();
2342}
2343
fca2f191
MJ
2344static
2345void ust_context_vuids_reset(void)
2346{
2347 lttng_context_vuid_reset();
2348 lttng_context_veuid_reset();
2349 lttng_context_vsuid_reset();
2350}
2351
2352static
2353void ust_context_vgids_reset(void)
2354{
2355 lttng_context_vgid_reset();
2356 lttng_context_vegid_reset();
2357 lttng_context_vsgid_reset();
2358}
2359
e822f505
MD
2360/*
2361 * We exclude the worker threads across fork and clone (except
2362 * CLONE_VM), because these system calls only keep the forking thread
2363 * running in the child. Therefore, we don't want to call fork or clone
2364 * in the middle of an tracepoint or ust tracing state modification.
2365 * Holding this mutex protects these structures across fork and clone.
2366 */
d0c8f180 2367void lttng_ust_before_fork(sigset_t *save_sigset)
e822f505
MD
2368{
2369 /*
2370 * Disable signals. This is to avoid that the child intervenes
2371 * before it is properly setup for tracing. It is safer to
2372 * disable all signals, because then we know we are not breaking
2373 * anything by restoring the original mask.
2374 */
2375 sigset_t all_sigs;
2376 int ret;
2377
a9fd951a
MJ
2378 /* Allocate lttng-ust TLS. */
2379 lttng_ust_alloc_tls();
c362addf 2380
8c90a710 2381 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2382 return;
e822f505
MD
2383 /* Disable signals */
2384 sigfillset(&all_sigs);
b728d87e 2385 ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset);
e822f505
MD
2386 if (ret == -1) {
2387 PERROR("sigprocmask");
2388 }
458d678c
PW
2389
2390 pthread_mutex_lock(&ust_fork_mutex);
2391
3327ac33 2392 ust_lock_nocheck();
10544ee8 2393 lttng_ust_urcu_before_fork();
c1be081a 2394 lttng_ust_lock_fd_tracker();
20142124 2395 lttng_perf_lock();
e822f505
MD
2396}
2397
b728d87e 2398static void ust_after_fork_common(sigset_t *restore_sigset)
e822f505
MD
2399{
2400 int ret;
2401
17dfb34b 2402 DBG("process %d", getpid());
20142124 2403 lttng_perf_unlock();
c1be081a 2404 lttng_ust_unlock_fd_tracker();
17dfb34b 2405 ust_unlock();
458d678c
PW
2406
2407 pthread_mutex_unlock(&ust_fork_mutex);
2408
e822f505 2409 /* Restore signals */
23c8854a 2410 ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL);
e822f505
MD
2411 if (ret == -1) {
2412 PERROR("sigprocmask");
2413 }
2414}
2415
d0c8f180 2416void lttng_ust_after_fork_parent(sigset_t *restore_sigset)
e822f505 2417{
8c90a710 2418 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2419 return;
17dfb34b 2420 DBG("process %d", getpid());
10544ee8 2421 lttng_ust_urcu_after_fork_parent();
e822f505 2422 /* Release mutexes and reenable signals */
b728d87e 2423 ust_after_fork_common(restore_sigset);
e822f505
MD
2424}
2425
17dfb34b
MD
2426/*
2427 * After fork, in the child, we need to cleanup all the leftover state,
2428 * except the worker thread which already magically disappeared thanks
2429 * to the weird Linux fork semantics. After tyding up, we call
fca97dfd 2430 * lttng_ust_ctor() again to start over as a new PID.
17dfb34b
MD
2431 *
2432 * This is meant for forks() that have tracing in the child between the
2433 * fork and following exec call (if there is any).
2434 */
d0c8f180 2435void lttng_ust_after_fork_child(sigset_t *restore_sigset)
e822f505 2436{
8c90a710 2437 if (URCU_TLS(lttng_ust_nest_count))
e8508a49 2438 return;
06b16a0b 2439 lttng_context_vpid_reset();
8478887d 2440 lttng_context_vtid_reset();
0af0bdb2 2441 lttng_ust_context_procname_reset();
735bef47 2442 ust_context_ns_reset();
fca2f191
MJ
2443 ust_context_vuids_reset();
2444 ust_context_vgids_reset();
17dfb34b 2445 DBG("process %d", getpid());
e822f505 2446 /* Release urcu mutexes */
10544ee8 2447 lttng_ust_urcu_after_fork_child();
17dfb34b 2448 lttng_ust_cleanup(0);
e822f505 2449 /* Release mutexes and reenable signals */
b728d87e 2450 ust_after_fork_common(restore_sigset);
fca97dfd 2451 lttng_ust_ctor();
e822f505 2452}
95c25348 2453
d0c8f180 2454void lttng_ust_after_setns(void)
735bef47
MJ
2455{
2456 ust_context_ns_reset();
fca2f191
MJ
2457 ust_context_vuids_reset();
2458 ust_context_vgids_reset();
735bef47
MJ
2459}
2460
d0c8f180 2461void lttng_ust_after_unshare(void)
735bef47
MJ
2462{
2463 ust_context_ns_reset();
fca2f191
MJ
2464 ust_context_vuids_reset();
2465 ust_context_vgids_reset();
2466}
2467
d0c8f180 2468void lttng_ust_after_setuid(void)
fca2f191
MJ
2469{
2470 ust_context_vuids_reset();
2471}
2472
d0c8f180 2473void lttng_ust_after_seteuid(void)
fca2f191
MJ
2474{
2475 ust_context_vuids_reset();
2476}
2477
d0c8f180 2478void lttng_ust_after_setreuid(void)
fca2f191
MJ
2479{
2480 ust_context_vuids_reset();
2481}
2482
d0c8f180 2483void lttng_ust_after_setresuid(void)
fca2f191
MJ
2484{
2485 ust_context_vuids_reset();
2486}
2487
d0c8f180 2488void lttng_ust_after_setgid(void)
fca2f191
MJ
2489{
2490 ust_context_vgids_reset();
2491}
2492
d0c8f180 2493void lttng_ust_after_setegid(void)
fca2f191
MJ
2494{
2495 ust_context_vgids_reset();
2496}
2497
d0c8f180 2498void lttng_ust_after_setregid(void)
fca2f191
MJ
2499{
2500 ust_context_vgids_reset();
2501}
2502
d0c8f180 2503void lttng_ust_after_setresgid(void)
fca2f191
MJ
2504{
2505 ust_context_vgids_reset();
735bef47
MJ
2506}
2507
246be17e 2508void lttng_ust_sockinfo_session_enabled(void *owner)
95c25348
PW
2509{
2510 struct sock_info *sock_info = owner;
37dddb65 2511 sock_info->statedump_pending = 1;
95c25348 2512}
This page took 0.193402 seconds and 4 git commands to generate.