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