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