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