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