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