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