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