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.
31 #include <sys/resource.h>
33 #include <sys/socket.h>
35 #include <sys/types.h>
36 #include <urcu/list.h>
42 #include <urcu/compiler.h>
45 #include <common/defaults.h>
46 #include <common/common.h>
47 #include <common/consumer.h>
48 #include <common/consumer-timer.h>
49 #include <common/compat/poll.h>
50 #include <common/compat/getenv.h>
51 #include <common/sessiond-comm/sessiond-comm.h>
52 #include <common/utils.h>
54 #include "lttng-consumerd.h"
55 #include "health-consumerd.h"
57 /* threads (channel handling, poll, metadata, sessiond) */
59 static pthread_t channel_thread
, data_thread
, metadata_thread
,
60 sessiond_thread
, metadata_timer_thread
, health_thread
;
62 /* to count the number of times the user pressed ctrl+c */
63 static int sigintcount
= 0;
65 /* Argument variables */
66 int lttng_opt_quiet
; /* not static in error.h */
67 int lttng_opt_verbose
; /* not static in error.h */
68 int lttng_opt_mi
; /* not static in error.h */
70 static int opt_daemon
;
71 static const char *progname
;
72 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
73 static char error_sock_path
[PATH_MAX
]; /* Global error path */
74 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
76 /* the liblttngconsumerd context */
77 static struct lttng_consumer_local_data
*ctx
;
79 /* Consumerd health monitoring */
80 struct health_app
*health_consumerd
;
82 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
84 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
86 enum lttng_consumer_type
lttng_consumer_get_type(void)
89 return LTTNG_CONSUMER_UNKNOWN
;
95 * Signal handler for the daemon
97 static void sighandler(int sig
)
99 if (sig
== SIGINT
&& sigintcount
++ == 0) {
100 DBG("ignoring first SIGINT");
105 lttng_consumer_should_exit(ctx
);
110 * Setup signal handler for :
111 * SIGINT, SIGTERM, SIGPIPE
113 static int set_signal_handler(void)
119 if ((ret
= sigemptyset(&sigset
)) < 0) {
120 PERROR("sigemptyset");
127 sa
.sa_handler
= sighandler
;
128 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
133 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
138 sa
.sa_handler
= SIG_IGN
;
139 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
148 * Usage function on stream file.
150 static void usage(FILE *fp
)
152 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
153 fprintf(fp
, " -h, --help "
154 "Display this usage.\n");
155 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
156 "Specify path for the command socket\n");
157 fprintf(fp
, " -e, --consumerd-err-sock PATH "
158 "Specify path for the error socket\n");
159 fprintf(fp
, " -d, --daemonize "
160 "Start as a daemon.\n");
161 fprintf(fp
, " -q, --quiet "
162 "No output at all.\n");
163 fprintf(fp
, " -v, --verbose "
164 "Verbose mode. Activate DBG() macro.\n");
165 fprintf(fp
, " -V, --version "
166 "Show version number.\n");
167 fprintf(fp
, " -g, --group NAME "
168 "Specify the tracing group name. (default: tracing)\n");
169 fprintf(fp
, " -k, --kernel "
170 "Consumer kernel buffers (default).\n");
171 fprintf(fp
, " -u, --ust "
172 "Consumer UST buffers.%s\n",
173 #ifdef HAVE_LIBLTTNG_UST_CTL
176 " (support not compiled in)"
182 * daemon argument parsing
184 static int parse_args(int argc
, char **argv
)
188 static struct option long_options
[] = {
189 { "consumerd-cmd-sock", 1, 0, 'c' },
190 { "consumerd-err-sock", 1, 0, 'e' },
191 { "daemonize", 0, 0, 'd' },
192 { "group", 1, 0, 'g' },
193 { "help", 0, 0, 'h' },
194 { "quiet", 0, 0, 'q' },
195 { "verbose", 0, 0, 'v' },
196 { "version", 0, 0, 'V' },
197 { "kernel", 0, 0, 'k' },
198 #ifdef HAVE_LIBLTTNG_UST_CTL
199 { "ust", 0, 0, 'u' },
205 int option_index
= 0;
206 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
207 long_options
, &option_index
);
214 fprintf(stderr
, "option %s",
215 long_options
[option_index
].name
);
217 fprintf(stderr
, " with arg %s\n", optarg
);
223 if (lttng_is_setuid_setgid()) {
224 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
225 "-c, --consumerd-cmd-sock");
227 snprintf(command_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.",
233 "-e, --consumerd-err-sock");
235 snprintf(error_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.",
246 tracing_group_name
= optarg
;
256 lttng_opt_verbose
= 1;
259 fprintf(stdout
, "%s\n", VERSION
);
262 opt_type
= LTTNG_CONSUMER_KERNEL
;
264 #ifdef HAVE_LIBLTTNG_UST_CTL
266 # if (CAA_BITS_PER_LONG == 64)
267 opt_type
= LTTNG_CONSUMER64_UST
;
268 # elif (CAA_BITS_PER_LONG == 32)
269 opt_type
= LTTNG_CONSUMER32_UST
;
271 # error "Unknown bitness"
286 * Set open files limit to unlimited. This daemon can open a large number of
287 * file descriptors in order to consumer multiple kernel traces.
289 static void set_ulimit(void)
294 /* The kernel does not allowed an infinite limit for open files */
295 lim
.rlim_cur
= 65535;
296 lim
.rlim_max
= 65535;
298 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
300 PERROR("failed to set open files limit");
307 int main(int argc
, char **argv
)
309 int ret
= 0, retval
= 0;
311 struct lttng_consumer_local_data
*tmp_ctx
;
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 /* Set up max poll set size */
362 if (lttng_poll_set_max_size()) {
367 if (*command_sock_path
== '\0') {
369 case LTTNG_CONSUMER_KERNEL
:
370 ret
= snprintf(command_sock_path
, PATH_MAX
,
371 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
372 DEFAULT_LTTNG_RUNDIR
);
378 case LTTNG_CONSUMER64_UST
:
379 ret
= snprintf(command_sock_path
, PATH_MAX
,
380 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
381 DEFAULT_LTTNG_RUNDIR
);
387 case LTTNG_CONSUMER32_UST
:
388 ret
= snprintf(command_sock_path
, PATH_MAX
,
389 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
390 DEFAULT_LTTNG_RUNDIR
);
397 ERR("Unknown consumerd type");
404 if (lttng_consumer_init()) {
409 /* Initialize communication library */
411 /* Initialize TCP timeout values */
415 /* Set limit for open files */
419 if (run_as_create_worker(argv
[0]) < 0) {
423 /* create the consumer instance with and assign the callbacks */
424 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
425 NULL
, lttng_consumer_on_recv_stream
, NULL
);
431 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
432 if (*error_sock_path
== '\0') {
434 case LTTNG_CONSUMER_KERNEL
:
435 ret
= snprintf(error_sock_path
, PATH_MAX
,
436 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
437 DEFAULT_LTTNG_RUNDIR
);
443 case LTTNG_CONSUMER64_UST
:
444 ret
= snprintf(error_sock_path
, PATH_MAX
,
445 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
446 DEFAULT_LTTNG_RUNDIR
);
452 case LTTNG_CONSUMER32_UST
:
453 ret
= snprintf(error_sock_path
, PATH_MAX
,
454 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
455 DEFAULT_LTTNG_RUNDIR
);
462 ERR("Unknown consumerd type");
468 /* Connect to the socket created by lttng-sessiond to report errors */
469 DBG("Connecting to error socket %s", error_sock_path
);
470 ret
= lttcomm_connect_unix_sock(error_sock_path
);
472 * Not a fatal error, but all communication with lttng-sessiond will
476 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
478 lttng_consumer_set_error_sock(ctx
, ret
);
481 * Block RT signals used for UST periodical metadata flush and the live
482 * timer in main, and create a dedicated thread to handle these signals.
484 if (consumer_signal_init()) {
489 ctx
->type
= opt_type
;
491 if (utils_create_pipe(health_quit_pipe
)) {
493 goto exit_health_pipe
;
496 /* Create thread to manage the client socket */
497 ret
= pthread_create(&health_thread
, NULL
,
498 thread_manage_health
, (void *) NULL
);
501 PERROR("pthread_create health");
503 goto exit_health_thread
;
507 * Wait for health thread to be initialized before letting the
508 * sessiond thread reply to the sessiond that we are ready.
510 while (uatomic_read(<tng_consumer_ready
)) {
513 cmm_smp_mb(); /* Read ready before following operations */
515 /* Create thread to manage channels */
516 ret
= pthread_create(&channel_thread
, NULL
,
517 consumer_thread_channel_poll
,
521 PERROR("pthread_create");
523 goto exit_channel_thread
;
526 /* Create thread to manage the polling/writing of trace metadata */
527 ret
= pthread_create(&metadata_thread
, NULL
,
528 consumer_thread_metadata_poll
,
532 PERROR("pthread_create");
534 goto exit_metadata_thread
;
537 /* Create thread to manage the polling/writing of trace data */
538 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
542 PERROR("pthread_create");
544 goto exit_data_thread
;
547 /* Create the thread to manage the receive of fd */
548 ret
= pthread_create(&sessiond_thread
, NULL
,
549 consumer_thread_sessiond_poll
,
553 PERROR("pthread_create");
555 goto exit_sessiond_thread
;
559 * Create the thread to manage the UST metadata periodic timer and
562 ret
= pthread_create(&metadata_timer_thread
, NULL
,
563 consumer_timer_thread
, (void *) ctx
);
566 PERROR("pthread_create");
568 goto exit_metadata_timer_thread
;
571 ret
= pthread_detach(metadata_timer_thread
);
574 PERROR("pthread_detach");
576 goto exit_metadata_timer_detach
;
580 * This is where we start awaiting program completion (e.g. through
581 * signal that asks threads to teardown.
584 exit_metadata_timer_detach
:
585 exit_metadata_timer_thread
:
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 ret
= pthread_join(health_thread
, &status
);
621 PERROR("pthread_join health_thread");
626 utils_close_pipe(health_quit_pipe
);
632 cmm_barrier(); /* Clear ctx for signal handler. */
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
: