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