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>
30 #include <urcu/compiler.h>
33 #include <common/defaults.h>
34 #include <common/common.h>
35 #include <common/consumer/consumer.h>
36 #include <common/consumer/consumer-timer.h>
37 #include <common/compat/poll.h>
38 #include <common/compat/getenv.h>
39 #include <common/sessiond-comm/sessiond-comm.h>
40 #include <common/utils.h>
42 #include "lttng-consumerd.h"
43 #include "health-consumerd.h"
45 /* threads (channel handling, poll, metadata, sessiond) */
47 static pthread_t channel_thread
, data_thread
, metadata_thread
,
48 sessiond_thread
, metadata_timer_thread
, health_thread
;
49 static bool metadata_timer_thread_online
;
51 /* to count the number of times the user pressed ctrl+c */
52 static int sigintcount
= 0;
54 /* Argument variables */
55 int lttng_opt_quiet
; /* not static in error.h */
56 int lttng_opt_verbose
; /* not static in error.h */
57 int lttng_opt_mi
; /* not static in error.h */
59 static int opt_daemon
;
60 static const char *progname
;
61 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
62 static char error_sock_path
[PATH_MAX
]; /* Global error path */
63 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
65 /* the liblttngconsumerd context */
66 static struct lttng_consumer_local_data
*ctx
;
68 /* Consumerd health monitoring */
69 struct health_app
*health_consumerd
;
71 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
73 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
75 enum lttng_consumer_type
lttng_consumer_get_type(void)
78 return LTTNG_CONSUMER_UNKNOWN
;
84 * Signal handler for the daemon
86 static void sighandler(int sig
, siginfo_t
*siginfo
, void *arg
)
88 if (sig
== SIGINT
&& sigintcount
++ == 0) {
89 DBG("ignoring first SIGINT");
95 const char msg
[] = "Received SIGBUS, aborting program.\n";
97 lttng_consumer_sigbus_handle(siginfo
->si_addr
);
99 * If ustctl did not catch this signal (triggering a
100 * siglongjmp), abort the program. Otherwise, the execution
101 * will resume from the ust-ctl call which caused this error.
103 * The return value is ignored since the program aborts anyhow.
105 write_ret
= write(STDERR_FILENO
, msg
, sizeof(msg
));
111 lttng_consumer_should_exit(ctx
);
116 * Setup signal handler for :
117 * SIGINT, SIGTERM, SIGPIPE, SIGBUS
119 static int set_signal_handler(void)
125 if ((ret
= sigemptyset(&sigset
)) < 0) {
126 PERROR("sigemptyset");
131 sa
.sa_flags
= SA_SIGINFO
;
133 sa
.sa_sigaction
= sighandler
;
134 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
139 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
144 if ((ret
= sigaction(SIGBUS
, &sa
, NULL
)) < 0) {
150 sa
.sa_handler
= SIG_IGN
;
151 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
160 * Usage function on stream file.
162 static void usage(FILE *fp
)
164 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
165 fprintf(fp
, " -h, --help "
166 "Display this usage.\n");
167 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
168 "Specify path for the command socket\n");
169 fprintf(fp
, " -e, --consumerd-err-sock PATH "
170 "Specify path for the error socket\n");
171 fprintf(fp
, " -d, --daemonize "
172 "Start as a daemon.\n");
173 fprintf(fp
, " -q, --quiet "
174 "No output at all.\n");
175 fprintf(fp
, " -v, --verbose "
176 "Verbose mode. Activate DBG() macro.\n");
177 fprintf(fp
, " -V, --version "
178 "Show version number.\n");
179 fprintf(fp
, " -g, --group NAME "
180 "Specify the tracing group name. (default: tracing)\n");
181 fprintf(fp
, " -k, --kernel "
182 "Consumer kernel buffers (default).\n");
183 fprintf(fp
, " -u, --ust "
184 "Consumer UST buffers.%s\n",
185 #ifdef HAVE_LIBLTTNG_UST_CTL
188 " (support not compiled in)"
194 * daemon argument parsing
196 static int parse_args(int argc
, char **argv
)
200 static struct option long_options
[] = {
201 { "consumerd-cmd-sock", 1, 0, 'c' },
202 { "consumerd-err-sock", 1, 0, 'e' },
203 { "daemonize", 0, 0, 'd' },
204 { "group", 1, 0, 'g' },
205 { "help", 0, 0, 'h' },
206 { "quiet", 0, 0, 'q' },
207 { "verbose", 0, 0, 'v' },
208 { "version", 0, 0, 'V' },
209 { "kernel", 0, 0, 'k' },
210 #ifdef HAVE_LIBLTTNG_UST_CTL
211 { "ust", 0, 0, 'u' },
217 int option_index
= 0;
218 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
219 long_options
, &option_index
);
226 fprintf(stderr
, "option %s",
227 long_options
[option_index
].name
);
229 fprintf(stderr
, " with arg %s\n", optarg
);
235 if (lttng_is_setuid_setgid()) {
236 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
237 "-c, --consumerd-cmd-sock");
239 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
243 if (lttng_is_setuid_setgid()) {
244 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
245 "-e, --consumerd-err-sock");
247 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
254 if (lttng_is_setuid_setgid()) {
255 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
258 tracing_group_name
= optarg
;
268 lttng_opt_verbose
= 3;
271 fprintf(stdout
, "%s\n", VERSION
);
274 opt_type
= LTTNG_CONSUMER_KERNEL
;
276 #ifdef HAVE_LIBLTTNG_UST_CTL
278 # if (CAA_BITS_PER_LONG == 64)
279 opt_type
= LTTNG_CONSUMER64_UST
;
280 # elif (CAA_BITS_PER_LONG == 32)
281 opt_type
= LTTNG_CONSUMER32_UST
;
283 # error "Unknown bitness"
298 * Set open files limit to unlimited. This daemon can open a large number of
299 * file descriptors in order to consumer multiple kernel traces.
301 static void set_ulimit(void)
306 /* The kernel does not allowed an infinite limit for open files */
307 lim
.rlim_cur
= 65535;
308 lim
.rlim_max
= 65535;
310 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
312 PERROR("failed to set open files limit");
319 int main(int argc
, char **argv
)
321 int ret
= 0, retval
= 0;
323 struct lttng_consumer_local_data
*tmp_ctx
;
325 rcu_register_thread();
327 if (run_as_create_worker(argv
[0], NULL
, NULL
) < 0) {
328 goto exit_set_signal_handler
;
331 if (set_signal_handler()) {
333 goto exit_set_signal_handler
;
336 /* Parse arguments */
338 if (parse_args(argc
, argv
)) {
349 * child: setsid, close FD 0, 1, 2, chdir /
350 * parent: exit (if fork is successful)
359 * We are in the child. Make sure all other file
360 * descriptors are closed, in case we are called with
361 * more opened file descriptors than the standard ones.
363 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
369 * Starting from here, we can create threads. This needs to be after
370 * lttng_daemonize due to RCU.
373 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
374 if (!health_consumerd
) {
376 goto exit_health_consumerd_cleanup
;
379 if (*command_sock_path
== '\0') {
381 case LTTNG_CONSUMER_KERNEL
:
382 ret
= snprintf(command_sock_path
, PATH_MAX
,
383 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
384 DEFAULT_LTTNG_RUNDIR
);
390 case LTTNG_CONSUMER64_UST
:
391 ret
= snprintf(command_sock_path
, PATH_MAX
,
392 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
393 DEFAULT_LTTNG_RUNDIR
);
399 case LTTNG_CONSUMER32_UST
:
400 ret
= snprintf(command_sock_path
, PATH_MAX
,
401 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
402 DEFAULT_LTTNG_RUNDIR
);
409 ERR("Unknown consumerd type");
416 if (lttng_consumer_init()) {
421 /* Initialize communication library */
423 /* Initialize TCP timeout values */
427 /* Set limit for open files */
431 /* create the consumer instance with and assign the callbacks */
432 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
433 NULL
, lttng_consumer_on_recv_stream
, NULL
);
439 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
440 if (*error_sock_path
== '\0') {
442 case LTTNG_CONSUMER_KERNEL
:
443 ret
= snprintf(error_sock_path
, PATH_MAX
,
444 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
445 DEFAULT_LTTNG_RUNDIR
);
451 case LTTNG_CONSUMER64_UST
:
452 ret
= snprintf(error_sock_path
, PATH_MAX
,
453 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
454 DEFAULT_LTTNG_RUNDIR
);
460 case LTTNG_CONSUMER32_UST
:
461 ret
= snprintf(error_sock_path
, PATH_MAX
,
462 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
463 DEFAULT_LTTNG_RUNDIR
);
470 ERR("Unknown consumerd type");
476 /* Connect to the socket created by lttng-sessiond to report errors */
477 DBG("Connecting to error socket %s", error_sock_path
);
478 ret
= lttcomm_connect_unix_sock(error_sock_path
);
480 * Not a fatal error, but all communication with lttng-sessiond will
484 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
486 lttng_consumer_set_error_sock(ctx
, ret
);
489 * Block RT signals used for UST periodical metadata flush and the live
490 * timer in main, and create a dedicated thread to handle these signals.
492 if (consumer_signal_init()) {
497 ctx
->type
= opt_type
;
499 if (utils_create_pipe(health_quit_pipe
)) {
501 goto exit_health_pipe
;
504 /* Create thread to manage the client socket */
505 ret
= pthread_create(&health_thread
, default_pthread_attr(),
506 thread_manage_health
, (void *) NULL
);
509 PERROR("pthread_create health");
511 goto exit_health_thread
;
515 * Wait for health thread to be initialized before letting the
516 * sessiond thread reply to the sessiond that we are ready.
518 while (uatomic_read(<tng_consumer_ready
)) {
521 cmm_smp_mb(); /* Read ready before following operations */
524 * Create the thread to manage the UST metadata periodic timer and
527 ret
= pthread_create(&metadata_timer_thread
, NULL
,
528 consumer_timer_thread
, (void *) ctx
);
531 PERROR("pthread_create");
533 goto exit_metadata_timer_thread
;
535 metadata_timer_thread_online
= true;
537 /* Create thread to manage channels */
538 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
539 consumer_thread_channel_poll
,
543 PERROR("pthread_create");
545 goto exit_channel_thread
;
548 /* Create thread to manage the polling/writing of trace metadata */
549 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
550 consumer_thread_metadata_poll
,
554 PERROR("pthread_create");
556 goto exit_metadata_thread
;
559 /* Create thread to manage the polling/writing of trace data */
560 ret
= pthread_create(&data_thread
, default_pthread_attr(),
561 consumer_thread_data_poll
, (void *) ctx
);
564 PERROR("pthread_create");
566 goto exit_data_thread
;
569 /* Create the thread to manage the reception of fds */
570 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
571 consumer_thread_sessiond_poll
,
575 PERROR("pthread_create");
577 goto exit_sessiond_thread
;
582 * This is where we start awaiting program completion (e.g. through
583 * signal that asks threads to teardown.
586 ret
= pthread_join(sessiond_thread
, &status
);
589 PERROR("pthread_join sessiond_thread");
592 exit_sessiond_thread
:
594 ret
= pthread_join(data_thread
, &status
);
597 PERROR("pthread_join data_thread");
602 ret
= pthread_join(metadata_thread
, &status
);
605 PERROR("pthread_join metadata_thread");
608 exit_metadata_thread
:
610 ret
= pthread_join(channel_thread
, &status
);
613 PERROR("pthread_join channel_thread");
618 exit_metadata_timer_thread
:
620 ret
= pthread_join(health_thread
, &status
);
623 PERROR("pthread_join health_thread");
628 utils_close_pipe(health_quit_pipe
);
633 * Wait for all pending call_rcu work to complete before tearing
634 * down data structures. call_rcu worker may be trying to
635 * perform lookups in those structures.
638 lttng_consumer_cleanup();
640 * Tearing down the metadata timer thread in a
641 * non-fully-symmetric fashion compared to its creation in case
642 * lttng_consumer_cleanup() ends up tearing down timers (which
643 * requires the timer thread to be alive).
645 if (metadata_timer_thread_online
) {
647 * Ensure the metadata timer thread exits only after all other
648 * threads are gone, because it is required to perform timer
649 * teardown synchronization.
651 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT
);
652 ret
= pthread_join(metadata_timer_thread
, &status
);
655 PERROR("pthread_join metadata_timer_thread");
658 ret
= consumer_timer_thread_get_channel_monitor_pipe();
662 PERROR("close channel monitor pipe");
665 metadata_timer_thread_online
= false;
669 cmm_barrier(); /* Clear ctx for signal handler. */
670 lttng_consumer_destroy(tmp_ctx
);
672 if (health_consumerd
) {
673 health_app_destroy(health_consumerd
);
675 /* Ensure all prior call_rcu are done. */
678 run_as_destroy_worker();
680 exit_health_consumerd_cleanup
:
682 exit_set_signal_handler
:
684 rcu_unregister_thread();