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