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