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