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