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