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 /* TODO : support UST (all direct kernel-ctl accesses). */
58 /* threads (channel handling, poll, metadata, sessiond) */
60 static pthread_t channel_thread
, data_thread
, metadata_thread
,
61 sessiond_thread
, metadata_timer_thread
, health_thread
;
63 /* to count the number of times the user pressed ctrl+c */
64 static int sigintcount
= 0;
66 /* Argument variables */
67 int lttng_opt_quiet
; /* not static in error.h */
68 int lttng_opt_verbose
; /* not static in error.h */
69 int lttng_opt_mi
; /* not static in error.h */
71 static int opt_daemon
;
72 static const char *progname
;
73 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
74 static char error_sock_path
[PATH_MAX
]; /* Global error path */
75 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
77 /* the liblttngconsumerd context */
78 static struct lttng_consumer_local_data
*ctx
;
80 /* Consumerd health monitoring */
81 struct health_app
*health_consumerd
;
83 const char *tracing_group_name
= DEFAULT_TRACING_GROUP
;
85 int lttng_consumer_ready
= NR_LTTNG_CONSUMER_READY
;
87 enum lttng_consumer_type
lttng_consumer_get_type(void)
90 return LTTNG_CONSUMER_UNKNOWN
;
96 * Signal handler for the daemon
98 static void sighandler(int sig
)
100 if (sig
== SIGINT
&& sigintcount
++ == 0) {
101 DBG("ignoring first SIGINT");
106 * Ignore SIGPIPE because it should not stop the consumer whenever a
107 * SIGPIPE is catched through a FD operation.
109 if (sig
== SIGPIPE
) {
113 lttng_consumer_should_exit(ctx
);
117 * Setup signal handler for :
118 * SIGINT, SIGTERM, SIGPIPE
120 static int set_signal_handler(void)
126 if ((ret
= sigemptyset(&sigset
)) < 0) {
127 perror("sigemptyset");
131 sa
.sa_handler
= sighandler
;
134 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
139 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
144 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
153 * Usage function on stream file.
155 static void usage(FILE *fp
)
157 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
158 fprintf(fp
, " -h, --help "
159 "Display this usage.\n");
160 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
161 "Specify path for the command socket\n");
162 fprintf(fp
, " -e, --consumerd-err-sock PATH "
163 "Specify path for the error socket\n");
164 fprintf(fp
, " -d, --daemonize "
165 "Start as a daemon.\n");
166 fprintf(fp
, " -q, --quiet "
167 "No output at all.\n");
168 fprintf(fp
, " -v, --verbose "
169 "Verbose mode. Activate DBG() macro.\n");
170 fprintf(fp
, " -V, --version "
171 "Show version number.\n");
172 fprintf(fp
, " -g, --group NAME "
173 "Specify the tracing group name. (default: tracing)\n");
174 fprintf(fp
, " -k, --kernel "
175 "Consumer kernel buffers (default).\n");
176 fprintf(fp
, " -u, --ust "
177 "Consumer UST buffers.%s\n",
178 #ifdef HAVE_LIBLTTNG_UST_CTL
181 " (support not compiled in)"
187 * daemon argument parsing
189 static void parse_args(int argc
, char **argv
)
193 static struct option long_options
[] = {
194 { "consumerd-cmd-sock", 1, 0, 'c' },
195 { "consumerd-err-sock", 1, 0, 'e' },
196 { "daemonize", 0, 0, 'd' },
197 { "group", 1, 0, 'g' },
198 { "help", 0, 0, 'h' },
199 { "quiet", 0, 0, 'q' },
200 { "verbose", 0, 0, 'v' },
201 { "version", 0, 0, 'V' },
202 { "kernel", 0, 0, 'k' },
203 #ifdef HAVE_LIBLTTNG_UST_CTL
204 { "ust", 0, 0, 'u' },
210 int option_index
= 0;
211 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:g:", long_options
, &option_index
);
218 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
220 fprintf(stderr
, " with arg %s\n", optarg
);
224 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
227 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
233 tracing_group_name
= optarg
;
242 lttng_opt_verbose
= 1;
245 fprintf(stdout
, "%s\n", VERSION
);
248 opt_type
= LTTNG_CONSUMER_KERNEL
;
250 #ifdef HAVE_LIBLTTNG_UST_CTL
252 # if (CAA_BITS_PER_LONG == 64)
253 opt_type
= LTTNG_CONSUMER64_UST
;
254 # elif (CAA_BITS_PER_LONG == 32)
255 opt_type
= LTTNG_CONSUMER32_UST
;
257 # error "Unknown bitness"
269 * Set open files limit to unlimited. This daemon can open a large number of
270 * file descriptors in order to consumer multiple kernel traces.
272 static void set_ulimit(void)
277 /* The kernel does not allowed an infinite limit for open files */
278 lim
.rlim_cur
= 65535;
279 lim
.rlim_max
= 65535;
281 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
283 PERROR("failed to set open files limit");
290 int main(int argc
, char **argv
)
295 /* Parse arguments */
297 parse_args(argc
, argv
);
305 * child: setsid, close FD 0, 1, 2, chdir /
306 * parent: exit (if fork is successful)
314 * We are in the child. Make sure all other file
315 * descriptors are closed, in case we are called with
316 * more opened file descriptors than the standard ones.
318 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
323 /* Set up max poll set size */
324 lttng_poll_set_max_size();
326 if (*command_sock_path
== '\0') {
328 case LTTNG_CONSUMER_KERNEL
:
329 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
330 DEFAULT_LTTNG_RUNDIR
);
332 case LTTNG_CONSUMER64_UST
:
333 snprintf(command_sock_path
, PATH_MAX
,
334 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
336 case LTTNG_CONSUMER32_UST
:
337 snprintf(command_sock_path
, PATH_MAX
,
338 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
341 WARN("Unknown consumerd type");
347 if (lttng_consumer_init() < 0) {
351 /* Init socket timeouts */
356 /* Set limit for open files */
360 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
361 if (!health_consumerd
) {
365 /* create the consumer instance with and assign the callbacks */
366 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
367 NULL
, lttng_consumer_on_recv_stream
, NULL
);
372 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
373 if (*error_sock_path
== '\0') {
375 case LTTNG_CONSUMER_KERNEL
:
376 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
377 DEFAULT_LTTNG_RUNDIR
);
379 case LTTNG_CONSUMER64_UST
:
380 snprintf(error_sock_path
, PATH_MAX
,
381 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
383 case LTTNG_CONSUMER32_UST
:
384 snprintf(error_sock_path
, PATH_MAX
,
385 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
388 WARN("Unknown consumerd type");
393 if (set_signal_handler() < 0) {
397 /* Connect to the socket created by lttng-sessiond to report errors */
398 DBG("Connecting to error socket %s", error_sock_path
);
399 ret
= lttcomm_connect_unix_sock(error_sock_path
);
400 /* not a fatal error, but all communication with lttng-sessiond will fail */
402 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
404 lttng_consumer_set_error_sock(ctx
, ret
);
407 * Block RT signals used for UST periodical metadata flush and the live
408 * timer in main, and create a dedicated thread to handle these signals.
410 consumer_signal_init();
412 ctx
->type
= opt_type
;
414 ret
= utils_create_pipe(health_quit_pipe
);
416 goto error_health_pipe
;
419 /* Create thread to manage the client socket */
420 ret
= pthread_create(&health_thread
, NULL
,
421 thread_manage_health
, (void *) NULL
);
423 PERROR("pthread_create health");
428 * Wait for health thread to be initialized before letting the
429 * sessiond thread reply to the sessiond that we are ready.
431 while (uatomic_read(<tng_consumer_ready
)) {
434 cmm_smp_mb(); /* Read ready before following operations */
436 /* Create thread to manage channels */
437 ret
= pthread_create(&channel_thread
, NULL
, consumer_thread_channel_poll
,
440 perror("pthread_create");
444 /* Create thread to manage the polling/writing of trace metadata */
445 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
448 perror("pthread_create");
452 /* Create thread to manage the polling/writing of trace data */
453 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
456 perror("pthread_create");
460 /* Create the thread to manage the receive of fd */
461 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
464 perror("pthread_create");
469 * Create the thread to manage the UST metadata periodic timer and
472 ret
= pthread_create(&metadata_timer_thread
, NULL
,
473 consumer_timer_thread
, (void *) ctx
);
475 perror("pthread_create");
476 goto metadata_timer_error
;
479 ret
= pthread_detach(metadata_timer_thread
);
482 perror("pthread_detach");
485 metadata_timer_error
:
486 ret
= pthread_join(sessiond_thread
, &status
);
488 perror("pthread_join");
493 ret
= pthread_join(data_thread
, &status
);
495 perror("pthread_join");
500 ret
= pthread_join(metadata_thread
, &status
);
502 perror("pthread_join");
507 ret
= pthread_join(channel_thread
, &status
);
509 perror("pthread_join");
514 ret
= pthread_join(health_thread
, &status
);
516 PERROR("pthread_join health thread");
517 goto error
; /* join error, exit without cleanup */
521 utils_close_pipe(health_quit_pipe
);
533 lttng_consumer_destroy(ctx
);
534 lttng_consumer_cleanup();
535 if (health_consumerd
) {
536 health_app_destroy(health_consumerd
);