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