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