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