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