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