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>
41 #include <urcu/compiler.h>
44 #include <common/defaults.h>
45 #include <common/common.h>
46 #include <common/consumer.h>
47 #include <common/consumer-timer.h>
48 #include <common/compat/poll.h>
49 #include <common/sessiond-comm/sessiond-comm.h>
51 #include "lttng-consumerd.h"
52 #include "health-consumerd.h"
54 /* TODO : support UST (all direct kernel-ctl accesses). */
56 /* threads (channel handling, poll, metadata, sessiond) */
58 static pthread_t channel_thread
, data_thread
, metadata_thread
, sessiond_thread
;
59 static pthread_t metadata_timer_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 static int opt_daemon
;
68 static const char *progname
;
69 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
70 static char error_sock_path
[PATH_MAX
]; /* Global error path */
71 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
73 /* the liblttngconsumerd context */
74 static struct lttng_consumer_local_data
*ctx
;
76 /* Consumerd health monitoring */
77 struct health_app
*health_consumerd
;
80 * Signal handler for the daemon
82 static void sighandler(int sig
)
84 if (sig
== SIGINT
&& sigintcount
++ == 0) {
85 DBG("ignoring first SIGINT");
90 * Ignore SIGPIPE because it should not stop the consumer whenever a
91 * SIGPIPE is catched through a FD operation.
97 lttng_consumer_should_exit(ctx
);
101 * Setup signal handler for :
102 * SIGINT, SIGTERM, SIGPIPE
104 static int set_signal_handler(void)
110 if ((ret
= sigemptyset(&sigset
)) < 0) {
111 perror("sigemptyset");
115 sa
.sa_handler
= sighandler
;
118 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
123 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
128 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
137 * Usage function on stream file.
139 static void usage(FILE *fp
)
141 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
142 fprintf(fp
, " -h, --help "
143 "Display this usage.\n");
144 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
145 "Specify path for the command socket\n");
146 fprintf(fp
, " -e, --consumerd-err-sock PATH "
147 "Specify path for the error socket\n");
148 fprintf(fp
, " -d, --daemonize "
149 "Start as a daemon.\n");
150 fprintf(fp
, " -q, --quiet "
151 "No output at all.\n");
152 fprintf(fp
, " -v, --verbose "
153 "Verbose mode. Activate DBG() macro.\n");
154 fprintf(fp
, " -V, --version "
155 "Show version number.\n");
156 fprintf(fp
, " -k, --kernel "
157 "Consumer kernel buffers (default).\n");
158 fprintf(fp
, " -u, --ust "
159 "Consumer UST buffers.%s\n",
160 #ifdef HAVE_LIBLTTNG_UST_CTL
163 " (support not compiled in)"
169 * daemon argument parsing
171 static void parse_args(int argc
, char **argv
)
175 static struct option long_options
[] = {
176 { "consumerd-cmd-sock", 1, 0, 'c' },
177 { "consumerd-err-sock", 1, 0, 'e' },
178 { "daemonize", 0, 0, 'd' },
179 { "help", 0, 0, 'h' },
180 { "quiet", 0, 0, 'q' },
181 { "verbose", 0, 0, 'v' },
182 { "version", 0, 0, 'V' },
183 { "kernel", 0, 0, 'k' },
184 #ifdef HAVE_LIBLTTNG_UST_CTL
185 { "ust", 0, 0, 'u' },
191 int option_index
= 0;
192 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
199 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
201 fprintf(stderr
, " with arg %s\n", optarg
);
205 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
208 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
220 lttng_opt_verbose
= 1;
223 fprintf(stdout
, "%s\n", VERSION
);
226 opt_type
= LTTNG_CONSUMER_KERNEL
;
228 #ifdef HAVE_LIBLTTNG_UST_CTL
230 # if (CAA_BITS_PER_LONG == 64)
231 opt_type
= LTTNG_CONSUMER64_UST
;
232 # elif (CAA_BITS_PER_LONG == 32)
233 opt_type
= LTTNG_CONSUMER32_UST
;
235 # error "Unknown bitness"
247 * Set open files limit to unlimited. This daemon can open a large number of
248 * file descriptors in order to consumer multiple kernel traces.
250 static void set_ulimit(void)
255 /* The kernel does not allowed an infinite limit for open files */
256 lim
.rlim_cur
= 65535;
257 lim
.rlim_max
= 65535;
259 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
261 PERROR("failed to set open files limit");
268 int main(int argc
, char **argv
)
273 /* Parse arguments */
275 parse_args(argc
, argv
);
283 * child: setsid, close FD 0, 1, 2, chdir /
284 * parent: exit (if fork is successful)
292 * We are in the child. Make sure all other file
293 * descriptors are closed, in case we are called with
294 * more opened file descriptors than the standard ones.
296 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
301 /* Set up max poll set size */
302 lttng_poll_set_max_size();
304 if (*command_sock_path
== '\0') {
306 case LTTNG_CONSUMER_KERNEL
:
307 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
308 DEFAULT_LTTNG_RUNDIR
);
310 case LTTNG_CONSUMER64_UST
:
311 snprintf(command_sock_path
, PATH_MAX
,
312 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
314 case LTTNG_CONSUMER32_UST
:
315 snprintf(command_sock_path
, PATH_MAX
,
316 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
319 WARN("Unknown consumerd type");
325 lttng_consumer_init();
328 /* Set limit for open files */
332 health_consumerd
= health_app_create(NR_HEALTH_CONSUMERD_TYPES
);
333 if (!health_consumerd
) {
337 /* create the consumer instance with and assign the callbacks */
338 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
339 NULL
, lttng_consumer_on_recv_stream
, NULL
);
344 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
345 if (*error_sock_path
== '\0') {
347 case LTTNG_CONSUMER_KERNEL
:
348 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
349 DEFAULT_LTTNG_RUNDIR
);
351 case LTTNG_CONSUMER64_UST
:
352 snprintf(error_sock_path
, PATH_MAX
,
353 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
355 case LTTNG_CONSUMER32_UST
:
356 snprintf(error_sock_path
, PATH_MAX
,
357 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
360 WARN("Unknown consumerd type");
365 if (set_signal_handler() < 0) {
369 /* Connect to the socket created by lttng-sessiond to report errors */
370 DBG("Connecting to error socket %s", error_sock_path
);
371 ret
= lttcomm_connect_unix_sock(error_sock_path
);
372 /* not a fatal error, but all communication with lttng-sessiond will fail */
374 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
376 lttng_consumer_set_error_sock(ctx
, ret
);
379 * Block RT signals used for UST periodical metadata flush and the live
380 * timer in main, and create a dedicated thread to handle these signals.
382 consumer_signal_init();
384 ctx
->type
= opt_type
;
386 /* Initialize communication library */
389 /* Create thread to manage channels */
390 ret
= pthread_create(&channel_thread
, NULL
, consumer_thread_channel_poll
,
393 perror("pthread_create");
397 /* Create thread to manage the polling/writing of trace metadata */
398 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
401 perror("pthread_create");
405 /* Create thread to manage the polling/writing of trace data */
406 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
409 perror("pthread_create");
413 /* Create the thread to manage the receive of fd */
414 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
417 perror("pthread_create");
422 * Create the thread to manage the UST metadata periodic timer and
425 ret
= pthread_create(&metadata_timer_thread
, NULL
,
426 consumer_timer_thread
, (void *) ctx
);
428 perror("pthread_create");
429 goto metadata_timer_error
;
432 ret
= pthread_detach(metadata_timer_thread
);
435 perror("pthread_detach");
438 metadata_timer_error
:
439 ret
= pthread_join(sessiond_thread
, &status
);
441 perror("pthread_join");
446 ret
= pthread_join(data_thread
, &status
);
448 perror("pthread_join");
453 ret
= pthread_join(metadata_thread
, &status
);
455 perror("pthread_join");
460 ret
= pthread_join(channel_thread
, &status
);
462 perror("pthread_join");
468 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_SUCCESS
);
475 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_FAILURE
);
479 lttng_consumer_destroy(ctx
);
480 lttng_consumer_cleanup();
481 if (health_consumerd
) {
482 health_app_destroy(health_consumerd
);