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