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
)
88 if (sig
== SIGINT
&& sigintcount
++ == 0) {
89 DBG("ignoring first SIGINT");
94 lttng_consumer_should_exit(ctx
);
99 * Setup signal handler for :
100 * SIGINT, SIGTERM, SIGPIPE
102 static int set_signal_handler(void)
108 if ((ret
= sigemptyset(&sigset
)) < 0) {
109 PERROR("sigemptyset");
116 sa
.sa_handler
= sighandler
;
117 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
122 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
127 sa
.sa_handler
= SIG_IGN
;
128 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
137 * Usage function on stream file.
139 static void usage(FILE *fp
)
141 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
142 fprintf(fp
, " -h, --help "
143 "Display this usage.\n");
144 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
145 "Specify path for the command socket\n");
146 fprintf(fp
, " -e, --consumerd-err-sock PATH "
147 "Specify path for the error socket\n");
148 fprintf(fp
, " -d, --daemonize "
149 "Start as a daemon.\n");
150 fprintf(fp
, " -q, --quiet "
151 "No output at all.\n");
152 fprintf(fp
, " -v, --verbose "
153 "Verbose mode. Activate DBG() macro.\n");
154 fprintf(fp
, " -V, --version "
155 "Show version number.\n");
156 fprintf(fp
, " -g, --group NAME "
157 "Specify the tracing group name. (default: tracing)\n");
158 fprintf(fp
, " -k, --kernel "
159 "Consumer kernel buffers (default).\n");
160 fprintf(fp
, " -u, --ust "
161 "Consumer UST buffers.%s\n",
162 #ifdef HAVE_LIBLTTNG_UST_CTL
165 " (support not compiled in)"
171 * daemon argument parsing
173 static int parse_args(int argc
, char **argv
)
177 static struct option long_options
[] = {
178 { "consumerd-cmd-sock", 1, 0, 'c' },
179 { "consumerd-err-sock", 1, 0, 'e' },
180 { "daemonize", 0, 0, 'd' },
181 { "group", 1, 0, 'g' },
182 { "help", 0, 0, 'h' },
183 { "quiet", 0, 0, 'q' },
184 { "verbose", 0, 0, 'v' },
185 { "version", 0, 0, 'V' },
186 { "kernel", 0, 0, 'k' },
187 #ifdef HAVE_LIBLTTNG_UST_CTL
188 { "ust", 0, 0, 'u' },
194 int option_index
= 0;
195 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
196 long_options
, &option_index
);
203 fprintf(stderr
, "option %s",
204 long_options
[option_index
].name
);
206 fprintf(stderr
, " with arg %s\n", optarg
);
212 if (lttng_is_setuid_setgid()) {
213 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
214 "-c, --consumerd-cmd-sock");
216 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
220 if (lttng_is_setuid_setgid()) {
221 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
222 "-e, --consumerd-err-sock");
224 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
231 if (lttng_is_setuid_setgid()) {
232 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
235 tracing_group_name
= optarg
;
245 lttng_opt_verbose
= 3;
248 fprintf(stdout
, "%s\n", VERSION
);
251 opt_type
= LTTNG_CONSUMER_KERNEL
;
253 #ifdef HAVE_LIBLTTNG_UST_CTL
255 # if (CAA_BITS_PER_LONG == 64)
256 opt_type
= LTTNG_CONSUMER64_UST
;
257 # elif (CAA_BITS_PER_LONG == 32)
258 opt_type
= LTTNG_CONSUMER32_UST
;
260 # error "Unknown bitness"
275 * Set open files limit to unlimited. This daemon can open a large number of
276 * file descriptors in order to consumer multiple kernel traces.
278 static void set_ulimit(void)
283 /* The kernel does not allowed an infinite limit for open files */
284 lim
.rlim_cur
= 65535;
285 lim
.rlim_max
= 65535;
287 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
289 PERROR("failed to set open files limit");
296 int main(int argc
, char **argv
)
298 int ret
= 0, retval
= 0;
300 struct lttng_consumer_local_data
*tmp_ctx
;
302 rcu_register_thread();
304 if (run_as_create_worker(argv
[0], NULL
, NULL
) < 0) {
305 goto exit_set_signal_handler
;
308 if (set_signal_handler()) {
310 goto exit_set_signal_handler
;
313 /* Parse arguments */
315 if (parse_args(argc
, argv
)) {
326 * child: setsid, close FD 0, 1, 2, chdir /
327 * parent: exit (if fork is successful)
336 * We are in the child. Make sure all other file
337 * descriptors are closed, in case we are called with
338 * more opened file descriptors than the standard ones.
340 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
346 * Starting from here, we can create threads. This needs to be after
347 * lttng_daemonize due to RCU.
350 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
351 if (!health_consumerd
) {
353 goto exit_health_consumerd_cleanup
;
356 if (*command_sock_path
== '\0') {
358 case LTTNG_CONSUMER_KERNEL
:
359 ret
= snprintf(command_sock_path
, PATH_MAX
,
360 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
361 DEFAULT_LTTNG_RUNDIR
);
367 case LTTNG_CONSUMER64_UST
:
368 ret
= snprintf(command_sock_path
, PATH_MAX
,
369 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
370 DEFAULT_LTTNG_RUNDIR
);
376 case LTTNG_CONSUMER32_UST
:
377 ret
= snprintf(command_sock_path
, PATH_MAX
,
378 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
379 DEFAULT_LTTNG_RUNDIR
);
386 ERR("Unknown consumerd type");
393 if (lttng_consumer_init()) {
398 /* Initialize communication library */
400 /* Initialize TCP timeout values */
404 /* Set limit for open files */
408 /* create the consumer instance with and assign the callbacks */
409 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
410 NULL
, lttng_consumer_on_recv_stream
, NULL
);
416 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
417 if (*error_sock_path
== '\0') {
419 case LTTNG_CONSUMER_KERNEL
:
420 ret
= snprintf(error_sock_path
, PATH_MAX
,
421 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
422 DEFAULT_LTTNG_RUNDIR
);
428 case LTTNG_CONSUMER64_UST
:
429 ret
= snprintf(error_sock_path
, PATH_MAX
,
430 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
431 DEFAULT_LTTNG_RUNDIR
);
437 case LTTNG_CONSUMER32_UST
:
438 ret
= snprintf(error_sock_path
, PATH_MAX
,
439 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
440 DEFAULT_LTTNG_RUNDIR
);
447 ERR("Unknown consumerd type");
453 /* Connect to the socket created by lttng-sessiond to report errors */
454 DBG("Connecting to error socket %s", error_sock_path
);
455 ret
= lttcomm_connect_unix_sock(error_sock_path
);
457 * Not a fatal error, but all communication with lttng-sessiond will
461 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
463 lttng_consumer_set_error_sock(ctx
, ret
);
466 * Block RT signals used for UST periodical metadata flush and the live
467 * timer in main, and create a dedicated thread to handle these signals.
469 if (consumer_signal_init()) {
474 ctx
->type
= opt_type
;
476 if (utils_create_pipe(health_quit_pipe
)) {
478 goto exit_health_pipe
;
481 /* Create thread to manage the client socket */
482 ret
= pthread_create(&health_thread
, default_pthread_attr(),
483 thread_manage_health
, (void *) NULL
);
486 PERROR("pthread_create health");
488 goto exit_health_thread
;
492 * Wait for health thread to be initialized before letting the
493 * sessiond thread reply to the sessiond that we are ready.
495 while (uatomic_read(<tng_consumer_ready
)) {
498 cmm_smp_mb(); /* Read ready before following operations */
501 * Create the thread to manage the UST metadata periodic timer and
504 ret
= pthread_create(&metadata_timer_thread
, NULL
,
505 consumer_timer_thread
, (void *) ctx
);
508 PERROR("pthread_create");
510 goto exit_metadata_timer_thread
;
512 metadata_timer_thread_online
= true;
514 /* Create thread to manage channels */
515 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
516 consumer_thread_channel_poll
,
520 PERROR("pthread_create");
522 goto exit_channel_thread
;
525 /* Create thread to manage the polling/writing of trace metadata */
526 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
527 consumer_thread_metadata_poll
,
531 PERROR("pthread_create");
533 goto exit_metadata_thread
;
536 /* Create thread to manage the polling/writing of trace data */
537 ret
= pthread_create(&data_thread
, default_pthread_attr(),
538 consumer_thread_data_poll
, (void *) ctx
);
541 PERROR("pthread_create");
543 goto exit_data_thread
;
546 /* Create the thread to manage the reception of fds */
547 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
548 consumer_thread_sessiond_poll
,
552 PERROR("pthread_create");
554 goto exit_sessiond_thread
;
559 * This is where we start awaiting program completion (e.g. through
560 * signal that asks threads to teardown.
563 ret
= pthread_join(sessiond_thread
, &status
);
566 PERROR("pthread_join sessiond_thread");
569 exit_sessiond_thread
:
571 ret
= pthread_join(data_thread
, &status
);
574 PERROR("pthread_join data_thread");
579 ret
= pthread_join(metadata_thread
, &status
);
582 PERROR("pthread_join metadata_thread");
585 exit_metadata_thread
:
587 ret
= pthread_join(channel_thread
, &status
);
590 PERROR("pthread_join channel_thread");
595 exit_metadata_timer_thread
:
597 ret
= pthread_join(health_thread
, &status
);
600 PERROR("pthread_join health_thread");
605 utils_close_pipe(health_quit_pipe
);
610 * Wait for all pending call_rcu work to complete before tearing
611 * down data structures. call_rcu worker may be trying to
612 * perform lookups in those structures.
615 lttng_consumer_cleanup();
617 * Tearing down the metadata timer thread in a
618 * non-fully-symmetric fashion compared to its creation in case
619 * lttng_consumer_cleanup() ends up tearing down timers (which
620 * requires the timer thread to be alive).
622 if (metadata_timer_thread_online
) {
624 * Ensure the metadata timer thread exits only after all other
625 * threads are gone, because it is required to perform timer
626 * teardown synchronization.
628 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT
);
629 ret
= pthread_join(metadata_timer_thread
, &status
);
632 PERROR("pthread_join metadata_timer_thread");
635 ret
= consumer_timer_thread_get_channel_monitor_pipe();
639 PERROR("close channel monitor pipe");
642 metadata_timer_thread_online
= false;
646 cmm_barrier(); /* Clear ctx for signal handler. */
647 lttng_consumer_destroy(tmp_ctx
);
649 if (health_consumerd
) {
650 health_app_destroy(health_consumerd
);
652 /* Ensure all prior call_rcu are done. */
655 run_as_destroy_worker();
657 exit_health_consumerd_cleanup
:
659 exit_set_signal_handler
:
661 rcu_unregister_thread();