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(); | |
c216b58e | 421 | lttng_ust_fixup_perf_counter_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 | ||
2eab9f28 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 | { |
2eab9f28 | 467 | int ret = 0; |
2691221a | 468 | const char *home_dir; |
7fc90dca | 469 | uid_t uid; |
2691221a | 470 | |
2eab9f28 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); |
2eab9f28 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); | |
2eab9f28 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); | |
2eab9f28 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) { | |
49254388 | 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 | ||
2691221a | 582 | static |
32ce8569 | 583 | int register_to_sessiond(int socket, enum ustctl_socket_type type) |
2691221a | 584 | { |
32ce8569 MD |
585 | return ustcomm_send_reg_msg(socket, |
586 | type, | |
587 | CAA_BITS_PER_LONG, | |
588 | lttng_alignof(uint8_t) * CHAR_BIT, | |
589 | lttng_alignof(uint16_t) * CHAR_BIT, | |
590 | lttng_alignof(uint32_t) * CHAR_BIT, | |
591 | lttng_alignof(uint64_t) * CHAR_BIT, | |
592 | lttng_alignof(unsigned long) * CHAR_BIT); | |
2691221a MD |
593 | } |
594 | ||
d9e99d10 | 595 | static |
57773204 | 596 | int send_reply(int sock, struct ustcomm_ust_reply *lur) |
d9e99d10 | 597 | { |
9eb62b9c | 598 | ssize_t len; |
d3a492d1 | 599 | |
57773204 | 600 | len = ustcomm_send_unix_sock(sock, lur, sizeof(*lur)); |
d3a492d1 | 601 | switch (len) { |
a4be8962 | 602 | case sizeof(*lur): |
d3a492d1 MD |
603 | DBG("message successfully sent"); |
604 | return 0; | |
7bc53e94 MD |
605 | default: |
606 | if (len == -ECONNRESET) { | |
607 | DBG("remote end closed connection"); | |
d3a492d1 MD |
608 | return 0; |
609 | } | |
7bc53e94 MD |
610 | if (len < 0) |
611 | return len; | |
612 | DBG("incorrect message size: %zd", len); | |
613 | return -EINVAL; | |
d3a492d1 MD |
614 | } |
615 | } | |
616 | ||
617 | static | |
3f5339b0 | 618 | void decrement_sem_count(unsigned int count) |
11ff9c7d MD |
619 | { |
620 | int ret; | |
621 | ||
3f5339b0 GAPG |
622 | assert(uatomic_read(&sem_count) >= count); |
623 | ||
56cd7e2f | 624 | if (uatomic_read(&sem_count) <= 0) { |
3f5339b0 | 625 | return; |
56cd7e2f | 626 | } |
3f5339b0 GAPG |
627 | |
628 | ret = uatomic_add_return(&sem_count, -count); | |
95259bd0 MD |
629 | if (ret == 0) { |
630 | ret = sem_post(&constructor_wait); | |
631 | assert(!ret); | |
632 | } | |
3f5339b0 GAPG |
633 | } |
634 | ||
635 | static | |
636 | int handle_register_done(struct sock_info *sock_info) | |
637 | { | |
638 | if (sock_info->registration_done) | |
639 | return 0; | |
640 | sock_info->registration_done = 1; | |
641 | ||
642 | decrement_sem_count(1); | |
c41cdc2a MD |
643 | if (!sock_info->statedump_pending) { |
644 | sock_info->initial_statedump_done = 1; | |
645 | decrement_sem_count(1); | |
646 | } | |
3f5339b0 GAPG |
647 | |
648 | return 0; | |
649 | } | |
650 | ||
651 | static | |
652 | int handle_register_failed(struct sock_info *sock_info) | |
653 | { | |
654 | if (sock_info->registration_done) | |
655 | return 0; | |
656 | sock_info->registration_done = 1; | |
657 | sock_info->initial_statedump_done = 1; | |
658 | ||
659 | decrement_sem_count(2); | |
660 | ||
11ff9c7d MD |
661 | return 0; |
662 | } | |
663 | ||
37dddb65 MD |
664 | /* |
665 | * Only execute pending statedump after the constructor semaphore has | |
3f5339b0 GAPG |
666 | * been posted by the current listener thread. This means statedump will |
667 | * only be performed after the "registration done" command is received | |
668 | * from this thread's session daemon. | |
37dddb65 MD |
669 | * |
670 | * This ensures we don't run into deadlock issues with the dynamic | |
671 | * loader mutex, which is held while the constructor is called and | |
672 | * waiting on the constructor semaphore. All operations requiring this | |
673 | * dynamic loader lock need to be postponed using this mechanism. | |
3f5339b0 GAPG |
674 | * |
675 | * In a scenario with two session daemons connected to the application, | |
676 | * it is possible that the first listener thread which receives the | |
677 | * registration done command issues its statedump while the dynamic | |
678 | * loader lock is still held by the application constructor waiting on | |
679 | * the semaphore. It will however be allowed to proceed when the | |
680 | * second session daemon sends the registration done command to the | |
681 | * second listener thread. This situation therefore does not produce | |
682 | * a deadlock. | |
37dddb65 MD |
683 | */ |
684 | static | |
685 | void handle_pending_statedump(struct sock_info *sock_info) | |
686 | { | |
3f5339b0 | 687 | if (sock_info->registration_done && sock_info->statedump_pending) { |
37dddb65 | 688 | sock_info->statedump_pending = 0; |
2932a87f | 689 | pthread_mutex_lock(&ust_fork_mutex); |
37dddb65 | 690 | lttng_handle_pending_statedump(sock_info); |
458d678c | 691 | pthread_mutex_unlock(&ust_fork_mutex); |
3f5339b0 GAPG |
692 | |
693 | if (!sock_info->initial_statedump_done) { | |
694 | sock_info->initial_statedump_done = 1; | |
695 | decrement_sem_count(1); | |
696 | } | |
37dddb65 MD |
697 | } |
698 | } | |
699 | ||
11ff9c7d MD |
700 | static |
701 | int handle_message(struct sock_info *sock_info, | |
57773204 | 702 | int sock, struct ustcomm_ust_msg *lum) |
d3a492d1 | 703 | { |
1ea11eab | 704 | int ret = 0; |
b61ce3b2 | 705 | const struct lttng_ust_objd_ops *ops; |
57773204 | 706 | struct ustcomm_ust_reply lur; |
ef9ff354 | 707 | union ust_args args; |
8e696cfa | 708 | char ctxstr[LTTNG_UST_SYM_NAME_LEN]; /* App context string. */ |
40003310 | 709 | ssize_t len; |
1ea11eab | 710 | |
46050b1a MD |
711 | memset(&lur, 0, sizeof(lur)); |
712 | ||
3327ac33 | 713 | if (ust_lock()) { |
74d81a6c | 714 | ret = -LTTNG_UST_ERR_EXITING; |
0dafcd63 | 715 | goto error; |
1ea11eab | 716 | } |
9eb62b9c | 717 | |
46050b1a MD |
718 | ops = objd_ops(lum->handle); |
719 | if (!ops) { | |
720 | ret = -ENOENT; | |
0dafcd63 | 721 | goto error; |
1ea11eab | 722 | } |
46050b1a MD |
723 | |
724 | switch (lum->cmd) { | |
11ff9c7d MD |
725 | case LTTNG_UST_REGISTER_DONE: |
726 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
edaa1431 | 727 | ret = handle_register_done(sock_info); |
11ff9c7d MD |
728 | else |
729 | ret = -EINVAL; | |
730 | break; | |
46050b1a MD |
731 | case LTTNG_UST_RELEASE: |
732 | if (lum->handle == LTTNG_UST_ROOT_HANDLE) | |
733 | ret = -EPERM; | |
734 | else | |
1849ef7c | 735 | ret = lttng_ust_objd_unref(lum->handle, 1); |
d9e99d10 | 736 | break; |
2d78951a MD |
737 | case LTTNG_UST_FILTER: |
738 | { | |
739 | /* Receive filter data */ | |
f488575f | 740 | struct lttng_ust_filter_bytecode_node *bytecode; |
2d78951a | 741 | |
cd54f6d9 | 742 | if (lum->u.filter.data_size > FILTER_BYTECODE_MAX_LEN) { |
7bc53e94 | 743 | ERR("Filter data size is too large: %u bytes", |
2d78951a MD |
744 | lum->u.filter.data_size); |
745 | ret = -EINVAL; | |
746 | goto error; | |
747 | } | |
2734ca65 | 748 | |
885b1dfd | 749 | if (lum->u.filter.reloc_offset > lum->u.filter.data_size) { |
7bc53e94 | 750 | ERR("Filter reloc offset %u is not within data", |
2734ca65 CB |
751 | lum->u.filter.reloc_offset); |
752 | ret = -EINVAL; | |
753 | goto error; | |
754 | } | |
755 | ||
cd54f6d9 MD |
756 | bytecode = zmalloc(sizeof(*bytecode) + lum->u.filter.data_size); |
757 | if (!bytecode) { | |
758 | ret = -ENOMEM; | |
759 | goto error; | |
760 | } | |
f488575f | 761 | len = ustcomm_recv_unix_sock(sock, bytecode->bc.data, |
2d78951a MD |
762 | lum->u.filter.data_size); |
763 | switch (len) { | |
764 | case 0: /* orderly shutdown */ | |
765 | ret = 0; | |
cd54f6d9 | 766 | free(bytecode); |
2d78951a | 767 | goto error; |
2d78951a MD |
768 | default: |
769 | if (len == lum->u.filter.data_size) { | |
7bc53e94 | 770 | DBG("filter data received"); |
2d78951a | 771 | break; |
7bc53e94 MD |
772 | } else if (len < 0) { |
773 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
774 | if (len == -ECONNRESET) { | |
775 | ERR("%s remote end closed connection", sock_info->name); | |
776 | ret = len; | |
777 | free(bytecode); | |
778 | goto error; | |
779 | } | |
780 | ret = len; | |
eb8bf361 | 781 | free(bytecode); |
0dafcd63 | 782 | goto error; |
2d78951a | 783 | } else { |
7bc53e94 | 784 | DBG("incorrect filter data message size: %zd", len); |
2d78951a | 785 | ret = -EINVAL; |
cd54f6d9 | 786 | free(bytecode); |
0dafcd63 | 787 | goto error; |
2d78951a MD |
788 | } |
789 | } | |
f488575f MD |
790 | bytecode->bc.len = lum->u.filter.data_size; |
791 | bytecode->bc.reloc_offset = lum->u.filter.reloc_offset; | |
3f6fd224 | 792 | bytecode->bc.seqnum = lum->u.filter.seqnum; |
cd54f6d9 | 793 | if (ops->cmd) { |
2d78951a | 794 | ret = ops->cmd(lum->handle, lum->cmd, |
cd54f6d9 | 795 | (unsigned long) bytecode, |
f59ed768 | 796 | &args, sock_info); |
cd54f6d9 MD |
797 | if (ret) { |
798 | free(bytecode); | |
799 | } | |
800 | /* don't free bytecode if everything went fine. */ | |
801 | } else { | |
2d78951a | 802 | ret = -ENOSYS; |
cd54f6d9 MD |
803 | free(bytecode); |
804 | } | |
2d78951a MD |
805 | break; |
806 | } | |
86e36163 JI |
807 | case LTTNG_UST_EXCLUSION: |
808 | { | |
809 | /* Receive exclusion names */ | |
810 | struct lttng_ust_excluder_node *node; | |
811 | unsigned int count; | |
812 | ||
813 | count = lum->u.exclusion.count; | |
814 | if (count == 0) { | |
815 | /* There are no names to read */ | |
816 | ret = 0; | |
817 | goto error; | |
818 | } | |
819 | node = zmalloc(sizeof(*node) + | |
820 | count * LTTNG_UST_SYM_NAME_LEN); | |
821 | if (!node) { | |
822 | ret = -ENOMEM; | |
823 | goto error; | |
824 | } | |
825 | node->excluder.count = count; | |
826 | len = ustcomm_recv_unix_sock(sock, node->excluder.names, | |
827 | count * LTTNG_UST_SYM_NAME_LEN); | |
828 | switch (len) { | |
829 | case 0: /* orderly shutdown */ | |
830 | ret = 0; | |
831 | free(node); | |
832 | goto error; | |
833 | default: | |
834 | if (len == count * LTTNG_UST_SYM_NAME_LEN) { | |
835 | DBG("Exclusion data received"); | |
836 | break; | |
837 | } else if (len < 0) { | |
838 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
839 | if (len == -ECONNRESET) { | |
840 | ERR("%s remote end closed connection", sock_info->name); | |
841 | ret = len; | |
842 | free(node); | |
843 | goto error; | |
844 | } | |
845 | ret = len; | |
846 | free(node); | |
0dafcd63 | 847 | goto error; |
86e36163 JI |
848 | } else { |
849 | DBG("Incorrect exclusion data message size: %zd", len); | |
850 | ret = -EINVAL; | |
851 | free(node); | |
0dafcd63 | 852 | goto error; |
86e36163 JI |
853 | } |
854 | } | |
855 | if (ops->cmd) { | |
856 | ret = ops->cmd(lum->handle, lum->cmd, | |
857 | (unsigned long) node, | |
858 | &args, sock_info); | |
859 | if (ret) { | |
860 | free(node); | |
861 | } | |
862 | /* Don't free exclusion data if everything went fine. */ | |
863 | } else { | |
864 | ret = -ENOSYS; | |
865 | free(node); | |
866 | } | |
867 | break; | |
868 | } | |
74d81a6c MD |
869 | case LTTNG_UST_CHANNEL: |
870 | { | |
871 | void *chan_data; | |
ff0f5728 | 872 | int wakeup_fd; |
74d81a6c MD |
873 | |
874 | len = ustcomm_recv_channel_from_sessiond(sock, | |
ff0f5728 MD |
875 | &chan_data, lum->u.channel.len, |
876 | &wakeup_fd); | |
74d81a6c MD |
877 | switch (len) { |
878 | case 0: /* orderly shutdown */ | |
879 | ret = 0; | |
880 | goto error; | |
881 | default: | |
882 | if (len == lum->u.channel.len) { | |
883 | DBG("channel data received"); | |
884 | break; | |
885 | } else if (len < 0) { | |
886 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
887 | if (len == -ECONNRESET) { | |
888 | ERR("%s remote end closed connection", sock_info->name); | |
889 | ret = len; | |
890 | goto error; | |
891 | } | |
892 | ret = len; | |
0dafcd63 | 893 | goto error; |
74d81a6c MD |
894 | } else { |
895 | DBG("incorrect channel data message size: %zd", len); | |
896 | ret = -EINVAL; | |
0dafcd63 | 897 | goto error; |
74d81a6c MD |
898 | } |
899 | } | |
900 | args.channel.chan_data = chan_data; | |
ff0f5728 | 901 | args.channel.wakeup_fd = wakeup_fd; |
74d81a6c MD |
902 | if (ops->cmd) |
903 | ret = ops->cmd(lum->handle, lum->cmd, | |
904 | (unsigned long) &lum->u, | |
905 | &args, sock_info); | |
906 | else | |
907 | ret = -ENOSYS; | |
908 | break; | |
909 | } | |
910 | case LTTNG_UST_STREAM: | |
911 | { | |
912 | /* Receive shm_fd, wakeup_fd */ | |
913 | ret = ustcomm_recv_stream_from_sessiond(sock, | |
acafe8d8 | 914 | NULL, |
74d81a6c MD |
915 | &args.stream.shm_fd, |
916 | &args.stream.wakeup_fd); | |
917 | if (ret) { | |
0dafcd63 | 918 | goto error; |
74d81a6c | 919 | } |
61973368 | 920 | |
74d81a6c MD |
921 | if (ops->cmd) |
922 | ret = ops->cmd(lum->handle, lum->cmd, | |
923 | (unsigned long) &lum->u, | |
924 | &args, sock_info); | |
925 | else | |
926 | ret = -ENOSYS; | |
927 | break; | |
928 | } | |
8e696cfa MD |
929 | case LTTNG_UST_CONTEXT: |
930 | switch (lum->u.context.ctx) { | |
931 | case LTTNG_UST_CONTEXT_APP_CONTEXT: | |
932 | { | |
933 | char *p; | |
934 | size_t ctxlen, recvlen; | |
935 | ||
936 | ctxlen = strlen("$app.") + lum->u.context.u.app_ctx.provider_name_len - 1 | |
937 | + strlen(":") + lum->u.context.u.app_ctx.ctx_name_len; | |
938 | if (ctxlen >= LTTNG_UST_SYM_NAME_LEN) { | |
939 | ERR("Application context string length size is too large: %zu bytes", | |
940 | ctxlen); | |
941 | ret = -EINVAL; | |
942 | goto error; | |
943 | } | |
944 | strcpy(ctxstr, "$app."); | |
945 | p = &ctxstr[strlen("$app.")]; | |
946 | recvlen = ctxlen - strlen("$app."); | |
947 | len = ustcomm_recv_unix_sock(sock, p, recvlen); | |
948 | switch (len) { | |
949 | case 0: /* orderly shutdown */ | |
950 | ret = 0; | |
951 | goto error; | |
952 | default: | |
953 | if (len == recvlen) { | |
954 | DBG("app context data received"); | |
955 | break; | |
956 | } else if (len < 0) { | |
957 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
958 | if (len == -ECONNRESET) { | |
959 | ERR("%s remote end closed connection", sock_info->name); | |
960 | ret = len; | |
961 | goto error; | |
962 | } | |
963 | ret = len; | |
964 | goto error; | |
965 | } else { | |
966 | DBG("incorrect app context data message size: %zd", len); | |
967 | ret = -EINVAL; | |
968 | goto error; | |
969 | } | |
970 | } | |
971 | /* Put : between provider and ctxname. */ | |
972 | p[lum->u.context.u.app_ctx.provider_name_len - 1] = ':'; | |
973 | args.app_context.ctxname = ctxstr; | |
974 | break; | |
975 | } | |
976 | default: | |
977 | break; | |
978 | } | |
979 | if (ops->cmd) { | |
980 | ret = ops->cmd(lum->handle, lum->cmd, | |
981 | (unsigned long) &lum->u, | |
982 | &args, sock_info); | |
983 | } else { | |
984 | ret = -ENOSYS; | |
985 | } | |
986 | break; | |
d9e99d10 | 987 | default: |
46050b1a MD |
988 | if (ops->cmd) |
989 | ret = ops->cmd(lum->handle, lum->cmd, | |
ef9ff354 | 990 | (unsigned long) &lum->u, |
f59ed768 | 991 | &args, sock_info); |
46050b1a MD |
992 | else |
993 | ret = -ENOSYS; | |
994 | break; | |
d9e99d10 | 995 | } |
46050b1a | 996 | |
46050b1a MD |
997 | lur.handle = lum->handle; |
998 | lur.cmd = lum->cmd; | |
999 | lur.ret_val = ret; | |
1000 | if (ret >= 0) { | |
7bc53e94 | 1001 | lur.ret_code = LTTNG_UST_OK; |
46050b1a | 1002 | } else { |
7bc53e94 MD |
1003 | /* |
1004 | * Use -LTTNG_UST_ERR as wildcard for UST internal | |
1005 | * error that are not caused by the transport, except if | |
1006 | * we already have a more precise error message to | |
1007 | * report. | |
1008 | */ | |
64b2564e DG |
1009 | if (ret > -LTTNG_UST_ERR) { |
1010 | /* Translate code to UST error. */ | |
1011 | switch (ret) { | |
1012 | case -EEXIST: | |
1013 | lur.ret_code = -LTTNG_UST_ERR_EXIST; | |
1014 | break; | |
1015 | case -EINVAL: | |
1016 | lur.ret_code = -LTTNG_UST_ERR_INVAL; | |
1017 | break; | |
1018 | case -ENOENT: | |
1019 | lur.ret_code = -LTTNG_UST_ERR_NOENT; | |
1020 | break; | |
1021 | case -EPERM: | |
1022 | lur.ret_code = -LTTNG_UST_ERR_PERM; | |
1023 | break; | |
1024 | case -ENOSYS: | |
1025 | lur.ret_code = -LTTNG_UST_ERR_NOSYS; | |
1026 | break; | |
1027 | default: | |
1028 | lur.ret_code = -LTTNG_UST_ERR; | |
1029 | break; | |
1030 | } | |
1031 | } else { | |
7bc53e94 | 1032 | lur.ret_code = ret; |
64b2564e | 1033 | } |
46050b1a | 1034 | } |
e6ea14c5 MD |
1035 | if (ret >= 0) { |
1036 | switch (lum->cmd) { | |
e6ea14c5 MD |
1037 | case LTTNG_UST_TRACER_VERSION: |
1038 | lur.u.version = lum->u.version; | |
1039 | break; | |
1040 | case LTTNG_UST_TRACEPOINT_LIST_GET: | |
1041 | memcpy(&lur.u.tracepoint, &lum->u.tracepoint, sizeof(lur.u.tracepoint)); | |
1042 | break; | |
1043 | } | |
381c0f1e | 1044 | } |
74d81a6c | 1045 | DBG("Return value: %d", lur.ret_val); |
4c62d8d1 MD |
1046 | |
1047 | ust_unlock(); | |
1048 | ||
1049 | /* | |
1050 | * Performed delayed statedump operations outside of the UST | |
1051 | * lock. We need to take the dynamic loader lock before we take | |
1052 | * the UST lock internally within handle_pending_statedump(). | |
1053 | */ | |
1054 | handle_pending_statedump(sock_info); | |
1055 | ||
1056 | if (ust_lock()) { | |
1057 | ret = -LTTNG_UST_ERR_EXITING; | |
1058 | goto error; | |
1059 | } | |
1060 | ||
46050b1a | 1061 | ret = send_reply(sock, &lur); |
193183fb | 1062 | if (ret < 0) { |
7bc53e94 | 1063 | DBG("error sending reply"); |
193183fb MD |
1064 | goto error; |
1065 | } | |
46050b1a | 1066 | |
40003310 MD |
1067 | /* |
1068 | * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field | |
1069 | * after the reply. | |
1070 | */ | |
7bc53e94 | 1071 | if (lur.ret_code == LTTNG_UST_OK) { |
40003310 MD |
1072 | switch (lum->cmd) { |
1073 | case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET: | |
1074 | len = ustcomm_send_unix_sock(sock, | |
1075 | &args.field_list.entry, | |
1076 | sizeof(args.field_list.entry)); | |
7bc53e94 MD |
1077 | if (len < 0) { |
1078 | ret = len; | |
1079 | goto error; | |
1080 | } | |
40003310 | 1081 | if (len != sizeof(args.field_list.entry)) { |
7bc53e94 | 1082 | ret = -EINVAL; |
40003310 MD |
1083 | goto error; |
1084 | } | |
1085 | } | |
1086 | } | |
ef9ff354 | 1087 | |
381c0f1e | 1088 | error: |
17dfb34b | 1089 | ust_unlock(); |
d9e99d10 | 1090 | |
37dddb65 | 1091 | return ret; |
246be17e PW |
1092 | } |
1093 | ||
46050b1a | 1094 | static |
efe0de09 | 1095 | void cleanup_sock_info(struct sock_info *sock_info, int exiting) |
46050b1a MD |
1096 | { |
1097 | int ret; | |
1098 | ||
5b14aab3 MD |
1099 | if (sock_info->root_handle != -1) { |
1100 | ret = lttng_ust_objd_unref(sock_info->root_handle, 1); | |
1101 | if (ret) { | |
1102 | ERR("Error unref root handle"); | |
1103 | } | |
1104 | sock_info->root_handle = -1; | |
1105 | } | |
3f5339b0 GAPG |
1106 | sock_info->registration_done = 0; |
1107 | sock_info->initial_statedump_done = 0; | |
5b14aab3 MD |
1108 | |
1109 | /* | |
1110 | * wait_shm_mmap, socket and notify socket are used by listener | |
1111 | * threads outside of the ust lock, so we cannot tear them down | |
1112 | * ourselves, because we cannot join on these threads. Leave | |
1113 | * responsibility of cleaning up these resources to the OS | |
1114 | * process exit. | |
1115 | */ | |
1116 | if (exiting) | |
1117 | return; | |
1118 | ||
46050b1a | 1119 | if (sock_info->socket != -1) { |
e6973a89 | 1120 | ret = ustcomm_close_unix_sock(sock_info->socket); |
46050b1a | 1121 | if (ret) { |
32ce8569 | 1122 | ERR("Error closing ust cmd socket"); |
46050b1a MD |
1123 | } |
1124 | sock_info->socket = -1; | |
1125 | } | |
32ce8569 MD |
1126 | if (sock_info->notify_socket != -1) { |
1127 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); | |
1128 | if (ret) { | |
1129 | ERR("Error closing ust notify socket"); | |
1130 | } | |
1131 | sock_info->notify_socket = -1; | |
1132 | } | |
5b14aab3 | 1133 | if (sock_info->wait_shm_mmap) { |
172d6b68 MD |
1134 | long page_size; |
1135 | ||
1136 | page_size = sysconf(_SC_PAGE_SIZE); | |
2657d1ba MD |
1137 | if (page_size <= 0) { |
1138 | if (!page_size) { | |
1139 | errno = EINVAL; | |
1140 | } | |
1141 | PERROR("Error in sysconf(_SC_PAGE_SIZE)"); | |
1142 | } else { | |
172d6b68 MD |
1143 | ret = munmap(sock_info->wait_shm_mmap, page_size); |
1144 | if (ret) { | |
1145 | ERR("Error unmapping wait shm"); | |
1146 | } | |
7fc90dca MD |
1147 | } |
1148 | sock_info->wait_shm_mmap = NULL; | |
1149 | } | |
1150 | } | |
1151 | ||
58d4b2a2 | 1152 | /* |
33bbeb90 MD |
1153 | * Using fork to set umask in the child process (not multi-thread safe). |
1154 | * We deal with the shm_open vs ftruncate race (happening when the | |
1155 | * sessiond owns the shm and does not let everybody modify it, to ensure | |
1156 | * safety against shm_unlink) by simply letting the mmap fail and | |
1157 | * retrying after a few seconds. | |
1158 | * For global shm, everybody has rw access to it until the sessiond | |
1159 | * starts. | |
58d4b2a2 | 1160 | */ |
7fc90dca | 1161 | static |
58d4b2a2 | 1162 | int get_wait_shm(struct sock_info *sock_info, size_t mmap_size) |
7fc90dca | 1163 | { |
7fc90dca | 1164 | int wait_shm_fd, ret; |
58d4b2a2 | 1165 | pid_t pid; |
44e073f5 | 1166 | |
58d4b2a2 | 1167 | /* |
33bbeb90 | 1168 | * Try to open read-only. |
58d4b2a2 | 1169 | */ |
33bbeb90 | 1170 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 | 1171 | if (wait_shm_fd >= 0) { |
7aa76730 MD |
1172 | int32_t tmp_read; |
1173 | ssize_t len; | |
1174 | size_t bytes_read = 0; | |
1175 | ||
1176 | /* | |
1177 | * Try to read the fd. If unable to do so, try opening | |
1178 | * it in write mode. | |
1179 | */ | |
1180 | do { | |
1181 | len = read(wait_shm_fd, | |
1182 | &((char *) &tmp_read)[bytes_read], | |
1183 | sizeof(tmp_read) - bytes_read); | |
1184 | if (len > 0) { | |
1185 | bytes_read += len; | |
1186 | } | |
1187 | } while ((len < 0 && errno == EINTR) | |
1188 | || (len > 0 && bytes_read < sizeof(tmp_read))); | |
1189 | if (bytes_read != sizeof(tmp_read)) { | |
1190 | ret = close(wait_shm_fd); | |
1191 | if (ret) { | |
1192 | ERR("close wait_shm_fd"); | |
1193 | } | |
1194 | goto open_write; | |
1195 | } | |
58d4b2a2 MD |
1196 | goto end; |
1197 | } else if (wait_shm_fd < 0 && errno != ENOENT) { | |
1198 | /* | |
33bbeb90 MD |
1199 | * Real-only open did not work, and it's not because the |
1200 | * entry was not present. It's a failure that prohibits | |
1201 | * using shm. | |
58d4b2a2 | 1202 | */ |
7fc90dca | 1203 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
58d4b2a2 | 1204 | goto end; |
7fc90dca | 1205 | } |
7aa76730 MD |
1206 | |
1207 | open_write: | |
7fc90dca | 1208 | /* |
7aa76730 MD |
1209 | * If the open failed because the file did not exist, or because |
1210 | * the file was not truncated yet, try creating it ourself. | |
7fc90dca | 1211 | */ |
8c90a710 | 1212 | URCU_TLS(lttng_ust_nest_count)++; |
58d4b2a2 | 1213 | pid = fork(); |
8c90a710 | 1214 | URCU_TLS(lttng_ust_nest_count)--; |
58d4b2a2 MD |
1215 | if (pid > 0) { |
1216 | int status; | |
1217 | ||
1218 | /* | |
1219 | * Parent: wait for child to return, in which case the | |
1220 | * shared memory map will have been created. | |
1221 | */ | |
1222 | pid = wait(&status); | |
b7d3cb32 | 1223 | if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
58d4b2a2 MD |
1224 | wait_shm_fd = -1; |
1225 | goto end; | |
7fc90dca | 1226 | } |
58d4b2a2 MD |
1227 | /* |
1228 | * Try to open read-only again after creation. | |
1229 | */ | |
33bbeb90 | 1230 | wait_shm_fd = shm_open(sock_info->wait_shm_path, O_RDONLY, 0); |
58d4b2a2 MD |
1231 | if (wait_shm_fd < 0) { |
1232 | /* | |
1233 | * Real-only open did not work. It's a failure | |
1234 | * that prohibits using shm. | |
1235 | */ | |
1236 | ERR("Error opening shm %s", sock_info->wait_shm_path); | |
1237 | goto end; | |
1238 | } | |
1239 | goto end; | |
1240 | } else if (pid == 0) { | |
1241 | int create_mode; | |
1242 | ||
1243 | /* Child */ | |
33bbeb90 | 1244 | create_mode = S_IRUSR | S_IWUSR | S_IRGRP; |
58d4b2a2 | 1245 | if (sock_info->global) |
33bbeb90 | 1246 | create_mode |= S_IROTH | S_IWGRP | S_IWOTH; |
58d4b2a2 MD |
1247 | /* |
1248 | * We're alone in a child process, so we can modify the | |
1249 | * process-wide umask. | |
1250 | */ | |
33bbeb90 | 1251 | umask(~create_mode); |
58d4b2a2 | 1252 | /* |
33bbeb90 MD |
1253 | * Try creating shm (or get rw access). |
1254 | * We don't do an exclusive open, because we allow other | |
1255 | * processes to create+ftruncate it concurrently. | |
58d4b2a2 MD |
1256 | */ |
1257 | wait_shm_fd = shm_open(sock_info->wait_shm_path, | |
1258 | O_RDWR | O_CREAT, create_mode); | |
1259 | if (wait_shm_fd >= 0) { | |
1260 | ret = ftruncate(wait_shm_fd, mmap_size); | |
1261 | if (ret) { | |
1262 | PERROR("ftruncate"); | |
b0c1425d | 1263 | _exit(EXIT_FAILURE); |
58d4b2a2 | 1264 | } |
b0c1425d | 1265 | _exit(EXIT_SUCCESS); |
58d4b2a2 | 1266 | } |
33bbeb90 MD |
1267 | /* |
1268 | * For local shm, we need to have rw access to accept | |
1269 | * opening it: this means the local sessiond will be | |
1270 | * able to wake us up. For global shm, we open it even | |
1271 | * if rw access is not granted, because the root.root | |
1272 | * sessiond will be able to override all rights and wake | |
1273 | * us up. | |
1274 | */ | |
1275 | if (!sock_info->global && errno != EACCES) { | |
58d4b2a2 | 1276 | ERR("Error opening shm %s", sock_info->wait_shm_path); |
5d3bc5ed | 1277 | _exit(EXIT_FAILURE); |
58d4b2a2 MD |
1278 | } |
1279 | /* | |
33bbeb90 MD |
1280 | * The shm exists, but we cannot open it RW. Report |
1281 | * success. | |
58d4b2a2 | 1282 | */ |
5d3bc5ed | 1283 | _exit(EXIT_SUCCESS); |
58d4b2a2 MD |
1284 | } else { |
1285 | return -1; | |
7fc90dca | 1286 | } |
58d4b2a2 | 1287 | end: |
33bbeb90 MD |
1288 | if (wait_shm_fd >= 0 && !sock_info->global) { |
1289 | struct stat statbuf; | |
1290 | ||
1291 | /* | |
1292 | * Ensure that our user is the owner of the shm file for | |
1293 | * local shm. If we do not own the file, it means our | |
1294 | * sessiond will not have access to wake us up (there is | |
1295 | * probably a rogue process trying to fake our | |
1296 | * sessiond). Fallback to polling method in this case. | |
1297 | */ | |
1298 | ret = fstat(wait_shm_fd, &statbuf); | |
1299 | if (ret) { | |
1300 | PERROR("fstat"); | |
1301 | goto error_close; | |
1302 | } | |
1303 | if (statbuf.st_uid != getuid()) | |
1304 | goto error_close; | |
1305 | } | |
58d4b2a2 | 1306 | return wait_shm_fd; |
33bbeb90 MD |
1307 | |
1308 | error_close: | |
1309 | ret = close(wait_shm_fd); | |
1310 | if (ret) { | |
1311 | PERROR("Error closing fd"); | |
1312 | } | |
1313 | return -1; | |
58d4b2a2 MD |
1314 | } |
1315 | ||
1316 | static | |
1317 | char *get_map_shm(struct sock_info *sock_info) | |
1318 | { | |
172d6b68 | 1319 | long page_size; |
58d4b2a2 MD |
1320 | int wait_shm_fd, ret; |
1321 | char *wait_shm_mmap; | |
1322 | ||
172d6b68 | 1323 | page_size = sysconf(_SC_PAGE_SIZE); |
2657d1ba MD |
1324 | if (page_size <= 0) { |
1325 | if (!page_size) { | |
1326 | errno = EINVAL; | |
1327 | } | |
1328 | PERROR("Error in sysconf(_SC_PAGE_SIZE)"); | |
172d6b68 MD |
1329 | goto error; |
1330 | } | |
1331 | ||
6548fca4 | 1332 | lttng_ust_lock_fd_tracker(); |
172d6b68 | 1333 | wait_shm_fd = get_wait_shm(sock_info, page_size); |
58d4b2a2 | 1334 | if (wait_shm_fd < 0) { |
6548fca4 | 1335 | lttng_ust_unlock_fd_tracker(); |
58d4b2a2 | 1336 | goto error; |
44e073f5 | 1337 | } |
fc9a0ccb JR |
1338 | |
1339 | ret = lttng_ust_add_fd_to_tracker(wait_shm_fd); | |
1340 | if (ret < 0) { | |
1341 | ret = close(wait_shm_fd); | |
1342 | if (!ret) { | |
1343 | PERROR("Error closing fd"); | |
1344 | } | |
1345 | lttng_ust_unlock_fd_tracker(); | |
1346 | goto error; | |
1347 | } | |
1348 | ||
1349 | wait_shm_fd = ret; | |
6548fca4 MD |
1350 | lttng_ust_unlock_fd_tracker(); |
1351 | ||
172d6b68 | 1352 | wait_shm_mmap = mmap(NULL, page_size, PROT_READ, |
7fc90dca | 1353 | MAP_SHARED, wait_shm_fd, 0); |
6548fca4 | 1354 | |
7fc90dca | 1355 | /* close shm fd immediately after taking the mmap reference */ |
6548fca4 | 1356 | lttng_ust_lock_fd_tracker(); |
7fc90dca | 1357 | ret = close(wait_shm_fd); |
6548fca4 MD |
1358 | if (!ret) { |
1359 | lttng_ust_delete_fd_from_tracker(wait_shm_fd); | |
1360 | } else { | |
33bbeb90 MD |
1361 | PERROR("Error closing fd"); |
1362 | } | |
6548fca4 MD |
1363 | lttng_ust_unlock_fd_tracker(); |
1364 | ||
33bbeb90 MD |
1365 | if (wait_shm_mmap == MAP_FAILED) { |
1366 | DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode."); | |
1367 | goto error; | |
7fc90dca MD |
1368 | } |
1369 | return wait_shm_mmap; | |
1370 | ||
1371 | error: | |
1372 | return NULL; | |
1373 | } | |
1374 | ||
1375 | static | |
1376 | void wait_for_sessiond(struct sock_info *sock_info) | |
1377 | { | |
2eab9f28 | 1378 | /* Use ust_lock to check if we should quit. */ |
3327ac33 | 1379 | if (ust_lock()) { |
7fc90dca MD |
1380 | goto quit; |
1381 | } | |
37ed587a MD |
1382 | if (wait_poll_fallback) { |
1383 | goto error; | |
1384 | } | |
7fc90dca MD |
1385 | ust_unlock(); |
1386 | ||
2eab9f28 JR |
1387 | assert(sock_info->wait_shm_mmap); |
1388 | ||
7fc90dca | 1389 | DBG("Waiting for %s apps sessiond", sock_info->name); |
80e2814b | 1390 | /* Wait for futex wakeup */ |
ee7fcec8 MD |
1391 | if (uatomic_read((int32_t *) sock_info->wait_shm_mmap)) |
1392 | goto end_wait; | |
1393 | ||
1394 | while (futex_async((int32_t *) sock_info->wait_shm_mmap, | |
1395 | FUTEX_WAIT, 0, NULL, NULL, 0)) { | |
1396 | switch (errno) { | |
1397 | case EWOULDBLOCK: | |
1398 | /* Value already changed. */ | |
1399 | goto end_wait; | |
1400 | case EINTR: | |
1401 | /* Retry if interrupted by signal. */ | |
1402 | break; /* Get out of switch. */ | |
1403 | case EFAULT: | |
1404 | wait_poll_fallback = 1; | |
1405 | DBG( | |
37ed587a MD |
1406 | "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) " |
1407 | "do not support FUTEX_WAKE on read-only memory mappings correctly. " | |
1408 | "Please upgrade your kernel " | |
1409 | "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel " | |
1410 | "mainline). LTTng-UST will use polling mode fallback."); | |
ee7fcec8 MD |
1411 | if (ust_debug()) |
1412 | PERROR("futex"); | |
1413 | goto end_wait; | |
80e2814b MD |
1414 | } |
1415 | } | |
ee7fcec8 | 1416 | end_wait: |
7fc90dca MD |
1417 | return; |
1418 | ||
1419 | quit: | |
1420 | ust_unlock(); | |
1421 | return; | |
1422 | ||
1423 | error: | |
1424 | ust_unlock(); | |
7fc90dca | 1425 | return; |
46050b1a MD |
1426 | } |
1427 | ||
1ea11eab MD |
1428 | /* |
1429 | * This thread does not allocate any resource, except within | |
1430 | * handle_message, within mutex protection. This mutex protects against | |
1431 | * fork and exit. | |
98bf993f | 1432 | * The other moment it allocates resources is at socket connection, which |
1ea11eab MD |
1433 | * is also protected by the mutex. |
1434 | */ | |
d9e99d10 MD |
1435 | static |
1436 | void *ust_listener_thread(void *arg) | |
1437 | { | |
1ea11eab | 1438 | struct sock_info *sock_info = arg; |
fc9a0ccb | 1439 | int sock, ret, prev_connect_failed = 0, has_waited = 0, fd; |
ff517991 | 1440 | long timeout; |
d9e99d10 | 1441 | |
c362addf | 1442 | lttng_ust_fixup_tls(); |
01f0e40c RB |
1443 | /* |
1444 | * If available, add '-ust' to the end of this thread's | |
1445 | * process name | |
1446 | */ | |
1447 | ret = lttng_ust_setustprocname(); | |
1448 | if (ret) { | |
1449 | ERR("Unable to set UST process name"); | |
1450 | } | |
1451 | ||
9eb62b9c MD |
1452 | /* Restart trying to connect to the session daemon */ |
1453 | restart: | |
c0eedf81 MD |
1454 | if (prev_connect_failed) { |
1455 | /* Wait for sessiond availability with pipe */ | |
1456 | wait_for_sessiond(sock_info); | |
1457 | if (has_waited) { | |
1458 | has_waited = 0; | |
1459 | /* | |
1460 | * Sleep for 5 seconds before retrying after a | |
1461 | * sequence of failure / wait / failure. This | |
1462 | * deals with a killed or broken session daemon. | |
1463 | */ | |
1464 | sleep(5); | |
eacc4aa4 MD |
1465 | } else { |
1466 | has_waited = 1; | |
c0eedf81 | 1467 | } |
c0eedf81 MD |
1468 | prev_connect_failed = 0; |
1469 | } | |
9eb62b9c | 1470 | |
005aeac5 JR |
1471 | if (ust_lock()) { |
1472 | goto quit; | |
1473 | } | |
1474 | ||
1ea11eab | 1475 | if (sock_info->socket != -1) { |
6548fca4 | 1476 | /* FD tracker is updated by ustcomm_close_unix_sock() */ |
e6973a89 | 1477 | ret = ustcomm_close_unix_sock(sock_info->socket); |
1ea11eab | 1478 | if (ret) { |
32ce8569 MD |
1479 | ERR("Error closing %s ust cmd socket", |
1480 | sock_info->name); | |
1ea11eab MD |
1481 | } |
1482 | sock_info->socket = -1; | |
1483 | } | |
32ce8569 | 1484 | if (sock_info->notify_socket != -1) { |
6548fca4 | 1485 | /* FD tracker is updated by ustcomm_close_unix_sock() */ |
32ce8569 MD |
1486 | ret = ustcomm_close_unix_sock(sock_info->notify_socket); |
1487 | if (ret) { | |
1488 | ERR("Error closing %s ust notify socket", | |
1489 | sock_info->name); | |
1490 | } | |
1491 | sock_info->notify_socket = -1; | |
1492 | } | |
46050b1a | 1493 | |
6548fca4 | 1494 | |
321f2351 MD |
1495 | /* |
1496 | * Register. We need to perform both connect and sending | |
1497 | * registration message before doing the next connect otherwise | |
1498 | * we may reach unix socket connect queue max limits and block | |
1499 | * on the 2nd connect while the session daemon is awaiting the | |
1500 | * first connect registration message. | |
1501 | */ | |
1502 | /* Connect cmd socket */ | |
6548fca4 | 1503 | lttng_ust_lock_fd_tracker(); |
451d66b2 MD |
1504 | ret = ustcomm_connect_unix_sock(sock_info->sock_path, |
1505 | get_connect_sock_timeout()); | |
321f2351 | 1506 | if (ret < 0) { |
6548fca4 | 1507 | lttng_ust_unlock_fd_tracker(); |
321f2351 MD |
1508 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
1509 | prev_connect_failed = 1; | |
5b14aab3 | 1510 | |
e3426ddc | 1511 | /* |
321f2351 MD |
1512 | * If we cannot find the sessiond daemon, don't delay |
1513 | * constructor execution. | |
e3426ddc | 1514 | */ |
3f5339b0 | 1515 | ret = handle_register_failed(sock_info); |
321f2351 MD |
1516 | assert(!ret); |
1517 | ust_unlock(); | |
1518 | goto restart; | |
27fe9f21 | 1519 | } |
fc9a0ccb JR |
1520 | fd = ret; |
1521 | ret = lttng_ust_add_fd_to_tracker(fd); | |
1522 | if (ret < 0) { | |
1523 | ret = close(fd); | |
1524 | if (ret) { | |
1525 | PERROR("close on sock_info->socket"); | |
1526 | } | |
1527 | ret = -1; | |
1528 | lttng_ust_unlock_fd_tracker(); | |
1529 | ust_unlock(); | |
1530 | goto quit; | |
1531 | } | |
1532 | ||
321f2351 | 1533 | sock_info->socket = ret; |
fc9a0ccb | 1534 | lttng_ust_unlock_fd_tracker(); |
27fe9f21 | 1535 | |
6548fca4 MD |
1536 | ust_unlock(); |
1537 | /* | |
1538 | * Unlock/relock ust lock because connect is blocking (with | |
1539 | * timeout). Don't delay constructors on the ust lock for too | |
1540 | * long. | |
1541 | */ | |
3327ac33 | 1542 | if (ust_lock()) { |
5b14aab3 MD |
1543 | goto quit; |
1544 | } | |
1545 | ||
46050b1a MD |
1546 | /* |
1547 | * Create only one root handle per listener thread for the whole | |
f59ed768 MD |
1548 | * process lifetime, so we ensure we get ID which is statically |
1549 | * assigned to the root handle. | |
46050b1a MD |
1550 | */ |
1551 | if (sock_info->root_handle == -1) { | |
1552 | ret = lttng_abi_create_root_handle(); | |
a51070bb | 1553 | if (ret < 0) { |
46050b1a | 1554 | ERR("Error creating root handle"); |
46050b1a MD |
1555 | goto quit; |
1556 | } | |
1557 | sock_info->root_handle = ret; | |
9eb62b9c | 1558 | } |
1ea11eab | 1559 | |
32ce8569 | 1560 | ret = register_to_sessiond(sock_info->socket, USTCTL_SOCKET_CMD); |
9eb62b9c | 1561 | if (ret < 0) { |
32ce8569 MD |
1562 | ERR("Error registering to %s ust cmd socket", |
1563 | sock_info->name); | |
c0eedf81 | 1564 | prev_connect_failed = 1; |
11ff9c7d MD |
1565 | /* |
1566 | * If we cannot register to the sessiond daemon, don't | |
1567 | * delay constructor execution. | |
1568 | */ | |
3f5339b0 | 1569 | ret = handle_register_failed(sock_info); |
11ff9c7d | 1570 | assert(!ret); |
17dfb34b | 1571 | ust_unlock(); |
9eb62b9c MD |
1572 | goto restart; |
1573 | } | |
321f2351 MD |
1574 | |
1575 | ust_unlock(); | |
6548fca4 MD |
1576 | /* |
1577 | * Unlock/relock ust lock because connect is blocking (with | |
1578 | * timeout). Don't delay constructors on the ust lock for too | |
1579 | * long. | |
1580 | */ | |
1581 | if (ust_lock()) { | |
1582 | goto quit; | |
1583 | } | |
321f2351 MD |
1584 | |
1585 | /* Connect notify socket */ | |
6548fca4 | 1586 | lttng_ust_lock_fd_tracker(); |
451d66b2 MD |
1587 | ret = ustcomm_connect_unix_sock(sock_info->sock_path, |
1588 | get_connect_sock_timeout()); | |
321f2351 | 1589 | if (ret < 0) { |
6548fca4 | 1590 | lttng_ust_unlock_fd_tracker(); |
321f2351 MD |
1591 | DBG("Info: sessiond not accepting connections to %s apps socket", sock_info->name); |
1592 | prev_connect_failed = 1; | |
1593 | ||
321f2351 MD |
1594 | /* |
1595 | * If we cannot find the sessiond daemon, don't delay | |
1596 | * constructor execution. | |
1597 | */ | |
3f5339b0 | 1598 | ret = handle_register_failed(sock_info); |
321f2351 MD |
1599 | assert(!ret); |
1600 | ust_unlock(); | |
1601 | goto restart; | |
1602 | } | |
fc9a0ccb JR |
1603 | |
1604 | fd = ret; | |
1605 | ret = lttng_ust_add_fd_to_tracker(fd); | |
1606 | if (ret < 0) { | |
1607 | ret = close(fd); | |
1608 | if (ret) { | |
1609 | PERROR("close on sock_info->notify_socket"); | |
1610 | } | |
1611 | ret = -1; | |
1612 | lttng_ust_unlock_fd_tracker(); | |
1613 | ust_unlock(); | |
1614 | goto quit; | |
1615 | } | |
1616 | ||
321f2351 | 1617 | sock_info->notify_socket = ret; |
fc9a0ccb | 1618 | lttng_ust_unlock_fd_tracker(); |
321f2351 | 1619 | |
6548fca4 MD |
1620 | ust_unlock(); |
1621 | /* | |
1622 | * Unlock/relock ust lock because connect is blocking (with | |
1623 | * timeout). Don't delay constructors on the ust lock for too | |
1624 | * long. | |
1625 | */ | |
1626 | if (ust_lock()) { | |
1627 | goto quit; | |
1628 | } | |
1629 | ||
321f2351 MD |
1630 | timeout = get_notify_sock_timeout(); |
1631 | if (timeout >= 0) { | |
1632 | /* | |
1633 | * Give at least 10ms to sessiond to reply to | |
1634 | * notifications. | |
1635 | */ | |
1636 | if (timeout < 10) | |
1637 | timeout = 10; | |
1638 | ret = ustcomm_setsockopt_rcv_timeout(sock_info->notify_socket, | |
1639 | timeout); | |
1640 | if (ret < 0) { | |
1641 | WARN("Error setting socket receive timeout"); | |
1642 | } | |
1643 | ret = ustcomm_setsockopt_snd_timeout(sock_info->notify_socket, | |
1644 | timeout); | |
1645 | if (ret < 0) { | |
1646 | WARN("Error setting socket send timeout"); | |
1647 | } | |
1648 | } else if (timeout < -1) { | |
1649 | WARN("Unsupported timeout value %ld", timeout); | |
1650 | } | |
1651 | ||
32ce8569 MD |
1652 | ret = register_to_sessiond(sock_info->notify_socket, |
1653 | USTCTL_SOCKET_NOTIFY); | |
1654 | if (ret < 0) { | |
1655 | ERR("Error registering to %s ust notify socket", | |
1656 | sock_info->name); | |
1657 | prev_connect_failed = 1; | |
1658 | /* | |
1659 | * If we cannot register to the sessiond daemon, don't | |
1660 | * delay constructor execution. | |
1661 | */ | |
3f5339b0 | 1662 | ret = handle_register_failed(sock_info); |
32ce8569 MD |
1663 | assert(!ret); |
1664 | ust_unlock(); | |
1665 | goto restart; | |
1666 | } | |
1667 | sock = sock_info->socket; | |
1668 | ||
17dfb34b | 1669 | ust_unlock(); |
46050b1a | 1670 | |
d9e99d10 MD |
1671 | for (;;) { |
1672 | ssize_t len; | |
57773204 | 1673 | struct ustcomm_ust_msg lum; |
d9e99d10 | 1674 | |
57773204 | 1675 | len = ustcomm_recv_unix_sock(sock, &lum, sizeof(lum)); |
d9e99d10 MD |
1676 | switch (len) { |
1677 | case 0: /* orderly shutdown */ | |
7dd08bec | 1678 | DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info->name); |
3327ac33 | 1679 | if (ust_lock()) { |
d5e1fea6 MD |
1680 | goto quit; |
1681 | } | |
8236ba10 MD |
1682 | /* |
1683 | * Either sessiond has shutdown or refused us by closing the socket. | |
1684 | * In either case, we don't want to delay construction execution, | |
1685 | * and we need to wait before retry. | |
1686 | */ | |
1687 | prev_connect_failed = 1; | |
1688 | /* | |
1689 | * If we cannot register to the sessiond daemon, don't | |
1690 | * delay constructor execution. | |
1691 | */ | |
3f5339b0 | 1692 | ret = handle_register_failed(sock_info); |
8236ba10 MD |
1693 | assert(!ret); |
1694 | ust_unlock(); | |
d9e99d10 | 1695 | goto end; |
e7723462 | 1696 | case sizeof(lum): |
74d81a6c | 1697 | print_cmd(lum.cmd, lum.handle); |
11ff9c7d | 1698 | ret = handle_message(sock_info, sock, &lum); |
7bc53e94 | 1699 | if (ret) { |
0dafcd63 MD |
1700 | ERR("Error handling message for %s socket", |
1701 | sock_info->name); | |
1702 | /* | |
1703 | * Close socket if protocol error is | |
1704 | * detected. | |
1705 | */ | |
1706 | goto end; | |
d9e99d10 MD |
1707 | } |
1708 | continue; | |
7bc53e94 MD |
1709 | default: |
1710 | if (len < 0) { | |
1711 | DBG("Receive failed from lttng-sessiond with errno %d", (int) -len); | |
1712 | } else { | |
1713 | DBG("incorrect message size (%s socket): %zd", sock_info->name, len); | |
1714 | } | |
1715 | if (len == -ECONNRESET) { | |
1716 | DBG("%s remote end closed connection", sock_info->name); | |
d9e99d10 MD |
1717 | goto end; |
1718 | } | |
1719 | goto end; | |
d9e99d10 MD |
1720 | } |
1721 | ||
1722 | } | |
1723 | end: | |
3327ac33 | 1724 | if (ust_lock()) { |
d5e1fea6 MD |
1725 | goto quit; |
1726 | } | |
f59ed768 MD |
1727 | /* Cleanup socket handles before trying to reconnect */ |
1728 | lttng_ust_objd_table_owner_cleanup(sock_info); | |
1729 | ust_unlock(); | |
9eb62b9c | 1730 | goto restart; /* try to reconnect */ |
e33f3265 | 1731 | |
1ea11eab | 1732 | quit: |
e33f3265 | 1733 | ust_unlock(); |
3327ac33 MD |
1734 | |
1735 | pthread_mutex_lock(&ust_exit_mutex); | |
1736 | sock_info->thread_active = 0; | |
1737 | pthread_mutex_unlock(&ust_exit_mutex); | |
d9e99d10 MD |
1738 | return NULL; |
1739 | } | |
1740 | ||
2594a5b4 MD |
1741 | /* |
1742 | * Weak symbol to call when the ust malloc wrapper is not loaded. | |
1743 | */ | |
1744 | __attribute__((weak)) | |
1745 | void lttng_ust_malloc_wrapper_init(void) | |
1746 | { | |
1747 | } | |
1748 | ||
2691221a MD |
1749 | /* |
1750 | * sessiond monitoring thread: monitor presence of global and per-user | |
1751 | * sessiond by polling the application common named pipe. | |
1752 | */ | |
edaa1431 | 1753 | void __attribute__((constructor)) lttng_ust_init(void) |
2691221a | 1754 | { |
11ff9c7d | 1755 | struct timespec constructor_timeout; |
ae6a58bf | 1756 | sigset_t sig_all_blocked, orig_parent_mask; |
1879f67f | 1757 | pthread_attr_t thread_attr; |
cf12a773 | 1758 | int timeout_mode; |
2691221a MD |
1759 | int ret; |
1760 | ||
edaa1431 MD |
1761 | if (uatomic_xchg(&initialized, 1) == 1) |
1762 | return; | |
1763 | ||
eddd8d5d MD |
1764 | /* |
1765 | * Fixup interdependency between TLS fixup mutex (which happens | |
1766 | * to be the dynamic linker mutex) and ust_lock, taken within | |
1767 | * the ust lock. | |
1768 | */ | |
c362addf | 1769 | lttng_ust_fixup_tls(); |
eddd8d5d | 1770 | |
07b57e5e MD |
1771 | lttng_ust_loaded = 1; |
1772 | ||
edaa1431 MD |
1773 | /* |
1774 | * We want precise control over the order in which we construct | |
1775 | * our sub-libraries vs starting to receive commands from | |
1776 | * sessiond (otherwise leading to errors when trying to create | |
1777 | * sessiond before the init functions are completed). | |
1778 | */ | |
2691221a | 1779 | init_usterr(); |
49254388 | 1780 | lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */ |
edaa1431 | 1781 | init_tracepoint(); |
6548fca4 | 1782 | lttng_ust_init_fd_tracker(); |
f9364363 | 1783 | lttng_ust_clock_init(); |
5e1b7b8b | 1784 | lttng_ust_getcpu_init(); |
cf73e0fe | 1785 | lttng_ust_statedump_init(); |
7dd08bec MD |
1786 | lttng_ring_buffer_metadata_client_init(); |
1787 | lttng_ring_buffer_client_overwrite_init(); | |
34a91bdb | 1788 | lttng_ring_buffer_client_overwrite_rt_init(); |
7dd08bec | 1789 | lttng_ring_buffer_client_discard_init(); |
34a91bdb | 1790 | lttng_ring_buffer_client_discard_rt_init(); |
d58d1454 | 1791 | lttng_perf_counter_init(); |
2594a5b4 MD |
1792 | /* |
1793 | * Invoke ust malloc wrapper init before starting other threads. | |
1794 | */ | |
1795 | lttng_ust_malloc_wrapper_init(); | |
2691221a | 1796 | |
ff517991 | 1797 | timeout_mode = get_constructor_timeout(&constructor_timeout); |
11ff9c7d | 1798 | |
95259bd0 | 1799 | ret = sem_init(&constructor_wait, 0, 0); |
8aadb54a MD |
1800 | if (ret) { |
1801 | PERROR("sem_init"); | |
1802 | } | |
11ff9c7d | 1803 | |
2eab9f28 JR |
1804 | ret = setup_global_apps(); |
1805 | if (ret) { | |
1806 | assert(global_apps.allowed == 0); | |
1807 | DBG("global apps setup returned %d", ret); | |
1808 | } | |
1809 | ||
8d20bf54 | 1810 | ret = setup_local_apps(); |
2691221a | 1811 | if (ret) { |
2eab9f28 | 1812 | assert(local_apps.allowed == 0); |
9ec6895c | 1813 | DBG("local apps setup returned %d", ret); |
2691221a | 1814 | } |
ae6a58bf WP |
1815 | |
1816 | /* A new thread created by pthread_create inherits the signal mask | |
1817 | * from the parent. To avoid any signal being received by the | |
1818 | * listener thread, we block all signals temporarily in the parent, | |
1819 | * while we create the listener thread. | |
1820 | */ | |
1821 | sigfillset(&sig_all_blocked); | |
1822 | ret = pthread_sigmask(SIG_SETMASK, &sig_all_blocked, &orig_parent_mask); | |
1823 | if (ret) { | |
d94d802c | 1824 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1825 | } |
1826 | ||
1879f67f MG |
1827 | ret = pthread_attr_init(&thread_attr); |
1828 | if (ret) { | |
1829 | ERR("pthread_attr_init: %s", strerror(ret)); | |
1830 | } | |
1831 | ret = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED); | |
1832 | if (ret) { | |
1833 | ERR("pthread_attr_setdetachstate: %s", strerror(ret)); | |
1834 | } | |
1835 | ||
2eab9f28 JR |
1836 | if (global_apps.allowed) { |
1837 | pthread_mutex_lock(&ust_exit_mutex); | |
1838 | ret = pthread_create(&global_apps.ust_listener, &thread_attr, | |
1839 | ust_listener_thread, &global_apps); | |
1840 | if (ret) { | |
1841 | ERR("pthread_create global: %s", strerror(ret)); | |
1842 | } | |
1843 | global_apps.thread_active = 1; | |
1844 | pthread_mutex_unlock(&ust_exit_mutex); | |
1845 | } else { | |
1846 | handle_register_done(&global_apps); | |
d94d802c | 1847 | } |
e33f3265 | 1848 | |
8d20bf54 | 1849 | if (local_apps.allowed) { |
c0bbbd5a | 1850 | pthread_mutex_lock(&ust_exit_mutex); |
1879f67f | 1851 | ret = pthread_create(&local_apps.ust_listener, &thread_attr, |
dde70ea0 | 1852 | ust_listener_thread, &local_apps); |
d94d802c MD |
1853 | if (ret) { |
1854 | ERR("pthread_create local: %s", strerror(ret)); | |
1855 | } | |
e33f3265 | 1856 | local_apps.thread_active = 1; |
c0bbbd5a | 1857 | pthread_mutex_unlock(&ust_exit_mutex); |
8d20bf54 MD |
1858 | } else { |
1859 | handle_register_done(&local_apps); | |
1860 | } | |
1879f67f MG |
1861 | ret = pthread_attr_destroy(&thread_attr); |
1862 | if (ret) { | |
1863 | ERR("pthread_attr_destroy: %s", strerror(ret)); | |
1864 | } | |
8d20bf54 | 1865 | |
ae6a58bf WP |
1866 | /* Restore original signal mask in parent */ |
1867 | ret = pthread_sigmask(SIG_SETMASK, &orig_parent_mask, NULL); | |
1868 | if (ret) { | |
d94d802c | 1869 | ERR("pthread_sigmask: %s", strerror(ret)); |
ae6a58bf WP |
1870 | } |
1871 | ||
cf12a773 MD |
1872 | switch (timeout_mode) { |
1873 | case 1: /* timeout wait */ | |
95259bd0 MD |
1874 | do { |
1875 | ret = sem_timedwait(&constructor_wait, | |
1876 | &constructor_timeout); | |
1877 | } while (ret < 0 && errno == EINTR); | |
8aadb54a MD |
1878 | if (ret < 0) { |
1879 | switch (errno) { | |
1880 | case ETIMEDOUT: | |
1881 | ERR("Timed out waiting for lttng-sessiond"); | |
1882 | break; | |
1883 | case EINVAL: | |
1884 | PERROR("sem_timedwait"); | |
1885 | break; | |
1886 | default: | |
1887 | ERR("Unexpected error \"%s\" returned by sem_timedwait", | |
1888 | strerror(errno)); | |
1889 | } | |
cf12a773 MD |
1890 | } |
1891 | break; | |
7b766b16 | 1892 | case -1:/* wait forever */ |
95259bd0 MD |
1893 | do { |
1894 | ret = sem_wait(&constructor_wait); | |
1895 | } while (ret < 0 && errno == EINTR); | |
8aadb54a MD |
1896 | if (ret < 0) { |
1897 | switch (errno) { | |
1898 | case EINVAL: | |
1899 | PERROR("sem_wait"); | |
1900 | break; | |
1901 | default: | |
1902 | ERR("Unexpected error \"%s\" returned by sem_wait", | |
1903 | strerror(errno)); | |
1904 | } | |
1905 | } | |
cf12a773 | 1906 | break; |
7b766b16 | 1907 | case 0: /* no timeout */ |
cf12a773 | 1908 | break; |
11ff9c7d | 1909 | } |
2691221a MD |
1910 | } |
1911 | ||
17dfb34b MD |
1912 | static |
1913 | void lttng_ust_cleanup(int exiting) | |
1914 | { | |
efe0de09 | 1915 | cleanup_sock_info(&global_apps, exiting); |
932cfadb | 1916 | cleanup_sock_info(&local_apps, exiting); |
74f98bc9 | 1917 | local_apps.allowed = 0; |
2eab9f28 | 1918 | global_apps.allowed = 0; |
efe0de09 MD |
1919 | /* |
1920 | * The teardown in this function all affect data structures | |
1921 | * accessed under the UST lock by the listener thread. This | |
1922 | * lock, along with the lttng_ust_comm_should_quit flag, ensure | |
1923 | * that none of these threads are accessing this data at this | |
1924 | * point. | |
1925 | */ | |
17dfb34b | 1926 | lttng_ust_abi_exit(); |
003fedf4 | 1927 | lttng_ust_events_exit(); |
d58d1454 | 1928 | lttng_perf_counter_exit(); |
34a91bdb | 1929 | lttng_ring_buffer_client_discard_rt_exit(); |
7dd08bec | 1930 | lttng_ring_buffer_client_discard_exit(); |
34a91bdb | 1931 | lttng_ring_buffer_client_overwrite_rt_exit(); |
7dd08bec MD |
1932 | lttng_ring_buffer_client_overwrite_exit(); |
1933 | lttng_ring_buffer_metadata_client_exit(); | |
cf73e0fe | 1934 | lttng_ust_statedump_destroy(); |
17dfb34b MD |
1935 | exit_tracepoint(); |
1936 | if (!exiting) { | |
1937 | /* Reinitialize values for fork */ | |
3f5339b0 | 1938 | sem_count = sem_count_initial_value; |
17dfb34b MD |
1939 | lttng_ust_comm_should_quit = 0; |
1940 | initialized = 0; | |
1941 | } | |
1942 | } | |
1943 | ||
edaa1431 | 1944 | void __attribute__((destructor)) lttng_ust_exit(void) |
2691221a MD |
1945 | { |
1946 | int ret; | |
1947 | ||
9eb62b9c MD |
1948 | /* |
1949 | * Using pthread_cancel here because: | |
1950 | * A) we don't want to hang application teardown. | |
1951 | * B) the thread is not allocating any resource. | |
1952 | */ | |
1ea11eab MD |
1953 | |
1954 | /* | |
1955 | * Require the communication thread to quit. Synchronize with | |
1956 | * mutexes to ensure it is not in a mutex critical section when | |
1957 | * pthread_cancel is later called. | |
1958 | */ | |
3327ac33 | 1959 | ust_lock_nocheck(); |
1ea11eab | 1960 | lttng_ust_comm_should_quit = 1; |
3327ac33 | 1961 | ust_unlock(); |
1ea11eab | 1962 | |
3327ac33 | 1963 | pthread_mutex_lock(&ust_exit_mutex); |
f5f94532 | 1964 | /* cancel threads */ |
e33f3265 MD |
1965 | if (global_apps.thread_active) { |
1966 | ret = pthread_cancel(global_apps.ust_listener); | |
1967 | if (ret) { | |
1968 | ERR("Error cancelling global ust listener thread: %s", | |
1969 | strerror(ret)); | |
1970 | } else { | |
1971 | global_apps.thread_active = 0; | |
1972 | } | |
2691221a | 1973 | } |
e33f3265 | 1974 | if (local_apps.thread_active) { |
8d20bf54 MD |
1975 | ret = pthread_cancel(local_apps.ust_listener); |
1976 | if (ret) { | |
d94d802c MD |
1977 | ERR("Error cancelling local ust listener thread: %s", |
1978 | strerror(ret)); | |
e33f3265 MD |
1979 | } else { |
1980 | local_apps.thread_active = 0; | |
8d20bf54 | 1981 | } |
8d20bf54 | 1982 | } |
3327ac33 | 1983 | pthread_mutex_unlock(&ust_exit_mutex); |
e33f3265 | 1984 | |
efe0de09 MD |
1985 | /* |
1986 | * Do NOT join threads: use of sys_futex makes it impossible to | |
1987 | * join the threads without using async-cancel, but async-cancel | |
1988 | * is delivered by a signal, which could hit the target thread | |
1989 | * anywhere in its code path, including while the ust_lock() is | |
1990 | * held, causing a deadlock for the other thread. Let the OS | |
1991 | * cleanup the threads if there are stalled in a syscall. | |
1992 | */ | |
17dfb34b | 1993 | lttng_ust_cleanup(1); |
2691221a | 1994 | } |
e822f505 MD |
1995 | |
1996 | /* | |
1997 | * We exclude the worker threads across fork and clone (except | |
1998 | * CLONE_VM), because these system calls only keep the forking thread | |
1999 | * running in the child. Therefore, we don't want to call fork or clone | |
2000 | * in the middle of an tracepoint or ust tracing state modification. | |
2001 | * Holding this mutex protects these structures across fork and clone. | |
2002 | */ | |
b728d87e | 2003 | void ust_before_fork(sigset_t *save_sigset) |
e822f505 MD |
2004 | { |
2005 | /* | |
2006 | * Disable signals. This is to avoid that the child intervenes | |
2007 | * before it is properly setup for tracing. It is safer to | |
2008 | * disable all signals, because then we know we are not breaking | |
2009 | * anything by restoring the original mask. | |
2010 | */ | |
2011 | sigset_t all_sigs; | |
2012 | int ret; | |
2013 | ||
c362addf MD |
2014 | /* Fixup lttng-ust TLS. */ |
2015 | lttng_ust_fixup_tls(); | |
2016 | ||
8c90a710 | 2017 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 2018 | return; |
e822f505 MD |
2019 | /* Disable signals */ |
2020 | sigfillset(&all_sigs); | |
b728d87e | 2021 | ret = sigprocmask(SIG_BLOCK, &all_sigs, save_sigset); |
e822f505 MD |
2022 | if (ret == -1) { |
2023 | PERROR("sigprocmask"); | |
2024 | } | |
458d678c PW |
2025 | |
2026 | pthread_mutex_lock(&ust_fork_mutex); | |
2027 | ||
3327ac33 | 2028 | ust_lock_nocheck(); |
e822f505 | 2029 | rcu_bp_before_fork(); |
57349053 | 2030 | lttng_ust_lock_fd_tracker(); |
c216b58e | 2031 | lttng_perf_lock(); |
e822f505 MD |
2032 | } |
2033 | ||
b728d87e | 2034 | static void ust_after_fork_common(sigset_t *restore_sigset) |
e822f505 MD |
2035 | { |
2036 | int ret; | |
2037 | ||
17dfb34b | 2038 | DBG("process %d", getpid()); |
c216b58e | 2039 | lttng_perf_unlock(); |
57349053 | 2040 | lttng_ust_unlock_fd_tracker(); |
17dfb34b | 2041 | ust_unlock(); |
458d678c PW |
2042 | |
2043 | pthread_mutex_unlock(&ust_fork_mutex); | |
2044 | ||
e822f505 | 2045 | /* Restore signals */ |
23c8854a | 2046 | ret = sigprocmask(SIG_SETMASK, restore_sigset, NULL); |
e822f505 MD |
2047 | if (ret == -1) { |
2048 | PERROR("sigprocmask"); | |
2049 | } | |
2050 | } | |
2051 | ||
b728d87e | 2052 | void ust_after_fork_parent(sigset_t *restore_sigset) |
e822f505 | 2053 | { |
8c90a710 | 2054 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 2055 | return; |
17dfb34b | 2056 | DBG("process %d", getpid()); |
e822f505 MD |
2057 | rcu_bp_after_fork_parent(); |
2058 | /* Release mutexes and reenable signals */ | |
b728d87e | 2059 | ust_after_fork_common(restore_sigset); |
e822f505 MD |
2060 | } |
2061 | ||
17dfb34b MD |
2062 | /* |
2063 | * After fork, in the child, we need to cleanup all the leftover state, | |
2064 | * except the worker thread which already magically disappeared thanks | |
2065 | * to the weird Linux fork semantics. After tyding up, we call | |
2066 | * lttng_ust_init() again to start over as a new PID. | |
2067 | * | |
2068 | * This is meant for forks() that have tracing in the child between the | |
2069 | * fork and following exec call (if there is any). | |
2070 | */ | |
b728d87e | 2071 | void ust_after_fork_child(sigset_t *restore_sigset) |
e822f505 | 2072 | { |
8c90a710 | 2073 | if (URCU_TLS(lttng_ust_nest_count)) |
e8508a49 | 2074 | return; |
77971c85 | 2075 | lttng_context_vpid_reset(); |
8478887d | 2076 | lttng_context_vtid_reset(); |
efe79472 | 2077 | lttng_context_procname_reset(); |
17dfb34b | 2078 | DBG("process %d", getpid()); |
e822f505 MD |
2079 | /* Release urcu mutexes */ |
2080 | rcu_bp_after_fork_child(); | |
17dfb34b | 2081 | lttng_ust_cleanup(0); |
e822f505 | 2082 | /* Release mutexes and reenable signals */ |
b728d87e | 2083 | ust_after_fork_common(restore_sigset); |
318dfea9 | 2084 | lttng_ust_init(); |
e822f505 | 2085 | } |
95c25348 | 2086 | |
246be17e | 2087 | void lttng_ust_sockinfo_session_enabled(void *owner) |
95c25348 PW |
2088 | { |
2089 | struct sock_info *sock_info = owner; | |
37dddb65 | 2090 | sock_info->statedump_pending = 1; |
95c25348 | 2091 | } |