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
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; only version 2
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 #include <sys/socket.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
42 #include <lttng-consumerd.h>
43 #include <lttng-kernel-ctl.h>
44 #include <lttng-sessiond-comm.h>
45 #include <lttng/lttng-kconsumer.h>
46 #include <lttng/lttng-ustconsumer.h>
49 /* TODO : support UST (all direct kernctl accesses). */
51 /* the two threads (receive fd and poll) */
52 static pthread_t threads
[2];
54 /* to count the number of time the user pressed ctrl+c */
55 static int sigintcount
= 0;
57 /* Argument variables */
60 static int opt_daemon
;
61 static const char *progname
;
62 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
63 static char error_sock_path
[PATH_MAX
]; /* Global error path */
64 static enum lttng_consumer_type opt_type
= LTTNG_CONSUMER_KERNEL
;
66 /* the liblttngkconsumerd context */
67 static struct lttng_consumer_local_data
*ctx
;
70 * Signal handler for the daemon
72 static void sighandler(int sig
)
74 if (sig
== SIGINT
&& sigintcount
++ == 0) {
75 DBG("ignoring first SIGINT");
79 lttng_consumer_should_exit(ctx
);
83 * Setup signal handler for :
84 * SIGINT, SIGTERM, SIGPIPE
86 static int set_signal_handler(void)
92 if ((ret
= sigemptyset(&sigset
)) < 0) {
93 perror("sigemptyset");
97 sa
.sa_handler
= sighandler
;
100 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
105 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
110 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
119 * usage function on stderr
121 static void usage(void)
123 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
124 fprintf(stderr
, " -h, --help "
125 "Display this usage.\n");
126 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
127 "Specify path for the command socket\n");
128 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
129 "Specify path for the error socket\n");
130 fprintf(stderr
, " -d, --daemonize "
131 "Start as a daemon.\n");
132 fprintf(stderr
, " -q, --quiet "
133 "No output at all.\n");
134 fprintf(stderr
, " -v, --verbose "
135 "Verbose mode. Activate DBG() macro.\n");
136 fprintf(stderr
, " -V, --version "
137 "Show version number.\n");
138 fprintf(stderr
, " -k, --kernel "
139 "Consumer kernel buffers (default).\n");
140 fprintf(stderr
, " -u, --ust "
141 "Consumer UST buffers.%s\n",
142 #ifdef HAVE_LIBLTTNG_UST_CTL
145 " (support not compiled in)"
151 * daemon argument parsing
153 static void parse_args(int argc
, char **argv
)
157 static struct option long_options
[] = {
158 { "kconsumerd-cmd-sock", 1, 0, 'c' },
159 { "kconsumerd-err-sock", 1, 0, 'e' },
160 { "daemonize", 0, 0, 'd' },
161 { "help", 0, 0, 'h' },
162 { "quiet", 0, 0, 'q' },
163 { "verbose", 0, 0, 'v' },
164 { "version", 0, 0, 'V' },
165 { "kernel", 0, 0, 'k' },
166 #ifdef HAVE_LIBLTTNG_UST_CTL
167 { "ust", 0, 0, 'u' },
173 int option_index
= 0;
174 c
= getopt_long(argc
, argv
, "dhqvVku" "c:e:", long_options
, &option_index
);
181 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
183 fprintf(stderr
, " with arg %s\n", optarg
);
187 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
190 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
205 fprintf(stdout
, "%s\n", VERSION
);
208 opt_type
= LTTNG_CONSUMER_KERNEL
;
210 #ifdef HAVE_LIBLTTNG_UST_CTL
212 opt_type
= LTTNG_CONSUMER_UST
;
223 * Consume data on a file descriptor and write it on a trace file.
225 static int read_subbuffer(struct lttng_consumer_stream
*stream
)
230 int infd
= stream
->wait_fd
;
232 DBG("In read_subbuffer (infd : %d)", infd
);
233 /* Get the next subbuffer */
234 err
= kernctl_get_next_subbuf(infd
);
238 * This is a debug message even for single-threaded consumer,
239 * because poll() have more relaxed criterions than get subbuf,
240 * so get_subbuf may fail for short race windows where poll()
241 * would issue wakeups.
243 DBG("Reserving sub buffer failed (everything is normal, "
244 "it is due to concurrency)");
248 switch (stream
->output
) {
249 case LTTNG_EVENT_SPLICE
:
250 /* read the whole subbuffer */
251 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
254 perror("Getting sub-buffer len failed.");
258 /* splice the subbuffer to the tracefile */
259 ret
= lttng_consumer_on_read_subbuffer_splice(ctx
, stream
, len
);
262 * display the error but continue processing to try
263 * to release the subbuffer
265 ERR("Error splicing to tracefile");
268 case LTTNG_EVENT_MMAP
:
269 /* read the used subbuffer size */
270 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
273 perror("Getting sub-buffer len failed.");
276 /* write the subbuffer to the tracefile */
277 ret
= lttng_consumer_on_read_subbuffer_mmap(ctx
, stream
, len
);
280 * display the error but continue processing to try
281 * to release the subbuffer
283 ERR("Error writing to tracefile");
287 ERR("Unknown output method");
291 err
= kernctl_put_next_subbuf(infd
);
294 if (errno
== EFAULT
) {
295 perror("Error in unreserving sub buffer\n");
296 } else if (errno
== EIO
) {
297 /* Should never happen with newer LTTng versions */
298 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
307 static int on_recv_stream(struct lttng_consumer_stream
*stream
)
311 /* Opening the tracefile in write mode */
312 if (stream
->path_name
!= NULL
) {
313 ret
= open(stream
->path_name
,
314 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
316 ERR("Opening %s", stream
->path_name
);
320 stream
->out_fd
= ret
;
323 if (stream
->output
== LTTNG_EVENT_MMAP
) {
324 /* get the len of the mmap region */
325 unsigned long mmap_len
;
327 ret
= kernctl_get_mmap_len(stream
->wait_fd
, &mmap_len
);
330 perror("kernctl_get_mmap_len");
333 stream
->mmap_len
= (size_t) mmap_len
;
335 stream
->mmap_base
= mmap(NULL
, stream
->mmap_len
,
336 PROT_READ
, MAP_PRIVATE
, stream
->wait_fd
, 0);
337 if (stream
->mmap_base
== MAP_FAILED
) {
338 perror("Error mmaping");
344 /* we return 0 to let the library handle the FD internally */
351 err
= close(stream
->out_fd
);
361 int main(int argc
, char **argv
)
367 /* Parse arguments */
369 parse_args(argc
, argv
);
380 if (strlen(command_sock_path
) == 0) {
381 snprintf(command_sock_path
, PATH_MAX
,
382 opt_type
== LTTNG_CONSUMER_KERNEL
?
383 KCONSUMERD_CMD_SOCK_PATH
:
384 USTCONSUMERD_CMD_SOCK_PATH
);
386 /* create the consumer instance with and assign the callbacks */
387 ctx
= lttng_consumer_create(opt_type
, read_subbuffer
, NULL
, on_recv_stream
, NULL
);
392 lttng_consumer_set_command_sock_path(ctx
, command_sock_path
);
393 if (strlen(error_sock_path
) == 0) {
394 snprintf(error_sock_path
, PATH_MAX
,
395 opt_type
== LTTNG_CONSUMER_KERNEL
?
396 KCONSUMERD_ERR_SOCK_PATH
:
397 USTCONSUMERD_ERR_SOCK_PATH
);
400 if (set_signal_handler() < 0) {
404 /* Connect to the socket created by lttng-sessiond to report errors */
405 DBG("Connecting to error socket %s", error_sock_path
);
406 ret
= lttcomm_connect_unix_sock(error_sock_path
);
407 /* not a fatal error, but all communication with lttng-sessiond will fail */
409 WARN("Cannot connect to error socket, is lttng-sessiond started ?");
411 lttng_consumer_set_error_sock(ctx
, ret
);
413 /* Create the thread to manage the receive of fd */
414 ret
= pthread_create(&threads
[0], NULL
, lttng_consumer_thread_receive_fds
,
417 perror("pthread_create");
421 /* Create thread to manage the polling/writing of traces */
422 ret
= pthread_create(&threads
[1], NULL
, lttng_consumer_thread_poll_fds
,
425 perror("pthread_create");
429 for (i
= 0; i
< 2; i
++) {
430 ret
= pthread_join(threads
[i
], &status
);
432 perror("pthread_join");
437 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_SUCCESS
);
442 lttng_consumer_send_error(ctx
, CONSUMERD_EXIT_FAILURE
);
445 lttng_consumer_destroy(ctx
);
446 lttng_consumer_cleanup();