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