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