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