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