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