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 * Ignore SIGPIPE because it should not stop the consumer whenever a
104 * SIGPIPE is caught through a FD operation.
106 if (sig
== SIGPIPE
) {
111 lttng_consumer_should_exit(ctx
);
116 * Setup signal handler for :
117 * SIGINT, SIGTERM, SIGPIPE
119 static int set_signal_handler(void)
125 if ((ret
= sigemptyset(&sigset
)) < 0) {
126 PERROR("sigemptyset");
130 sa
.sa_handler
= sighandler
;
133 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
138 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
143 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
152 * Usage function on stream file.
154 static void usage(FILE *fp
)
156 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
157 fprintf(fp
, " -h, --help "
158 "Display this usage.\n");
159 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
160 "Specify path for the command socket\n");
161 fprintf(fp
, " -e, --consumerd-err-sock PATH "
162 "Specify path for the error socket\n");
163 fprintf(fp
, " -d, --daemonize "
164 "Start as a daemon.\n");
165 fprintf(fp
, " -q, --quiet "
166 "No output at all.\n");
167 fprintf(fp
, " -v, --verbose "
168 "Verbose mode. Activate DBG() macro.\n");
169 fprintf(fp
, " -V, --version "
170 "Show version number.\n");
171 fprintf(fp
, " -g, --group NAME "
172 "Specify the tracing group name. (default: tracing)\n");
173 fprintf(fp
, " -k, --kernel "
174 "Consumer kernel buffers (default).\n");
175 fprintf(fp
, " -u, --ust "
176 "Consumer UST buffers.%s\n",
177 #ifdef HAVE_LIBLTTNG_UST_CTL
180 " (support not compiled in)"
186 * daemon argument parsing
188 static int parse_args(int argc
, char **argv
)
192 static struct option long_options
[] = {
193 { "consumerd-cmd-sock", 1, 0, 'c' },
194 { "consumerd-err-sock", 1, 0, 'e' },
195 { "daemonize", 0, 0, 'd' },
196 { "group", 1, 0, 'g' },
197 { "help", 0, 0, 'h' },
198 { "quiet", 0, 0, 'q' },
199 { "verbose", 0, 0, 'v' },
200 { "version", 0, 0, 'V' },
201 { "kernel", 0, 0, 'k' },
202 #ifdef HAVE_LIBLTTNG_UST_CTL
203 { "ust", 0, 0, 'u' },
209 int option_index
= 0;
210 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:",
211 long_options
, &option_index
);
218 fprintf(stderr
, "option %s",
219 long_options
[option_index
].name
);
221 fprintf(stderr
, " with arg %s\n", optarg
);
227 if (lttng_is_setuid_setgid()) {
228 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
229 "-c, --consumerd-cmd-sock");
231 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
235 if (lttng_is_setuid_setgid()) {
236 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
237 "-e, --consumerd-err-sock");
239 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
246 if (lttng_is_setuid_setgid()) {
247 WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
250 tracing_group_name
= optarg
;
260 lttng_opt_verbose
= 1;
263 fprintf(stdout
, "%s\n", VERSION
);
266 opt_type
= LTTNG_CONSUMER_KERNEL
;
268 #ifdef HAVE_LIBLTTNG_UST_CTL
270 # if (CAA_BITS_PER_LONG == 64)
271 opt_type
= LTTNG_CONSUMER64_UST
;
272 # elif (CAA_BITS_PER_LONG == 32)
273 opt_type
= LTTNG_CONSUMER32_UST
;
275 # error "Unknown bitness"
290 * Set open files limit to unlimited. This daemon can open a large number of
291 * file descriptors in order to consumer multiple kernel traces.
293 static void set_ulimit(void)
298 /* The kernel does not allowed an infinite limit for open files */
299 lim
.rlim_cur
= 65535;
300 lim
.rlim_max
= 65535;
302 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
304 PERROR("failed to set open files limit");
311 int main(int argc
, char **argv
)
313 int ret
= 0, retval
= 0;
315 struct lttng_consumer_local_data
*tmp_ctx
;
317 if (set_signal_handler()) {
319 goto exit_set_signal_handler
;
322 /* Parse arguments */
324 if (parse_args(argc
, argv
)) {
335 * child: setsid, close FD 0, 1, 2, chdir /
336 * parent: exit (if fork is successful)
345 * We are in the child. Make sure all other file
346 * descriptors are closed, in case we are called with
347 * more opened file descriptors than the standard ones.
349 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
355 * Starting from here, we can create threads. This needs to be after
356 * lttng_daemonize due to RCU.
359 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
360 if (!health_consumerd
) {
362 goto exit_health_consumerd_cleanup
;
365 if (*command_sock_path
== '\0') {
367 case LTTNG_CONSUMER_KERNEL
:
368 ret
= snprintf(command_sock_path
, PATH_MAX
,
369 DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
370 DEFAULT_LTTNG_RUNDIR
);
376 case LTTNG_CONSUMER64_UST
:
377 ret
= snprintf(command_sock_path
, PATH_MAX
,
378 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
,
379 DEFAULT_LTTNG_RUNDIR
);
385 case LTTNG_CONSUMER32_UST
:
386 ret
= snprintf(command_sock_path
, PATH_MAX
,
387 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
,
388 DEFAULT_LTTNG_RUNDIR
);
395 ERR("Unknown consumerd type");
402 if (lttng_consumer_init()) {
407 /* Initialize communication library */
409 /* Initialize TCP timeout values */
413 /* Set limit for open files */
417 if (run_as_create_worker(argv
[0]) < 0) {
421 /* create the consumer instance with and assign the callbacks */
422 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
423 NULL
, lttng_consumer_on_recv_stream
, NULL
);
429 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
430 if (*error_sock_path
== '\0') {
432 case LTTNG_CONSUMER_KERNEL
:
433 ret
= snprintf(error_sock_path
, PATH_MAX
,
434 DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
435 DEFAULT_LTTNG_RUNDIR
);
441 case LTTNG_CONSUMER64_UST
:
442 ret
= snprintf(error_sock_path
, PATH_MAX
,
443 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
,
444 DEFAULT_LTTNG_RUNDIR
);
450 case LTTNG_CONSUMER32_UST
:
451 ret
= snprintf(error_sock_path
, PATH_MAX
,
452 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
,
453 DEFAULT_LTTNG_RUNDIR
);
460 ERR("Unknown consumerd type");
466 /* Connect to the socket created by lttng-sessiond to report errors */
467 DBG("Connecting to error socket %s", error_sock_path
);
468 ret
= lttcomm_connect_unix_sock(error_sock_path
);
470 * Not a fatal error, but all communication with lttng-sessiond will
474 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
476 lttng_consumer_set_error_sock(ctx
, ret
);
479 * Block RT signals used for UST periodical metadata flush and the live
480 * timer in main, and create a dedicated thread to handle these signals.
482 if (consumer_signal_init()) {
487 ctx
->type
= opt_type
;
489 if (utils_create_pipe(health_quit_pipe
)) {
491 goto exit_health_pipe
;
494 /* Create thread to manage the client socket */
495 ret
= pthread_create(&health_thread
, default_pthread_attr(),
496 thread_manage_health
, (void *) NULL
);
499 PERROR("pthread_create health");
501 goto exit_health_thread
;
505 * Wait for health thread to be initialized before letting the
506 * sessiond thread reply to the sessiond that we are ready.
508 while (uatomic_read(<tng_consumer_ready
)) {
511 cmm_smp_mb(); /* Read ready before following operations */
513 /* Create thread to manage channels */
514 ret
= pthread_create(&channel_thread
, default_pthread_attr(),
515 consumer_thread_channel_poll
,
519 PERROR("pthread_create");
521 goto exit_channel_thread
;
524 /* Create thread to manage the polling/writing of trace metadata */
525 ret
= pthread_create(&metadata_thread
, default_pthread_attr(),
526 consumer_thread_metadata_poll
,
530 PERROR("pthread_create");
532 goto exit_metadata_thread
;
535 /* Create thread to manage the polling/writing of trace data */
536 ret
= pthread_create(&data_thread
, default_pthread_attr(),
537 consumer_thread_data_poll
, (void *) ctx
);
540 PERROR("pthread_create");
542 goto exit_data_thread
;
545 /* Create the thread to manage the receive of fd */
546 ret
= pthread_create(&sessiond_thread
, default_pthread_attr(),
547 consumer_thread_sessiond_poll
,
551 PERROR("pthread_create");
553 goto exit_sessiond_thread
;
557 * Create the thread to manage the UST metadata periodic timer and
560 ret
= pthread_create(&metadata_timer_thread
, default_pthread_attr(),
561 consumer_timer_thread
, (void *) ctx
);
564 PERROR("pthread_create");
566 goto exit_metadata_timer_thread
;
569 ret
= pthread_detach(metadata_timer_thread
);
572 PERROR("pthread_detach");
574 goto exit_metadata_timer_detach
;
578 * This is where we start awaiting program completion (e.g. through
579 * signal that asks threads to teardown.
582 exit_metadata_timer_detach
:
583 exit_metadata_timer_thread
:
584 ret
= pthread_join(sessiond_thread
, &status
);
587 PERROR("pthread_join sessiond_thread");
590 exit_sessiond_thread
:
592 ret
= pthread_join(data_thread
, &status
);
595 PERROR("pthread_join data_thread");
600 ret
= pthread_join(metadata_thread
, &status
);
603 PERROR("pthread_join metadata_thread");
606 exit_metadata_thread
:
608 ret
= pthread_join(channel_thread
, &status
);
611 PERROR("pthread_join channel_thread");
616 ret
= pthread_join(health_thread
, &status
);
619 PERROR("pthread_join health_thread");
624 utils_close_pipe(health_quit_pipe
);
630 cmm_barrier(); /* Clear ctx for signal handler. */
631 lttng_consumer_destroy(tmp_ctx
);
632 lttng_consumer_cleanup();
634 if (health_consumerd
) {
635 health_app_destroy(health_consumerd
);
637 /* Ensure all prior call_rcu are done. */
640 run_as_destroy_worker();
642 exit_health_consumerd_cleanup
:
644 exit_set_signal_handler
: