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