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/sessiond-comm/sessiond-comm.h>
51 #include <common/utils.h>
53 #include "lttng-consumerd.h"
54 #include "health-consumerd.h"
56 /* threads (channel handling, poll, metadata, sessiond) */
58 static pthread_t channel_thread
, data_thread
, metadata_thread
,
59 sessiond_thread
, metadata_timer_thread
, health_thread
;
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 * Ignore SIGPIPE because it should not stop the consumer whenever a
105 * SIGPIPE is catched through a FD operation.
107 if (sig
== SIGPIPE
) {
111 lttng_consumer_should_exit(ctx
);
115 * Setup signal handler for :
116 * SIGINT, SIGTERM, SIGPIPE
118 static int set_signal_handler(void)
124 if ((ret
= sigemptyset(&sigset
)) < 0) {
125 PERROR("sigemptyset");
129 sa
.sa_handler
= sighandler
;
132 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
137 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
142 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
151 * Usage function on stream file.
153 static void usage(FILE *fp
)
155 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
156 fprintf(fp
, " -h, --help "
157 "Display this usage.\n");
158 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
159 "Specify path for the command socket\n");
160 fprintf(fp
, " -e, --consumerd-err-sock PATH "
161 "Specify path for the error socket\n");
162 fprintf(fp
, " -d, --daemonize "
163 "Start as a daemon.\n");
164 fprintf(fp
, " -q, --quiet "
165 "No output at all.\n");
166 fprintf(fp
, " -v, --verbose "
167 "Verbose mode. Activate DBG() macro.\n");
168 fprintf(fp
, " -V, --version "
169 "Show version number.\n");
170 fprintf(fp
, " -g, --group NAME "
171 "Specify the tracing group name. (default: tracing)\n");
172 fprintf(fp
, " -k, --kernel "
173 "Consumer kernel buffers (default).\n");
174 fprintf(fp
, " -u, --ust "
175 "Consumer UST buffers.%s\n",
176 #ifdef HAVE_LIBLTTNG_UST_CTL
179 " (support not compiled in)"
185 * daemon argument parsing
187 static int parse_args(int argc
, char **argv
)
191 static struct option long_options
[] = {
192 { "consumerd-cmd-sock", 1, 0, 'c' },
193 { "consumerd-err-sock", 1, 0, 'e' },
194 { "daemonize", 0, 0, 'd' },
195 { "group", 1, 0, 'g' },
196 { "help", 0, 0, 'h' },
197 { "quiet", 0, 0, 'q' },
198 { "verbose", 0, 0, 'v' },
199 { "version", 0, 0, 'V' },
200 { "kernel", 0, 0, 'k' },
201 #ifdef HAVE_LIBLTTNG_UST_CTL
202 { "ust", 0, 0, 'u' },
208 int option_index
= 0;
209 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
210 long_options
, &option_index
);
217 fprintf(stderr
, "option %s",
218 long_options
[option_index
].name
);
220 fprintf(stderr
, " with arg %s\n", optarg
);
226 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
229 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
235 tracing_group_name
= optarg
;
244 lttng_opt_verbose
= 1;
247 fprintf(stdout
, "%s\n", VERSION
);
250 opt_type
= LTTNG_CONSUMER_KERNEL
;
252 #ifdef HAVE_LIBLTTNG_UST_CTL
254 # if (CAA_BITS_PER_LONG == 64)
255 opt_type
= LTTNG_CONSUMER64_UST
;
256 # elif (CAA_BITS_PER_LONG == 32)
257 opt_type
= LTTNG_CONSUMER32_UST
;
259 # error "Unknown bitness"
274 * Set open files limit to unlimited. This daemon can open a large number of
275 * file descriptors in order to consumer multiple kernel traces.
277 static void set_ulimit(void)
282 /* The kernel does not allowed an infinite limit for open files */
283 lim
.rlim_cur
= 65535;
284 lim
.rlim_max
= 65535;
286 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
288 PERROR("failed to set open files limit");
295 int main(int argc
, char **argv
)
297 int ret
= 0, retval
= 0;
300 if (set_signal_handler()) {
302 goto exit_set_signal_handler
;
305 /* Parse arguments */
307 if (parse_args(argc
, argv
)) {
318 * child: setsid, close FD 0, 1, 2, chdir /
319 * parent: exit (if fork is successful)
328 * We are in the child. Make sure all other file
329 * descriptors are closed, in case we are called with
330 * more opened file descriptors than the standard ones.
332 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
338 * Starting from here, we can create threads. This needs to be after
339 * lttng_daemonize due to RCU.
342 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
343 if (!health_consumerd
) {
345 goto exit_health_consumerd_cleanup
;
348 /* Set up max poll set size */
349 if (lttng_poll_set_max_size()) {
354 if (*command_sock_path
== '\0') {
356 case LTTNG_CONSUMER_KERNEL
:
357 ret
= snprintf(command_sock_path
, PATH_MAX
,
358 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
359 DEFAULT_LTTNG_RUNDIR
);
365 case LTTNG_CONSUMER64_UST
:
366 ret
= snprintf(command_sock_path
, PATH_MAX
,
367 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
368 DEFAULT_LTTNG_RUNDIR
);
374 case LTTNG_CONSUMER32_UST
:
375 ret
= snprintf(command_sock_path
, PATH_MAX
,
376 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
377 DEFAULT_LTTNG_RUNDIR
);
384 ERR("Unknown consumerd type");
391 if (lttng_consumer_init()) {
396 /* Initialize communication library */
398 /* Initialize TCP timeout values */
402 /* Set limit for open files */
406 /* create the consumer instance with and assign the callbacks */
407 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
408 NULL
, lttng_consumer_on_recv_stream
, NULL
);
414 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
415 if (*error_sock_path
== '\0') {
417 case LTTNG_CONSUMER_KERNEL
:
418 ret
= snprintf(error_sock_path
, PATH_MAX
,
419 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
420 DEFAULT_LTTNG_RUNDIR
);
426 case LTTNG_CONSUMER64_UST
:
427 ret
= snprintf(error_sock_path
, PATH_MAX
,
428 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
429 DEFAULT_LTTNG_RUNDIR
);
435 case LTTNG_CONSUMER32_UST
:
436 ret
= snprintf(error_sock_path
, PATH_MAX
,
437 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
438 DEFAULT_LTTNG_RUNDIR
);
445 ERR("Unknown consumerd type");
451 /* Connect to the socket created by lttng-sessiond to report errors */
452 DBG("Connecting to error socket %s", error_sock_path
);
453 ret
= lttcomm_connect_unix_sock(error_sock_path
);
455 * Not a fatal error, but all communication with lttng-sessiond will
459 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
461 lttng_consumer_set_error_sock(ctx
, ret
);
464 * Block RT signals used for UST periodical metadata flush and the live
465 * timer in main, and create a dedicated thread to handle these signals.
467 if (consumer_signal_init()) {
472 ctx
->type
= opt_type
;
474 if (utils_create_pipe(health_quit_pipe
)) {
476 goto exit_health_pipe
;
479 /* Create thread to manage the client socket */
480 ret
= pthread_create(&health_thread
, NULL
,
481 thread_manage_health
, (void *) NULL
);
484 PERROR("pthread_create health");
486 goto exit_health_thread
;
490 * Wait for health thread to be initialized before letting the
491 * sessiond thread reply to the sessiond that we are ready.
493 while (uatomic_read(<tng_consumer_ready
)) {
496 cmm_smp_mb(); /* Read ready before following operations */
498 /* Create thread to manage channels */
499 ret
= pthread_create(&channel_thread
, NULL
,
500 consumer_thread_channel_poll
,
504 PERROR("pthread_create");
506 goto exit_channel_thread
;
509 /* Create thread to manage the polling/writing of trace metadata */
510 ret
= pthread_create(&metadata_thread
, NULL
,
511 consumer_thread_metadata_poll
,
515 PERROR("pthread_create");
517 goto exit_metadata_thread
;
520 /* Create thread to manage the polling/writing of trace data */
521 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
525 PERROR("pthread_create");
527 goto exit_data_thread
;
530 /* Create the thread to manage the receive of fd */
531 ret
= pthread_create(&sessiond_thread
, NULL
,
532 consumer_thread_sessiond_poll
,
536 PERROR("pthread_create");
538 goto exit_sessiond_thread
;
542 * Create the thread to manage the UST metadata periodic timer and
545 ret
= pthread_create(&metadata_timer_thread
, NULL
,
546 consumer_timer_thread
, (void *) ctx
);
549 PERROR("pthread_create");
551 goto exit_metadata_timer_thread
;
554 ret
= pthread_detach(metadata_timer_thread
);
557 PERROR("pthread_detach");
559 goto exit_metadata_timer_detach
;
563 * This is where we start awaiting program completion (e.g. through
564 * signal that asks threads to teardown.
567 exit_metadata_timer_detach
:
568 exit_metadata_timer_thread
:
569 ret
= pthread_join(sessiond_thread
, &status
);
572 PERROR("pthread_join sessiond_thread");
575 exit_sessiond_thread
:
577 ret
= pthread_join(data_thread
, &status
);
580 PERROR("pthread_join data_thread");
585 ret
= pthread_join(metadata_thread
, &status
);
588 PERROR("pthread_join metadata_thread");
591 exit_metadata_thread
:
593 ret
= pthread_join(channel_thread
, &status
);
596 PERROR("pthread_join channel_thread");
601 ret
= pthread_join(health_thread
, &status
);
604 PERROR("pthread_join health_thread");
609 utils_close_pipe(health_quit_pipe
);
613 lttng_consumer_destroy(ctx
);
614 lttng_consumer_cleanup();
616 if (health_consumerd
) {
617 health_app_destroy(health_consumerd
);
619 exit_health_consumerd_cleanup
:
623 exit_set_signal_handler
: