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