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