605e91dc382f8893cc9f480c590e70afbdd1e998
[lttng-tools.git] / src / bin / lttng-sessiond / main.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2 only,
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #define _GNU_SOURCE
21 #include <getopt.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <paths.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <inttypes.h>
31 #include <sys/mman.h>
32 #include <sys/mount.h>
33 #include <sys/resource.h>
34 #include <sys/socket.h>
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <urcu/uatomic.h>
39 #include <unistd.h>
40 #include <config.h>
41
42 #include <common/common.h>
43 #include <common/compat/socket.h>
44 #include <common/defaults.h>
45 #include <common/kernel-consumer/kernel-consumer.h>
46 #include <common/futex.h>
47 #include <common/relayd/relayd.h>
48 #include <common/utils.h>
49 #include <common/daemonize.h>
50 #include <common/config/config.h>
51
52 #include "lttng-sessiond.h"
53 #include "buffer-registry.h"
54 #include "channel.h"
55 #include "cmd.h"
56 #include "consumer.h"
57 #include "context.h"
58 #include "event.h"
59 #include "kernel.h"
60 #include "kernel-consumer.h"
61 #include "modprobe.h"
62 #include "shm.h"
63 #include "ust-ctl.h"
64 #include "ust-consumer.h"
65 #include "utils.h"
66 #include "fd-limit.h"
67 #include "health-sessiond.h"
68 #include "testpoint.h"
69 #include "ust-thread.h"
70 #include "jul-thread.h"
71
72 #define CONSUMERD_FILE "lttng-consumerd"
73
74 const char *progname;
75 static const char *tracing_group_name = DEFAULT_TRACING_GROUP;
76 static int tracing_group_name_override;
77 static char *opt_pidfile;
78 static int opt_sig_parent;
79 static int opt_verbose_consumer;
80 static int opt_daemon, opt_background;
81 static int opt_no_kernel;
82 static pid_t ppid; /* Parent PID for --sig-parent option */
83 static pid_t child_ppid; /* Internal parent PID use with daemonize. */
84 static char *rundir;
85
86 /* Set to 1 when a SIGUSR1 signal is received. */
87 static int recv_child_signal;
88
89 /*
90 * Consumer daemon specific control data. Every value not initialized here is
91 * set to 0 by the static definition.
92 */
93 static struct consumer_data kconsumer_data = {
94 .type = LTTNG_CONSUMER_KERNEL,
95 .err_unix_sock_path = DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
96 .cmd_unix_sock_path = DEFAULT_KCONSUMERD_CMD_SOCK_PATH,
97 .err_sock = -1,
98 .cmd_sock = -1,
99 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
100 .lock = PTHREAD_MUTEX_INITIALIZER,
101 .cond = PTHREAD_COND_INITIALIZER,
102 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
103 };
104 static struct consumer_data ustconsumer64_data = {
105 .type = LTTNG_CONSUMER64_UST,
106 .err_unix_sock_path = DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
107 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH,
108 .err_sock = -1,
109 .cmd_sock = -1,
110 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
111 .lock = PTHREAD_MUTEX_INITIALIZER,
112 .cond = PTHREAD_COND_INITIALIZER,
113 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
114 };
115 static struct consumer_data ustconsumer32_data = {
116 .type = LTTNG_CONSUMER32_UST,
117 .err_unix_sock_path = DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
118 .cmd_unix_sock_path = DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH,
119 .err_sock = -1,
120 .cmd_sock = -1,
121 .pid_mutex = PTHREAD_MUTEX_INITIALIZER,
122 .lock = PTHREAD_MUTEX_INITIALIZER,
123 .cond = PTHREAD_COND_INITIALIZER,
124 .cond_mutex = PTHREAD_MUTEX_INITIALIZER,
125 };
126
127 /* Command line options */
128 static const struct option long_options[] = {
129 { "client-sock", 1, 0, 'c' },
130 { "apps-sock", 1, 0, 'a' },
131 { "kconsumerd-cmd-sock", 1, 0, 'C' },
132 { "kconsumerd-err-sock", 1, 0, 'E' },
133 { "ustconsumerd32-cmd-sock", 1, 0, 'G' },
134 { "ustconsumerd32-err-sock", 1, 0, 'H' },
135 { "ustconsumerd64-cmd-sock", 1, 0, 'D' },
136 { "ustconsumerd64-err-sock", 1, 0, 'F' },
137 { "consumerd32-path", 1, 0, 'u' },
138 { "consumerd32-libdir", 1, 0, 'U' },
139 { "consumerd64-path", 1, 0, 't' },
140 { "consumerd64-libdir", 1, 0, 'T' },
141 { "daemonize", 0, 0, 'd' },
142 { "background", 0, 0, 'b' },
143 { "sig-parent", 0, 0, 'S' },
144 { "help", 0, 0, 'h' },
145 { "group", 1, 0, 'g' },
146 { "version", 0, 0, 'V' },
147 { "quiet", 0, 0, 'q' },
148 { "verbose", 0, 0, 'v' },
149 { "verbose-consumer", 0, 0, 'Z' },
150 { "no-kernel", 0, 0, 'N' },
151 { "pidfile", 1, 0, 'p' },
152 { "jul-tcp-port", 1, 0, 'J' },
153 { "config", 1, 0, 'f' },
154 { NULL, 0, 0, 0 }
155 };
156
157 /* Command line options to ignore from configuration file */
158 static const char *config_ignore_options[] = { "help", "version", "config" };
159
160 /* Shared between threads */
161 static int dispatch_thread_exit;
162
163 /* Global application Unix socket path */
164 static char apps_unix_sock_path[PATH_MAX];
165 /* Global client Unix socket path */
166 static char client_unix_sock_path[PATH_MAX];
167 /* global wait shm path for UST */
168 static char wait_shm_path[PATH_MAX];
169 /* Global health check unix path */
170 static char health_unix_sock_path[PATH_MAX];
171
172 /* Sockets and FDs */
173 static int client_sock = -1;
174 static int apps_sock = -1;
175 int kernel_tracer_fd = -1;
176 static int kernel_poll_pipe[2] = { -1, -1 };
177
178 /*
179 * Quit pipe for all threads. This permits a single cancellation point
180 * for all threads when receiving an event on the pipe.
181 */
182 static int thread_quit_pipe[2] = { -1, -1 };
183
184 /*
185 * This pipe is used to inform the thread managing application communication
186 * that a command is queued and ready to be processed.
187 */
188 static int apps_cmd_pipe[2] = { -1, -1 };
189
190 int apps_cmd_notify_pipe[2] = { -1, -1 };
191
192 /* Pthread, Mutexes and Semaphores */
193 static pthread_t apps_thread;
194 static pthread_t apps_notify_thread;
195 static pthread_t reg_apps_thread;
196 static pthread_t client_thread;
197 static pthread_t kernel_thread;
198 static pthread_t dispatch_thread;
199 static pthread_t health_thread;
200 static pthread_t ht_cleanup_thread;
201 static pthread_t jul_reg_thread;
202
203 /*
204 * UST registration command queue. This queue is tied with a futex and uses a N
205 * wakers / 1 waiter implemented and detailed in futex.c/.h
206 *
207 * The thread_manage_apps and thread_dispatch_ust_registration interact with
208 * this queue and the wait/wake scheme.
209 */
210 static struct ust_cmd_queue ust_cmd_queue;
211
212 /*
213 * Pointer initialized before thread creation.
214 *
215 * This points to the tracing session list containing the session count and a
216 * mutex lock. The lock MUST be taken if you iterate over the list. The lock
217 * MUST NOT be taken if you call a public function in session.c.
218 *
219 * The lock is nested inside the structure: session_list_ptr->lock. Please use
220 * session_lock_list and session_unlock_list for lock acquisition.
221 */
222 static struct ltt_session_list *session_list_ptr;
223
224 int ust_consumerd64_fd = -1;
225 int ust_consumerd32_fd = -1;
226
227 static const char *consumerd32_bin = CONFIG_CONSUMERD32_BIN;
228 static const char *consumerd64_bin = CONFIG_CONSUMERD64_BIN;
229 static const char *consumerd32_libdir = CONFIG_CONSUMERD32_LIBDIR;
230 static const char *consumerd64_libdir = CONFIG_CONSUMERD64_LIBDIR;
231 static int consumerd32_bin_override;
232 static int consumerd64_bin_override;
233 static int consumerd32_libdir_override;
234 static int consumerd64_libdir_override;
235
236 static const char *module_proc_lttng = "/proc/lttng";
237
238 /*
239 * Consumer daemon state which is changed when spawning it, killing it or in
240 * case of a fatal error.
241 */
242 enum consumerd_state {
243 CONSUMER_STARTED = 1,
244 CONSUMER_STOPPED = 2,
245 CONSUMER_ERROR = 3,
246 };
247
248 /*
249 * This consumer daemon state is used to validate if a client command will be
250 * able to reach the consumer. If not, the client is informed. For instance,
251 * doing a "lttng start" when the consumer state is set to ERROR will return an
252 * error to the client.
253 *
254 * The following example shows a possible race condition of this scheme:
255 *
256 * consumer thread error happens
257 * client cmd arrives
258 * client cmd checks state -> still OK
259 * consumer thread exit, sets error
260 * client cmd try to talk to consumer
261 * ...
262 *
263 * However, since the consumer is a different daemon, we have no way of making
264 * sure the command will reach it safely even with this state flag. This is why
265 * we consider that up to the state validation during command processing, the
266 * command is safe. After that, we can not guarantee the correctness of the
267 * client request vis-a-vis the consumer.
268 */
269 static enum consumerd_state ust_consumerd_state;
270 static enum consumerd_state kernel_consumerd_state;
271
272 /*
273 * Socket timeout for receiving and sending in seconds.
274 */
275 static int app_socket_timeout;
276
277 /* Set in main() with the current page size. */
278 long page_size;
279
280 /* Application health monitoring */
281 struct health_app *health_sessiond;
282
283 /* JUL TCP port for registration. Used by the JUL thread. */
284 unsigned int jul_tcp_port = DEFAULT_JUL_TCP_PORT;
285
286 /* Am I root or not. */
287 int is_root; /* Set to 1 if the daemon is running as root */
288
289 const char * const config_section_name = "sessiond";
290
291 /*
292 * Whether sessiond is ready for commands/health check requests.
293 * NR_LTTNG_SESSIOND_READY must match the number of calls to
294 * lttng_sessiond_notify_ready().
295 */
296 #define NR_LTTNG_SESSIOND_READY 2
297 int lttng_sessiond_ready = NR_LTTNG_SESSIOND_READY;
298
299 /* Notify parents that we are ready for cmd and health check */
300 static
301 void lttng_sessiond_notify_ready(void)
302 {
303 if (uatomic_sub_return(&lttng_sessiond_ready, 1) == 0) {
304 /*
305 * Notify parent pid that we are ready to accept command
306 * for client side. This ppid is the one from the
307 * external process that spawned us.
308 */
309 if (opt_sig_parent) {
310 kill(ppid, SIGUSR1);
311 }
312
313 /*
314 * Notify the parent of the fork() process that we are
315 * ready.
316 */
317 if (opt_daemon || opt_background) {
318 kill(child_ppid, SIGUSR1);
319 }
320 }
321 }
322
323 static
324 void setup_consumerd_path(void)
325 {
326 const char *bin, *libdir;
327
328 /*
329 * Allow INSTALL_BIN_PATH to be used as a target path for the
330 * native architecture size consumer if CONFIG_CONSUMER*_PATH
331 * has not been defined.
332 */
333 #if (CAA_BITS_PER_LONG == 32)
334 if (!consumerd32_bin[0]) {
335 consumerd32_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
336 }
337 if (!consumerd32_libdir[0]) {
338 consumerd32_libdir = INSTALL_LIB_PATH;
339 }
340 #elif (CAA_BITS_PER_LONG == 64)
341 if (!consumerd64_bin[0]) {
342 consumerd64_bin = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
343 }
344 if (!consumerd64_libdir[0]) {
345 consumerd64_libdir = INSTALL_LIB_PATH;
346 }
347 #else
348 #error "Unknown bitness"
349 #endif
350
351 /*
352 * runtime env. var. overrides the build default.
353 */
354 bin = getenv("LTTNG_CONSUMERD32_BIN");
355 if (bin) {
356 consumerd32_bin = bin;
357 }
358 bin = getenv("LTTNG_CONSUMERD64_BIN");
359 if (bin) {
360 consumerd64_bin = bin;
361 }
362 libdir = getenv("LTTNG_CONSUMERD32_LIBDIR");
363 if (libdir) {
364 consumerd32_libdir = libdir;
365 }
366 libdir = getenv("LTTNG_CONSUMERD64_LIBDIR");
367 if (libdir) {
368 consumerd64_libdir = libdir;
369 }
370 }
371
372 /*
373 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
374 */
375 int sessiond_set_thread_pollset(struct lttng_poll_event *events, size_t size)
376 {
377 int ret;
378
379 assert(events);
380
381 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
382 if (ret < 0) {
383 goto error;
384 }
385
386 /* Add quit pipe */
387 ret = lttng_poll_add(events, thread_quit_pipe[0], LPOLLIN | LPOLLERR);
388 if (ret < 0) {
389 goto error;
390 }
391
392 return 0;
393
394 error:
395 return ret;
396 }
397
398 /*
399 * Check if the thread quit pipe was triggered.
400 *
401 * Return 1 if it was triggered else 0;
402 */
403 int sessiond_check_thread_quit_pipe(int fd, uint32_t events)
404 {
405 if (fd == thread_quit_pipe[0] && (events & LPOLLIN)) {
406 return 1;
407 }
408
409 return 0;
410 }
411
412 /*
413 * Init thread quit pipe.
414 *
415 * Return -1 on error or 0 if all pipes are created.
416 */
417 static int init_thread_quit_pipe(void)
418 {
419 int ret, i;
420
421 ret = pipe(thread_quit_pipe);
422 if (ret < 0) {
423 PERROR("thread quit pipe");
424 goto error;
425 }
426
427 for (i = 0; i < 2; i++) {
428 ret = fcntl(thread_quit_pipe[i], F_SETFD, FD_CLOEXEC);
429 if (ret < 0) {
430 PERROR("fcntl");
431 goto error;
432 }
433 }
434
435 error:
436 return ret;
437 }
438
439 /*
440 * Stop all threads by closing the thread quit pipe.
441 */
442 static void stop_threads(void)
443 {
444 int ret;
445
446 /* Stopping all threads */
447 DBG("Terminating all threads");
448 ret = notify_thread_pipe(thread_quit_pipe[1]);
449 if (ret < 0) {
450 ERR("write error on thread quit pipe");
451 }
452
453 /* Dispatch thread */
454 CMM_STORE_SHARED(dispatch_thread_exit, 1);
455 futex_nto1_wake(&ust_cmd_queue.futex);
456 }
457
458 /*
459 * Close every consumer sockets.
460 */
461 static void close_consumer_sockets(void)
462 {
463 int ret;
464
465 if (kconsumer_data.err_sock >= 0) {
466 ret = close(kconsumer_data.err_sock);
467 if (ret < 0) {
468 PERROR("kernel consumer err_sock close");
469 }
470 }
471 if (ustconsumer32_data.err_sock >= 0) {
472 ret = close(ustconsumer32_data.err_sock);
473 if (ret < 0) {
474 PERROR("UST consumerd32 err_sock close");
475 }
476 }
477 if (ustconsumer64_data.err_sock >= 0) {
478 ret = close(ustconsumer64_data.err_sock);
479 if (ret < 0) {
480 PERROR("UST consumerd64 err_sock close");
481 }
482 }
483 if (kconsumer_data.cmd_sock >= 0) {
484 ret = close(kconsumer_data.cmd_sock);
485 if (ret < 0) {
486 PERROR("kernel consumer cmd_sock close");
487 }
488 }
489 if (ustconsumer32_data.cmd_sock >= 0) {
490 ret = close(ustconsumer32_data.cmd_sock);
491 if (ret < 0) {
492 PERROR("UST consumerd32 cmd_sock close");
493 }
494 }
495 if (ustconsumer64_data.cmd_sock >= 0) {
496 ret = close(ustconsumer64_data.cmd_sock);
497 if (ret < 0) {
498 PERROR("UST consumerd64 cmd_sock close");
499 }
500 }
501 }
502
503 /*
504 * Cleanup the daemon
505 */
506 static void cleanup(void)
507 {
508 int ret;
509 struct ltt_session *sess, *stmp;
510 char path[PATH_MAX];
511
512 DBG("Cleaning up");
513
514 /*
515 * Close the thread quit pipe. It has already done its job,
516 * since we are now called.
517 */
518 utils_close_pipe(thread_quit_pipe);
519
520 /*
521 * If opt_pidfile is undefined, the default file will be wiped when
522 * removing the rundir.
523 */
524 if (opt_pidfile) {
525 ret = remove(opt_pidfile);
526 if (ret < 0) {
527 PERROR("remove pidfile %s", opt_pidfile);
528 }
529 }
530
531 DBG("Removing sessiond and consumerd content of directory %s", rundir);
532
533 /* sessiond */
534 snprintf(path, PATH_MAX,
535 "%s/%s",
536 rundir, DEFAULT_LTTNG_SESSIOND_PIDFILE);
537 DBG("Removing %s", path);
538 (void) unlink(path);
539
540 snprintf(path, PATH_MAX, "%s/%s", rundir,
541 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE);
542 DBG("Removing %s", path);
543 (void) unlink(path);
544
545 /* kconsumerd */
546 snprintf(path, PATH_MAX,
547 DEFAULT_KCONSUMERD_ERR_SOCK_PATH,
548 rundir);
549 DBG("Removing %s", path);
550 (void) unlink(path);
551
552 snprintf(path, PATH_MAX,
553 DEFAULT_KCONSUMERD_PATH,
554 rundir);
555 DBG("Removing directory %s", path);
556 (void) rmdir(path);
557
558 /* ust consumerd 32 */
559 snprintf(path, PATH_MAX,
560 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH,
561 rundir);
562 DBG("Removing %s", path);
563 (void) unlink(path);
564
565 snprintf(path, PATH_MAX,
566 DEFAULT_USTCONSUMERD32_PATH,
567 rundir);
568 DBG("Removing directory %s", path);
569 (void) rmdir(path);
570
571 /* ust consumerd 64 */
572 snprintf(path, PATH_MAX,
573 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH,
574 rundir);
575 DBG("Removing %s", path);
576 (void) unlink(path);
577
578 snprintf(path, PATH_MAX,
579 DEFAULT_USTCONSUMERD64_PATH,
580 rundir);
581 DBG("Removing directory %s", path);
582 (void) rmdir(path);
583
584 /*
585 * We do NOT rmdir rundir because there are other processes
586 * using it, for instance lttng-relayd, which can start in
587 * parallel with this teardown.
588 */
589
590 free(rundir);
591
592 DBG("Cleaning up all sessions");
593
594 /* Destroy session list mutex */
595 if (session_list_ptr != NULL) {
596 pthread_mutex_destroy(&session_list_ptr->lock);
597
598 /* Cleanup ALL session */
599 cds_list_for_each_entry_safe(sess, stmp,
600 &session_list_ptr->head, list) {
601 cmd_destroy_session(sess, kernel_poll_pipe[1]);
602 }
603 }
604
605 DBG("Closing all UST sockets");
606 ust_app_clean_list();
607 buffer_reg_destroy_registries();
608
609 if (is_root && !opt_no_kernel) {
610 DBG2("Closing kernel fd");
611 if (kernel_tracer_fd >= 0) {
612 ret = close(kernel_tracer_fd);
613 if (ret) {
614 PERROR("close");
615 }
616 }
617 DBG("Unloading kernel modules");
618 modprobe_remove_lttng_all();
619 }
620
621 close_consumer_sockets();
622
623 /*
624 * If the override option is set, the pointer points to a *non* const thus
625 * freeing it even though the variable type is set to const.
626 */
627 if (tracing_group_name_override) {
628 free((void *) tracing_group_name);
629 }
630 if (consumerd32_bin_override) {
631 free((void *) consumerd32_bin);
632 }
633 if (consumerd64_bin_override) {
634 free((void *) consumerd64_bin);
635 }
636 if (consumerd32_libdir_override) {
637 free((void *) consumerd32_libdir);
638 }
639 if (consumerd64_libdir_override) {
640 free((void *) consumerd64_libdir);
641 }
642
643 if (opt_pidfile) {
644 free(opt_pidfile);
645 }
646
647 /* <fun> */
648 DBG("%c[%d;%dm*** assert failed :-) *** ==> %c[%dm%c[%d;%dm"
649 "Matthew, BEET driven development works!%c[%dm",
650 27, 1, 31, 27, 0, 27, 1, 33, 27, 0);
651 /* </fun> */
652 }
653
654 /*
655 * Send data on a unix socket using the liblttsessiondcomm API.
656 *
657 * Return lttcomm error code.
658 */
659 static int send_unix_sock(int sock, void *buf, size_t len)
660 {
661 /* Check valid length */
662 if (len == 0) {
663 return -1;
664 }
665
666 return lttcomm_send_unix_sock(sock, buf, len);
667 }
668
669 /*
670 * Free memory of a command context structure.
671 */
672 static void clean_command_ctx(struct command_ctx **cmd_ctx)
673 {
674 DBG("Clean command context structure");
675 if (*cmd_ctx) {
676 if ((*cmd_ctx)->llm) {
677 free((*cmd_ctx)->llm);
678 }
679 if ((*cmd_ctx)->lsm) {
680 free((*cmd_ctx)->lsm);
681 }
682 free(*cmd_ctx);
683 *cmd_ctx = NULL;
684 }
685 }
686
687 /*
688 * Notify UST applications using the shm mmap futex.
689 */
690 static int notify_ust_apps(int active)
691 {
692 char *wait_shm_mmap;
693
694 DBG("Notifying applications of session daemon state: %d", active);
695
696 /* See shm.c for this call implying mmap, shm and futex calls */
697 wait_shm_mmap = shm_ust_get_mmap(wait_shm_path, is_root);
698 if (wait_shm_mmap == NULL) {
699 goto error;
700 }
701
702 /* Wake waiting process */
703 futex_wait_update((int32_t *) wait_shm_mmap, active);
704
705 /* Apps notified successfully */
706 return 0;
707
708 error:
709 return -1;
710 }
711
712 /*
713 * Setup the outgoing data buffer for the response (llm) by allocating the
714 * right amount of memory and copying the original information from the lsm
715 * structure.
716 *
717 * Return total size of the buffer pointed by buf.
718 */
719 static int setup_lttng_msg(struct command_ctx *cmd_ctx, size_t size)
720 {
721 int ret, buf_size;
722
723 buf_size = size;
724
725 cmd_ctx->llm = zmalloc(sizeof(struct lttcomm_lttng_msg) + buf_size);
726 if (cmd_ctx->llm == NULL) {
727 PERROR("zmalloc");
728 ret = -ENOMEM;
729 goto error;
730 }
731
732 /* Copy common data */
733 cmd_ctx->llm->cmd_type = cmd_ctx->lsm->cmd_type;
734 cmd_ctx->llm->pid = cmd_ctx->lsm->domain.attr.pid;
735
736 cmd_ctx->llm->data_size = size;
737 cmd_ctx->lttng_msg_size = sizeof(struct lttcomm_lttng_msg) + buf_size;
738
739 return buf_size;
740
741 error:
742 return ret;
743 }
744
745 /*
746 * Update the kernel poll set of all channel fd available over all tracing
747 * session. Add the wakeup pipe at the end of the set.
748 */
749 static int update_kernel_poll(struct lttng_poll_event *events)
750 {
751 int ret;
752 struct ltt_session *session;
753 struct ltt_kernel_channel *channel;
754
755 DBG("Updating kernel poll set");
756
757 session_lock_list();
758 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
759 session_lock(session);
760 if (session->kernel_session == NULL) {
761 session_unlock(session);
762 continue;
763 }
764
765 cds_list_for_each_entry(channel,
766 &session->kernel_session->channel_list.head, list) {
767 /* Add channel fd to the kernel poll set */
768 ret = lttng_poll_add(events, channel->fd, LPOLLIN | LPOLLRDNORM);
769 if (ret < 0) {
770 session_unlock(session);
771 goto error;
772 }
773 DBG("Channel fd %d added to kernel set", channel->fd);
774 }
775 session_unlock(session);
776 }
777 session_unlock_list();
778
779 return 0;
780
781 error:
782 session_unlock_list();
783 return -1;
784 }
785
786 /*
787 * Find the channel fd from 'fd' over all tracing session. When found, check
788 * for new channel stream and send those stream fds to the kernel consumer.
789 *
790 * Useful for CPU hotplug feature.
791 */
792 static int update_kernel_stream(struct consumer_data *consumer_data, int fd)
793 {
794 int ret = 0;
795 struct ltt_session *session;
796 struct ltt_kernel_session *ksess;
797 struct ltt_kernel_channel *channel;
798
799 DBG("Updating kernel streams for channel fd %d", fd);
800
801 session_lock_list();
802 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
803 session_lock(session);
804 if (session->kernel_session == NULL) {
805 session_unlock(session);
806 continue;
807 }
808 ksess = session->kernel_session;
809
810 cds_list_for_each_entry(channel, &ksess->channel_list.head, list) {
811 if (channel->fd == fd) {
812 DBG("Channel found, updating kernel streams");
813 ret = kernel_open_channel_stream(channel);
814 if (ret < 0) {
815 goto error;
816 }
817 /* Update the stream global counter */
818 ksess->stream_count_global += ret;
819
820 /*
821 * Have we already sent fds to the consumer? If yes, it means
822 * that tracing is started so it is safe to send our updated
823 * stream fds.
824 */
825 if (ksess->consumer_fds_sent == 1 && ksess->consumer != NULL) {
826 struct lttng_ht_iter iter;
827 struct consumer_socket *socket;
828
829 rcu_read_lock();
830 cds_lfht_for_each_entry(ksess->consumer->socks->ht,
831 &iter.iter, socket, node.node) {
832 pthread_mutex_lock(socket->lock);
833 ret = kernel_consumer_send_channel_stream(socket,
834 channel, ksess,
835 session->output_traces ? 1 : 0);
836 pthread_mutex_unlock(socket->lock);
837 if (ret < 0) {
838 rcu_read_unlock();
839 goto error;
840 }
841 }
842 rcu_read_unlock();
843 }
844 goto error;
845 }
846 }
847 session_unlock(session);
848 }
849 session_unlock_list();
850 return ret;
851
852 error:
853 session_unlock(session);
854 session_unlock_list();
855 return ret;
856 }
857
858 /*
859 * For each tracing session, update newly registered apps. The session list
860 * lock MUST be acquired before calling this.
861 */
862 static void update_ust_app(int app_sock)
863 {
864 struct ltt_session *sess, *stmp;
865
866 /* Consumer is in an ERROR state. Stop any application update. */
867 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
868 /* Stop the update process since the consumer is dead. */
869 return;
870 }
871
872 /* For all tracing session(s) */
873 cds_list_for_each_entry_safe(sess, stmp, &session_list_ptr->head, list) {
874 session_lock(sess);
875 if (sess->ust_session) {
876 ust_app_global_update(sess->ust_session, app_sock);
877 }
878 session_unlock(sess);
879 }
880 }
881
882 /*
883 * This thread manage event coming from the kernel.
884 *
885 * Features supported in this thread:
886 * -) CPU Hotplug
887 */
888 static void *thread_manage_kernel(void *data)
889 {
890 int ret, i, pollfd, update_poll_flag = 1, err = -1;
891 uint32_t revents, nb_fd;
892 char tmp;
893 struct lttng_poll_event events;
894
895 DBG("[thread] Thread manage kernel started");
896
897 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_KERNEL);
898
899 /*
900 * This first step of the while is to clean this structure which could free
901 * non NULL pointers so initialize it before the loop.
902 */
903 lttng_poll_init(&events);
904
905 if (testpoint(sessiond_thread_manage_kernel)) {
906 goto error_testpoint;
907 }
908
909 health_code_update();
910
911 if (testpoint(sessiond_thread_manage_kernel_before_loop)) {
912 goto error_testpoint;
913 }
914
915 while (1) {
916 health_code_update();
917
918 if (update_poll_flag == 1) {
919 /* Clean events object. We are about to populate it again. */
920 lttng_poll_clean(&events);
921
922 ret = sessiond_set_thread_pollset(&events, 2);
923 if (ret < 0) {
924 goto error_poll_create;
925 }
926
927 ret = lttng_poll_add(&events, kernel_poll_pipe[0], LPOLLIN);
928 if (ret < 0) {
929 goto error;
930 }
931
932 /* This will add the available kernel channel if any. */
933 ret = update_kernel_poll(&events);
934 if (ret < 0) {
935 goto error;
936 }
937 update_poll_flag = 0;
938 }
939
940 DBG("Thread kernel polling on %d fds", LTTNG_POLL_GETNB(&events));
941
942 /* Poll infinite value of time */
943 restart:
944 health_poll_entry();
945 ret = lttng_poll_wait(&events, -1);
946 health_poll_exit();
947 if (ret < 0) {
948 /*
949 * Restart interrupted system call.
950 */
951 if (errno == EINTR) {
952 goto restart;
953 }
954 goto error;
955 } else if (ret == 0) {
956 /* Should not happen since timeout is infinite */
957 ERR("Return value of poll is 0 with an infinite timeout.\n"
958 "This should not have happened! Continuing...");
959 continue;
960 }
961
962 nb_fd = ret;
963
964 for (i = 0; i < nb_fd; i++) {
965 /* Fetch once the poll data */
966 revents = LTTNG_POLL_GETEV(&events, i);
967 pollfd = LTTNG_POLL_GETFD(&events, i);
968
969 health_code_update();
970
971 /* Thread quit pipe has been closed. Killing thread. */
972 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
973 if (ret) {
974 err = 0;
975 goto exit;
976 }
977
978 /* Check for data on kernel pipe */
979 if (pollfd == kernel_poll_pipe[0] && (revents & LPOLLIN)) {
980 (void) lttng_read(kernel_poll_pipe[0],
981 &tmp, 1);
982 /*
983 * Ret value is useless here, if this pipe gets any actions an
984 * update is required anyway.
985 */
986 update_poll_flag = 1;
987 continue;
988 } else {
989 /*
990 * New CPU detected by the kernel. Adding kernel stream to
991 * kernel session and updating the kernel consumer
992 */
993 if (revents & LPOLLIN) {
994 ret = update_kernel_stream(&kconsumer_data, pollfd);
995 if (ret < 0) {
996 continue;
997 }
998 break;
999 /*
1000 * TODO: We might want to handle the LPOLLERR | LPOLLHUP
1001 * and unregister kernel stream at this point.
1002 */
1003 }
1004 }
1005 }
1006 }
1007
1008 exit:
1009 error:
1010 lttng_poll_clean(&events);
1011 error_poll_create:
1012 error_testpoint:
1013 utils_close_pipe(kernel_poll_pipe);
1014 kernel_poll_pipe[0] = kernel_poll_pipe[1] = -1;
1015 if (err) {
1016 health_error();
1017 ERR("Health error occurred in %s", __func__);
1018 WARN("Kernel thread died unexpectedly. "
1019 "Kernel tracing can continue but CPU hotplug is disabled.");
1020 }
1021 health_unregister(health_sessiond);
1022 DBG("Kernel thread dying");
1023 return NULL;
1024 }
1025
1026 /*
1027 * Signal pthread condition of the consumer data that the thread.
1028 */
1029 static void signal_consumer_condition(struct consumer_data *data, int state)
1030 {
1031 pthread_mutex_lock(&data->cond_mutex);
1032
1033 /*
1034 * The state is set before signaling. It can be any value, it's the waiter
1035 * job to correctly interpret this condition variable associated to the
1036 * consumer pthread_cond.
1037 *
1038 * A value of 0 means that the corresponding thread of the consumer data
1039 * was not started. 1 indicates that the thread has started and is ready
1040 * for action. A negative value means that there was an error during the
1041 * thread bootstrap.
1042 */
1043 data->consumer_thread_is_ready = state;
1044 (void) pthread_cond_signal(&data->cond);
1045
1046 pthread_mutex_unlock(&data->cond_mutex);
1047 }
1048
1049 /*
1050 * This thread manage the consumer error sent back to the session daemon.
1051 */
1052 static void *thread_manage_consumer(void *data)
1053 {
1054 int sock = -1, i, ret, pollfd, err = -1;
1055 uint32_t revents, nb_fd;
1056 enum lttcomm_return_code code;
1057 struct lttng_poll_event events;
1058 struct consumer_data *consumer_data = data;
1059
1060 DBG("[thread] Manage consumer started");
1061
1062 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CONSUMER);
1063
1064 health_code_update();
1065
1066 /*
1067 * Pass 3 as size here for the thread quit pipe, consumerd_err_sock and the
1068 * metadata_sock. Nothing more will be added to this poll set.
1069 */
1070 ret = sessiond_set_thread_pollset(&events, 3);
1071 if (ret < 0) {
1072 goto error_poll;
1073 }
1074
1075 /*
1076 * The error socket here is already in a listening state which was done
1077 * just before spawning this thread to avoid a race between the consumer
1078 * daemon exec trying to connect and the listen() call.
1079 */
1080 ret = lttng_poll_add(&events, consumer_data->err_sock, LPOLLIN | LPOLLRDHUP);
1081 if (ret < 0) {
1082 goto error;
1083 }
1084
1085 health_code_update();
1086
1087 /* Infinite blocking call, waiting for transmission */
1088 restart:
1089 health_poll_entry();
1090
1091 if (testpoint(sessiond_thread_manage_consumer)) {
1092 goto error;
1093 }
1094
1095 ret = lttng_poll_wait(&events, -1);
1096 health_poll_exit();
1097 if (ret < 0) {
1098 /*
1099 * Restart interrupted system call.
1100 */
1101 if (errno == EINTR) {
1102 goto restart;
1103 }
1104 goto error;
1105 }
1106
1107 nb_fd = ret;
1108
1109 for (i = 0; i < nb_fd; i++) {
1110 /* Fetch once the poll data */
1111 revents = LTTNG_POLL_GETEV(&events, i);
1112 pollfd = LTTNG_POLL_GETFD(&events, i);
1113
1114 health_code_update();
1115
1116 /* Thread quit pipe has been closed. Killing thread. */
1117 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1118 if (ret) {
1119 err = 0;
1120 goto exit;
1121 }
1122
1123 /* Event on the registration socket */
1124 if (pollfd == consumer_data->err_sock) {
1125 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1126 ERR("consumer err socket poll error");
1127 goto error;
1128 }
1129 }
1130 }
1131
1132 sock = lttcomm_accept_unix_sock(consumer_data->err_sock);
1133 if (sock < 0) {
1134 goto error;
1135 }
1136
1137 /*
1138 * Set the CLOEXEC flag. Return code is useless because either way, the
1139 * show must go on.
1140 */
1141 (void) utils_set_fd_cloexec(sock);
1142
1143 health_code_update();
1144
1145 DBG2("Receiving code from consumer err_sock");
1146
1147 /* Getting status code from kconsumerd */
1148 ret = lttcomm_recv_unix_sock(sock, &code,
1149 sizeof(enum lttcomm_return_code));
1150 if (ret <= 0) {
1151 goto error;
1152 }
1153
1154 health_code_update();
1155 if (code == LTTCOMM_CONSUMERD_COMMAND_SOCK_READY) {
1156 /* Connect both socket, command and metadata. */
1157 consumer_data->cmd_sock =
1158 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1159 consumer_data->metadata_fd =
1160 lttcomm_connect_unix_sock(consumer_data->cmd_unix_sock_path);
1161 if (consumer_data->cmd_sock < 0
1162 || consumer_data->metadata_fd < 0) {
1163 PERROR("consumer connect cmd socket");
1164 /* On error, signal condition and quit. */
1165 signal_consumer_condition(consumer_data, -1);
1166 goto error;
1167 }
1168 consumer_data->metadata_sock.fd_ptr = &consumer_data->metadata_fd;
1169 /* Create metadata socket lock. */
1170 consumer_data->metadata_sock.lock = zmalloc(sizeof(pthread_mutex_t));
1171 if (consumer_data->metadata_sock.lock == NULL) {
1172 PERROR("zmalloc pthread mutex");
1173 ret = -1;
1174 goto error;
1175 }
1176 pthread_mutex_init(consumer_data->metadata_sock.lock, NULL);
1177
1178 signal_consumer_condition(consumer_data, 1);
1179 DBG("Consumer command socket ready (fd: %d", consumer_data->cmd_sock);
1180 DBG("Consumer metadata socket ready (fd: %d)",
1181 consumer_data->metadata_fd);
1182 } else {
1183 ERR("consumer error when waiting for SOCK_READY : %s",
1184 lttcomm_get_readable_code(-code));
1185 goto error;
1186 }
1187
1188 /* Remove the consumerd error sock since we've established a connexion */
1189 ret = lttng_poll_del(&events, consumer_data->err_sock);
1190 if (ret < 0) {
1191 goto error;
1192 }
1193
1194 /* Add new accepted error socket. */
1195 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLRDHUP);
1196 if (ret < 0) {
1197 goto error;
1198 }
1199
1200 /* Add metadata socket that is successfully connected. */
1201 ret = lttng_poll_add(&events, consumer_data->metadata_fd,
1202 LPOLLIN | LPOLLRDHUP);
1203 if (ret < 0) {
1204 goto error;
1205 }
1206
1207 health_code_update();
1208
1209 /* Infinite blocking call, waiting for transmission */
1210 restart_poll:
1211 while (1) {
1212 health_poll_entry();
1213 ret = lttng_poll_wait(&events, -1);
1214 health_poll_exit();
1215 if (ret < 0) {
1216 /*
1217 * Restart interrupted system call.
1218 */
1219 if (errno == EINTR) {
1220 goto restart_poll;
1221 }
1222 goto error;
1223 }
1224
1225 nb_fd = ret;
1226
1227 for (i = 0; i < nb_fd; i++) {
1228 /* Fetch once the poll data */
1229 revents = LTTNG_POLL_GETEV(&events, i);
1230 pollfd = LTTNG_POLL_GETFD(&events, i);
1231
1232 health_code_update();
1233
1234 /* Thread quit pipe has been closed. Killing thread. */
1235 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1236 if (ret) {
1237 err = 0;
1238 goto exit;
1239 }
1240
1241 if (pollfd == sock) {
1242 /* Event on the consumerd socket */
1243 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1244 ERR("consumer err socket second poll error");
1245 goto error;
1246 }
1247 health_code_update();
1248 /* Wait for any kconsumerd error */
1249 ret = lttcomm_recv_unix_sock(sock, &code,
1250 sizeof(enum lttcomm_return_code));
1251 if (ret <= 0) {
1252 ERR("consumer closed the command socket");
1253 goto error;
1254 }
1255
1256 ERR("consumer return code : %s",
1257 lttcomm_get_readable_code(-code));
1258
1259 goto exit;
1260 } else if (pollfd == consumer_data->metadata_fd) {
1261 /* UST metadata requests */
1262 ret = ust_consumer_metadata_request(
1263 &consumer_data->metadata_sock);
1264 if (ret < 0) {
1265 ERR("Handling metadata request");
1266 goto error;
1267 }
1268 break;
1269 } else {
1270 ERR("Unknown pollfd");
1271 goto error;
1272 }
1273 }
1274 health_code_update();
1275 }
1276
1277 exit:
1278 error:
1279 /*
1280 * We lock here because we are about to close the sockets and some other
1281 * thread might be using them so get exclusive access which will abort all
1282 * other consumer command by other threads.
1283 */
1284 pthread_mutex_lock(&consumer_data->lock);
1285
1286 /* Immediately set the consumerd state to stopped */
1287 if (consumer_data->type == LTTNG_CONSUMER_KERNEL) {
1288 uatomic_set(&kernel_consumerd_state, CONSUMER_ERROR);
1289 } else if (consumer_data->type == LTTNG_CONSUMER64_UST ||
1290 consumer_data->type == LTTNG_CONSUMER32_UST) {
1291 uatomic_set(&ust_consumerd_state, CONSUMER_ERROR);
1292 } else {
1293 /* Code flow error... */
1294 assert(0);
1295 }
1296
1297 if (consumer_data->err_sock >= 0) {
1298 ret = close(consumer_data->err_sock);
1299 if (ret) {
1300 PERROR("close");
1301 }
1302 consumer_data->err_sock = -1;
1303 }
1304 if (consumer_data->cmd_sock >= 0) {
1305 ret = close(consumer_data->cmd_sock);
1306 if (ret) {
1307 PERROR("close");
1308 }
1309 consumer_data->cmd_sock = -1;
1310 }
1311 if (consumer_data->metadata_sock.fd_ptr &&
1312 *consumer_data->metadata_sock.fd_ptr >= 0) {
1313 ret = close(*consumer_data->metadata_sock.fd_ptr);
1314 if (ret) {
1315 PERROR("close");
1316 }
1317 }
1318 if (sock >= 0) {
1319 ret = close(sock);
1320 if (ret) {
1321 PERROR("close");
1322 }
1323 }
1324
1325 unlink(consumer_data->err_unix_sock_path);
1326 unlink(consumer_data->cmd_unix_sock_path);
1327 consumer_data->pid = 0;
1328 pthread_mutex_unlock(&consumer_data->lock);
1329
1330 /* Cleanup metadata socket mutex. */
1331 if (consumer_data->metadata_sock.lock) {
1332 pthread_mutex_destroy(consumer_data->metadata_sock.lock);
1333 free(consumer_data->metadata_sock.lock);
1334 }
1335 lttng_poll_clean(&events);
1336 error_poll:
1337 if (err) {
1338 health_error();
1339 ERR("Health error occurred in %s", __func__);
1340 }
1341 health_unregister(health_sessiond);
1342 DBG("consumer thread cleanup completed");
1343
1344 return NULL;
1345 }
1346
1347 /*
1348 * This thread manage application communication.
1349 */
1350 static void *thread_manage_apps(void *data)
1351 {
1352 int i, ret, pollfd, err = -1;
1353 ssize_t size_ret;
1354 uint32_t revents, nb_fd;
1355 struct lttng_poll_event events;
1356
1357 DBG("[thread] Manage application started");
1358
1359 rcu_register_thread();
1360 rcu_thread_online();
1361
1362 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_MANAGE);
1363
1364 if (testpoint(sessiond_thread_manage_apps)) {
1365 goto error_testpoint;
1366 }
1367
1368 health_code_update();
1369
1370 ret = sessiond_set_thread_pollset(&events, 2);
1371 if (ret < 0) {
1372 goto error_poll_create;
1373 }
1374
1375 ret = lttng_poll_add(&events, apps_cmd_pipe[0], LPOLLIN | LPOLLRDHUP);
1376 if (ret < 0) {
1377 goto error;
1378 }
1379
1380 if (testpoint(sessiond_thread_manage_apps_before_loop)) {
1381 goto error;
1382 }
1383
1384 health_code_update();
1385
1386 while (1) {
1387 DBG("Apps thread polling on %d fds", LTTNG_POLL_GETNB(&events));
1388
1389 /* Inifinite blocking call, waiting for transmission */
1390 restart:
1391 health_poll_entry();
1392 ret = lttng_poll_wait(&events, -1);
1393 health_poll_exit();
1394 if (ret < 0) {
1395 /*
1396 * Restart interrupted system call.
1397 */
1398 if (errno == EINTR) {
1399 goto restart;
1400 }
1401 goto error;
1402 }
1403
1404 nb_fd = ret;
1405
1406 for (i = 0; i < nb_fd; i++) {
1407 /* Fetch once the poll data */
1408 revents = LTTNG_POLL_GETEV(&events, i);
1409 pollfd = LTTNG_POLL_GETFD(&events, i);
1410
1411 health_code_update();
1412
1413 /* Thread quit pipe has been closed. Killing thread. */
1414 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1415 if (ret) {
1416 err = 0;
1417 goto exit;
1418 }
1419
1420 /* Inspect the apps cmd pipe */
1421 if (pollfd == apps_cmd_pipe[0]) {
1422 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1423 ERR("Apps command pipe error");
1424 goto error;
1425 } else if (revents & LPOLLIN) {
1426 int sock;
1427
1428 /* Empty pipe */
1429 size_ret = lttng_read(apps_cmd_pipe[0], &sock, sizeof(sock));
1430 if (size_ret < sizeof(sock)) {
1431 PERROR("read apps cmd pipe");
1432 goto error;
1433 }
1434
1435 health_code_update();
1436
1437 /*
1438 * We only monitor the error events of the socket. This
1439 * thread does not handle any incoming data from UST
1440 * (POLLIN).
1441 */
1442 ret = lttng_poll_add(&events, sock,
1443 LPOLLERR | LPOLLHUP | LPOLLRDHUP);
1444 if (ret < 0) {
1445 goto error;
1446 }
1447
1448 DBG("Apps with sock %d added to poll set", sock);
1449 }
1450 } else {
1451 /*
1452 * At this point, we know that a registered application made
1453 * the event at poll_wait.
1454 */
1455 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1456 /* Removing from the poll set */
1457 ret = lttng_poll_del(&events, pollfd);
1458 if (ret < 0) {
1459 goto error;
1460 }
1461
1462 /* Socket closed on remote end. */
1463 ust_app_unregister(pollfd);
1464 }
1465 }
1466
1467 health_code_update();
1468 }
1469 }
1470
1471 exit:
1472 error:
1473 lttng_poll_clean(&events);
1474 error_poll_create:
1475 error_testpoint:
1476 utils_close_pipe(apps_cmd_pipe);
1477 apps_cmd_pipe[0] = apps_cmd_pipe[1] = -1;
1478
1479 /*
1480 * We don't clean the UST app hash table here since already registered
1481 * applications can still be controlled so let them be until the session
1482 * daemon dies or the applications stop.
1483 */
1484
1485 if (err) {
1486 health_error();
1487 ERR("Health error occurred in %s", __func__);
1488 }
1489 health_unregister(health_sessiond);
1490 DBG("Application communication apps thread cleanup complete");
1491 rcu_thread_offline();
1492 rcu_unregister_thread();
1493 return NULL;
1494 }
1495
1496 /*
1497 * Send a socket to a thread This is called from the dispatch UST registration
1498 * thread once all sockets are set for the application.
1499 *
1500 * The sock value can be invalid, we don't really care, the thread will handle
1501 * it and make the necessary cleanup if so.
1502 *
1503 * On success, return 0 else a negative value being the errno message of the
1504 * write().
1505 */
1506 static int send_socket_to_thread(int fd, int sock)
1507 {
1508 ssize_t ret;
1509
1510 /*
1511 * It's possible that the FD is set as invalid with -1 concurrently just
1512 * before calling this function being a shutdown state of the thread.
1513 */
1514 if (fd < 0) {
1515 ret = -EBADF;
1516 goto error;
1517 }
1518
1519 ret = lttng_write(fd, &sock, sizeof(sock));
1520 if (ret < sizeof(sock)) {
1521 PERROR("write apps pipe %d", fd);
1522 if (ret < 0) {
1523 ret = -errno;
1524 }
1525 goto error;
1526 }
1527
1528 /* All good. Don't send back the write positive ret value. */
1529 ret = 0;
1530 error:
1531 return (int) ret;
1532 }
1533
1534 /*
1535 * Sanitize the wait queue of the dispatch registration thread meaning removing
1536 * invalid nodes from it. This is to avoid memory leaks for the case the UST
1537 * notify socket is never received.
1538 */
1539 static void sanitize_wait_queue(struct ust_reg_wait_queue *wait_queue)
1540 {
1541 int ret, nb_fd = 0, i;
1542 unsigned int fd_added = 0;
1543 struct lttng_poll_event events;
1544 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1545
1546 assert(wait_queue);
1547
1548 lttng_poll_init(&events);
1549
1550 /* Just skip everything for an empty queue. */
1551 if (!wait_queue->count) {
1552 goto end;
1553 }
1554
1555 ret = lttng_poll_create(&events, wait_queue->count, LTTNG_CLOEXEC);
1556 if (ret < 0) {
1557 goto error_create;
1558 }
1559
1560 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1561 &wait_queue->head, head) {
1562 assert(wait_node->app);
1563 ret = lttng_poll_add(&events, wait_node->app->sock,
1564 LPOLLHUP | LPOLLERR);
1565 if (ret < 0) {
1566 goto error;
1567 }
1568
1569 fd_added = 1;
1570 }
1571
1572 if (!fd_added) {
1573 goto end;
1574 }
1575
1576 /*
1577 * Poll but don't block so we can quickly identify the faulty events and
1578 * clean them afterwards from the wait queue.
1579 */
1580 ret = lttng_poll_wait(&events, 0);
1581 if (ret < 0) {
1582 goto error;
1583 }
1584 nb_fd = ret;
1585
1586 for (i = 0; i < nb_fd; i++) {
1587 /* Get faulty FD. */
1588 uint32_t revents = LTTNG_POLL_GETEV(&events, i);
1589 int pollfd = LTTNG_POLL_GETFD(&events, i);
1590
1591 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1592 &wait_queue->head, head) {
1593 if (pollfd == wait_node->app->sock &&
1594 (revents & (LPOLLHUP | LPOLLERR))) {
1595 cds_list_del(&wait_node->head);
1596 wait_queue->count--;
1597 ust_app_destroy(wait_node->app);
1598 free(wait_node);
1599 break;
1600 }
1601 }
1602 }
1603
1604 if (nb_fd > 0) {
1605 DBG("Wait queue sanitized, %d node were cleaned up", nb_fd);
1606 }
1607
1608 end:
1609 lttng_poll_clean(&events);
1610 return;
1611
1612 error:
1613 lttng_poll_clean(&events);
1614 error_create:
1615 ERR("Unable to sanitize wait queue");
1616 return;
1617 }
1618
1619 /*
1620 * Dispatch request from the registration threads to the application
1621 * communication thread.
1622 */
1623 static void *thread_dispatch_ust_registration(void *data)
1624 {
1625 int ret, err = -1;
1626 struct cds_wfq_node *node;
1627 struct ust_command *ust_cmd = NULL;
1628 struct ust_reg_wait_node *wait_node = NULL, *tmp_wait_node;
1629 struct ust_reg_wait_queue wait_queue = {
1630 .count = 0,
1631 };
1632
1633 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG_DISPATCH);
1634
1635 if (testpoint(sessiond_thread_app_reg_dispatch)) {
1636 goto error_testpoint;
1637 }
1638
1639 health_code_update();
1640
1641 CDS_INIT_LIST_HEAD(&wait_queue.head);
1642
1643 DBG("[thread] Dispatch UST command started");
1644
1645 while (!CMM_LOAD_SHARED(dispatch_thread_exit)) {
1646 health_code_update();
1647
1648 /* Atomically prepare the queue futex */
1649 futex_nto1_prepare(&ust_cmd_queue.futex);
1650
1651 do {
1652 struct ust_app *app = NULL;
1653 ust_cmd = NULL;
1654
1655 /*
1656 * Make sure we don't have node(s) that have hung up before receiving
1657 * the notify socket. This is to clean the list in order to avoid
1658 * memory leaks from notify socket that are never seen.
1659 */
1660 sanitize_wait_queue(&wait_queue);
1661
1662 health_code_update();
1663 /* Dequeue command for registration */
1664 node = cds_wfq_dequeue_blocking(&ust_cmd_queue.queue);
1665 if (node == NULL) {
1666 DBG("Woken up but nothing in the UST command queue");
1667 /* Continue thread execution */
1668 break;
1669 }
1670
1671 ust_cmd = caa_container_of(node, struct ust_command, node);
1672
1673 DBG("Dispatching UST registration pid:%d ppid:%d uid:%d"
1674 " gid:%d sock:%d name:%s (version %d.%d)",
1675 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
1676 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
1677 ust_cmd->sock, ust_cmd->reg_msg.name,
1678 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
1679
1680 if (ust_cmd->reg_msg.type == USTCTL_SOCKET_CMD) {
1681 wait_node = zmalloc(sizeof(*wait_node));
1682 if (!wait_node) {
1683 PERROR("zmalloc wait_node dispatch");
1684 ret = close(ust_cmd->sock);
1685 if (ret < 0) {
1686 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1687 }
1688 lttng_fd_put(LTTNG_FD_APPS, 1);
1689 free(ust_cmd);
1690 goto error;
1691 }
1692 CDS_INIT_LIST_HEAD(&wait_node->head);
1693
1694 /* Create application object if socket is CMD. */
1695 wait_node->app = ust_app_create(&ust_cmd->reg_msg,
1696 ust_cmd->sock);
1697 if (!wait_node->app) {
1698 ret = close(ust_cmd->sock);
1699 if (ret < 0) {
1700 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1701 }
1702 lttng_fd_put(LTTNG_FD_APPS, 1);
1703 free(wait_node);
1704 free(ust_cmd);
1705 continue;
1706 }
1707 /*
1708 * Add application to the wait queue so we can set the notify
1709 * socket before putting this object in the global ht.
1710 */
1711 cds_list_add(&wait_node->head, &wait_queue.head);
1712 wait_queue.count++;
1713
1714 free(ust_cmd);
1715 /*
1716 * We have to continue here since we don't have the notify
1717 * socket and the application MUST be added to the hash table
1718 * only at that moment.
1719 */
1720 continue;
1721 } else {
1722 /*
1723 * Look for the application in the local wait queue and set the
1724 * notify socket if found.
1725 */
1726 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1727 &wait_queue.head, head) {
1728 health_code_update();
1729 if (wait_node->app->pid == ust_cmd->reg_msg.pid) {
1730 wait_node->app->notify_sock = ust_cmd->sock;
1731 cds_list_del(&wait_node->head);
1732 wait_queue.count--;
1733 app = wait_node->app;
1734 free(wait_node);
1735 DBG3("UST app notify socket %d is set", ust_cmd->sock);
1736 break;
1737 }
1738 }
1739
1740 /*
1741 * With no application at this stage the received socket is
1742 * basically useless so close it before we free the cmd data
1743 * structure for good.
1744 */
1745 if (!app) {
1746 ret = close(ust_cmd->sock);
1747 if (ret < 0) {
1748 PERROR("close ust sock dispatch %d", ust_cmd->sock);
1749 }
1750 lttng_fd_put(LTTNG_FD_APPS, 1);
1751 }
1752 free(ust_cmd);
1753 }
1754
1755 if (app) {
1756 /*
1757 * @session_lock_list
1758 *
1759 * Lock the global session list so from the register up to the
1760 * registration done message, no thread can see the application
1761 * and change its state.
1762 */
1763 session_lock_list();
1764 rcu_read_lock();
1765
1766 /*
1767 * Add application to the global hash table. This needs to be
1768 * done before the update to the UST registry can locate the
1769 * application.
1770 */
1771 ust_app_add(app);
1772
1773 /* Set app version. This call will print an error if needed. */
1774 (void) ust_app_version(app);
1775
1776 /* Send notify socket through the notify pipe. */
1777 ret = send_socket_to_thread(apps_cmd_notify_pipe[1],
1778 app->notify_sock);
1779 if (ret < 0) {
1780 rcu_read_unlock();
1781 session_unlock_list();
1782 /*
1783 * No notify thread, stop the UST tracing. However, this is
1784 * not an internal error of the this thread thus setting
1785 * the health error code to a normal exit.
1786 */
1787 err = 0;
1788 goto error;
1789 }
1790
1791 /*
1792 * Update newly registered application with the tracing
1793 * registry info already enabled information.
1794 */
1795 update_ust_app(app->sock);
1796
1797 /*
1798 * Don't care about return value. Let the manage apps threads
1799 * handle app unregistration upon socket close.
1800 */
1801 (void) ust_app_register_done(app->sock);
1802
1803 /*
1804 * Even if the application socket has been closed, send the app
1805 * to the thread and unregistration will take place at that
1806 * place.
1807 */
1808 ret = send_socket_to_thread(apps_cmd_pipe[1], app->sock);
1809 if (ret < 0) {
1810 rcu_read_unlock();
1811 session_unlock_list();
1812 /*
1813 * No apps. thread, stop the UST tracing. However, this is
1814 * not an internal error of the this thread thus setting
1815 * the health error code to a normal exit.
1816 */
1817 err = 0;
1818 goto error;
1819 }
1820
1821 rcu_read_unlock();
1822 session_unlock_list();
1823 }
1824 } while (node != NULL);
1825
1826 health_poll_entry();
1827 /* Futex wait on queue. Blocking call on futex() */
1828 futex_nto1_wait(&ust_cmd_queue.futex);
1829 health_poll_exit();
1830 }
1831 /* Normal exit, no error */
1832 err = 0;
1833
1834 error:
1835 /* Clean up wait queue. */
1836 cds_list_for_each_entry_safe(wait_node, tmp_wait_node,
1837 &wait_queue.head, head) {
1838 cds_list_del(&wait_node->head);
1839 wait_queue.count--;
1840 free(wait_node);
1841 }
1842
1843 error_testpoint:
1844 DBG("Dispatch thread dying");
1845 if (err) {
1846 health_error();
1847 ERR("Health error occurred in %s", __func__);
1848 }
1849 health_unregister(health_sessiond);
1850 return NULL;
1851 }
1852
1853 /*
1854 * This thread manage application registration.
1855 */
1856 static void *thread_registration_apps(void *data)
1857 {
1858 int sock = -1, i, ret, pollfd, err = -1;
1859 uint32_t revents, nb_fd;
1860 struct lttng_poll_event events;
1861 /*
1862 * Get allocated in this thread, enqueued to a global queue, dequeued and
1863 * freed in the manage apps thread.
1864 */
1865 struct ust_command *ust_cmd = NULL;
1866
1867 DBG("[thread] Manage application registration started");
1868
1869 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_APP_REG);
1870
1871 if (testpoint(sessiond_thread_registration_apps)) {
1872 goto error_testpoint;
1873 }
1874
1875 ret = lttcomm_listen_unix_sock(apps_sock);
1876 if (ret < 0) {
1877 goto error_listen;
1878 }
1879
1880 /*
1881 * Pass 2 as size here for the thread quit pipe and apps socket. Nothing
1882 * more will be added to this poll set.
1883 */
1884 ret = sessiond_set_thread_pollset(&events, 2);
1885 if (ret < 0) {
1886 goto error_create_poll;
1887 }
1888
1889 /* Add the application registration socket */
1890 ret = lttng_poll_add(&events, apps_sock, LPOLLIN | LPOLLRDHUP);
1891 if (ret < 0) {
1892 goto error_poll_add;
1893 }
1894
1895 /* Notify all applications to register */
1896 ret = notify_ust_apps(1);
1897 if (ret < 0) {
1898 ERR("Failed to notify applications or create the wait shared memory.\n"
1899 "Execution continues but there might be problem for already\n"
1900 "running applications that wishes to register.");
1901 }
1902
1903 while (1) {
1904 DBG("Accepting application registration");
1905
1906 /* Inifinite blocking call, waiting for transmission */
1907 restart:
1908 health_poll_entry();
1909 ret = lttng_poll_wait(&events, -1);
1910 health_poll_exit();
1911 if (ret < 0) {
1912 /*
1913 * Restart interrupted system call.
1914 */
1915 if (errno == EINTR) {
1916 goto restart;
1917 }
1918 goto error;
1919 }
1920
1921 nb_fd = ret;
1922
1923 for (i = 0; i < nb_fd; i++) {
1924 health_code_update();
1925
1926 /* Fetch once the poll data */
1927 revents = LTTNG_POLL_GETEV(&events, i);
1928 pollfd = LTTNG_POLL_GETFD(&events, i);
1929
1930 /* Thread quit pipe has been closed. Killing thread. */
1931 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
1932 if (ret) {
1933 err = 0;
1934 goto exit;
1935 }
1936
1937 /* Event on the registration socket */
1938 if (pollfd == apps_sock) {
1939 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
1940 ERR("Register apps socket poll error");
1941 goto error;
1942 } else if (revents & LPOLLIN) {
1943 sock = lttcomm_accept_unix_sock(apps_sock);
1944 if (sock < 0) {
1945 goto error;
1946 }
1947
1948 /*
1949 * Set socket timeout for both receiving and ending.
1950 * app_socket_timeout is in seconds, whereas
1951 * lttcomm_setsockopt_rcv_timeout and
1952 * lttcomm_setsockopt_snd_timeout expect msec as
1953 * parameter.
1954 */
1955 (void) lttcomm_setsockopt_rcv_timeout(sock,
1956 app_socket_timeout * 1000);
1957 (void) lttcomm_setsockopt_snd_timeout(sock,
1958 app_socket_timeout * 1000);
1959
1960 /*
1961 * Set the CLOEXEC flag. Return code is useless because
1962 * either way, the show must go on.
1963 */
1964 (void) utils_set_fd_cloexec(sock);
1965
1966 /* Create UST registration command for enqueuing */
1967 ust_cmd = zmalloc(sizeof(struct ust_command));
1968 if (ust_cmd == NULL) {
1969 PERROR("ust command zmalloc");
1970 goto error;
1971 }
1972
1973 /*
1974 * Using message-based transmissions to ensure we don't
1975 * have to deal with partially received messages.
1976 */
1977 ret = lttng_fd_get(LTTNG_FD_APPS, 1);
1978 if (ret < 0) {
1979 ERR("Exhausted file descriptors allowed for applications.");
1980 free(ust_cmd);
1981 ret = close(sock);
1982 if (ret) {
1983 PERROR("close");
1984 }
1985 sock = -1;
1986 continue;
1987 }
1988
1989 health_code_update();
1990 ret = ust_app_recv_registration(sock, &ust_cmd->reg_msg);
1991 if (ret < 0) {
1992 free(ust_cmd);
1993 /* Close socket of the application. */
1994 ret = close(sock);
1995 if (ret) {
1996 PERROR("close");
1997 }
1998 lttng_fd_put(LTTNG_FD_APPS, 1);
1999 sock = -1;
2000 continue;
2001 }
2002 health_code_update();
2003
2004 ust_cmd->sock = sock;
2005 sock = -1;
2006
2007 DBG("UST registration received with pid:%d ppid:%d uid:%d"
2008 " gid:%d sock:%d name:%s (version %d.%d)",
2009 ust_cmd->reg_msg.pid, ust_cmd->reg_msg.ppid,
2010 ust_cmd->reg_msg.uid, ust_cmd->reg_msg.gid,
2011 ust_cmd->sock, ust_cmd->reg_msg.name,
2012 ust_cmd->reg_msg.major, ust_cmd->reg_msg.minor);
2013
2014 /*
2015 * Lock free enqueue the registration request. The red pill
2016 * has been taken! This apps will be part of the *system*.
2017 */
2018 cds_wfq_enqueue(&ust_cmd_queue.queue, &ust_cmd->node);
2019
2020 /*
2021 * Wake the registration queue futex. Implicit memory
2022 * barrier with the exchange in cds_wfq_enqueue.
2023 */
2024 futex_nto1_wake(&ust_cmd_queue.futex);
2025 }
2026 }
2027 }
2028 }
2029
2030 exit:
2031 error:
2032 /* Notify that the registration thread is gone */
2033 notify_ust_apps(0);
2034
2035 if (apps_sock >= 0) {
2036 ret = close(apps_sock);
2037 if (ret) {
2038 PERROR("close");
2039 }
2040 }
2041 if (sock >= 0) {
2042 ret = close(sock);
2043 if (ret) {
2044 PERROR("close");
2045 }
2046 lttng_fd_put(LTTNG_FD_APPS, 1);
2047 }
2048 unlink(apps_unix_sock_path);
2049
2050 error_poll_add:
2051 lttng_poll_clean(&events);
2052 error_listen:
2053 error_create_poll:
2054 error_testpoint:
2055 DBG("UST Registration thread cleanup complete");
2056 if (err) {
2057 health_error();
2058 ERR("Health error occurred in %s", __func__);
2059 }
2060 health_unregister(health_sessiond);
2061
2062 return NULL;
2063 }
2064
2065 /*
2066 * Start the thread_manage_consumer. This must be done after a lttng-consumerd
2067 * exec or it will fails.
2068 */
2069 static int spawn_consumer_thread(struct consumer_data *consumer_data)
2070 {
2071 int ret, clock_ret;
2072 struct timespec timeout;
2073
2074 /* Make sure we set the readiness flag to 0 because we are NOT ready */
2075 consumer_data->consumer_thread_is_ready = 0;
2076
2077 /* Setup pthread condition */
2078 ret = pthread_condattr_init(&consumer_data->condattr);
2079 if (ret != 0) {
2080 errno = ret;
2081 PERROR("pthread_condattr_init consumer data");
2082 goto error;
2083 }
2084
2085 /*
2086 * Set the monotonic clock in order to make sure we DO NOT jump in time
2087 * between the clock_gettime() call and the timedwait call. See bug #324
2088 * for a more details and how we noticed it.
2089 */
2090 ret = pthread_condattr_setclock(&consumer_data->condattr, CLOCK_MONOTONIC);
2091 if (ret != 0) {
2092 errno = ret;
2093 PERROR("pthread_condattr_setclock consumer data");
2094 goto error;
2095 }
2096
2097 ret = pthread_cond_init(&consumer_data->cond, &consumer_data->condattr);
2098 if (ret != 0) {
2099 errno = ret;
2100 PERROR("pthread_cond_init consumer data");
2101 goto error;
2102 }
2103
2104 ret = pthread_create(&consumer_data->thread, NULL, thread_manage_consumer,
2105 consumer_data);
2106 if (ret != 0) {
2107 PERROR("pthread_create consumer");
2108 ret = -1;
2109 goto error;
2110 }
2111
2112 /* We are about to wait on a pthread condition */
2113 pthread_mutex_lock(&consumer_data->cond_mutex);
2114
2115 /* Get time for sem_timedwait absolute timeout */
2116 clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2117 /*
2118 * Set the timeout for the condition timed wait even if the clock gettime
2119 * call fails since we might loop on that call and we want to avoid to
2120 * increment the timeout too many times.
2121 */
2122 timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
2123
2124 /*
2125 * The following loop COULD be skipped in some conditions so this is why we
2126 * set ret to 0 in order to make sure at least one round of the loop is
2127 * done.
2128 */
2129 ret = 0;
2130
2131 /*
2132 * Loop until the condition is reached or when a timeout is reached. Note
2133 * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
2134 * be returned but the pthread_cond(3), from the glibc-doc, says that it is
2135 * possible. This loop does not take any chances and works with both of
2136 * them.
2137 */
2138 while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
2139 if (clock_ret < 0) {
2140 PERROR("clock_gettime spawn consumer");
2141 /* Infinite wait for the consumerd thread to be ready */
2142 ret = pthread_cond_wait(&consumer_data->cond,
2143 &consumer_data->cond_mutex);
2144 } else {
2145 ret = pthread_cond_timedwait(&consumer_data->cond,
2146 &consumer_data->cond_mutex, &timeout);
2147 }
2148 }
2149
2150 /* Release the pthread condition */
2151 pthread_mutex_unlock(&consumer_data->cond_mutex);
2152
2153 if (ret != 0) {
2154 errno = ret;
2155 if (ret == ETIMEDOUT) {
2156 int pth_ret;
2157
2158 /*
2159 * Call has timed out so we kill the kconsumerd_thread and return
2160 * an error.
2161 */
2162 ERR("Condition timed out. The consumer thread was never ready."
2163 " Killing it");
2164 pth_ret = pthread_cancel(consumer_data->thread);
2165 if (pth_ret < 0) {
2166 PERROR("pthread_cancel consumer thread");
2167 }
2168 } else {
2169 PERROR("pthread_cond_wait failed consumer thread");
2170 }
2171 /* Caller is expecting a negative value on failure. */
2172 ret = -1;
2173 goto error;
2174 }
2175
2176 pthread_mutex_lock(&consumer_data->pid_mutex);
2177 if (consumer_data->pid == 0) {
2178 ERR("Consumerd did not start");
2179 pthread_mutex_unlock(&consumer_data->pid_mutex);
2180 goto error;
2181 }
2182 pthread_mutex_unlock(&consumer_data->pid_mutex);
2183
2184 return 0;
2185
2186 error:
2187 return ret;
2188 }
2189
2190 /*
2191 * Join consumer thread
2192 */
2193 static int join_consumer_thread(struct consumer_data *consumer_data)
2194 {
2195 void *status;
2196
2197 /* Consumer pid must be a real one. */
2198 if (consumer_data->pid > 0) {
2199 int ret;
2200 ret = kill(consumer_data->pid, SIGTERM);
2201 if (ret) {
2202 ERR("Error killing consumer daemon");
2203 return ret;
2204 }
2205 return pthread_join(consumer_data->thread, &status);
2206 } else {
2207 return 0;
2208 }
2209 }
2210
2211 /*
2212 * Fork and exec a consumer daemon (consumerd).
2213 *
2214 * Return pid if successful else -1.
2215 */
2216 static pid_t spawn_consumerd(struct consumer_data *consumer_data)
2217 {
2218 int ret;
2219 pid_t pid;
2220 const char *consumer_to_use;
2221 const char *verbosity;
2222 struct stat st;
2223
2224 DBG("Spawning consumerd");
2225
2226 pid = fork();
2227 if (pid == 0) {
2228 /*
2229 * Exec consumerd.
2230 */
2231 if (opt_verbose_consumer) {
2232 verbosity = "--verbose";
2233 } else {
2234 verbosity = "--quiet";
2235 }
2236 switch (consumer_data->type) {
2237 case LTTNG_CONSUMER_KERNEL:
2238 /*
2239 * Find out which consumerd to execute. We will first try the
2240 * 64-bit path, then the sessiond's installation directory, and
2241 * fallback on the 32-bit one,
2242 */
2243 DBG3("Looking for a kernel consumer at these locations:");
2244 DBG3(" 1) %s", consumerd64_bin);
2245 DBG3(" 2) %s/%s", INSTALL_BIN_PATH, CONSUMERD_FILE);
2246 DBG3(" 3) %s", consumerd32_bin);
2247 if (stat(consumerd64_bin, &st) == 0) {
2248 DBG3("Found location #1");
2249 consumer_to_use = consumerd64_bin;
2250 } else if (stat(INSTALL_BIN_PATH "/" CONSUMERD_FILE, &st) == 0) {
2251 DBG3("Found location #2");
2252 consumer_to_use = INSTALL_BIN_PATH "/" CONSUMERD_FILE;
2253 } else if (stat(consumerd32_bin, &st) == 0) {
2254 DBG3("Found location #3");
2255 consumer_to_use = consumerd32_bin;
2256 } else {
2257 DBG("Could not find any valid consumerd executable");
2258 ret = -EINVAL;
2259 break;
2260 }
2261 DBG("Using kernel consumer at: %s", consumer_to_use);
2262 ret = execl(consumer_to_use,
2263 "lttng-consumerd", verbosity, "-k",
2264 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2265 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2266 "--group", tracing_group_name,
2267 NULL);
2268 break;
2269 case LTTNG_CONSUMER64_UST:
2270 {
2271 char *tmpnew = NULL;
2272
2273 if (consumerd64_libdir[0] != '\0') {
2274 char *tmp;
2275 size_t tmplen;
2276
2277 tmp = getenv("LD_LIBRARY_PATH");
2278 if (!tmp) {
2279 tmp = "";
2280 }
2281 tmplen = strlen("LD_LIBRARY_PATH=")
2282 + strlen(consumerd64_libdir) + 1 /* : */ + strlen(tmp);
2283 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2284 if (!tmpnew) {
2285 ret = -ENOMEM;
2286 goto error;
2287 }
2288 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2289 strcat(tmpnew, consumerd64_libdir);
2290 if (tmp[0] != '\0') {
2291 strcat(tmpnew, ":");
2292 strcat(tmpnew, tmp);
2293 }
2294 ret = putenv(tmpnew);
2295 if (ret) {
2296 ret = -errno;
2297 free(tmpnew);
2298 goto error;
2299 }
2300 }
2301 DBG("Using 64-bit UST consumer at: %s", consumerd64_bin);
2302 ret = execl(consumerd64_bin, "lttng-consumerd", verbosity, "-u",
2303 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2304 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2305 "--group", tracing_group_name,
2306 NULL);
2307 if (consumerd64_libdir[0] != '\0') {
2308 free(tmpnew);
2309 }
2310 break;
2311 }
2312 case LTTNG_CONSUMER32_UST:
2313 {
2314 char *tmpnew = NULL;
2315
2316 if (consumerd32_libdir[0] != '\0') {
2317 char *tmp;
2318 size_t tmplen;
2319
2320 tmp = getenv("LD_LIBRARY_PATH");
2321 if (!tmp) {
2322 tmp = "";
2323 }
2324 tmplen = strlen("LD_LIBRARY_PATH=")
2325 + strlen(consumerd32_libdir) + 1 /* : */ + strlen(tmp);
2326 tmpnew = zmalloc(tmplen + 1 /* \0 */);
2327 if (!tmpnew) {
2328 ret = -ENOMEM;
2329 goto error;
2330 }
2331 strcpy(tmpnew, "LD_LIBRARY_PATH=");
2332 strcat(tmpnew, consumerd32_libdir);
2333 if (tmp[0] != '\0') {
2334 strcat(tmpnew, ":");
2335 strcat(tmpnew, tmp);
2336 }
2337 ret = putenv(tmpnew);
2338 if (ret) {
2339 ret = -errno;
2340 free(tmpnew);
2341 goto error;
2342 }
2343 }
2344 DBG("Using 32-bit UST consumer at: %s", consumerd32_bin);
2345 ret = execl(consumerd32_bin, "lttng-consumerd", verbosity, "-u",
2346 "--consumerd-cmd-sock", consumer_data->cmd_unix_sock_path,
2347 "--consumerd-err-sock", consumer_data->err_unix_sock_path,
2348 "--group", tracing_group_name,
2349 NULL);
2350 if (consumerd32_libdir[0] != '\0') {
2351 free(tmpnew);
2352 }
2353 break;
2354 }
2355 default:
2356 PERROR("unknown consumer type");
2357 exit(EXIT_FAILURE);
2358 }
2359 if (errno != 0) {
2360 PERROR("Consumer execl()");
2361 }
2362 /* Reaching this point, we got a failure on our execl(). */
2363 exit(EXIT_FAILURE);
2364 } else if (pid > 0) {
2365 ret = pid;
2366 } else {
2367 PERROR("start consumer fork");
2368 ret = -errno;
2369 }
2370 error:
2371 return ret;
2372 }
2373
2374 /*
2375 * Spawn the consumerd daemon and session daemon thread.
2376 */
2377 static int start_consumerd(struct consumer_data *consumer_data)
2378 {
2379 int ret;
2380
2381 /*
2382 * Set the listen() state on the socket since there is a possible race
2383 * between the exec() of the consumer daemon and this call if place in the
2384 * consumer thread. See bug #366 for more details.
2385 */
2386 ret = lttcomm_listen_unix_sock(consumer_data->err_sock);
2387 if (ret < 0) {
2388 goto error;
2389 }
2390
2391 pthread_mutex_lock(&consumer_data->pid_mutex);
2392 if (consumer_data->pid != 0) {
2393 pthread_mutex_unlock(&consumer_data->pid_mutex);
2394 goto end;
2395 }
2396
2397 ret = spawn_consumerd(consumer_data);
2398 if (ret < 0) {
2399 ERR("Spawning consumerd failed");
2400 pthread_mutex_unlock(&consumer_data->pid_mutex);
2401 goto error;
2402 }
2403
2404 /* Setting up the consumer_data pid */
2405 consumer_data->pid = ret;
2406 DBG2("Consumer pid %d", consumer_data->pid);
2407 pthread_mutex_unlock(&consumer_data->pid_mutex);
2408
2409 DBG2("Spawning consumer control thread");
2410 ret = spawn_consumer_thread(consumer_data);
2411 if (ret < 0) {
2412 ERR("Fatal error spawning consumer control thread");
2413 goto error;
2414 }
2415
2416 end:
2417 return 0;
2418
2419 error:
2420 /* Cleanup already created sockets on error. */
2421 if (consumer_data->err_sock >= 0) {
2422 int err;
2423
2424 err = close(consumer_data->err_sock);
2425 if (err < 0) {
2426 PERROR("close consumer data error socket");
2427 }
2428 }
2429 return ret;
2430 }
2431
2432 /*
2433 * Setup necessary data for kernel tracer action.
2434 */
2435 static int init_kernel_tracer(void)
2436 {
2437 int ret;
2438
2439 /* Modprobe lttng kernel modules */
2440 ret = modprobe_lttng_control();
2441 if (ret < 0) {
2442 goto error;
2443 }
2444
2445 /* Open debugfs lttng */
2446 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2447 if (kernel_tracer_fd < 0) {
2448 DBG("Failed to open %s", module_proc_lttng);
2449 ret = -1;
2450 goto error_open;
2451 }
2452
2453 /* Validate kernel version */
2454 ret = kernel_validate_version(kernel_tracer_fd);
2455 if (ret < 0) {
2456 goto error_version;
2457 }
2458
2459 ret = modprobe_lttng_data();
2460 if (ret < 0) {
2461 goto error_modules;
2462 }
2463
2464 DBG("Kernel tracer fd %d", kernel_tracer_fd);
2465 return 0;
2466
2467 error_version:
2468 modprobe_remove_lttng_control();
2469 ret = close(kernel_tracer_fd);
2470 if (ret) {
2471 PERROR("close");
2472 }
2473 kernel_tracer_fd = -1;
2474 return LTTNG_ERR_KERN_VERSION;
2475
2476 error_modules:
2477 ret = close(kernel_tracer_fd);
2478 if (ret) {
2479 PERROR("close");
2480 }
2481
2482 error_open:
2483 modprobe_remove_lttng_control();
2484
2485 error:
2486 WARN("No kernel tracer available");
2487 kernel_tracer_fd = -1;
2488 if (!is_root) {
2489 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2490 } else {
2491 return LTTNG_ERR_KERN_NA;
2492 }
2493 }
2494
2495
2496 /*
2497 * Copy consumer output from the tracing session to the domain session. The
2498 * function also applies the right modification on a per domain basis for the
2499 * trace files destination directory.
2500 *
2501 * Should *NOT* be called with RCU read-side lock held.
2502 */
2503 static int copy_session_consumer(int domain, struct ltt_session *session)
2504 {
2505 int ret;
2506 const char *dir_name;
2507 struct consumer_output *consumer;
2508
2509 assert(session);
2510 assert(session->consumer);
2511
2512 switch (domain) {
2513 case LTTNG_DOMAIN_KERNEL:
2514 DBG3("Copying tracing session consumer output in kernel session");
2515 /*
2516 * XXX: We should audit the session creation and what this function
2517 * does "extra" in order to avoid a destroy since this function is used
2518 * in the domain session creation (kernel and ust) only. Same for UST
2519 * domain.
2520 */
2521 if (session->kernel_session->consumer) {
2522 consumer_destroy_output(session->kernel_session->consumer);
2523 }
2524 session->kernel_session->consumer =
2525 consumer_copy_output(session->consumer);
2526 /* Ease our life a bit for the next part */
2527 consumer = session->kernel_session->consumer;
2528 dir_name = DEFAULT_KERNEL_TRACE_DIR;
2529 break;
2530 case LTTNG_DOMAIN_JUL:
2531 case LTTNG_DOMAIN_UST:
2532 DBG3("Copying tracing session consumer output in UST session");
2533 if (session->ust_session->consumer) {
2534 consumer_destroy_output(session->ust_session->consumer);
2535 }
2536 session->ust_session->consumer =
2537 consumer_copy_output(session->consumer);
2538 /* Ease our life a bit for the next part */
2539 consumer = session->ust_session->consumer;
2540 dir_name = DEFAULT_UST_TRACE_DIR;
2541 break;
2542 default:
2543 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2544 goto error;
2545 }
2546
2547 /* Append correct directory to subdir */
2548 strncat(consumer->subdir, dir_name,
2549 sizeof(consumer->subdir) - strlen(consumer->subdir) - 1);
2550 DBG3("Copy session consumer subdir %s", consumer->subdir);
2551
2552 ret = LTTNG_OK;
2553
2554 error:
2555 return ret;
2556 }
2557
2558 /*
2559 * Create an UST session and add it to the session ust list.
2560 *
2561 * Should *NOT* be called with RCU read-side lock held.
2562 */
2563 static int create_ust_session(struct ltt_session *session,
2564 struct lttng_domain *domain)
2565 {
2566 int ret;
2567 struct ltt_ust_session *lus = NULL;
2568
2569 assert(session);
2570 assert(domain);
2571 assert(session->consumer);
2572
2573 switch (domain->type) {
2574 case LTTNG_DOMAIN_JUL:
2575 case LTTNG_DOMAIN_UST:
2576 break;
2577 default:
2578 ERR("Unknown UST domain on create session %d", domain->type);
2579 ret = LTTNG_ERR_UNKNOWN_DOMAIN;
2580 goto error;
2581 }
2582
2583 DBG("Creating UST session");
2584
2585 lus = trace_ust_create_session(session->id);
2586 if (lus == NULL) {
2587 ret = LTTNG_ERR_UST_SESS_FAIL;
2588 goto error;
2589 }
2590
2591 lus->uid = session->uid;
2592 lus->gid = session->gid;
2593 lus->output_traces = session->output_traces;
2594 lus->snapshot_mode = session->snapshot_mode;
2595 lus->live_timer_interval = session->live_timer;
2596 session->ust_session = lus;
2597
2598 /* Copy session output to the newly created UST session */
2599 ret = copy_session_consumer(domain->type, session);
2600 if (ret != LTTNG_OK) {
2601 goto error;
2602 }
2603
2604 return LTTNG_OK;
2605
2606 error:
2607 free(lus);
2608 session->ust_session = NULL;
2609 return ret;
2610 }
2611
2612 /*
2613 * Create a kernel tracer session then create the default channel.
2614 */
2615 static int create_kernel_session(struct ltt_session *session)
2616 {
2617 int ret;
2618
2619 DBG("Creating kernel session");
2620
2621 ret = kernel_create_session(session, kernel_tracer_fd);
2622 if (ret < 0) {
2623 ret = LTTNG_ERR_KERN_SESS_FAIL;
2624 goto error;
2625 }
2626
2627 /* Code flow safety */
2628 assert(session->kernel_session);
2629
2630 /* Copy session output to the newly created Kernel session */
2631 ret = copy_session_consumer(LTTNG_DOMAIN_KERNEL, session);
2632 if (ret != LTTNG_OK) {
2633 goto error;
2634 }
2635
2636 /* Create directory(ies) on local filesystem. */
2637 if (session->kernel_session->consumer->type == CONSUMER_DST_LOCAL &&
2638 strlen(session->kernel_session->consumer->dst.trace_path) > 0) {
2639 ret = run_as_mkdir_recursive(
2640 session->kernel_session->consumer->dst.trace_path,
2641 S_IRWXU | S_IRWXG, session->uid, session->gid);
2642 if (ret < 0) {
2643 if (ret != -EEXIST) {
2644 ERR("Trace directory creation error");
2645 goto error;
2646 }
2647 }
2648 }
2649
2650 session->kernel_session->uid = session->uid;
2651 session->kernel_session->gid = session->gid;
2652 session->kernel_session->output_traces = session->output_traces;
2653 session->kernel_session->snapshot_mode = session->snapshot_mode;
2654
2655 return LTTNG_OK;
2656
2657 error:
2658 trace_kernel_destroy_session(session->kernel_session);
2659 session->kernel_session = NULL;
2660 return ret;
2661 }
2662
2663 /*
2664 * Count number of session permitted by uid/gid.
2665 */
2666 static unsigned int lttng_sessions_count(uid_t uid, gid_t gid)
2667 {
2668 unsigned int i = 0;
2669 struct ltt_session *session;
2670
2671 DBG("Counting number of available session for UID %d GID %d",
2672 uid, gid);
2673 cds_list_for_each_entry(session, &session_list_ptr->head, list) {
2674 /*
2675 * Only list the sessions the user can control.
2676 */
2677 if (!session_access_ok(session, uid, gid)) {
2678 continue;
2679 }
2680 i++;
2681 }
2682 return i;
2683 }
2684
2685 /*
2686 * Process the command requested by the lttng client within the command
2687 * context structure. This function make sure that the return structure (llm)
2688 * is set and ready for transmission before returning.
2689 *
2690 * Return any error encountered or 0 for success.
2691 *
2692 * "sock" is only used for special-case var. len data.
2693 *
2694 * Should *NOT* be called with RCU read-side lock held.
2695 */
2696 static int process_client_msg(struct command_ctx *cmd_ctx, int sock,
2697 int *sock_error)
2698 {
2699 int ret = LTTNG_OK;
2700 int need_tracing_session = 1;
2701 int need_domain;
2702
2703 DBG("Processing client command %d", cmd_ctx->lsm->cmd_type);
2704
2705 *sock_error = 0;
2706
2707 switch (cmd_ctx->lsm->cmd_type) {
2708 case LTTNG_CREATE_SESSION:
2709 case LTTNG_CREATE_SESSION_SNAPSHOT:
2710 case LTTNG_CREATE_SESSION_LIVE:
2711 case LTTNG_DESTROY_SESSION:
2712 case LTTNG_LIST_SESSIONS:
2713 case LTTNG_LIST_DOMAINS:
2714 case LTTNG_START_TRACE:
2715 case LTTNG_STOP_TRACE:
2716 case LTTNG_DATA_PENDING:
2717 case LTTNG_SNAPSHOT_ADD_OUTPUT:
2718 case LTTNG_SNAPSHOT_DEL_OUTPUT:
2719 case LTTNG_SNAPSHOT_LIST_OUTPUT:
2720 case LTTNG_SNAPSHOT_RECORD:
2721 need_domain = 0;
2722 break;
2723 default:
2724 need_domain = 1;
2725 }
2726
2727 if (opt_no_kernel && need_domain
2728 && cmd_ctx->lsm->domain.type == LTTNG_DOMAIN_KERNEL) {
2729 if (!is_root) {
2730 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2731 } else {
2732 ret = LTTNG_ERR_KERN_NA;
2733 }
2734 goto error;
2735 }
2736
2737 /* Deny register consumer if we already have a spawned consumer. */
2738 if (cmd_ctx->lsm->cmd_type == LTTNG_REGISTER_CONSUMER) {
2739 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2740 if (kconsumer_data.pid > 0) {
2741 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2742 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2743 goto error;
2744 }
2745 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2746 }
2747
2748 /*
2749 * Check for command that don't needs to allocate a returned payload. We do
2750 * this here so we don't have to make the call for no payload at each
2751 * command.
2752 */
2753 switch(cmd_ctx->lsm->cmd_type) {
2754 case LTTNG_LIST_SESSIONS:
2755 case LTTNG_LIST_TRACEPOINTS:
2756 case LTTNG_LIST_TRACEPOINT_FIELDS:
2757 case LTTNG_LIST_DOMAINS:
2758 case LTTNG_LIST_CHANNELS:
2759 case LTTNG_LIST_EVENTS:
2760 break;
2761 default:
2762 /* Setup lttng message with no payload */
2763 ret = setup_lttng_msg(cmd_ctx, 0);
2764 if (ret < 0) {
2765 /* This label does not try to unlock the session */
2766 goto init_setup_error;
2767 }
2768 }
2769
2770 /* Commands that DO NOT need a session. */
2771 switch (cmd_ctx->lsm->cmd_type) {
2772 case LTTNG_CREATE_SESSION:
2773 case LTTNG_CREATE_SESSION_SNAPSHOT:
2774 case LTTNG_CREATE_SESSION_LIVE:
2775 case LTTNG_CALIBRATE:
2776 case LTTNG_LIST_SESSIONS:
2777 case LTTNG_LIST_TRACEPOINTS:
2778 case LTTNG_LIST_TRACEPOINT_FIELDS:
2779 need_tracing_session = 0;
2780 break;
2781 default:
2782 DBG("Getting session %s by name", cmd_ctx->lsm->session.name);
2783 /*
2784 * We keep the session list lock across _all_ commands
2785 * for now, because the per-session lock does not
2786 * handle teardown properly.
2787 */
2788 session_lock_list();
2789 cmd_ctx->session = session_find_by_name(cmd_ctx->lsm->session.name);
2790 if (cmd_ctx->session == NULL) {
2791 ret = LTTNG_ERR_SESS_NOT_FOUND;
2792 goto error;
2793 } else {
2794 /* Acquire lock for the session */
2795 session_lock(cmd_ctx->session);
2796 }
2797 break;
2798 }
2799
2800 if (!need_domain) {
2801 goto skip_domain;
2802 }
2803
2804 /*
2805 * Check domain type for specific "pre-action".
2806 */
2807 switch (cmd_ctx->lsm->domain.type) {
2808 case LTTNG_DOMAIN_KERNEL:
2809 if (!is_root) {
2810 ret = LTTNG_ERR_NEED_ROOT_SESSIOND;
2811 goto error;
2812 }
2813
2814 /* Kernel tracer check */
2815 if (kernel_tracer_fd == -1) {
2816 /* Basically, load kernel tracer modules */
2817 ret = init_kernel_tracer();
2818 if (ret != 0) {
2819 goto error;
2820 }
2821 }
2822
2823 /* Consumer is in an ERROR state. Report back to client */
2824 if (uatomic_read(&kernel_consumerd_state) == CONSUMER_ERROR) {
2825 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2826 goto error;
2827 }
2828
2829 /* Need a session for kernel command */
2830 if (need_tracing_session) {
2831 if (cmd_ctx->session->kernel_session == NULL) {
2832 ret = create_kernel_session(cmd_ctx->session);
2833 if (ret < 0) {
2834 ret = LTTNG_ERR_KERN_SESS_FAIL;
2835 goto error;
2836 }
2837 }
2838
2839 /* Start the kernel consumer daemon */
2840 pthread_mutex_lock(&kconsumer_data.pid_mutex);
2841 if (kconsumer_data.pid == 0 &&
2842 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2843 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2844 ret = start_consumerd(&kconsumer_data);
2845 if (ret < 0) {
2846 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2847 goto error;
2848 }
2849 uatomic_set(&kernel_consumerd_state, CONSUMER_STARTED);
2850 } else {
2851 pthread_mutex_unlock(&kconsumer_data.pid_mutex);
2852 }
2853
2854 /*
2855 * The consumer was just spawned so we need to add the socket to
2856 * the consumer output of the session if exist.
2857 */
2858 ret = consumer_create_socket(&kconsumer_data,
2859 cmd_ctx->session->kernel_session->consumer);
2860 if (ret < 0) {
2861 goto error;
2862 }
2863 }
2864
2865 break;
2866 case LTTNG_DOMAIN_JUL:
2867 case LTTNG_DOMAIN_UST:
2868 {
2869 if (!ust_app_supported()) {
2870 ret = LTTNG_ERR_NO_UST;
2871 goto error;
2872 }
2873 /* Consumer is in an ERROR state. Report back to client */
2874 if (uatomic_read(&ust_consumerd_state) == CONSUMER_ERROR) {
2875 ret = LTTNG_ERR_NO_USTCONSUMERD;
2876 goto error;
2877 }
2878
2879 if (need_tracing_session) {
2880 /* Create UST session if none exist. */
2881 if (cmd_ctx->session->ust_session == NULL) {
2882 ret = create_ust_session(cmd_ctx->session,
2883 &cmd_ctx->lsm->domain);
2884 if (ret != LTTNG_OK) {
2885 goto error;
2886 }
2887 }
2888
2889 /* Start the UST consumer daemons */
2890 /* 64-bit */
2891 pthread_mutex_lock(&ustconsumer64_data.pid_mutex);
2892 if (consumerd64_bin[0] != '\0' &&
2893 ustconsumer64_data.pid == 0 &&
2894 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2895 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2896 ret = start_consumerd(&ustconsumer64_data);
2897 if (ret < 0) {
2898 ret = LTTNG_ERR_UST_CONSUMER64_FAIL;
2899 uatomic_set(&ust_consumerd64_fd, -EINVAL);
2900 goto error;
2901 }
2902
2903 uatomic_set(&ust_consumerd64_fd, ustconsumer64_data.cmd_sock);
2904 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2905 } else {
2906 pthread_mutex_unlock(&ustconsumer64_data.pid_mutex);
2907 }
2908
2909 /*
2910 * Setup socket for consumer 64 bit. No need for atomic access
2911 * since it was set above and can ONLY be set in this thread.
2912 */
2913 ret = consumer_create_socket(&ustconsumer64_data,
2914 cmd_ctx->session->ust_session->consumer);
2915 if (ret < 0) {
2916 goto error;
2917 }
2918
2919 /* 32-bit */
2920 if (consumerd32_bin[0] != '\0' &&
2921 ustconsumer32_data.pid == 0 &&
2922 cmd_ctx->lsm->cmd_type != LTTNG_REGISTER_CONSUMER) {
2923 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2924 ret = start_consumerd(&ustconsumer32_data);
2925 if (ret < 0) {
2926 ret = LTTNG_ERR_UST_CONSUMER32_FAIL;
2927 uatomic_set(&ust_consumerd32_fd, -EINVAL);
2928 goto error;
2929 }
2930
2931 uatomic_set(&ust_consumerd32_fd, ustconsumer32_data.cmd_sock);
2932 uatomic_set(&ust_consumerd_state, CONSUMER_STARTED);
2933 } else {
2934 pthread_mutex_unlock(&ustconsumer32_data.pid_mutex);
2935 }
2936
2937 /*
2938 * Setup socket for consumer 64 bit. No need for atomic access
2939 * since it was set above and can ONLY be set in this thread.
2940 */
2941 ret = consumer_create_socket(&ustconsumer32_data,
2942 cmd_ctx->session->ust_session->consumer);
2943 if (ret < 0) {
2944 goto error;
2945 }
2946 }
2947 break;
2948 }
2949 default:
2950 break;
2951 }
2952 skip_domain:
2953
2954 /* Validate consumer daemon state when start/stop trace command */
2955 if (cmd_ctx->lsm->cmd_type == LTTNG_START_TRACE ||
2956 cmd_ctx->lsm->cmd_type == LTTNG_STOP_TRACE) {
2957 switch (cmd_ctx->lsm->domain.type) {
2958 case LTTNG_DOMAIN_JUL:
2959 case LTTNG_DOMAIN_UST:
2960 if (uatomic_read(&ust_consumerd_state) != CONSUMER_STARTED) {
2961 ret = LTTNG_ERR_NO_USTCONSUMERD;
2962 goto error;
2963 }
2964 break;
2965 case LTTNG_DOMAIN_KERNEL:
2966 if (uatomic_read(&kernel_consumerd_state) != CONSUMER_STARTED) {
2967 ret = LTTNG_ERR_NO_KERNCONSUMERD;
2968 goto error;
2969 }
2970 break;
2971 }
2972 }
2973
2974 /*
2975 * Check that the UID or GID match that of the tracing session.
2976 * The root user can interact with all sessions.
2977 */
2978 if (need_tracing_session) {
2979 if (!session_access_ok(cmd_ctx->session,
2980 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
2981 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds))) {
2982 ret = LTTNG_ERR_EPERM;
2983 goto error;
2984 }
2985 }
2986
2987 /*
2988 * Send relayd information to consumer as soon as we have a domain and a
2989 * session defined.
2990 */
2991 if (cmd_ctx->session && need_domain) {
2992 /*
2993 * Setup relayd if not done yet. If the relayd information was already
2994 * sent to the consumer, this call will gracefully return.
2995 */
2996 ret = cmd_setup_relayd(cmd_ctx->session);
2997 if (ret != LTTNG_OK) {
2998 goto error;
2999 }
3000 }
3001
3002 /* Process by command type */
3003 switch (cmd_ctx->lsm->cmd_type) {
3004 case LTTNG_ADD_CONTEXT:
3005 {
3006 ret = cmd_add_context(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3007 cmd_ctx->lsm->u.context.channel_name,
3008 &cmd_ctx->lsm->u.context.ctx, kernel_poll_pipe[1]);
3009 break;
3010 }
3011 case LTTNG_DISABLE_CHANNEL:
3012 {
3013 ret = cmd_disable_channel(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3014 cmd_ctx->lsm->u.disable.channel_name);
3015 break;
3016 }
3017 case LTTNG_DISABLE_EVENT:
3018 {
3019 ret = cmd_disable_event(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3020 cmd_ctx->lsm->u.disable.channel_name,
3021 cmd_ctx->lsm->u.disable.name);
3022 break;
3023 }
3024 case LTTNG_DISABLE_ALL_EVENT:
3025 {
3026 DBG("Disabling all events");
3027
3028 ret = cmd_disable_event_all(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3029 cmd_ctx->lsm->u.disable.channel_name);
3030 break;
3031 }
3032 case LTTNG_ENABLE_CHANNEL:
3033 {
3034 ret = cmd_enable_channel(cmd_ctx->session, &cmd_ctx->lsm->domain,
3035 &cmd_ctx->lsm->u.channel.chan, kernel_poll_pipe[1]);
3036 break;
3037 }
3038 case LTTNG_ENABLE_EVENT:
3039 {
3040 struct lttng_event_exclusion *exclusion = NULL;
3041 struct lttng_filter_bytecode *bytecode = NULL;
3042
3043 /* Handle exclusion events and receive it from the client. */
3044 if (cmd_ctx->lsm->u.enable.exclusion_count > 0) {
3045 size_t count = cmd_ctx->lsm->u.enable.exclusion_count;
3046
3047 exclusion = zmalloc(sizeof(struct lttng_event_exclusion) +
3048 (count * LTTNG_SYMBOL_NAME_LEN));
3049 if (!exclusion) {
3050 ret = LTTNG_ERR_EXCLUSION_NOMEM;
3051 goto error;
3052 }
3053
3054 DBG("Receiving var len exclusion event list from client ...");
3055 exclusion->count = count;
3056 ret = lttcomm_recv_unix_sock(sock, exclusion->names,
3057 count * LTTNG_SYMBOL_NAME_LEN);
3058 if (ret <= 0) {
3059 DBG("Nothing recv() from client var len data... continuing");
3060 *sock_error = 1;
3061 free(exclusion);
3062 ret = LTTNG_ERR_EXCLUSION_INVAL;
3063 goto error;
3064 }
3065 }
3066
3067 /* Handle filter and get bytecode from client. */
3068 if (cmd_ctx->lsm->u.enable.bytecode_len > 0) {
3069 size_t bytecode_len = cmd_ctx->lsm->u.enable.bytecode_len;
3070
3071 if (bytecode_len > LTTNG_FILTER_MAX_LEN) {
3072 ret = LTTNG_ERR_FILTER_INVAL;
3073 free(exclusion);
3074 goto error;
3075 }
3076
3077 bytecode = zmalloc(bytecode_len);
3078 if (!bytecode) {
3079 free(exclusion);
3080 ret = LTTNG_ERR_FILTER_NOMEM;
3081 goto error;
3082 }
3083
3084 /* Receive var. len. data */
3085 DBG("Receiving var len filter's bytecode from client ...");
3086 ret = lttcomm_recv_unix_sock(sock, bytecode, bytecode_len);
3087 if (ret <= 0) {
3088 DBG("Nothing recv() from client car len data... continuing");
3089 *sock_error = 1;
3090 free(bytecode);
3091 free(exclusion);
3092 ret = LTTNG_ERR_FILTER_INVAL;
3093 goto error;
3094 }
3095
3096 if ((bytecode->len + sizeof(*bytecode)) != bytecode_len) {
3097 free(bytecode);
3098 free(exclusion);
3099 ret = LTTNG_ERR_FILTER_INVAL;
3100 goto error;
3101 }
3102 }
3103
3104 ret = cmd_enable_event(cmd_ctx->session, &cmd_ctx->lsm->domain,
3105 cmd_ctx->lsm->u.enable.channel_name,
3106 &cmd_ctx->lsm->u.enable.event, bytecode, exclusion,
3107 kernel_poll_pipe[1]);
3108 break;
3109 }
3110 case LTTNG_ENABLE_ALL_EVENT:
3111 {
3112 DBG("Enabling all events");
3113
3114 ret = cmd_enable_event_all(cmd_ctx->session, &cmd_ctx->lsm->domain,
3115 cmd_ctx->lsm->u.enable.channel_name,
3116 cmd_ctx->lsm->u.enable.event.type, NULL, kernel_poll_pipe[1]);
3117 break;
3118 }
3119 case LTTNG_LIST_TRACEPOINTS:
3120 {
3121 struct lttng_event *events;
3122 ssize_t nb_events;
3123
3124 nb_events = cmd_list_tracepoints(cmd_ctx->lsm->domain.type, &events);
3125 if (nb_events < 0) {
3126 /* Return value is a negative lttng_error_code. */
3127 ret = -nb_events;
3128 goto error;
3129 }
3130
3131 /*
3132 * Setup lttng message with payload size set to the event list size in
3133 * bytes and then copy list into the llm payload.
3134 */
3135 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_event) * nb_events);
3136 if (ret < 0) {
3137 free(events);
3138 goto setup_error;
3139 }
3140
3141 /* Copy event list into message payload */
3142 memcpy(cmd_ctx->llm->payload, events,
3143 sizeof(struct lttng_event) * nb_events);
3144
3145 free(events);
3146
3147 ret = LTTNG_OK;
3148 break;
3149 }
3150 case LTTNG_LIST_TRACEPOINT_FIELDS:
3151 {
3152 struct lttng_event_field *fields;
3153 ssize_t nb_fields;
3154
3155 nb_fields = cmd_list_tracepoint_fields(cmd_ctx->lsm->domain.type,
3156 &fields);
3157 if (nb_fields < 0) {
3158 /* Return value is a negative lttng_error_code. */
3159 ret = -nb_fields;
3160 goto error;
3161 }
3162
3163 /*
3164 * Setup lttng message with payload size set to the event list size in
3165 * bytes and then copy list into the llm payload.
3166 */
3167 ret = setup_lttng_msg(cmd_ctx,
3168 sizeof(struct lttng_event_field) * nb_fields);
3169 if (ret < 0) {
3170 free(fields);
3171 goto setup_error;
3172 }
3173
3174 /* Copy event list into message payload */
3175 memcpy(cmd_ctx->llm->payload, fields,
3176 sizeof(struct lttng_event_field) * nb_fields);
3177
3178 free(fields);
3179
3180 ret = LTTNG_OK;
3181 break;
3182 }
3183 case LTTNG_SET_CONSUMER_URI:
3184 {
3185 size_t nb_uri, len;
3186 struct lttng_uri *uris;
3187
3188 nb_uri = cmd_ctx->lsm->u.uri.size;
3189 len = nb_uri * sizeof(struct lttng_uri);
3190
3191 if (nb_uri == 0) {
3192 ret = LTTNG_ERR_INVALID;
3193 goto error;
3194 }
3195
3196 uris = zmalloc(len);
3197 if (uris == NULL) {
3198 ret = LTTNG_ERR_FATAL;
3199 goto error;
3200 }
3201
3202 /* Receive variable len data */
3203 DBG("Receiving %zu URI(s) from client ...", nb_uri);
3204 ret = lttcomm_recv_unix_sock(sock, uris, len);
3205 if (ret <= 0) {
3206 DBG("No URIs received from client... continuing");
3207 *sock_error = 1;
3208 ret = LTTNG_ERR_SESSION_FAIL;
3209 free(uris);
3210 goto error;
3211 }
3212
3213 ret = cmd_set_consumer_uri(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3214 nb_uri, uris);
3215 if (ret != LTTNG_OK) {
3216 free(uris);
3217 goto error;
3218 }
3219
3220 /*
3221 * XXX: 0 means that this URI should be applied on the session. Should
3222 * be a DOMAIN enuam.
3223 */
3224 if (cmd_ctx->lsm->domain.type == 0) {
3225 /* Add the URI for the UST session if a consumer is present. */
3226 if (cmd_ctx->session->ust_session &&
3227 cmd_ctx->session->ust_session->consumer) {
3228 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_UST, cmd_ctx->session,
3229 nb_uri, uris);
3230 } else if (cmd_ctx->session->kernel_session &&
3231 cmd_ctx->session->kernel_session->consumer) {
3232 ret = cmd_set_consumer_uri(LTTNG_DOMAIN_KERNEL,
3233 cmd_ctx->session, nb_uri, uris);
3234 }
3235 }
3236
3237 free(uris);
3238
3239 break;
3240 }
3241 case LTTNG_START_TRACE:
3242 {
3243 ret = cmd_start_trace(cmd_ctx->session);
3244 break;
3245 }
3246 case LTTNG_STOP_TRACE:
3247 {
3248 ret = cmd_stop_trace(cmd_ctx->session);
3249 break;
3250 }
3251 case LTTNG_CREATE_SESSION:
3252 {
3253 size_t nb_uri, len;
3254 struct lttng_uri *uris = NULL;
3255
3256 nb_uri = cmd_ctx->lsm->u.uri.size;
3257 len = nb_uri * sizeof(struct lttng_uri);
3258
3259 if (nb_uri > 0) {
3260 uris = zmalloc(len);
3261 if (uris == NULL) {
3262 ret = LTTNG_ERR_FATAL;
3263 goto error;
3264 }
3265
3266 /* Receive variable len data */
3267 DBG("Waiting for %zu URIs from client ...", nb_uri);
3268 ret = lttcomm_recv_unix_sock(sock, uris, len);
3269 if (ret <= 0) {
3270 DBG("No URIs received from client... continuing");
3271 *sock_error = 1;
3272 ret = LTTNG_ERR_SESSION_FAIL;
3273 free(uris);
3274 goto error;
3275 }
3276
3277 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3278 DBG("Creating session with ONE network URI is a bad call");
3279 ret = LTTNG_ERR_SESSION_FAIL;
3280 free(uris);
3281 goto error;
3282 }
3283 }
3284
3285 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris, nb_uri,
3286 &cmd_ctx->creds, 0);
3287
3288 free(uris);
3289
3290 break;
3291 }
3292 case LTTNG_DESTROY_SESSION:
3293 {
3294 ret = cmd_destroy_session(cmd_ctx->session, kernel_poll_pipe[1]);
3295
3296 /* Set session to NULL so we do not unlock it after free. */
3297 cmd_ctx->session = NULL;
3298 break;
3299 }
3300 case LTTNG_LIST_DOMAINS:
3301 {
3302 ssize_t nb_dom;
3303 struct lttng_domain *domains;
3304
3305 nb_dom = cmd_list_domains(cmd_ctx->session, &domains);
3306 if (nb_dom < 0) {
3307 /* Return value is a negative lttng_error_code. */
3308 ret = -nb_dom;
3309 goto error;
3310 }
3311
3312 ret = setup_lttng_msg(cmd_ctx, nb_dom * sizeof(struct lttng_domain));
3313 if (ret < 0) {
3314 free(domains);
3315 goto setup_error;
3316 }
3317
3318 /* Copy event list into message payload */
3319 memcpy(cmd_ctx->llm->payload, domains,
3320 nb_dom * sizeof(struct lttng_domain));
3321
3322 free(domains);
3323
3324 ret = LTTNG_OK;
3325 break;
3326 }
3327 case LTTNG_LIST_CHANNELS:
3328 {
3329 int nb_chan;
3330 struct lttng_channel *channels;
3331
3332 nb_chan = cmd_list_channels(cmd_ctx->lsm->domain.type,
3333 cmd_ctx->session, &channels);
3334 if (nb_chan < 0) {
3335 /* Return value is a negative lttng_error_code. */
3336 ret = -nb_chan;
3337 goto error;
3338 }
3339
3340 ret = setup_lttng_msg(cmd_ctx, nb_chan * sizeof(struct lttng_channel));
3341 if (ret < 0) {
3342 free(channels);
3343 goto setup_error;
3344 }
3345
3346 /* Copy event list into message payload */
3347 memcpy(cmd_ctx->llm->payload, channels,
3348 nb_chan * sizeof(struct lttng_channel));
3349
3350 free(channels);
3351
3352 ret = LTTNG_OK;
3353 break;
3354 }
3355 case LTTNG_LIST_EVENTS:
3356 {
3357 ssize_t nb_event;
3358 struct lttng_event *events = NULL;
3359
3360 nb_event = cmd_list_events(cmd_ctx->lsm->domain.type, cmd_ctx->session,
3361 cmd_ctx->lsm->u.list.channel_name, &events);
3362 if (nb_event < 0) {
3363 /* Return value is a negative lttng_error_code. */
3364 ret = -nb_event;
3365 goto error;
3366 }
3367
3368 ret = setup_lttng_msg(cmd_ctx, nb_event * sizeof(struct lttng_event));
3369 if (ret < 0) {
3370 free(events);
3371 goto setup_error;
3372 }
3373
3374 /* Copy event list into message payload */
3375 memcpy(cmd_ctx->llm->payload, events,
3376 nb_event * sizeof(struct lttng_event));
3377
3378 free(events);
3379
3380 ret = LTTNG_OK;
3381 break;
3382 }
3383 case LTTNG_LIST_SESSIONS:
3384 {
3385 unsigned int nr_sessions;
3386
3387 session_lock_list();
3388 nr_sessions = lttng_sessions_count(
3389 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3390 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3391
3392 ret = setup_lttng_msg(cmd_ctx, sizeof(struct lttng_session) * nr_sessions);
3393 if (ret < 0) {
3394 session_unlock_list();
3395 goto setup_error;
3396 }
3397
3398 /* Filled the session array */
3399 cmd_list_lttng_sessions((struct lttng_session *)(cmd_ctx->llm->payload),
3400 LTTNG_SOCK_GET_UID_CRED(&cmd_ctx->creds),
3401 LTTNG_SOCK_GET_GID_CRED(&cmd_ctx->creds));
3402
3403 session_unlock_list();
3404
3405 ret = LTTNG_OK;
3406 break;
3407 }
3408 case LTTNG_CALIBRATE:
3409 {
3410 ret = cmd_calibrate(cmd_ctx->lsm->domain.type,
3411 &cmd_ctx->lsm->u.calibrate);
3412 break;
3413 }
3414 case LTTNG_REGISTER_CONSUMER:
3415 {
3416 struct consumer_data *cdata;
3417
3418 switch (cmd_ctx->lsm->domain.type) {
3419 case LTTNG_DOMAIN_KERNEL:
3420 cdata = &kconsumer_data;
3421 break;
3422 default:
3423 ret = LTTNG_ERR_UND;
3424 goto error;
3425 }
3426
3427 ret = cmd_register_consumer(cmd_ctx->session, cmd_ctx->lsm->domain.type,
3428 cmd_ctx->lsm->u.reg.path, cdata);
3429 break;
3430 }
3431 case LTTNG_DATA_PENDING:
3432 {
3433 ret = cmd_data_pending(cmd_ctx->session);
3434 break;
3435 }
3436 case LTTNG_SNAPSHOT_ADD_OUTPUT:
3437 {
3438 struct lttcomm_lttng_output_id reply;
3439
3440 ret = cmd_snapshot_add_output(cmd_ctx->session,
3441 &cmd_ctx->lsm->u.snapshot_output.output, &reply.id);
3442 if (ret != LTTNG_OK) {
3443 goto error;
3444 }
3445
3446 ret = setup_lttng_msg(cmd_ctx, sizeof(reply));
3447 if (ret < 0) {
3448 goto setup_error;
3449 }
3450
3451 /* Copy output list into message payload */
3452 memcpy(cmd_ctx->llm->payload, &reply, sizeof(reply));
3453 ret = LTTNG_OK;
3454 break;
3455 }
3456 case LTTNG_SNAPSHOT_DEL_OUTPUT:
3457 {
3458 ret = cmd_snapshot_del_output(cmd_ctx->session,
3459 &cmd_ctx->lsm->u.snapshot_output.output);
3460 break;
3461 }
3462 case LTTNG_SNAPSHOT_LIST_OUTPUT:
3463 {
3464 ssize_t nb_output;
3465 struct lttng_snapshot_output *outputs = NULL;
3466
3467 nb_output = cmd_snapshot_list_outputs(cmd_ctx->session, &outputs);
3468 if (nb_output < 0) {
3469 ret = -nb_output;
3470 goto error;
3471 }
3472
3473 ret = setup_lttng_msg(cmd_ctx,
3474 nb_output * sizeof(struct lttng_snapshot_output));
3475 if (ret < 0) {
3476 free(outputs);
3477 goto setup_error;
3478 }
3479
3480 if (outputs) {
3481 /* Copy output list into message payload */
3482 memcpy(cmd_ctx->llm->payload, outputs,
3483 nb_output * sizeof(struct lttng_snapshot_output));
3484 free(outputs);
3485 }
3486
3487 ret = LTTNG_OK;
3488 break;
3489 }
3490 case LTTNG_SNAPSHOT_RECORD:
3491 {
3492 ret = cmd_snapshot_record(cmd_ctx->session,
3493 &cmd_ctx->lsm->u.snapshot_record.output,
3494 cmd_ctx->lsm->u.snapshot_record.wait);
3495 break;
3496 }
3497 case LTTNG_CREATE_SESSION_SNAPSHOT:
3498 {
3499 size_t nb_uri, len;
3500 struct lttng_uri *uris = NULL;
3501
3502 nb_uri = cmd_ctx->lsm->u.uri.size;
3503 len = nb_uri * sizeof(struct lttng_uri);
3504
3505 if (nb_uri > 0) {
3506 uris = zmalloc(len);
3507 if (uris == NULL) {
3508 ret = LTTNG_ERR_FATAL;
3509 goto error;
3510 }
3511
3512 /* Receive variable len data */
3513 DBG("Waiting for %zu URIs from client ...", nb_uri);
3514 ret = lttcomm_recv_unix_sock(sock, uris, len);
3515 if (ret <= 0) {
3516 DBG("No URIs received from client... continuing");
3517 *sock_error = 1;
3518 ret = LTTNG_ERR_SESSION_FAIL;
3519 free(uris);
3520 goto error;
3521 }
3522
3523 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3524 DBG("Creating session with ONE network URI is a bad call");
3525 ret = LTTNG_ERR_SESSION_FAIL;
3526 free(uris);
3527 goto error;
3528 }
3529 }
3530
3531 ret = cmd_create_session_snapshot(cmd_ctx->lsm->session.name, uris,
3532 nb_uri, &cmd_ctx->creds);
3533 free(uris);
3534 break;
3535 }
3536 case LTTNG_CREATE_SESSION_LIVE:
3537 {
3538 size_t nb_uri, len;
3539 struct lttng_uri *uris = NULL;
3540
3541 nb_uri = cmd_ctx->lsm->u.uri.size;
3542 len = nb_uri * sizeof(struct lttng_uri);
3543
3544 if (nb_uri > 0) {
3545 uris = zmalloc(len);
3546 if (uris == NULL) {
3547 ret = LTTNG_ERR_FATAL;
3548 goto error;
3549 }
3550
3551 /* Receive variable len data */
3552 DBG("Waiting for %zu URIs from client ...", nb_uri);
3553 ret = lttcomm_recv_unix_sock(sock, uris, len);
3554 if (ret <= 0) {
3555 DBG("No URIs received from client... continuing");
3556 *sock_error = 1;
3557 ret = LTTNG_ERR_SESSION_FAIL;
3558 free(uris);
3559 goto error;
3560 }
3561
3562 if (nb_uri == 1 && uris[0].dtype != LTTNG_DST_PATH) {
3563 DBG("Creating session with ONE network URI is a bad call");
3564 ret = LTTNG_ERR_SESSION_FAIL;
3565 free(uris);
3566 goto error;
3567 }
3568 }
3569
3570 ret = cmd_create_session_uri(cmd_ctx->lsm->session.name, uris,
3571 nb_uri, &cmd_ctx->creds, cmd_ctx->lsm->u.session_live.timer_interval);
3572 free(uris);
3573 break;
3574 }
3575 default:
3576 ret = LTTNG_ERR_UND;
3577 break;
3578 }
3579
3580 error:
3581 if (cmd_ctx->llm == NULL) {
3582 DBG("Missing llm structure. Allocating one.");
3583 if (setup_lttng_msg(cmd_ctx, 0) < 0) {
3584 goto setup_error;
3585 }
3586 }
3587 /* Set return code */
3588 cmd_ctx->llm->ret_code = ret;
3589 setup_error:
3590 if (cmd_ctx->session) {
3591 session_unlock(cmd_ctx->session);
3592 }
3593 if (need_tracing_session) {
3594 session_unlock_list();
3595 }
3596 init_setup_error:
3597 return ret;
3598 }
3599
3600 /*
3601 * Thread managing health check socket.
3602 */
3603 static void *thread_manage_health(void *data)
3604 {
3605 int sock = -1, new_sock = -1, ret, i, pollfd, err = -1;
3606 uint32_t revents, nb_fd;
3607 struct lttng_poll_event events;
3608 struct health_comm_msg msg;
3609 struct health_comm_reply reply;
3610
3611 DBG("[thread] Manage health check started");
3612
3613 rcu_register_thread();
3614
3615 /* We might hit an error path before this is created. */
3616 lttng_poll_init(&events);
3617
3618 /* Create unix socket */
3619 sock = lttcomm_create_unix_sock(health_unix_sock_path);
3620 if (sock < 0) {
3621 ERR("Unable to create health check Unix socket");
3622 ret = -1;
3623 goto error;
3624 }
3625
3626 if (is_root) {
3627 /* lttng health client socket path permissions */
3628 ret = chown(health_unix_sock_path, 0,
3629 utils_get_group_id(tracing_group_name));
3630 if (ret < 0) {
3631 ERR("Unable to set group on %s", health_unix_sock_path);
3632 PERROR("chown");
3633 ret = -1;
3634 goto error;
3635 }
3636
3637 ret = chmod(health_unix_sock_path,
3638 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
3639 if (ret < 0) {
3640 ERR("Unable to set permissions on %s", health_unix_sock_path);
3641 PERROR("chmod");
3642 ret = -1;
3643 goto error;
3644 }
3645 }
3646
3647 /*
3648 * Set the CLOEXEC flag. Return code is useless because either way, the
3649 * show must go on.
3650 */
3651 (void) utils_set_fd_cloexec(sock);
3652
3653 ret = lttcomm_listen_unix_sock(sock);
3654 if (ret < 0) {
3655 goto error;
3656 }
3657
3658 /*
3659 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3660 * more will be added to this poll set.
3661 */
3662 ret = sessiond_set_thread_pollset(&events, 2);
3663 if (ret < 0) {
3664 goto error;
3665 }
3666
3667 /* Add the application registration socket */
3668 ret = lttng_poll_add(&events, sock, LPOLLIN | LPOLLPRI);
3669 if (ret < 0) {
3670 goto error;
3671 }
3672
3673 lttng_sessiond_notify_ready();
3674
3675 while (1) {
3676 DBG("Health check ready");
3677
3678 /* Inifinite blocking call, waiting for transmission */
3679 restart:
3680 ret = lttng_poll_wait(&events, -1);
3681 if (ret < 0) {
3682 /*
3683 * Restart interrupted system call.
3684 */
3685 if (errno == EINTR) {
3686 goto restart;
3687 }
3688 goto error;
3689 }
3690
3691 nb_fd = ret;
3692
3693 for (i = 0; i < nb_fd; i++) {
3694 /* Fetch once the poll data */
3695 revents = LTTNG_POLL_GETEV(&events, i);
3696 pollfd = LTTNG_POLL_GETFD(&events, i);
3697
3698 /* Thread quit pipe has been closed. Killing thread. */
3699 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3700 if (ret) {
3701 err = 0;
3702 goto exit;
3703 }
3704
3705 /* Event on the registration socket */
3706 if (pollfd == sock) {
3707 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3708 ERR("Health socket poll error");
3709 goto error;
3710 }
3711 }
3712 }
3713
3714 new_sock = lttcomm_accept_unix_sock(sock);
3715 if (new_sock < 0) {
3716 goto error;
3717 }
3718
3719 /*
3720 * Set the CLOEXEC flag. Return code is useless because either way, the
3721 * show must go on.
3722 */
3723 (void) utils_set_fd_cloexec(new_sock);
3724
3725 DBG("Receiving data from client for health...");
3726 ret = lttcomm_recv_unix_sock(new_sock, (void *)&msg, sizeof(msg));
3727 if (ret <= 0) {
3728 DBG("Nothing recv() from client... continuing");
3729 ret = close(new_sock);
3730 if (ret) {
3731 PERROR("close");
3732 }
3733 new_sock = -1;
3734 continue;
3735 }
3736
3737 rcu_thread_online();
3738
3739 memset(&reply, 0, sizeof(reply));
3740 for (i = 0; i < NR_HEALTH_SESSIOND_TYPES; i++) {
3741 /*
3742 * health_check_state returns 0 if health is
3743 * bad.
3744 */
3745 if (!health_check_state(health_sessiond, i)) {
3746 reply.ret_code |= 1ULL << i;
3747 }
3748 }
3749
3750 DBG2("Health check return value %" PRIx64, reply.ret_code);
3751
3752 ret = send_unix_sock(new_sock, (void *) &reply, sizeof(reply));
3753 if (ret < 0) {
3754 ERR("Failed to send health data back to client");
3755 }
3756
3757 /* End of transmission */
3758 ret = close(new_sock);
3759 if (ret) {
3760 PERROR("close");
3761 }
3762 new_sock = -1;
3763 }
3764
3765 exit:
3766 error:
3767 if (err) {
3768 ERR("Health error occurred in %s", __func__);
3769 }
3770 DBG("Health check thread dying");
3771 unlink(health_unix_sock_path);
3772 if (sock >= 0) {
3773 ret = close(sock);
3774 if (ret) {
3775 PERROR("close");
3776 }
3777 }
3778
3779 lttng_poll_clean(&events);
3780
3781 rcu_unregister_thread();
3782 return NULL;
3783 }
3784
3785 /*
3786 * This thread manage all clients request using the unix client socket for
3787 * communication.
3788 */
3789 static void *thread_manage_clients(void *data)
3790 {
3791 int sock = -1, ret, i, pollfd, err = -1;
3792 int sock_error;
3793 uint32_t revents, nb_fd;
3794 struct command_ctx *cmd_ctx = NULL;
3795 struct lttng_poll_event events;
3796
3797 DBG("[thread] Manage client started");
3798
3799 rcu_register_thread();
3800
3801 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_CMD);
3802
3803 health_code_update();
3804
3805 ret = lttcomm_listen_unix_sock(client_sock);
3806 if (ret < 0) {
3807 goto error_listen;
3808 }
3809
3810 /*
3811 * Pass 2 as size here for the thread quit pipe and client_sock. Nothing
3812 * more will be added to this poll set.
3813 */
3814 ret = sessiond_set_thread_pollset(&events, 2);
3815 if (ret < 0) {
3816 goto error_create_poll;
3817 }
3818
3819 /* Add the application registration socket */
3820 ret = lttng_poll_add(&events, client_sock, LPOLLIN | LPOLLPRI);
3821 if (ret < 0) {
3822 goto error;
3823 }
3824
3825 lttng_sessiond_notify_ready();
3826
3827 /* This testpoint is after we signal readiness to the parent. */
3828 if (testpoint(sessiond_thread_manage_clients)) {
3829 goto error;
3830 }
3831
3832 if (testpoint(sessiond_thread_manage_clients_before_loop)) {
3833 goto error;
3834 }
3835
3836 health_code_update();
3837
3838 while (1) {
3839 DBG("Accepting client command ...");
3840
3841 /* Inifinite blocking call, waiting for transmission */
3842 restart:
3843 health_poll_entry();
3844 ret = lttng_poll_wait(&events, -1);
3845 health_poll_exit();
3846 if (ret < 0) {
3847 /*
3848 * Restart interrupted system call.
3849 */
3850 if (errno == EINTR) {
3851 goto restart;
3852 }
3853 goto error;
3854 }
3855
3856 nb_fd = ret;
3857
3858 for (i = 0; i < nb_fd; i++) {
3859 /* Fetch once the poll data */
3860 revents = LTTNG_POLL_GETEV(&events, i);
3861 pollfd = LTTNG_POLL_GETFD(&events, i);
3862
3863 health_code_update();
3864
3865 /* Thread quit pipe has been closed. Killing thread. */
3866 ret = sessiond_check_thread_quit_pipe(pollfd, revents);
3867 if (ret) {
3868 err = 0;
3869 goto exit;
3870 }
3871
3872 /* Event on the registration socket */
3873 if (pollfd == client_sock) {
3874 if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
3875 ERR("Client socket poll error");
3876 goto error;
3877 }
3878 }
3879 }
3880
3881 DBG("Wait for client response");
3882
3883 health_code_update();
3884
3885 sock = lttcomm_accept_unix_sock(client_sock);
3886 if (sock < 0) {
3887 goto error;
3888 }
3889
3890 /*
3891 * Set the CLOEXEC flag. Return code is useless because either way, the
3892 * show must go on.
3893 */
3894 (void) utils_set_fd_cloexec(sock);
3895
3896 /* Set socket option for credentials retrieval */
3897 ret = lttcomm_setsockopt_creds_unix_sock(sock);
3898 if (ret < 0) {
3899 goto error;
3900 }
3901
3902 /* Allocate context command to process the client request */
3903 cmd_ctx = zmalloc(sizeof(struct command_ctx));
3904 if (cmd_ctx == NULL) {
3905 PERROR("zmalloc cmd_ctx");
3906 goto error;
3907 }
3908
3909 /* Allocate data buffer for reception */
3910 cmd_ctx->lsm = zmalloc(sizeof(struct lttcomm_session_msg));
3911 if (cmd_ctx->lsm == NULL) {
3912 PERROR("zmalloc cmd_ctx->lsm");
3913 goto error;
3914 }
3915
3916 cmd_ctx->llm = NULL;
3917 cmd_ctx->session = NULL;
3918
3919 health_code_update();
3920
3921 /*
3922 * Data is received from the lttng client. The struct
3923 * lttcomm_session_msg (lsm) contains the command and data request of
3924 * the client.
3925 */
3926 DBG("Receiving data from client ...");
3927 ret = lttcomm_recv_creds_unix_sock(sock, cmd_ctx->lsm,
3928 sizeof(struct lttcomm_session_msg), &cmd_ctx->creds);
3929 if (ret <= 0) {
3930 DBG("Nothing recv() from client... continuing");
3931 ret = close(sock);
3932 if (ret) {
3933 PERROR("close");
3934 }
3935 sock = -1;
3936 clean_command_ctx(&cmd_ctx);
3937 continue;
3938 }
3939
3940 health_code_update();
3941
3942 // TODO: Validate cmd_ctx including sanity check for
3943 // security purpose.
3944
3945 rcu_thread_online();
3946 /*
3947 * This function dispatch the work to the kernel or userspace tracer
3948 * libs and fill the lttcomm_lttng_msg data structure of all the needed
3949 * informations for the client. The command context struct contains
3950 * everything this function may needs.
3951 */
3952 ret = process_client_msg(cmd_ctx, sock, &sock_error);
3953 rcu_thread_offline();
3954 if (ret < 0) {
3955 ret = close(sock);
3956 if (ret) {
3957 PERROR("close");
3958 }
3959 sock = -1;
3960 /*
3961 * TODO: Inform client somehow of the fatal error. At
3962 * this point, ret < 0 means that a zmalloc failed
3963 * (ENOMEM). Error detected but still accept
3964 * command, unless a socket error has been
3965 * detected.
3966 */
3967 clean_command_ctx(&cmd_ctx);
3968 continue;
3969 }
3970
3971 health_code_update();
3972
3973 DBG("Sending response (size: %d, retcode: %s)",
3974 cmd_ctx->lttng_msg_size,
3975 lttng_strerror(-cmd_ctx->llm->ret_code));
3976 ret = send_unix_sock(sock, cmd_ctx->llm, cmd_ctx->lttng_msg_size);
3977 if (ret < 0) {
3978 ERR("Failed to send data back to client");
3979 }
3980
3981 /* End of transmission */
3982 ret = close(sock);
3983 if (ret) {
3984 PERROR("close");
3985 }
3986 sock = -1;
3987
3988 clean_command_ctx(&cmd_ctx);
3989
3990 health_code_update();
3991 }
3992
3993 exit:
3994 error:
3995 if (sock >= 0) {
3996 ret = close(sock);
3997 if (ret) {
3998 PERROR("close");
3999 }
4000 }
4001
4002 lttng_poll_clean(&events);
4003 clean_command_ctx(&cmd_ctx);
4004
4005 error_listen:
4006 error_create_poll:
4007 unlink(client_unix_sock_path);
4008 if (client_sock >= 0) {
4009 ret = close(client_sock);
4010 if (ret) {
4011 PERROR("close");
4012 }
4013 }
4014
4015 if (err) {
4016 health_error();
4017 ERR("Health error occurred in %s", __func__);
4018 }
4019
4020 health_unregister(health_sessiond);
4021
4022 DBG("Client thread dying");
4023
4024 rcu_unregister_thread();
4025 return NULL;
4026 }
4027
4028
4029 /*
4030 * usage function on stderr
4031 */
4032 static void usage(void)
4033 {
4034 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
4035 fprintf(stderr, " -h, --help Display this usage.\n");
4036 fprintf(stderr, " -c, --client-sock PATH Specify path for the client unix socket\n");
4037 fprintf(stderr, " -a, --apps-sock PATH Specify path for apps unix socket\n");
4038 fprintf(stderr, " --kconsumerd-err-sock PATH Specify path for the kernel consumer error socket\n");
4039 fprintf(stderr, " --kconsumerd-cmd-sock PATH Specify path for the kernel consumer command socket\n");
4040 fprintf(stderr, " --ustconsumerd32-err-sock PATH Specify path for the 32-bit UST consumer error socket\n");
4041 fprintf(stderr, " --ustconsumerd64-err-sock PATH Specify path for the 64-bit UST consumer error socket\n");
4042 fprintf(stderr, " --ustconsumerd32-cmd-sock PATH Specify path for the 32-bit UST consumer command socket\n");
4043 fprintf(stderr, " --ustconsumerd64-cmd-sock PATH Specify path for the 64-bit UST consumer command socket\n");
4044 fprintf(stderr, " --consumerd32-path PATH Specify path for the 32-bit UST consumer daemon binary\n");
4045 fprintf(stderr, " --consumerd32-libdir PATH Specify path for the 32-bit UST consumer daemon libraries\n");
4046 fprintf(stderr, " --consumerd64-path PATH Specify path for the 64-bit UST consumer daemon binary\n");
4047 fprintf(stderr, " --consumerd64-libdir PATH Specify path for the 64-bit UST consumer daemon libraries\n");
4048 fprintf(stderr, " -d, --daemonize Start as a daemon.\n");
4049 fprintf(stderr, " -b, --background Start as a daemon, keeping console open.\n");
4050 fprintf(stderr, " -g, --group NAME Specify the tracing group name. (default: tracing)\n");
4051 fprintf(stderr, " -V, --version Show version number.\n");
4052 fprintf(stderr, " -S, --sig-parent Send SIGUSR1 to parent pid to notify readiness.\n");
4053 fprintf(stderr, " -q, --quiet No output at all.\n");
4054 fprintf(stderr, " -v, --verbose Verbose mode. Activate DBG() macro.\n");
4055 fprintf(stderr, " -p, --pidfile FILE Write a pid to FILE name overriding the default value.\n");
4056 fprintf(stderr, " --verbose-consumer Verbose mode for consumer. Activate DBG() macro.\n");
4057 fprintf(stderr, " --no-kernel Disable kernel tracer\n");
4058 fprintf(stderr, " --jul-tcp-port JUL application registration TCP port\n");
4059 fprintf(stderr, " -f --config Load daemon configuration file\n");
4060 }
4061
4062 /*
4063 * Take an option from the getopt output and set it in the right variable to be
4064 * used later.
4065 *
4066 * Return 0 on success else a negative value.
4067 */
4068 static int set_option(int opt, const char *arg, const char *optname)
4069 {
4070 int ret = 0;
4071
4072 switch (opt) {
4073 case 0:
4074 fprintf(stderr, "option %s", optname);
4075 if (arg) {
4076 fprintf(stderr, " with arg %s\n", arg);
4077 }
4078 break;
4079 case 'c':
4080 snprintf(client_unix_sock_path, PATH_MAX, "%s", arg);
4081 break;
4082 case 'a':
4083 snprintf(apps_unix_sock_path, PATH_MAX, "%s", arg);
4084 break;
4085 case 'd':
4086 opt_daemon = 1;
4087 break;
4088 case 'b':
4089 opt_background = 1;
4090 break;
4091 case 'g':
4092 tracing_group_name = strdup(arg);
4093 break;
4094 case 'h':
4095 usage();
4096 exit(EXIT_FAILURE);
4097 case 'V':
4098 fprintf(stdout, "%s\n", VERSION);
4099 exit(EXIT_SUCCESS);
4100 case 'S':
4101 opt_sig_parent = 1;
4102 break;
4103 case 'E':
4104 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4105 break;
4106 case 'C':
4107 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4108 break;
4109 case 'F':
4110 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4111 break;
4112 case 'D':
4113 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4114 break;
4115 case 'H':
4116 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX, "%s", arg);
4117 break;
4118 case 'G':
4119 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX, "%s", arg);
4120 break;
4121 case 'N':
4122 opt_no_kernel = 1;
4123 break;
4124 case 'q':
4125 lttng_opt_quiet = 1;
4126 break;
4127 case 'v':
4128 /* Verbose level can increase using multiple -v */
4129 if (arg) {
4130 lttng_opt_verbose = config_parse_value(arg);
4131 } else {
4132 lttng_opt_verbose += 1;
4133 }
4134 break;
4135 case 'Z':
4136 if (arg) {
4137 opt_verbose_consumer = config_parse_value(arg);
4138 } else {
4139 opt_verbose_consumer += 1;
4140 }
4141 break;
4142 case 'u':
4143 consumerd32_bin = strdup(arg);
4144 consumerd32_bin_override = 1;
4145 break;
4146 case 'U':
4147 consumerd32_libdir = strdup(arg);
4148 consumerd32_libdir_override = 1;
4149 break;
4150 case 't':
4151 consumerd64_bin = strdup(arg);
4152 consumerd64_bin_override = 1;
4153 break;
4154 case 'T':
4155 consumerd64_libdir = strdup(arg);
4156 consumerd64_libdir_override = 1;
4157 break;
4158 case 'p':
4159 opt_pidfile = strdup(arg);
4160 break;
4161 case 'J': /* JUL TCP port. */
4162 {
4163 unsigned long v;
4164
4165 errno = 0;
4166 v = strtoul(arg, NULL, 0);
4167 if (errno != 0 || !isdigit(arg[0])) {
4168 ERR("Wrong value in --jul-tcp-port parameter: %s", arg);
4169 return -1;
4170 }
4171 if (v == 0 || v >= 65535) {
4172 ERR("Port overflow in --jul-tcp-port parameter: %s", arg);
4173 return -1;
4174 }
4175 jul_tcp_port = (uint32_t) v;
4176 DBG3("JUL TCP port set to non default: %u", jul_tcp_port);
4177 break;
4178 }
4179 default:
4180 /* Unknown option or other error.
4181 * Error is printed by getopt, just return */
4182 ret = -1;
4183 }
4184
4185 return ret;
4186 }
4187
4188 /*
4189 * config_entry_handler_cb used to handle options read from a config file.
4190 * See config_entry_handler_cb comment in common/config/config.h for the
4191 * return value conventions.
4192 */
4193 static int config_entry_handler(const struct config_entry *entry, void *unused)
4194 {
4195 int ret = 0, i;
4196
4197 if (!entry || !entry->name || !entry->value) {
4198 ret = -EINVAL;
4199 goto end;
4200 }
4201
4202 /* Check if the option is to be ignored */
4203 for (i = 0; i < sizeof(config_ignore_options) / sizeof(char *); i++) {
4204 if (!strcmp(entry->name, config_ignore_options[i])) {
4205 goto end;
4206 }
4207 }
4208
4209 for (i = 0; i < (sizeof(long_options) / sizeof(struct option)) - 1;
4210 i++) {
4211
4212 /* Ignore if not fully matched. */
4213 if (strcmp(entry->name, long_options[i].name)) {
4214 continue;
4215 }
4216
4217 /*
4218 * If the option takes no argument on the command line, we have to
4219 * check if the value is "true". We support non-zero numeric values,
4220 * true, on and yes.
4221 */
4222 if (!long_options[i].has_arg) {
4223 ret = config_parse_value(entry->value);
4224 if (ret <= 0) {
4225 if (ret) {
4226 WARN("Invalid configuration value \"%s\" for option %s",
4227 entry->value, entry->name);
4228 }
4229 /* False, skip boolean config option. */
4230 goto end;
4231 }
4232 }
4233
4234 ret = set_option(long_options[i].val, entry->value, entry->name);
4235 goto end;
4236 }
4237
4238 WARN("Unrecognized option \"%s\" in daemon configuration file.", entry->name);
4239
4240 end:
4241 return ret;
4242 }
4243
4244 /*
4245 * daemon configuration loading and argument parsing
4246 */
4247 static int set_options(int argc, char **argv)
4248 {
4249 int ret = 0, c = 0, option_index = 0;
4250 int orig_optopt = optopt, orig_optind = optind;
4251 char *optstring;
4252 const char *config_path = NULL;
4253
4254 optstring = utils_generate_optstring(long_options,
4255 sizeof(long_options) / sizeof(struct option));
4256 if (!optstring) {
4257 ret = -ENOMEM;
4258 goto end;
4259 }
4260
4261 /* Check for the --config option */
4262 while ((c = getopt_long(argc, argv, optstring, long_options,
4263 &option_index)) != -1) {
4264 if (c == '?') {
4265 ret = -EINVAL;
4266 goto end;
4267 } else if (c != 'f') {
4268 /* if not equal to --config option. */
4269 continue;
4270 }
4271
4272 config_path = utils_expand_path(optarg);
4273 if (!config_path) {
4274 ERR("Failed to resolve path: %s", optarg);
4275 }
4276 }
4277
4278 ret = config_get_section_entries(config_path, config_section_name,
4279 config_entry_handler, NULL);
4280 if (ret) {
4281 if (ret > 0) {
4282 ERR("Invalid configuration option at line %i", ret);
4283 ret = -1;
4284 }
4285 goto end;
4286 }
4287
4288 /* Reset getopt's global state */
4289 optopt = orig_optopt;
4290 optind = orig_optind;
4291 while (1) {
4292 c = getopt_long(argc, argv, optstring, long_options, &option_index);
4293 if (c == -1) {
4294 break;
4295 }
4296
4297 ret = set_option(c, optarg, long_options[option_index].name);
4298 if (ret < 0) {
4299 break;
4300 }
4301 }
4302
4303 end:
4304 free(optstring);
4305 return ret;
4306 }
4307
4308 /*
4309 * Creates the two needed socket by the daemon.
4310 * apps_sock - The communication socket for all UST apps.
4311 * client_sock - The communication of the cli tool (lttng).
4312 */
4313 static int init_daemon_socket(void)
4314 {
4315 int ret = 0;
4316 mode_t old_umask;
4317
4318 old_umask = umask(0);
4319
4320 /* Create client tool unix socket */
4321 client_sock = lttcomm_create_unix_sock(client_unix_sock_path);
4322 if (client_sock < 0) {
4323 ERR("Create unix sock failed: %s", client_unix_sock_path);
4324 ret = -1;
4325 goto end;
4326 }
4327
4328 /* Set the cloexec flag */
4329 ret = utils_set_fd_cloexec(client_sock);
4330 if (ret < 0) {
4331 ERR("Unable to set CLOEXEC flag to the client Unix socket (fd: %d). "
4332 "Continuing but note that the consumer daemon will have a "
4333 "reference to this socket on exec()", client_sock);
4334 }
4335
4336 /* File permission MUST be 660 */
4337 ret = chmod(client_unix_sock_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4338 if (ret < 0) {
4339 ERR("Set file permissions failed: %s", client_unix_sock_path);
4340 PERROR("chmod");
4341 goto end;
4342 }
4343
4344 /* Create the application unix socket */
4345 apps_sock = lttcomm_create_unix_sock(apps_unix_sock_path);
4346 if (apps_sock < 0) {
4347 ERR("Create unix sock failed: %s", apps_unix_sock_path);
4348 ret = -1;
4349 goto end;
4350 }
4351
4352 /* Set the cloexec flag */
4353 ret = utils_set_fd_cloexec(apps_sock);
4354 if (ret < 0) {
4355 ERR("Unable to set CLOEXEC flag to the app Unix socket (fd: %d). "
4356 "Continuing but note that the consumer daemon will have a "
4357 "reference to this socket on exec()", apps_sock);
4358 }
4359
4360 /* File permission MUST be 666 */
4361 ret = chmod(apps_unix_sock_path,
4362 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
4363 if (ret < 0) {
4364 ERR("Set file permissions failed: %s", apps_unix_sock_path);
4365 PERROR("chmod");
4366 goto end;
4367 }
4368
4369 DBG3("Session daemon client socket %d and application socket %d created",
4370 client_sock, apps_sock);
4371
4372 end:
4373 umask(old_umask);
4374 return ret;
4375 }
4376
4377 /*
4378 * Check if the global socket is available, and if a daemon is answering at the
4379 * other side. If yes, error is returned.
4380 */
4381 static int check_existing_daemon(void)
4382 {
4383 /* Is there anybody out there ? */
4384 if (lttng_session_daemon_alive()) {
4385 return -EEXIST;
4386 }
4387
4388 return 0;
4389 }
4390
4391 /*
4392 * Set the tracing group gid onto the client socket.
4393 *
4394 * Race window between mkdir and chown is OK because we are going from more
4395 * permissive (root.root) to less permissive (root.tracing).
4396 */
4397 static int set_permissions(char *rundir)
4398 {
4399 int ret;
4400 gid_t gid;
4401
4402 gid = utils_get_group_id(tracing_group_name);
4403
4404 /* Set lttng run dir */
4405 ret = chown(rundir, 0, gid);
4406 if (ret < 0) {
4407 ERR("Unable to set group on %s", rundir);
4408 PERROR("chown");
4409 }
4410
4411 /*
4412 * Ensure all applications and tracing group can search the run
4413 * dir. Allow everyone to read the directory, since it does not
4414 * buy us anything to hide its content.
4415 */
4416 ret = chmod(rundir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
4417 if (ret < 0) {
4418 ERR("Unable to set permissions on %s", rundir);
4419 PERROR("chmod");
4420 }
4421
4422 /* lttng client socket path */
4423 ret = chown(client_unix_sock_path, 0, gid);
4424 if (ret < 0) {
4425 ERR("Unable to set group on %s", client_unix_sock_path);
4426 PERROR("chown");
4427 }
4428
4429 /* kconsumer error socket path */
4430 ret = chown(kconsumer_data.err_unix_sock_path, 0, 0);
4431 if (ret < 0) {
4432 ERR("Unable to set group on %s", kconsumer_data.err_unix_sock_path);
4433 PERROR("chown");
4434 }
4435
4436 /* 64-bit ustconsumer error socket path */
4437 ret = chown(ustconsumer64_data.err_unix_sock_path, 0, 0);
4438 if (ret < 0) {
4439 ERR("Unable to set group on %s", ustconsumer64_data.err_unix_sock_path);
4440 PERROR("chown");
4441 }
4442
4443 /* 32-bit ustconsumer compat32 error socket path */
4444 ret = chown(ustconsumer32_data.err_unix_sock_path, 0, 0);
4445 if (ret < 0) {
4446 ERR("Unable to set group on %s", ustconsumer32_data.err_unix_sock_path);
4447 PERROR("chown");
4448 }
4449
4450 DBG("All permissions are set");
4451
4452 return ret;
4453 }
4454
4455 /*
4456 * Create the lttng run directory needed for all global sockets and pipe.
4457 */
4458 static int create_lttng_rundir(const char *rundir)
4459 {
4460 int ret;
4461
4462 DBG3("Creating LTTng run directory: %s", rundir);
4463
4464 ret = mkdir(rundir, S_IRWXU);
4465 if (ret < 0) {
4466 if (errno != EEXIST) {
4467 ERR("Unable to create %s", rundir);
4468 goto error;
4469 } else {
4470 ret = 0;
4471 }
4472 }
4473
4474 error:
4475 return ret;
4476 }
4477
4478 /*
4479 * Setup sockets and directory needed by the kconsumerd communication with the
4480 * session daemon.
4481 */
4482 static int set_consumer_sockets(struct consumer_data *consumer_data,
4483 const char *rundir)
4484 {
4485 int ret;
4486 char path[PATH_MAX];
4487
4488 switch (consumer_data->type) {
4489 case LTTNG_CONSUMER_KERNEL:
4490 snprintf(path, PATH_MAX, DEFAULT_KCONSUMERD_PATH, rundir);
4491 break;
4492 case LTTNG_CONSUMER64_UST:
4493 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD64_PATH, rundir);
4494 break;
4495 case LTTNG_CONSUMER32_UST:
4496 snprintf(path, PATH_MAX, DEFAULT_USTCONSUMERD32_PATH, rundir);
4497 break;
4498 default:
4499 ERR("Consumer type unknown");
4500 ret = -EINVAL;
4501 goto error;
4502 }
4503
4504 DBG2("Creating consumer directory: %s", path);
4505
4506 ret = mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP);
4507 if (ret < 0) {
4508 if (errno != EEXIST) {
4509 PERROR("mkdir");
4510 ERR("Failed to create %s", path);
4511 goto error;
4512 }
4513 ret = -1;
4514 }
4515 if (is_root) {
4516 ret = chown(path, 0, utils_get_group_id(tracing_group_name));
4517 if (ret < 0) {
4518 ERR("Unable to set group on %s", path);
4519 PERROR("chown");
4520 goto error;
4521 }
4522 }
4523
4524 /* Create the kconsumerd error unix socket */
4525 consumer_data->err_sock =
4526 lttcomm_create_unix_sock(consumer_data->err_unix_sock_path);
4527 if (consumer_data->err_sock < 0) {
4528 ERR("Create unix sock failed: %s", consumer_data->err_unix_sock_path);
4529 ret = -1;
4530 goto error;
4531 }
4532
4533 /*
4534 * Set the CLOEXEC flag. Return code is useless because either way, the
4535 * show must go on.
4536 */
4537 ret = utils_set_fd_cloexec(consumer_data->err_sock);
4538 if (ret < 0) {
4539 PERROR("utils_set_fd_cloexec");
4540 /* continue anyway */
4541 }
4542
4543 /* File permission MUST be 660 */
4544 ret = chmod(consumer_data->err_unix_sock_path,
4545 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
4546 if (ret < 0) {
4547 ERR("Set file permissions failed: %s", consumer_data->err_unix_sock_path);
4548 PERROR("chmod");
4549 goto error;
4550 }
4551
4552 error:
4553 return ret;
4554 }
4555
4556 /*
4557 * Signal handler for the daemon
4558 *
4559 * Simply stop all worker threads, leaving main() return gracefully after
4560 * joining all threads and calling cleanup().
4561 */
4562 static void sighandler(int sig)
4563 {
4564 switch (sig) {
4565 case SIGPIPE:
4566 DBG("SIGPIPE caught");
4567 return;
4568 case SIGINT:
4569 DBG("SIGINT caught");
4570 stop_threads();
4571 break;
4572 case SIGTERM:
4573 DBG("SIGTERM caught");
4574 stop_threads();
4575 break;
4576 case SIGUSR1:
4577 CMM_STORE_SHARED(recv_child_signal, 1);
4578 break;
4579 default:
4580 break;
4581 }
4582 }
4583
4584 /*
4585 * Setup signal handler for :
4586 * SIGINT, SIGTERM, SIGPIPE
4587 */
4588 static int set_signal_handler(void)
4589 {
4590 int ret = 0;
4591 struct sigaction sa;
4592 sigset_t sigset;
4593
4594 if ((ret = sigemptyset(&sigset)) < 0) {
4595 PERROR("sigemptyset");
4596 return ret;
4597 }
4598
4599 sa.sa_handler = sighandler;
4600 sa.sa_mask = sigset;
4601 sa.sa_flags = 0;
4602 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
4603 PERROR("sigaction");
4604 return ret;
4605 }
4606
4607 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
4608 PERROR("sigaction");
4609 return ret;
4610 }
4611
4612 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
4613 PERROR("sigaction");
4614 return ret;
4615 }
4616
4617 if ((ret = sigaction(SIGUSR1, &sa, NULL)) < 0) {
4618 PERROR("sigaction");
4619 return ret;
4620 }
4621
4622 DBG("Signal handler set for SIGTERM, SIGUSR1, SIGPIPE and SIGINT");
4623
4624 return ret;
4625 }
4626
4627 /*
4628 * Set open files limit to unlimited. This daemon can open a large number of
4629 * file descriptors in order to consumer multiple kernel traces.
4630 */
4631 static void set_ulimit(void)
4632 {
4633 int ret;
4634 struct rlimit lim;
4635
4636 /* The kernel does not allowed an infinite limit for open files */
4637 lim.rlim_cur = 65535;
4638 lim.rlim_max = 65535;
4639
4640 ret = setrlimit(RLIMIT_NOFILE, &lim);
4641 if (ret < 0) {
4642 PERROR("failed to set open files limit");
4643 }
4644 }
4645
4646 /*
4647 * Write pidfile using the rundir and opt_pidfile.
4648 */
4649 static void write_pidfile(void)
4650 {
4651 int ret;
4652 char pidfile_path[PATH_MAX];
4653
4654 assert(rundir);
4655
4656 if (opt_pidfile) {
4657 strncpy(pidfile_path, opt_pidfile, sizeof(pidfile_path));
4658 } else {
4659 /* Build pidfile path from rundir and opt_pidfile. */
4660 ret = snprintf(pidfile_path, sizeof(pidfile_path), "%s/"
4661 DEFAULT_LTTNG_SESSIOND_PIDFILE, rundir);
4662 if (ret < 0) {
4663 PERROR("snprintf pidfile path");
4664 goto error;
4665 }
4666 }
4667
4668 /*
4669 * Create pid file in rundir. Return value is of no importance. The
4670 * execution will continue even though we are not able to write the file.
4671 */
4672 (void) utils_create_pid_file(getpid(), pidfile_path);
4673
4674 error:
4675 return;
4676 }
4677
4678 /*
4679 * Write JUL TCP port using the rundir.
4680 */
4681 static void write_julport(void)
4682 {
4683 int ret;
4684 char path[PATH_MAX];
4685
4686 assert(rundir);
4687
4688 ret = snprintf(path, sizeof(path), "%s/"
4689 DEFAULT_LTTNG_SESSIOND_JULPORT_FILE, rundir);
4690 if (ret < 0) {
4691 PERROR("snprintf julport path");
4692 goto error;
4693 }
4694
4695 /*
4696 * Create TCP JUL port file in rundir. Return value is of no importance.
4697 * The execution will continue even though we are not able to write the
4698 * file.
4699 */
4700 (void) utils_create_pid_file(jul_tcp_port, path);
4701
4702 error:
4703 return;
4704 }
4705
4706 /*
4707 * main
4708 */
4709 int main(int argc, char **argv)
4710 {
4711 int ret = 0;
4712 void *status;
4713 const char *home_path, *env_app_timeout;
4714
4715 init_kernel_workarounds();
4716
4717 rcu_register_thread();
4718
4719 if ((ret = set_signal_handler()) < 0) {
4720 goto error;
4721 }
4722
4723 setup_consumerd_path();
4724
4725 page_size = sysconf(_SC_PAGESIZE);
4726 if (page_size < 0) {
4727 PERROR("sysconf _SC_PAGESIZE");
4728 page_size = LONG_MAX;
4729 WARN("Fallback page size to %ld", page_size);
4730 }
4731
4732 /* Parse arguments and load the daemon configuration file */
4733 progname = argv[0];
4734 if ((ret = set_options(argc, argv)) < 0) {
4735 goto error;
4736 }
4737
4738 /* Daemonize */
4739 if (opt_daemon || opt_background) {
4740 int i;
4741
4742 ret = lttng_daemonize(&child_ppid, &recv_child_signal,
4743 !opt_background);
4744 if (ret < 0) {
4745 goto error;
4746 }
4747
4748 /*
4749 * We are in the child. Make sure all other file descriptors are
4750 * closed, in case we are called with more opened file descriptors than
4751 * the standard ones.
4752 */
4753 for (i = 3; i < sysconf(_SC_OPEN_MAX); i++) {
4754 (void) close(i);
4755 }
4756 }
4757
4758 /* Create thread quit pipe */
4759 if ((ret = init_thread_quit_pipe()) < 0) {
4760 goto error;
4761 }
4762
4763 /* Check if daemon is UID = 0 */
4764 is_root = !getuid();
4765
4766 if (is_root) {
4767 rundir = strdup(DEFAULT_LTTNG_RUNDIR);
4768
4769 /* Create global run dir with root access */
4770 ret = create_lttng_rundir(rundir);
4771 if (ret < 0) {
4772 goto error;
4773 }
4774
4775 if (strlen(apps_unix_sock_path) == 0) {
4776 snprintf(apps_unix_sock_path, PATH_MAX,
4777 DEFAULT_GLOBAL_APPS_UNIX_SOCK);
4778 }
4779
4780 if (strlen(client_unix_sock_path) == 0) {
4781 snprintf(client_unix_sock_path, PATH_MAX,
4782 DEFAULT_GLOBAL_CLIENT_UNIX_SOCK);
4783 }
4784
4785 /* Set global SHM for ust */
4786 if (strlen(wait_shm_path) == 0) {
4787 snprintf(wait_shm_path, PATH_MAX,
4788 DEFAULT_GLOBAL_APPS_WAIT_SHM_PATH);
4789 }
4790
4791 if (strlen(health_unix_sock_path) == 0) {
4792 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4793 DEFAULT_GLOBAL_HEALTH_UNIX_SOCK);
4794 }
4795
4796 /* Setup kernel consumerd path */
4797 snprintf(kconsumer_data.err_unix_sock_path, PATH_MAX,
4798 DEFAULT_KCONSUMERD_ERR_SOCK_PATH, rundir);
4799 snprintf(kconsumer_data.cmd_unix_sock_path, PATH_MAX,
4800 DEFAULT_KCONSUMERD_CMD_SOCK_PATH, rundir);
4801
4802 DBG2("Kernel consumer err path: %s",
4803 kconsumer_data.err_unix_sock_path);
4804 DBG2("Kernel consumer cmd path: %s",
4805 kconsumer_data.cmd_unix_sock_path);
4806 } else {
4807 home_path = utils_get_home_dir();
4808 if (home_path == NULL) {
4809 /* TODO: Add --socket PATH option */
4810 ERR("Can't get HOME directory for sockets creation.");
4811 ret = -EPERM;
4812 goto error;
4813 }
4814
4815 /*
4816 * Create rundir from home path. This will create something like
4817 * $HOME/.lttng
4818 */
4819 ret = asprintf(&rundir, DEFAULT_LTTNG_HOME_RUNDIR, home_path);
4820 if (ret < 0) {
4821 ret = -ENOMEM;
4822 goto error;
4823 }
4824
4825 ret = create_lttng_rundir(rundir);
4826 if (ret < 0) {
4827 goto error;
4828 }
4829
4830 if (strlen(apps_unix_sock_path) == 0) {
4831 snprintf(apps_unix_sock_path, PATH_MAX,
4832 DEFAULT_HOME_APPS_UNIX_SOCK, home_path);
4833 }
4834
4835 /* Set the cli tool unix socket path */
4836 if (strlen(client_unix_sock_path) == 0) {
4837 snprintf(client_unix_sock_path, PATH_MAX,
4838 DEFAULT_HOME_CLIENT_UNIX_SOCK, home_path);
4839 }
4840
4841 /* Set global SHM for ust */
4842 if (strlen(wait_shm_path) == 0) {
4843 snprintf(wait_shm_path, PATH_MAX,
4844 DEFAULT_HOME_APPS_WAIT_SHM_PATH, getuid());
4845 }
4846
4847 /* Set health check Unix path */
4848 if (strlen(health_unix_sock_path) == 0) {
4849 snprintf(health_unix_sock_path, sizeof(health_unix_sock_path),
4850 DEFAULT_HOME_HEALTH_UNIX_SOCK, home_path);
4851 }
4852 }
4853
4854 /* Set consumer initial state */
4855 kernel_consumerd_state = CONSUMER_STOPPED;
4856 ust_consumerd_state = CONSUMER_STOPPED;
4857
4858 DBG("Client socket path %s", client_unix_sock_path);
4859 DBG("Application socket path %s", apps_unix_sock_path);
4860 DBG("Application wait path %s", wait_shm_path);
4861 DBG("LTTng run directory path: %s", rundir);
4862
4863 /* 32 bits consumerd path setup */
4864 snprintf(ustconsumer32_data.err_unix_sock_path, PATH_MAX,
4865 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH, rundir);
4866 snprintf(ustconsumer32_data.cmd_unix_sock_path, PATH_MAX,
4867 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH, rundir);
4868
4869 DBG2("UST consumer 32 bits err path: %s",
4870 ustconsumer32_data.err_unix_sock_path);
4871 DBG2("UST consumer 32 bits cmd path: %s",
4872 ustconsumer32_data.cmd_unix_sock_path);
4873
4874 /* 64 bits consumerd path setup */
4875 snprintf(ustconsumer64_data.err_unix_sock_path, PATH_MAX,
4876 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH, rundir);
4877 snprintf(ustconsumer64_data.cmd_unix_sock_path, PATH_MAX,
4878 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH, rundir);
4879
4880 DBG2("UST consumer 64 bits err path: %s",
4881 ustconsumer64_data.err_unix_sock_path);
4882 DBG2("UST consumer 64 bits cmd path: %s",
4883 ustconsumer64_data.cmd_unix_sock_path);
4884
4885 /*
4886 * See if daemon already exist.
4887 */
4888 if ((ret = check_existing_daemon()) < 0) {
4889 ERR("Already running daemon.\n");
4890 /*
4891 * We do not goto exit because we must not cleanup()
4892 * because a daemon is already running.
4893 */
4894 goto error;
4895 }
4896
4897 /*
4898 * Init UST app hash table. Alloc hash table before this point since
4899 * cleanup() can get called after that point.
4900 */
4901 ust_app_ht_alloc();
4902
4903 /* Initialize JUL domain subsystem. */
4904 if ((ret = jul_init()) < 0) {
4905 /* ENOMEM at this point. */
4906 goto error;
4907 }
4908
4909 /* After this point, we can safely call cleanup() with "goto exit" */
4910
4911 /*
4912 * These actions must be executed as root. We do that *after* setting up
4913 * the sockets path because we MUST make the check for another daemon using
4914 * those paths *before* trying to set the kernel consumer sockets and init
4915 * kernel tracer.
4916 */
4917 if (is_root) {
4918 ret = set_consumer_sockets(&kconsumer_data, rundir);
4919 if (ret < 0) {
4920 goto exit;
4921 }
4922
4923 /* Setup kernel tracer */
4924 if (!opt_no_kernel) {
4925 init_kernel_tracer();
4926 }
4927
4928 /* Set ulimit for open files */
4929 set_ulimit();
4930 }
4931 /* init lttng_fd tracking must be done after set_ulimit. */
4932 lttng_fd_init();
4933
4934 ret = set_consumer_sockets(&ustconsumer64_data, rundir);
4935 if (ret < 0) {
4936 goto exit;
4937 }
4938
4939 ret = set_consumer_sockets(&ustconsumer32_data, rundir);
4940 if (ret < 0) {
4941 goto exit;
4942 }
4943
4944 /* Setup the needed unix socket */
4945 if ((ret = init_daemon_socket()) < 0) {
4946 goto exit;
4947 }
4948
4949 /* Set credentials to socket */
4950 if (is_root && ((ret = set_permissions(rundir)) < 0)) {
4951 goto exit;
4952 }
4953
4954 /* Get parent pid if -S, --sig-parent is specified. */
4955 if (opt_sig_parent) {
4956 ppid = getppid();
4957 }
4958
4959 /* Setup the kernel pipe for waking up the kernel thread */
4960 if (is_root && !opt_no_kernel) {
4961 if ((ret = utils_create_pipe_cloexec(kernel_poll_pipe)) < 0) {
4962 goto exit;
4963 }
4964 }
4965
4966 /* Setup the thread ht_cleanup communication pipe. */
4967 if (utils_create_pipe_cloexec(ht_cleanup_pipe) < 0) {
4968 goto exit;
4969 }
4970
4971 /* Setup the thread apps communication pipe. */
4972 if ((ret = utils_create_pipe_cloexec(apps_cmd_pipe)) < 0) {
4973 goto exit;
4974 }
4975
4976 /* Setup the thread apps notify communication pipe. */
4977 if (utils_create_pipe_cloexec(apps_cmd_notify_pipe) < 0) {
4978 goto exit;
4979 }
4980
4981 /* Initialize global buffer per UID and PID registry. */
4982 buffer_reg_init_uid_registry();
4983 buffer_reg_init_pid_registry();
4984
4985 /* Init UST command queue. */
4986 cds_wfq_init(&ust_cmd_queue.queue);
4987
4988 /*
4989 * Get session list pointer. This pointer MUST NOT be free(). This list is
4990 * statically declared in session.c
4991 */
4992 session_list_ptr = session_get_list();
4993
4994 /* Set up max poll set size */
4995 lttng_poll_set_max_size();
4996
4997 cmd_init();
4998
4999 /* Check for the application socket timeout env variable. */
5000 env_app_timeout = getenv(DEFAULT_APP_SOCKET_TIMEOUT_ENV);
5001 if (env_app_timeout) {
5002 app_socket_timeout = atoi(env_app_timeout);
5003 } else {
5004 app_socket_timeout = DEFAULT_APP_SOCKET_RW_TIMEOUT;
5005 }
5006
5007 write_pidfile();
5008 write_julport();
5009
5010 /* Initialize communication library */
5011 lttcomm_init();
5012 /* This is to get the TCP timeout value. */
5013 lttcomm_inet_init();
5014
5015 /*
5016 * Initialize the health check subsystem. This call should set the
5017 * appropriate time values.
5018 */
5019 health_sessiond = health_app_create(NR_HEALTH_SESSIOND_TYPES);
5020 if (!health_sessiond) {
5021 PERROR("health_app_create error");
5022 goto exit_health_sessiond_cleanup;
5023 }
5024
5025 /* Create thread to clean up RCU hash tables */
5026 ret = pthread_create(&ht_cleanup_thread, NULL,
5027 thread_ht_cleanup, (void *) NULL);
5028 if (ret != 0) {
5029 PERROR("pthread_create ht_cleanup");
5030 goto exit_ht_cleanup;
5031 }
5032
5033 /* Create health-check thread */
5034 ret = pthread_create(&health_thread, NULL,
5035 thread_manage_health, (void *) NULL);
5036 if (ret != 0) {
5037 PERROR("pthread_create health");
5038 goto exit_health;
5039 }
5040
5041 /* Create thread to manage the client socket */
5042 ret = pthread_create(&client_thread, NULL,
5043 thread_manage_clients, (void *) NULL);
5044 if (ret != 0) {
5045 PERROR("pthread_create clients");
5046 goto exit_client;
5047 }
5048
5049 /* Create thread to dispatch registration */
5050 ret = pthread_create(&dispatch_thread, NULL,
5051 thread_dispatch_ust_registration, (void *) NULL);
5052 if (ret != 0) {
5053 PERROR("pthread_create dispatch");
5054 goto exit_dispatch;
5055 }
5056
5057 /* Create thread to manage application registration. */
5058 ret = pthread_create(&reg_apps_thread, NULL,
5059 thread_registration_apps, (void *) NULL);
5060 if (ret != 0) {
5061 PERROR("pthread_create registration");
5062 goto exit_reg_apps;
5063 }
5064
5065 /* Create thread to manage application socket */
5066 ret = pthread_create(&apps_thread, NULL,
5067 thread_manage_apps, (void *) NULL);
5068 if (ret != 0) {
5069 PERROR("pthread_create apps");
5070 goto exit_apps;
5071 }
5072
5073 /* Create thread to manage application notify socket */
5074 ret = pthread_create(&apps_notify_thread, NULL,
5075 ust_thread_manage_notify, (void *) NULL);
5076 if (ret != 0) {
5077 PERROR("pthread_create apps");
5078 goto exit_apps_notify;
5079 }
5080
5081 /* Create JUL registration thread. */
5082 ret = pthread_create(&jul_reg_thread, NULL,
5083 jul_thread_manage_registration, (void *) NULL);
5084 if (ret != 0) {
5085 PERROR("pthread_create apps");
5086 goto exit_jul_reg;
5087 }
5088
5089 /* Don't start this thread if kernel tracing is not requested nor root */
5090 if (is_root && !opt_no_kernel) {
5091 /* Create kernel thread to manage kernel event */
5092 ret = pthread_create(&kernel_thread, NULL,
5093 thread_manage_kernel, (void *) NULL);
5094 if (ret != 0) {
5095 PERROR("pthread_create kernel");
5096 goto exit_kernel;
5097 }
5098
5099 ret = pthread_join(kernel_thread, &status);
5100 if (ret != 0) {
5101 PERROR("pthread_join");
5102 goto error; /* join error, exit without cleanup */
5103 }
5104 }
5105
5106 exit_kernel:
5107 ret = pthread_join(jul_reg_thread, &status);
5108 if (ret != 0) {
5109 PERROR("pthread_join JUL");
5110 goto error; /* join error, exit without cleanup */
5111 }
5112
5113 exit_jul_reg:
5114 ret = pthread_join(apps_notify_thread, &status);
5115 if (ret != 0) {
5116 PERROR("pthread_join apps notify");
5117 goto error; /* join error, exit without cleanup */
5118 }
5119
5120 exit_apps_notify:
5121 ret = pthread_join(apps_thread, &status);
5122 if (ret != 0) {
5123 PERROR("pthread_join apps");
5124 goto error; /* join error, exit without cleanup */
5125 }
5126
5127
5128 exit_apps:
5129 ret = pthread_join(reg_apps_thread, &status);
5130 if (ret != 0) {
5131 PERROR("pthread_join");
5132 goto error; /* join error, exit without cleanup */
5133 }
5134
5135 exit_reg_apps:
5136 ret = pthread_join(dispatch_thread, &status);
5137 if (ret != 0) {
5138 PERROR("pthread_join");
5139 goto error; /* join error, exit without cleanup */
5140 }
5141
5142 exit_dispatch:
5143 ret = pthread_join(client_thread, &status);
5144 if (ret != 0) {
5145 PERROR("pthread_join");
5146 goto error; /* join error, exit without cleanup */
5147 }
5148
5149 ret = join_consumer_thread(&kconsumer_data);
5150 if (ret != 0) {
5151 PERROR("join_consumer");
5152 goto error; /* join error, exit without cleanup */
5153 }
5154
5155 ret = join_consumer_thread(&ustconsumer32_data);
5156 if (ret != 0) {
5157 PERROR("join_consumer ust32");
5158 goto error; /* join error, exit without cleanup */
5159 }
5160
5161 ret = join_consumer_thread(&ustconsumer64_data);
5162 if (ret != 0) {
5163 PERROR("join_consumer ust64");
5164 goto error; /* join error, exit without cleanup */
5165 }
5166
5167 exit_client:
5168 ret = pthread_join(health_thread, &status);
5169 if (ret != 0) {
5170 PERROR("pthread_join health thread");
5171 goto error; /* join error, exit without cleanup */
5172 }
5173
5174 exit_health:
5175 ret = pthread_join(ht_cleanup_thread, &status);
5176 if (ret != 0) {
5177 PERROR("pthread_join ht cleanup thread");
5178 goto error; /* join error, exit without cleanup */
5179 }
5180 exit_ht_cleanup:
5181 health_app_destroy(health_sessiond);
5182 exit_health_sessiond_cleanup:
5183 exit:
5184 /*
5185 * cleanup() is called when no other thread is running.
5186 */
5187 rcu_thread_online();
5188 cleanup();
5189 rcu_thread_offline();
5190 rcu_unregister_thread();
5191 if (!ret) {
5192 exit(EXIT_SUCCESS);
5193 }
5194 error:
5195 exit(EXIT_FAILURE);
5196 }
This page took 0.172959 seconds and 3 git commands to generate.