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
;
60 /* to count the number of times the user pressed ctrl+c */
61 static int sigintcount
= 0;
63 /* Argument variables */
64 int lttng_opt_quiet
; /* not static in error.h */
65 int lttng_opt_verbose
; /* not static in error.h */
66 int lttng_opt_mi
; /* not static in error.h */
68 static int opt_daemon
;
69 static const char *progname
;
70 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
71 static char error_sock_path
[PATH_MAX
]; /* Global error path */
72 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
74 /* the liblttngconsumerd context */
75 static struct lttng_consumer_local_data
*ctx
;
77 /* Consumerd health monitoring */
78 struct health_app
*health_consumerd
;
80 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
82 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
84 enum lttng_consumer_type
lttng_consumer_get_type(void)
87 return LTTNG_CONSUMER_UNKNOWN
;
93 * Signal handler for the daemon
95 static void sighandler(int sig
)
97 if (sig
== SIGINT
&& sigintcount
++ == 0) {
98 DBG("ignoring first SIGINT");
103 lttng_consumer_should_exit(ctx
);
108 * Setup signal handler for :
109 * SIGINT, SIGTERM, SIGPIPE
111 static int set_signal_handler(void)
117 if ((ret
= sigemptyset(&sigset
)) < 0) {
118 PERROR("sigemptyset");
125 sa
.sa_handler
= sighandler
;
126 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
131 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
136 sa
.sa_handler
= SIG_IGN
;
137 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
146 * Usage function on stream file.
148 static void usage(FILE *fp
)
150 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
151 fprintf(fp
, " -h, --help "
152 "Display this usage.\n");
153 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
154 "Specify path for the command socket\n");
155 fprintf(fp
, " -e, --consumerd-err-sock PATH "
156 "Specify path for the error socket\n");
157 fprintf(fp
, " -d, --daemonize "
158 "Start as a daemon.\n");
159 fprintf(fp
, " -q, --quiet "
160 "No output at all.\n");
161 fprintf(fp
, " -v, --verbose "
162 "Verbose mode. Activate DBG() macro.\n");
163 fprintf(fp
, " -V, --version "
164 "Show version number.\n");
165 fprintf(fp
, " -g, --group NAME "
166 "Specify the tracing group name. (default: tracing)\n");
167 fprintf(fp
, " -k, --kernel "
168 "Consumer kernel buffers (default).\n");
169 fprintf(fp
, " -u, --ust "
170 "Consumer UST buffers.%s\n",
171 #ifdef HAVE_LIBLTTNG_UST_CTL
174 " (support not compiled in)"
180 * daemon argument parsing
182 static int parse_args(int argc
, char **argv
)
186 static struct option long_options
[] = {
187 { "consumerd-cmd-sock", 1, 0, 'c' },
188 { "consumerd-err-sock", 1, 0, 'e' },
189 { "daemonize", 0, 0, 'd' },
190 { "group", 1, 0, 'g' },
191 { "help", 0, 0, 'h' },
192 { "quiet", 0, 0, 'q' },
193 { "verbose", 0, 0, 'v' },
194 { "version", 0, 0, 'V' },
195 { "kernel", 0, 0, 'k' },
196 #ifdef HAVE_LIBLTTNG_UST_CTL
197 { "ust", 0, 0, 'u' },
203 int option_index
= 0;
204 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
205 long_options
, &option_index
);
212 fprintf(stderr
, "option %s",
213 long_options
[option_index
].name
);
215 fprintf(stderr
, " with arg %s\n", optarg
);
221 if (lttng_is_setuid_setgid()) {
222 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
223 "-c, --consumerd-cmd-sock");
225 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
229 if (lttng_is_setuid_setgid()) {
230 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
231 "-e, --consumerd-err-sock");
233 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
240 if (lttng_is_setuid_setgid()) {
241 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
244 tracing_group_name
= optarg
;
254 lttng_opt_verbose
= 1;
257 fprintf(stdout
, "%s\n", VERSION
);
260 opt_type
= LTTNG_CONSUMER_KERNEL
;
262 #ifdef HAVE_LIBLTTNG_UST_CTL
264 # if (CAA_BITS_PER_LONG == 64)
265 opt_type
= LTTNG_CONSUMER64_UST
;
266 # elif (CAA_BITS_PER_LONG == 32)
267 opt_type
= LTTNG_CONSUMER32_UST
;
269 # error "Unknown bitness"
284 * Set open files limit to unlimited. This daemon can open a large number of
285 * file descriptors in order to consumer multiple kernel traces.
287 static void set_ulimit(void)
292 /* The kernel does not allowed an infinite limit for open files */
293 lim
.rlim_cur
= 65535;
294 lim
.rlim_max
= 65535;
296 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
298 PERROR("failed to set open files limit");
305 int main(int argc
, char **argv
)
307 int ret
= 0, retval
= 0;
309 struct lttng_consumer_local_data
*tmp_ctx
;
311 rcu_register_thread();
313 if (set_signal_handler()) {
315 goto exit_set_signal_handler
;
318 /* Parse arguments */
320 if (parse_args(argc
, argv
)) {
331 * child: setsid, close FD 0, 1, 2, chdir /
332 * parent: exit (if fork is successful)
341 * We are in the child. Make sure all other file
342 * descriptors are closed, in case we are called with
343 * more opened file descriptors than the standard ones.
345 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
351 * Starting from here, we can create threads. This needs to be after
352 * lttng_daemonize due to RCU.
355 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
356 if (!health_consumerd
) {
358 goto exit_health_consumerd_cleanup
;
361 if (*command_sock_path
== '\0') {
363 case LTTNG_CONSUMER_KERNEL
:
364 ret
= snprintf(command_sock_path
, PATH_MAX
,
365 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
366 DEFAULT_LTTNG_RUNDIR
);
372 case LTTNG_CONSUMER64_UST
:
373 ret
= snprintf(command_sock_path
, PATH_MAX
,
374 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
375 DEFAULT_LTTNG_RUNDIR
);
381 case LTTNG_CONSUMER32_UST
:
382 ret
= snprintf(command_sock_path
, PATH_MAX
,
383 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
384 DEFAULT_LTTNG_RUNDIR
);
391 ERR("Unknown consumerd type");
398 if (lttng_consumer_init()) {
403 /* Initialize communication library */
405 /* Initialize TCP timeout values */
409 /* Set limit for open files */
413 if (run_as_create_worker(argv
[0]) < 0) {
417 /* create the consumer instance with and assign the callbacks */
418 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
419 NULL
, lttng_consumer_on_recv_stream
, NULL
);
425 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
426 if (*error_sock_path
== '\0') {
428 case LTTNG_CONSUMER_KERNEL
:
429 ret
= snprintf(error_sock_path
, PATH_MAX
,
430 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
431 DEFAULT_LTTNG_RUNDIR
);
437 case LTTNG_CONSUMER64_UST
:
438 ret
= snprintf(error_sock_path
, PATH_MAX
,
439 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
440 DEFAULT_LTTNG_RUNDIR
);
446 case LTTNG_CONSUMER32_UST
:
447 ret
= snprintf(error_sock_path
, PATH_MAX
,
448 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
449 DEFAULT_LTTNG_RUNDIR
);
456 ERR("Unknown consumerd type");
462 /* Connect to the socket created by lttng-sessiond to report errors */
463 DBG("Connecting to error socket %s", error_sock_path
);
464 ret
= lttcomm_connect_unix_sock(error_sock_path
);
466 * Not a fatal error, but all communication with lttng-sessiond will
470 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
472 lttng_consumer_set_error_sock(ctx
, ret
);
475 * Block RT signals used for UST periodical metadata flush and the live
476 * timer in main, and create a dedicated thread to handle these signals.
478 if (consumer_signal_init()) {
483 ctx
->type
= opt_type
;
485 if (utils_create_pipe(health_quit_pipe
)) {
487 goto exit_health_pipe
;
490 /* Create thread to manage the client socket */
491 ret
= pthread_create(&health_thread
, default_pthread_attr(),
492 thread_manage_health
, (void *) NULL
);
495 PERROR("pthread_create health");
497 goto exit_health_thread
;
501 * Wait for health thread to be initialized before letting the
502 * sessiond thread reply to the sessiond that we are ready.
504 while (uatomic_read(<tng_consumer_ready
)) {
507 cmm_smp_mb(); /* Read ready before following operations */
509 /* Create thread to manage channels */
510 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
511 consumer_thread_channel_poll
,
515 PERROR("pthread_create");
517 goto exit_channel_thread
;
520 /* Create thread to manage the polling/writing of trace metadata */
521 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
522 consumer_thread_metadata_poll
,
526 PERROR("pthread_create");
528 goto exit_metadata_thread
;
531 /* Create thread to manage the polling/writing of trace data */
532 ret
= pthread_create(&data_thread
, default_pthread_attr(),
533 consumer_thread_data_poll
, (void *) ctx
);
536 PERROR("pthread_create");
538 goto exit_data_thread
;
541 /* Create the thread to manage the receive of fd */
542 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
543 consumer_thread_sessiond_poll
,
547 PERROR("pthread_create");
549 goto exit_sessiond_thread
;
553 * Create the thread to manage the UST metadata periodic timer and
556 ret
= pthread_create(&metadata_timer_thread
, default_pthread_attr(),
557 consumer_timer_thread
, (void *) ctx
);
560 PERROR("pthread_create");
562 goto exit_metadata_timer_thread
;
565 ret
= pthread_detach(metadata_timer_thread
);
568 PERROR("pthread_detach");
570 goto exit_metadata_timer_detach
;
574 * This is where we start awaiting program completion (e.g. through
575 * signal that asks threads to teardown.
578 exit_metadata_timer_detach
:
579 exit_metadata_timer_thread
:
580 ret
= pthread_join(sessiond_thread
, &status
);
583 PERROR("pthread_join sessiond_thread");
586 exit_sessiond_thread
:
588 ret
= pthread_join(data_thread
, &status
);
591 PERROR("pthread_join data_thread");
596 ret
= pthread_join(metadata_thread
, &status
);
599 PERROR("pthread_join metadata_thread");
602 exit_metadata_thread
:
604 ret
= pthread_join(channel_thread
, &status
);
607 PERROR("pthread_join channel_thread");
612 ret
= pthread_join(health_thread
, &status
);
615 PERROR("pthread_join health_thread");
620 utils_close_pipe(health_quit_pipe
);
626 cmm_barrier(); /* Clear ctx for signal handler. */
628 * Wait for all pending call_rcu work to complete before tearing
629 * down data structures. call_rcu worker may be trying to
630 * perform lookups in those structures.
633 lttng_consumer_destroy(tmp_ctx
);
634 lttng_consumer_cleanup();
636 if (health_consumerd
) {
637 health_app_destroy(health_consumerd
);
639 /* Ensure all prior call_rcu are done. */
642 run_as_destroy_worker();
644 exit_health_consumerd_cleanup
:
646 exit_set_signal_handler
:
648 rcu_unregister_thread();