2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include <sys/resource.h>
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
40 #include <urcu/compiler.h>
43 #include <common/defaults.h>
44 #include <common/common.h>
45 #include <common/consumer/consumer.h>
46 #include <common/consumer/consumer-timer.h>
47 #include <common/compat/poll.h>
48 #include <common/compat/getenv.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
50 #include <common/utils.h>
52 #include "lttng-consumerd.h"
53 #include "health-consumerd.h"
55 /* threads (channel handling, poll, metadata, sessiond) */
57 static pthread_t channel_thread
, data_thread
, metadata_thread
,
58 sessiond_thread
, metadata_timer_thread
, health_thread
;
59 static bool metadata_timer_thread_online
;
61 /* to count the number of times the user pressed ctrl+c */
62 static int sigintcount
= 0;
64 /* Argument variables */
65 int lttng_opt_quiet
; /* not static in error.h */
66 int lttng_opt_verbose
; /* not static in error.h */
67 int lttng_opt_mi
; /* not static in error.h */
69 static int opt_daemon
;
70 static const char *progname
;
71 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
72 static char error_sock_path
[PATH_MAX
]; /* Global error path */
73 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
75 /* the liblttngconsumerd context */
76 static struct lttng_consumer_local_data
*ctx
;
78 /* Consumerd health monitoring */
79 struct health_app
*health_consumerd
;
81 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
83 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
85 enum lttng_consumer_type
lttng_consumer_get_type(void)
88 return LTTNG_CONSUMER_UNKNOWN
;
94 * Signal handler for the daemon
96 static void sighandler(int sig
)
98 if (sig
== SIGINT
&& sigintcount
++ == 0) {
99 DBG("ignoring first SIGINT");
104 lttng_consumer_should_exit(ctx
);
109 * Setup signal handler for :
110 * SIGINT, SIGTERM, SIGPIPE
112 static int set_signal_handler(void)
118 if ((ret
= sigemptyset(&sigset
)) < 0) {
119 PERROR("sigemptyset");
126 sa
.sa_handler
= sighandler
;
127 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
132 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
137 sa
.sa_handler
= SIG_IGN
;
138 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
147 * Usage function on stream file.
149 static void usage(FILE *fp
)
151 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
152 fprintf(fp
, " -h, --help "
153 "Display this usage.\n");
154 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
155 "Specify path for the command socket\n");
156 fprintf(fp
, " -e, --consumerd-err-sock PATH "
157 "Specify path for the error socket\n");
158 fprintf(fp
, " -d, --daemonize "
159 "Start as a daemon.\n");
160 fprintf(fp
, " -q, --quiet "
161 "No output at all.\n");
162 fprintf(fp
, " -v, --verbose "
163 "Verbose mode. Activate DBG() macro.\n");
164 fprintf(fp
, " -V, --version "
165 "Show version number.\n");
166 fprintf(fp
, " -g, --group NAME "
167 "Specify the tracing group name. (default: tracing)\n");
168 fprintf(fp
, " -k, --kernel "
169 "Consumer kernel buffers (default).\n");
170 fprintf(fp
, " -u, --ust "
171 "Consumer UST buffers.%s\n",
172 #ifdef HAVE_LIBLTTNG_UST_CTL
175 " (support not compiled in)"
181 * daemon argument parsing
183 static int parse_args(int argc
, char **argv
)
187 static struct option long_options
[] = {
188 { "consumerd-cmd-sock", 1, 0, 'c' },
189 { "consumerd-err-sock", 1, 0, 'e' },
190 { "daemonize", 0, 0, 'd' },
191 { "group", 1, 0, 'g' },
192 { "help", 0, 0, 'h' },
193 { "quiet", 0, 0, 'q' },
194 { "verbose", 0, 0, 'v' },
195 { "version", 0, 0, 'V' },
196 { "kernel", 0, 0, 'k' },
197 #ifdef HAVE_LIBLTTNG_UST_CTL
198 { "ust", 0, 0, 'u' },
204 int option_index
= 0;
205 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
206 long_options
, &option_index
);
213 fprintf(stderr
, "option %s",
214 long_options
[option_index
].name
);
216 fprintf(stderr
, " with arg %s\n", optarg
);
222 if (lttng_is_setuid_setgid()) {
223 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
224 "-c, --consumerd-cmd-sock");
226 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
230 if (lttng_is_setuid_setgid()) {
231 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
232 "-e, --consumerd-err-sock");
234 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
241 if (lttng_is_setuid_setgid()) {
242 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
245 tracing_group_name
= optarg
;
255 lttng_opt_verbose
= 3;
258 fprintf(stdout
, "%s\n", VERSION
);
261 opt_type
= LTTNG_CONSUMER_KERNEL
;
263 #ifdef HAVE_LIBLTTNG_UST_CTL
265 # if (CAA_BITS_PER_LONG == 64)
266 opt_type
= LTTNG_CONSUMER64_UST
;
267 # elif (CAA_BITS_PER_LONG == 32)
268 opt_type
= LTTNG_CONSUMER32_UST
;
270 # error "Unknown bitness"
285 * Set open files limit to unlimited. This daemon can open a large number of
286 * file descriptors in order to consumer multiple kernel traces.
288 static void set_ulimit(void)
293 /* The kernel does not allowed an infinite limit for open files */
294 lim
.rlim_cur
= 65535;
295 lim
.rlim_max
= 65535;
297 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
299 PERROR("failed to set open files limit");
306 int main(int argc
, char **argv
)
308 int ret
= 0, retval
= 0;
310 struct lttng_consumer_local_data
*tmp_ctx
;
312 rcu_register_thread();
314 if (run_as_create_worker(argv
[0], NULL
, NULL
) < 0) {
315 goto exit_set_signal_handler
;
318 if (set_signal_handler()) {
320 goto exit_set_signal_handler
;
323 /* Parse arguments */
325 if (parse_args(argc
, argv
)) {
336 * child: setsid, close FD 0, 1, 2, chdir /
337 * parent: exit (if fork is successful)
346 * We are in the child. Make sure all other file
347 * descriptors are closed, in case we are called with
348 * more opened file descriptors than the standard ones.
350 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
356 * Starting from here, we can create threads. This needs to be after
357 * lttng_daemonize due to RCU.
360 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
361 if (!health_consumerd
) {
363 goto exit_health_consumerd_cleanup
;
366 if (*command_sock_path
== '\0') {
368 case LTTNG_CONSUMER_KERNEL
:
369 ret
= snprintf(command_sock_path
, PATH_MAX
,
370 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
371 DEFAULT_LTTNG_RUNDIR
);
377 case LTTNG_CONSUMER64_UST
:
378 ret
= snprintf(command_sock_path
, PATH_MAX
,
379 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
380 DEFAULT_LTTNG_RUNDIR
);
386 case LTTNG_CONSUMER32_UST
:
387 ret
= snprintf(command_sock_path
, PATH_MAX
,
388 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
389 DEFAULT_LTTNG_RUNDIR
);
396 ERR("Unknown consumerd type");
403 if (lttng_consumer_init()) {
408 /* Initialize communication library */
410 /* Initialize TCP timeout values */
414 /* Set limit for open files */
418 /* create the consumer instance with and assign the callbacks */
419 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
420 NULL
, lttng_consumer_on_recv_stream
, NULL
);
426 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
427 if (*error_sock_path
== '\0') {
429 case LTTNG_CONSUMER_KERNEL
:
430 ret
= snprintf(error_sock_path
, PATH_MAX
,
431 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
432 DEFAULT_LTTNG_RUNDIR
);
438 case LTTNG_CONSUMER64_UST
:
439 ret
= snprintf(error_sock_path
, PATH_MAX
,
440 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
441 DEFAULT_LTTNG_RUNDIR
);
447 case LTTNG_CONSUMER32_UST
:
448 ret
= snprintf(error_sock_path
, PATH_MAX
,
449 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
450 DEFAULT_LTTNG_RUNDIR
);
457 ERR("Unknown consumerd type");
463 /* Connect to the socket created by lttng-sessiond to report errors */
464 DBG("Connecting to error socket %s", error_sock_path
);
465 ret
= lttcomm_connect_unix_sock(error_sock_path
);
467 * Not a fatal error, but all communication with lttng-sessiond will
471 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
473 lttng_consumer_set_error_sock(ctx
, ret
);
476 * Block RT signals used for UST periodical metadata flush and the live
477 * timer in main, and create a dedicated thread to handle these signals.
479 if (consumer_signal_init()) {
484 ctx
->type
= opt_type
;
486 if (utils_create_pipe(health_quit_pipe
)) {
488 goto exit_health_pipe
;
491 /* Create thread to manage the client socket */
492 ret
= pthread_create(&health_thread
, default_pthread_attr(),
493 thread_manage_health
, (void *) NULL
);
496 PERROR("pthread_create health");
498 goto exit_health_thread
;
502 * Wait for health thread to be initialized before letting the
503 * sessiond thread reply to the sessiond that we are ready.
505 while (uatomic_read(<tng_consumer_ready
)) {
508 cmm_smp_mb(); /* Read ready before following operations */
511 * Create the thread to manage the UST metadata periodic timer and
514 ret
= pthread_create(&metadata_timer_thread
, NULL
,
515 consumer_timer_thread
, (void *) ctx
);
518 PERROR("pthread_create");
520 goto exit_metadata_timer_thread
;
522 metadata_timer_thread_online
= true;
524 /* Create thread to manage channels */
525 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
526 consumer_thread_channel_poll
,
530 PERROR("pthread_create");
532 goto exit_channel_thread
;
535 /* Create thread to manage the polling/writing of trace metadata */
536 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
537 consumer_thread_metadata_poll
,
541 PERROR("pthread_create");
543 goto exit_metadata_thread
;
546 /* Create thread to manage the polling/writing of trace data */
547 ret
= pthread_create(&data_thread
, default_pthread_attr(),
548 consumer_thread_data_poll
, (void *) ctx
);
551 PERROR("pthread_create");
553 goto exit_data_thread
;
556 /* Create the thread to manage the reception of fds */
557 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
558 consumer_thread_sessiond_poll
,
562 PERROR("pthread_create");
564 goto exit_sessiond_thread
;
569 * This is where we start awaiting program completion (e.g. through
570 * signal that asks threads to teardown.
573 ret
= pthread_join(sessiond_thread
, &status
);
576 PERROR("pthread_join sessiond_thread");
579 exit_sessiond_thread
:
581 ret
= pthread_join(data_thread
, &status
);
584 PERROR("pthread_join data_thread");
589 ret
= pthread_join(metadata_thread
, &status
);
592 PERROR("pthread_join metadata_thread");
595 exit_metadata_thread
:
597 ret
= pthread_join(channel_thread
, &status
);
600 PERROR("pthread_join channel_thread");
605 exit_metadata_timer_thread
:
607 ret
= pthread_join(health_thread
, &status
);
610 PERROR("pthread_join health_thread");
615 utils_close_pipe(health_quit_pipe
);
620 * Wait for all pending call_rcu work to complete before tearing
621 * down data structures. call_rcu worker may be trying to
622 * perform lookups in those structures.
625 lttng_consumer_cleanup();
627 * Tearing down the metadata timer thread in a
628 * non-fully-symmetric fashion compared to its creation in case
629 * lttng_consumer_cleanup() ends up tearing down timers (which
630 * requires the timer thread to be alive).
632 if (metadata_timer_thread_online
) {
634 * Ensure the metadata timer thread exits only after all other
635 * threads are gone, because it is required to perform timer
636 * teardown synchronization.
638 kill(getpid(), LTTNG_CONSUMER_SIG_EXIT
);
639 ret
= pthread_join(metadata_timer_thread
, &status
);
642 PERROR("pthread_join metadata_timer_thread");
645 ret
= consumer_timer_thread_get_channel_monitor_pipe();
649 PERROR("close channel monitor pipe");
652 metadata_timer_thread_online
= false;
656 cmm_barrier(); /* Clear ctx for signal handler. */
657 lttng_consumer_destroy(tmp_ctx
);
659 if (health_consumerd
) {
660 health_app_destroy(health_consumerd
);
662 /* Ensure all prior call_rcu are done. */
665 run_as_destroy_worker();
667 exit_health_consumerd_cleanup
:
669 exit_set_signal_handler
:
671 rcu_unregister_thread();