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