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