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