2 * Copyright (C) 2011 Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
20 #include <sys/resource.h>
22 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <urcu/list.h>
29 #include <urcu/compiler.h>
32 #include <common/defaults.h>
33 #include <common/common.h>
34 #include <common/consumer/consumer.h>
35 #include <common/consumer/consumer-timer.h>
36 #include <common/compat/poll.h>
37 #include <common/compat/getenv.h>
38 #include <common/sessiond-comm/sessiond-comm.h>
39 #include <common/utils.h>
41 #include "lttng-consumerd.h"
42 #include "health-consumerd.h"
44 /* threads (channel handling, poll, metadata, sessiond) */
46 static pthread_t channel_thread
, data_thread
, metadata_thread
,
47 sessiond_thread
, 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
*ctx
;
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)
77 return LTTNG_CONSUMER_UNKNOWN
;
83 * Signal handler for the daemon
85 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
)
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
));
110 lttng_consumer_should_exit(ctx
);
115 * Setup signal handler for :
116 * SIGINT, SIGTERM, SIGPIPE, SIGBUS
118 static int set_signal_handler(void)
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
, NULL
)) < 0) {
138 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
143 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
149 sa
.sa_handler
= SIG_IGN
;
150 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
159 * Usage function on stream file.
161 static void usage(FILE *fp
)
163 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
164 fprintf(fp
, " -h, --help "
165 "Display this usage.\n");
166 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
167 "Specify path for the command socket\n");
168 fprintf(fp
, " -e, --consumerd-err-sock PATH "
169 "Specify path for the error socket\n");
170 fprintf(fp
, " -d, --daemonize "
171 "Start as a daemon.\n");
172 fprintf(fp
, " -q, --quiet "
173 "No output at all.\n");
174 fprintf(fp
, " -v, --verbose "
175 "Verbose mode. Activate DBG() macro.\n");
176 fprintf(fp
, " -V, --version "
177 "Show version number.\n");
178 fprintf(fp
, " -g, --group NAME "
179 "Specify the tracing group name. (default: tracing)\n");
180 fprintf(fp
, " -k, --kernel "
181 "Consumer kernel buffers (default).\n");
182 fprintf(fp
, " -u, --ust "
183 "Consumer UST buffers.%s\n",
184 #ifdef HAVE_LIBLTTNG_UST_CTL
187 " (support not compiled in)"
193 * daemon argument parsing
195 static int parse_args(int argc
, char **argv
)
199 static struct option long_options
[] = {
200 { "consumerd-cmd-sock", 1, 0, 'c' },
201 { "consumerd-err-sock", 1, 0, 'e' },
202 { "daemonize", 0, 0, 'd' },
203 { "group", 1, 0, 'g' },
204 { "help", 0, 0, 'h' },
205 { "quiet", 0, 0, 'q' },
206 { "verbose", 0, 0, 'v' },
207 { "version", 0, 0, 'V' },
208 { "kernel", 0, 0, 'k' },
209 #ifdef HAVE_LIBLTTNG_UST_CTL
210 { "ust", 0, 0, 'u' },
216 int option_index
= 0;
217 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
218 long_options
, &option_index
);
225 fprintf(stderr
, "option %s",
226 long_options
[option_index
].name
);
228 fprintf(stderr
, " with arg %s\n", optarg
);
234 if (lttng_is_setuid_setgid()) {
235 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
236 "-c, --consumerd-cmd-sock");
238 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
242 if (lttng_is_setuid_setgid()) {
243 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
244 "-e, --consumerd-err-sock");
246 snprintf(error_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.",
257 tracing_group_name
= optarg
;
267 lttng_opt_verbose
= 3;
270 fprintf(stdout
, "%s\n", VERSION
);
273 opt_type
= LTTNG_CONSUMER_KERNEL
;
275 #ifdef HAVE_LIBLTTNG_UST_CTL
277 # if (CAA_BITS_PER_LONG == 64)
278 opt_type
= LTTNG_CONSUMER64_UST
;
279 # elif (CAA_BITS_PER_LONG == 32)
280 opt_type
= LTTNG_CONSUMER32_UST
;
282 # error "Unknown bitness"
297 * Set open files limit to unlimited. This daemon can open a large number of
298 * file descriptors in order to consumer multiple kernel traces.
300 static void set_ulimit(void)
305 /* The kernel does not allowed an infinite limit for open files */
306 lim
.rlim_cur
= 65535;
307 lim
.rlim_max
= 65535;
309 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
311 PERROR("failed to set open files limit");
318 int main(int argc
, char **argv
)
320 int ret
= 0, retval
= 0;
322 struct lttng_consumer_local_data
*tmp_ctx
;
324 rcu_register_thread();
326 if (run_as_create_worker(argv
[0], NULL
, NULL
) < 0) {
327 goto exit_set_signal_handler
;
330 if (set_signal_handler()) {
332 goto exit_set_signal_handler
;
335 /* Parse arguments */
337 if (parse_args(argc
, argv
)) {
348 * child: setsid, close FD 0, 1, 2, chdir /
349 * parent: exit (if fork is successful)
358 * We are in the child. Make sure all other file
359 * descriptors are closed, in case we are called with
360 * more opened file descriptors than the standard ones.
362 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
368 * Starting from here, we can create threads. This needs to be after
369 * lttng_daemonize due to RCU.
372 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
373 if (!health_consumerd
) {
375 goto exit_health_consumerd_cleanup
;
378 if (*command_sock_path
== '\0') {
380 case LTTNG_CONSUMER_KERNEL
:
381 ret
= snprintf(command_sock_path
, PATH_MAX
,
382 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
383 DEFAULT_LTTNG_RUNDIR
);
389 case LTTNG_CONSUMER64_UST
:
390 ret
= snprintf(command_sock_path
, PATH_MAX
,
391 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
392 DEFAULT_LTTNG_RUNDIR
);
398 case LTTNG_CONSUMER32_UST
:
399 ret
= snprintf(command_sock_path
, PATH_MAX
,
400 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
401 DEFAULT_LTTNG_RUNDIR
);
408 ERR("Unknown consumerd type");
415 if (lttng_consumer_init()) {
420 /* Initialize communication library */
422 /* Initialize TCP timeout values */
426 /* Set limit for open files */
430 /* create the consumer instance with and assign the callbacks */
431 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
432 NULL
, lttng_consumer_on_recv_stream
, NULL
);
438 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
439 if (*error_sock_path
== '\0') {
441 case LTTNG_CONSUMER_KERNEL
:
442 ret
= snprintf(error_sock_path
, PATH_MAX
,
443 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
444 DEFAULT_LTTNG_RUNDIR
);
450 case LTTNG_CONSUMER64_UST
:
451 ret
= snprintf(error_sock_path
, PATH_MAX
,
452 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
453 DEFAULT_LTTNG_RUNDIR
);
459 case LTTNG_CONSUMER32_UST
:
460 ret
= snprintf(error_sock_path
, PATH_MAX
,
461 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
462 DEFAULT_LTTNG_RUNDIR
);
469 ERR("Unknown consumerd type");
475 /* Connect to the socket created by lttng-sessiond to report errors */
476 DBG("Connecting to error socket %s", error_sock_path
);
477 ret
= lttcomm_connect_unix_sock(error_sock_path
);
479 * Not a fatal error, but all communication with lttng-sessiond will
483 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
485 lttng_consumer_set_error_sock(ctx
, ret
);
488 * Block RT signals used for UST periodical metadata flush and the live
489 * timer in main, and create a dedicated thread to handle these signals.
491 if (consumer_signal_init()) {
496 ctx
->type
= opt_type
;
498 if (utils_create_pipe(health_quit_pipe
)) {
500 goto exit_health_pipe
;
503 /* Create thread to manage the client socket */
504 ret
= pthread_create(&health_thread
, default_pthread_attr(),
505 thread_manage_health
, (void *) NULL
);
508 PERROR("pthread_create health");
510 goto exit_health_thread
;
514 * Wait for health thread to be initialized before letting the
515 * sessiond thread reply to the sessiond that we are ready.
517 while (uatomic_read(<tng_consumer_ready
)) {
520 cmm_smp_mb(); /* Read ready before following operations */
523 * Create the thread to manage the UST metadata periodic timer and
526 ret
= pthread_create(&metadata_timer_thread
, NULL
,
527 consumer_timer_thread
, (void *) ctx
);
530 PERROR("pthread_create");
532 goto exit_metadata_timer_thread
;
534 metadata_timer_thread_online
= true;
536 /* Create thread to manage channels */
537 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
538 consumer_thread_channel_poll
,
542 PERROR("pthread_create");
544 goto exit_channel_thread
;
547 /* Create thread to manage the polling/writing of trace metadata */
548 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
549 consumer_thread_metadata_poll
,
553 PERROR("pthread_create");
555 goto exit_metadata_thread
;
558 /* Create thread to manage the polling/writing of trace data */
559 ret
= pthread_create(&data_thread
, default_pthread_attr(),
560 consumer_thread_data_poll
, (void *) ctx
);
563 PERROR("pthread_create");
565 goto exit_data_thread
;
568 /* Create the thread to manage the reception of fds */
569 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
570 consumer_thread_sessiond_poll
,
574 PERROR("pthread_create");
576 goto exit_sessiond_thread
;
581 * This is where we start awaiting program completion (e.g. through
582 * signal that asks threads to teardown.
585 ret
= pthread_join(sessiond_thread
, &status
);
588 PERROR("pthread_join sessiond_thread");
591 exit_sessiond_thread
:
593 ret
= pthread_join(data_thread
, &status
);
596 PERROR("pthread_join data_thread");
601 ret
= pthread_join(metadata_thread
, &status
);
604 PERROR("pthread_join metadata_thread");
607 exit_metadata_thread
:
609 ret
= pthread_join(channel_thread
, &status
);
612 PERROR("pthread_join channel_thread");
617 exit_metadata_timer_thread
:
619 ret
= pthread_join(health_thread
, &status
);
622 PERROR("pthread_join health_thread");
627 utils_close_pipe(health_quit_pipe
);
632 * Wait for all pending call_rcu work to complete before tearing
633 * down data structures. call_rcu worker may be trying to
634 * perform lookups in those structures.
637 lttng_consumer_cleanup();
639 * Tearing down the metadata timer thread in a
640 * non-fully-symmetric fashion compared to its creation in case
641 * lttng_consumer_cleanup() ends up tearing down timers (which
642 * requires the timer thread to be alive).
644 if (metadata_timer_thread_online
) {
646 * Ensure the metadata timer thread exits only after all other
647 * threads are gone, because it is required to perform timer
648 * teardown synchronization.
650 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT
);
651 ret
= pthread_join(metadata_timer_thread
, &status
);
654 PERROR("pthread_join metadata_timer_thread");
657 ret
= consumer_timer_thread_get_channel_monitor_pipe();
661 PERROR("close channel monitor pipe");
664 metadata_timer_thread_online
= false;
668 cmm_barrier(); /* Clear ctx for signal handler. */
669 lttng_consumer_destroy(tmp_ctx
);
671 if (health_consumerd
) {
672 health_app_destroy(health_consumerd
);
674 /* Ensure all prior call_rcu are done. */
677 run_as_destroy_worker();
679 exit_health_consumerd_cleanup
:
681 exit_set_signal_handler
:
683 rcu_unregister_thread();