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