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