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"
53 /* TODO : support UST (all direct kernel-ctl accesses). */
55 /* threads (channel handling, poll, metadata, sessiond) */
57 static pthread_t channel_thread
, data_thread
, metadata_thread
, sessiond_thread
;
58 static pthread_t metadata_timer_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 static int opt_daemon
;
67 static const char *progname
;
68 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
69 static char error_sock_path
[PATH_MAX
]; /* Global error path */
70 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
72 /* the liblttngconsumerd context */
73 static struct lttng_consumer_local_data
*ctx
;
76 * Signal handler for the daemon
78 static void sighandler(int sig
)
80 if (sig
== SIGINT
&& sigintcount
++ == 0) {
81 DBG("ignoring first SIGINT");
86 * Ignore SIGPIPE because it should not stop the consumer whenever a
87 * SIGPIPE is catched through a FD operation.
93 lttng_consumer_should_exit(ctx
);
97 * Setup signal handler for :
98 * SIGINT, SIGTERM, SIGPIPE
100 static int set_signal_handler(void)
106 if ((ret
= sigemptyset(&sigset
)) < 0) {
107 perror("sigemptyset");
111 sa
.sa_handler
= sighandler
;
114 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
119 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
124 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
133 * Usage function on stream file.
135 static void usage(FILE *fp
)
137 fprintf(fp
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
138 fprintf(fp
, " -h, --help "
139 "Display this usage.\n");
140 fprintf(fp
, " -c, --consumerd-cmd-sock PATH "
141 "Specify path for the command socket\n");
142 fprintf(fp
, " -e, --consumerd-err-sock PATH "
143 "Specify path for the error socket\n");
144 fprintf(fp
, " -d, --daemonize "
145 "Start as a daemon.\n");
146 fprintf(fp
, " -q, --quiet "
147 "No output at all.\n");
148 fprintf(fp
, " -v, --verbose "
149 "Verbose mode. Activate DBG() macro.\n");
150 fprintf(fp
, " -V, --version "
151 "Show version number.\n");
152 fprintf(fp
, " -k, --kernel "
153 "Consumer kernel buffers (default).\n");
154 fprintf(fp
, " -u, --ust "
155 "Consumer UST buffers.%s\n",
156 #ifdef HAVE_LIBLTTNG_UST_CTL
159 " (support not compiled in)"
165 * daemon argument parsing
167 static void parse_args(int argc
, char **argv
)
171 static struct option long_options
[] = {
172 { "consumerd-cmd-sock", 1, 0, 'c' },
173 { "consumerd-err-sock", 1, 0, 'e' },
174 { "daemonize", 0, 0, 'd' },
175 { "help", 0, 0, 'h' },
176 { "quiet", 0, 0, 'q' },
177 { "verbose", 0, 0, 'v' },
178 { "version", 0, 0, 'V' },
179 { "kernel", 0, 0, 'k' },
180 #ifdef HAVE_LIBLTTNG_UST_CTL
181 { "ust", 0, 0, 'u' },
187 int option_index
= 0;
188 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
195 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
197 fprintf(stderr
, " with arg %s\n", optarg
);
201 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
204 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
216 lttng_opt_verbose
= 1;
219 fprintf(stdout
, "%s\n", VERSION
);
222 opt_type
= LTTNG_CONSUMER_KERNEL
;
224 #ifdef HAVE_LIBLTTNG_UST_CTL
226 # if (CAA_BITS_PER_LONG == 64)
227 opt_type
= LTTNG_CONSUMER64_UST
;
228 # elif (CAA_BITS_PER_LONG == 32)
229 opt_type
= LTTNG_CONSUMER32_UST
;
231 # error "Unknown bitness"
243 * Set open files limit to unlimited. This daemon can open a large number of
244 * file descriptors in order to consumer multiple kernel traces.
246 static void set_ulimit(void)
251 /* The kernel does not allowed an infinite limit for open files */
252 lim
.rlim_cur
= 65535;
253 lim
.rlim_max
= 65535;
255 ret
= setrlimit(RLIMIT_NOFILE
, &lim
);
257 PERROR("failed to set open files limit");
264 int main(int argc
, char **argv
)
269 /* Parse arguments */
271 parse_args(argc
, argv
);
279 * child: setsid, close FD 0, 1, 2, chdir /
280 * parent: exit (if fork is successful)
288 * We are in the child. Make sure all other file
289 * descriptors are closed, in case we are called with
290 * more opened file descriptors than the standard ones.
292 for (i
= 3; i
< sysconf(_SC_OPEN_MAX
); i
++) {
297 /* Set up max poll set size */
298 lttng_poll_set_max_size();
300 if (*command_sock_path
== '\0') {
302 case LTTNG_CONSUMER_KERNEL
:
303 snprintf(command_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_CMD_SOCK_PATH
,
304 DEFAULT_LTTNG_RUNDIR
);
306 case LTTNG_CONSUMER64_UST
:
307 snprintf(command_sock_path
, PATH_MAX
,
308 DEFAULT_USTCONSUMERD64_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
310 case LTTNG_CONSUMER32_UST
:
311 snprintf(command_sock_path
, PATH_MAX
,
312 DEFAULT_USTCONSUMERD32_CMD_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
315 WARN("Unknown consumerd type");
321 lttng_consumer_init();
324 /* Set limit for open files */
328 /* create the consumer instance with and assign the callbacks */
329 ctx
= lttng_consumer_create(opt_type
, lttng_consumer_read_subbuffer
,
330 NULL
, lttng_consumer_on_recv_stream
, NULL
);
335 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
336 if (*error_sock_path
== '\0') {
338 case LTTNG_CONSUMER_KERNEL
:
339 snprintf(error_sock_path
, PATH_MAX
, DEFAULT_KCONSUMERD_ERR_SOCK_PATH
,
340 DEFAULT_LTTNG_RUNDIR
);
342 case LTTNG_CONSUMER64_UST
:
343 snprintf(error_sock_path
, PATH_MAX
,
344 DEFAULT_USTCONSUMERD64_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
346 case LTTNG_CONSUMER32_UST
:
347 snprintf(error_sock_path
, PATH_MAX
,
348 DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH
, DEFAULT_LTTNG_RUNDIR
);
351 WARN("Unknown consumerd type");
356 if (set_signal_handler() < 0) {
360 /* Connect to the socket created by lttng-sessiond to report errors */
361 DBG("Connecting to error socket %s", error_sock_path
);
362 ret
= lttcomm_connect_unix_sock(error_sock_path
);
363 /* not a fatal error, but all communication with lttng-sessiond will fail */
365 WARN("Cannot connect to error socket (is lttng-sessiond started?)");
367 lttng_consumer_set_error_sock(ctx
, ret
);
370 * For UST consumer, we block RT signals used for periodical metadata flush
371 * in main and create a dedicated thread to handle these signals.
374 case LTTNG_CONSUMER32_UST
:
375 case LTTNG_CONSUMER64_UST
:
376 consumer_signal_init();
381 ctx
->type
= opt_type
;
383 /* Create thread to manage channels */
384 ret
= pthread_create(&channel_thread
, NULL
, consumer_thread_channel_poll
,
387 perror("pthread_create");
391 /* Create thread to manage the polling/writing of trace metadata */
392 ret
= pthread_create(&metadata_thread
, NULL
, consumer_thread_metadata_poll
,
395 perror("pthread_create");
399 /* Create thread to manage the polling/writing of trace data */
400 ret
= pthread_create(&data_thread
, NULL
, consumer_thread_data_poll
,
403 perror("pthread_create");
407 /* Create the thread to manage the receive of fd */
408 ret
= pthread_create(&sessiond_thread
, NULL
, consumer_thread_sessiond_poll
,
411 perror("pthread_create");
416 case LTTNG_CONSUMER32_UST
:
417 case LTTNG_CONSUMER64_UST
:
418 /* Create the thread to manage the metadata periodic timers */
419 ret
= pthread_create(&metadata_timer_thread
, NULL
,
420 consumer_timer_metadata_thread
, (void *) ctx
);
422 perror("pthread_create");
423 goto metadata_timer_error
;
426 ret
= pthread_detach(metadata_timer_thread
);
429 perror("pthread_detach");
436 metadata_timer_error
:
437 ret
= pthread_join(sessiond_thread
, &status
);
439 perror("pthread_join");
444 ret
= pthread_join(data_thread
, &status
);
446 perror("pthread_join");
451 ret
= pthread_join(metadata_thread
, &status
);
453 perror("pthread_join");
458 ret
= pthread_join(channel_thread
, &status
);
460 perror("pthread_join");
466 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_SUCCESS
);
473 lttng_consumer_send_error(ctx
, LTTCOMM_CONSUMERD_EXIT_FAILURE
);
477 lttng_consumer_destroy(ctx
);
478 lttng_consumer_cleanup();