2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #include "health-consumerd.hpp"
11 #include "lttng-consumerd.hpp"
13 #include <common/common.hpp>
14 #include <common/compat/getenv.hpp>
15 #include <common/compat/poll.hpp>
16 #include <common/consumer/consumer-timer.hpp>
17 #include <common/consumer/consumer.hpp>
18 #include <common/defaults.hpp>
19 #include <common/sessiond-comm/sessiond-comm.hpp>
20 #include <common/utils.hpp>
34 #include <sys/resource.h>
36 #include <sys/socket.h>
38 #include <sys/types.h>
41 #include <urcu/compiler.h>
42 #include <urcu/list.h>
44 /* threads (channel handling, poll, metadata, sessiond) */
46 static pthread_t channel_thread
, data_thread
, metadata_thread
, sessiond_thread
,
47 metadata_timer_thread
, health_thread
;
48 static bool metadata_timer_thread_online
;
50 /* to count the number of times the user pressed ctrl+c */
51 static int sigintcount
= 0;
53 /* Argument variables */
54 int lttng_opt_quiet
; /* not static in error.h */
55 int lttng_opt_verbose
; /* not static in error.h */
56 int lttng_opt_mi
; /* not static in error.h */
58 static int opt_daemon
;
59 static const char *progname
;
60 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
61 static char error_sock_path
[PATH_MAX
]; /* Global error path */
62 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
64 /* the liblttngconsumerd context */
65 static struct lttng_consumer_local_data
*the_consumer_context
;
67 /* Consumerd health monitoring */
68 struct health_app
*health_consumerd
;
70 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
72 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
74 enum lttng_consumer_type
lttng_consumer_get_type(void)
76 if (!the_consumer_context
) {
77 return LTTNG_CONSUMER_UNKNOWN
;
79 return the_consumer_context
->type
;
83 * Signal handler for the daemon
85 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
__attribute__((unused
)))
87 if (sig
== SIGINT
&& sigintcount
++ == 0) {
88 DBG("ignoring first SIGINT");
94 const char msg
[] = "Received SIGBUS, aborting program.\n";
96 lttng_consumer_sigbus_handle(siginfo
->si_addr
);
98 * If ustctl did not catch this signal (triggering a
99 * siglongjmp), abort the program. Otherwise, the execution
100 * will resume from the ust-ctl call which caused this error.
102 * The return value is ignored since the program aborts anyhow.
104 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
109 if (the_consumer_context
) {
110 lttng_consumer_should_exit(the_consumer_context
);
115 * Setup signal handler for :
116 * SIGINT, SIGTERM, SIGPIPE, SIGBUS
118 static int set_signal_handler()
124 if ((ret
= sigemptyset(&sigset
)) < 0) {
125 PERROR("sigemptyset");
130 sa
.sa_flags
= SA_SIGINFO
;
132 sa
.sa_sigaction
= sighandler
;
133 if ((ret
= sigaction(SIGTERM
, &sa
, nullptr)) < 0) {
138 if ((ret
= sigaction(SIGINT
, &sa
, nullptr)) < 0) {
143 if ((ret
= sigaction(SIGBUS
, &sa
, nullptr)) < 0) {
149 sa
.sa_handler
= SIG_IGN
;
150 if ((ret
= sigaction(SIGPIPE
, &sa
, nullptr)) < 0) {
159 * Usage function on stream file.
161 static void usage(FILE *fp
)
163 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
166 "Display this usage.\n");
168 " -c, --consumerd-cmd-sock PATH "
169 "Specify path for the command socket\n");
171 " -e, --consumerd-err-sock PATH "
172 "Specify path for the error socket\n");
175 "Start as a daemon.\n");
178 "No output at all.\n");
181 "Verbose mode. Activate DBG() macro.\n");
184 "Show version number.\n");
187 "Specify the tracing group name. (default: tracing)\n");
190 "Consumer kernel buffers (default).\n");
193 "Consumer UST buffers.%s\n",
194 #ifdef HAVE_LIBLTTNG_UST_CTL
197 " (support not compiled in)"
203 * daemon argument parsing
205 static int parse_args(int argc
, char **argv
)
209 static struct option long_options
[] = { { "consumerd-cmd-sock", 1, nullptr, 'c' },
210 { "consumerd-err-sock", 1, nullptr, 'e' },
211 { "daemonize", 0, nullptr, 'd' },
212 { "group", 1, nullptr, 'g' },
213 { "help", 0, nullptr, 'h' },
214 { "quiet", 0, nullptr, 'q' },
215 { "verbose", 0, nullptr, 'v' },
216 { "version", 0, nullptr, 'V' },
217 { "kernel", 0, nullptr, 'k' },
218 #ifdef HAVE_LIBLTTNG_UST_CTL
219 { "ust", 0, nullptr, 'u' },
221 { nullptr, 0, nullptr, 0 } };
224 int option_index
= 0;
225 c
= getopt_long(argc
,
237 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
239 fprintf(stderr
, " with arg %s\n", optarg
);
245 if (lttng_is_setuid_setgid()) {
246 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
247 "-c, --consumerd-cmd-sock");
249 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
253 if (lttng_is_setuid_setgid()) {
254 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
255 "-e, --consumerd-err-sock");
257 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
264 if (lttng_is_setuid_setgid()) {
265 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
268 tracing_group_name
= optarg
;
278 lttng_opt_verbose
= 3;
281 fprintf(stdout
, "%s\n", VERSION
);
284 opt_type
= LTTNG_CONSUMER_KERNEL
;
286 #ifdef HAVE_LIBLTTNG_UST_CTL
288 #if (CAA_BITS_PER_LONG == 64)
289 opt_type
= LTTNG_CONSUMER64_UST
;
290 #elif (CAA_BITS_PER_LONG == 32)
291 opt_type
= LTTNG_CONSUMER32_UST
;
293 #error "Unknown bitness"
308 * Set open files limit to unlimited. This daemon can open a large number of
309 * file descriptors in order to consumer multiple kernel traces.
311 static void set_ulimit()
316 /* The kernel does not allowed an infinite limit for open files */
317 lim
.rlim_cur
= 65535;
318 lim
.rlim_max
= 65535;
320 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
322 PERROR("failed to set open files limit");
329 int main(int argc
, char **argv
)
331 int ret
= 0, retval
= 0;
333 struct lttng_consumer_local_data
*tmp_ctx
;
335 rcu_register_thread();
337 if (run_as_create_worker(argv
[0], nullptr, nullptr) < 0) {
338 goto exit_set_signal_handler
;
341 if (set_signal_handler()) {
343 goto exit_set_signal_handler
;
346 /* Parse arguments */
348 if (parse_args(argc
, argv
)) {
359 * child: setsid, close FD 0, 1, 2, chdir /
360 * parent: exit (if fork is successful)
369 * We are in the child. Make sure all other file
370 * descriptors are closed, in case we are called with
371 * more opened file descriptors than the standard ones.
373 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
379 * Starting from here, we can create threads. This needs to be after
380 * lttng_daemonize due to RCU.
383 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
384 if (!health_consumerd
) {
386 goto exit_health_consumerd_cleanup
;
389 if (*command_sock_path
== '\0') {
391 case LTTNG_CONSUMER_KERNEL
:
392 ret
= snprintf(command_sock_path
,
394 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
395 DEFAULT_LTTNG_RUNDIR
);
401 case LTTNG_CONSUMER64_UST
:
402 ret
= snprintf(command_sock_path
,
404 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
405 DEFAULT_LTTNG_RUNDIR
);
411 case LTTNG_CONSUMER32_UST
:
412 ret
= snprintf(command_sock_path
,
414 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
415 DEFAULT_LTTNG_RUNDIR
);
422 ERR("Unknown consumerd type");
429 if (lttng_consumer_init()) {
434 /* Initialize communication library */
436 /* Initialize TCP timeout values */
440 /* Set limit for open files */
444 /* create the consumer instance with and assign the callbacks */
445 the_consumer_context
= lttng_consumer_create(opt_type
,
446 lttng_consumer_read_subbuffer
,
448 lttng_consumer_on_recv_stream
,
450 if (!the_consumer_context
) {
455 lttng_consumer_set_command_sock_path(the_consumer_context
, command_sock_path
);
456 if (*error_sock_path
== '\0') {
458 case LTTNG_CONSUMER_KERNEL
:
459 ret
= snprintf(error_sock_path
,
461 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
462 DEFAULT_LTTNG_RUNDIR
);
468 case LTTNG_CONSUMER64_UST
:
469 ret
= snprintf(error_sock_path
,
471 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
472 DEFAULT_LTTNG_RUNDIR
);
478 case LTTNG_CONSUMER32_UST
:
479 ret
= snprintf(error_sock_path
,
481 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
482 DEFAULT_LTTNG_RUNDIR
);
489 ERR("Unknown consumerd type");
495 /* Connect to the socket created by lttng-sessiond to report errors */
496 DBG("Connecting to error socket %s", error_sock_path
);
497 ret
= lttcomm_connect_unix_sock(error_sock_path
);
499 * Not a fatal error, but all communication with lttng-sessiond will
503 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
505 lttng_consumer_set_error_sock(the_consumer_context
, ret
);
508 * Block RT signals used for UST periodical metadata flush and the live
509 * timer in main, and create a dedicated thread to handle these signals.
511 if (consumer_signal_init()) {
516 the_consumer_context
->type
= opt_type
;
518 if (utils_create_pipe(health_quit_pipe
)) {
520 goto exit_health_pipe
;
523 /* Create thread to manage the client socket */
524 ret
= pthread_create(&health_thread
,
525 default_pthread_attr(),
526 thread_manage_health_consumerd
,
530 PERROR("pthread_create health");
532 goto exit_health_thread
;
536 * Wait for health thread to be initialized before letting the
537 * sessiond thread reply to the sessiond that we are ready.
539 while (uatomic_read(<tng_consumer_ready
)) {
542 cmm_smp_mb(); /* Read ready before following operations */
545 * Create the thread to manage the UST metadata periodic timer and
548 ret
= pthread_create(&metadata_timer_thread
,
550 consumer_timer_thread
,
551 (void *) the_consumer_context
);
554 PERROR("pthread_create");
556 goto exit_metadata_timer_thread
;
558 metadata_timer_thread_online
= true;
560 /* Create thread to manage channels */
561 ret
= pthread_create(&channel_thread
,
562 default_pthread_attr(),
563 consumer_thread_channel_poll
,
564 (void *) the_consumer_context
);
567 PERROR("pthread_create");
569 goto exit_channel_thread
;
572 /* Create thread to manage the polling/writing of trace metadata */
573 ret
= pthread_create(&metadata_thread
,
574 default_pthread_attr(),
575 consumer_thread_metadata_poll
,
576 (void *) the_consumer_context
);
579 PERROR("pthread_create");
581 goto exit_metadata_thread
;
584 /* Create thread to manage the polling/writing of trace data */
585 ret
= pthread_create(&data_thread
,
586 default_pthread_attr(),
587 consumer_thread_data_poll
,
588 (void *) the_consumer_context
);
591 PERROR("pthread_create");
593 goto exit_data_thread
;
596 /* Create the thread to manage the reception of fds */
597 ret
= pthread_create(&sessiond_thread
,
598 default_pthread_attr(),
599 consumer_thread_sessiond_poll
,
600 (void *) the_consumer_context
);
603 PERROR("pthread_create");
605 goto exit_sessiond_thread
;
609 * This is where we start awaiting program completion (e.g. through
610 * signal that asks threads to teardown.
613 ret
= pthread_join(sessiond_thread
, &status
);
616 PERROR("pthread_join sessiond_thread");
619 exit_sessiond_thread
:
621 ret
= pthread_join(data_thread
, &status
);
624 PERROR("pthread_join data_thread");
629 ret
= pthread_join(metadata_thread
, &status
);
632 PERROR("pthread_join metadata_thread");
635 exit_metadata_thread
:
637 ret
= pthread_join(channel_thread
, &status
);
640 PERROR("pthread_join channel_thread");
645 exit_metadata_timer_thread
:
647 ret
= pthread_join(health_thread
, &status
);
650 PERROR("pthread_join health_thread");
655 utils_close_pipe(health_quit_pipe
);
660 * Wait for all pending call_rcu work to complete before tearing
661 * down data structures. call_rcu worker may be trying to
662 * perform lookups in those structures.
665 lttng_consumer_cleanup();
667 * Tearing down the metadata timer thread in a
668 * non-fully-symmetric fashion compared to its creation in case
669 * lttng_consumer_cleanup() ends up tearing down timers (which
670 * requires the timer thread to be alive).
672 if (metadata_timer_thread_online
) {
674 * Ensure the metadata timer thread exits only after all other
675 * threads are gone, because it is required to perform timer
676 * teardown synchronization.
678 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT
);
679 ret
= pthread_join(metadata_timer_thread
, &status
);
682 PERROR("pthread_join metadata_timer_thread");
685 ret
= consumer_timer_thread_get_channel_monitor_pipe();
689 PERROR("close channel monitor pipe");
692 metadata_timer_thread_online
= false;
694 tmp_ctx
= the_consumer_context
;
695 the_consumer_context
= nullptr;
696 cmm_barrier(); /* Clear ctx for signal handler. */
697 lttng_consumer_destroy(tmp_ctx
);
699 if (health_consumerd
) {
700 health_app_destroy(health_consumerd
);
702 /* Ensure all prior call_rcu are done. */
705 run_as_destroy_worker();
707 exit_health_consumerd_cleanup
:
709 exit_set_signal_handler
:
711 rcu_unregister_thread();