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>
41 #include <lttng/lttng-kconsumerd.h>
44 #include "kernelctl.h"
45 #include "ltt-kconsumerd.h"
46 #include "lttng-sessiond-comm.h"
48 /* the two threads (receive fd and poll) */
49 static pthread_t threads
[2];
51 /* to count the number of time the user pressed ctrl+c */
52 static int sigintcount
= 0;
54 /* Argument variables */
57 static int opt_daemon
;
58 static const char *progname
;
59 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
60 static char error_sock_path
[PATH_MAX
]; /* Global error path */
62 /* the liblttngkconsumerd context */
63 static struct lttng_kconsumerd_local_data
*ctx
;
66 * Signal handler for the daemon
68 static void sighandler(int sig
)
70 if (sig
== SIGINT
&& sigintcount
++ == 0) {
71 DBG("ignoring first SIGINT");
75 lttng_kconsumerd_should_exit(ctx
);
79 * Setup signal handler for :
80 * SIGINT, SIGTERM, SIGPIPE
82 static int set_signal_handler(void)
88 if ((ret
= sigemptyset(&sigset
)) < 0) {
89 perror("sigemptyset");
93 sa
.sa_handler
= sighandler
;
96 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
101 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
106 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
115 * usage function on stderr
117 static void usage(void)
119 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
120 fprintf(stderr
, " -h, --help "
121 "Display this usage.\n");
122 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
123 "Specify path for the command socket\n");
124 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
125 "Specify path for the error socket\n");
126 fprintf(stderr
, " -d, --daemonize "
127 "Start as a daemon.\n");
128 fprintf(stderr
, " -q, --quiet "
129 "No output at all.\n");
130 fprintf(stderr
, " -v, --verbose "
131 "Verbose mode. Activate DBG() macro.\n");
132 fprintf(stderr
, " -V, --version "
133 "Show version number.\n");
137 * daemon argument parsing
139 static void parse_args(int argc
, char **argv
)
143 static struct option long_options
[] = {
144 { "kconsumerd-cmd-sock", 1, 0, 'c' },
145 { "kconsumerd-err-sock", 1, 0, 'e' },
146 { "daemonize", 0, 0, 'd' },
147 { "help", 0, 0, 'h' },
148 { "quiet", 0, 0, 'q' },
149 { "verbose", 0, 0, 'v' },
150 { "version", 0, 0, 'V' },
155 int option_index
= 0;
156 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
163 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
165 fprintf(stderr
, " with arg %s\n", optarg
);
169 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
172 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
187 fprintf(stdout
, "%s\n", VERSION
);
197 * Consume data on a file descriptor and write it on a trace file.
199 static int read_subbuffer(struct lttng_kconsumerd_fd
*kconsumerd_fd
)
204 int infd
= kconsumerd_fd
->consumerd_fd
;
206 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd
);
207 /* Get the next subbuffer */
208 err
= kernctl_get_next_subbuf(infd
);
212 * This is a debug message even for single-threaded consumer,
213 * because poll() have more relaxed criterions than get subbuf,
214 * so get_subbuf may fail for short race windows where poll()
215 * would issue wakeups.
217 DBG("Reserving sub buffer failed (everything is normal, "
218 "it is due to concurrency)");
222 switch (kconsumerd_fd
->output
) {
223 case LTTNG_EVENT_SPLICE
:
224 /* read the whole subbuffer */
225 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
228 perror("Getting sub-buffer len failed.");
232 /* splice the subbuffer to the tracefile */
233 ret
= lttng_kconsumerd_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
236 * display the error but continue processing to try
237 * to release the subbuffer
239 ERR("Error splicing to tracefile");
242 case LTTNG_EVENT_MMAP
:
243 /* read the used subbuffer size */
244 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
247 perror("Getting sub-buffer len failed.");
250 /* write the subbuffer to the tracefile */
251 ret
= lttng_kconsumerd_on_read_subbuffer_mmap(ctx
, kconsumerd_fd
, len
);
254 * display the error but continue processing to try
255 * to release the subbuffer
257 ERR("Error writing to tracefile");
261 ERR("Unknown output method");
265 err
= kernctl_put_next_subbuf(infd
);
268 if (errno
== EFAULT
) {
269 perror("Error in unreserving sub buffer\n");
270 } else if (errno
== EIO
) {
271 /* Should never happen with newer LTTng versions */
272 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
281 static int on_recv_fd(struct lttng_kconsumerd_fd
*kconsumerd_fd
)
285 /* Opening the tracefile in write mode */
286 if (kconsumerd_fd
->path_name
!= NULL
) {
287 ret
= open(kconsumerd_fd
->path_name
,
288 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
290 ERR("Opening %s", kconsumerd_fd
->path_name
);
294 kconsumerd_fd
->out_fd
= ret
;
297 if (kconsumerd_fd
->output
== LTTNG_EVENT_MMAP
) {
298 /* get the len of the mmap region */
299 ret
= kernctl_get_mmap_len(kconsumerd_fd
->consumerd_fd
, &kconsumerd_fd
->mmap_len
);
302 perror("kernctl_get_mmap_len");
306 kconsumerd_fd
->mmap_base
= mmap(NULL
, kconsumerd_fd
->mmap_len
,
307 PROT_READ
, MAP_PRIVATE
, kconsumerd_fd
->consumerd_fd
, 0);
308 if (kconsumerd_fd
->mmap_base
== MAP_FAILED
) {
309 perror("Error mmaping");
315 /* we return 0 to let the library handle the FD internally */
322 err
= close(kconsumerd_fd
->out_fd
);
332 int main(int argc
, char **argv
)
338 /* Parse arguments */
340 parse_args(argc
, argv
);
351 if (strlen(command_sock_path
) == 0) {
352 snprintf(command_sock_path
, PATH_MAX
,
353 KCONSUMERD_CMD_SOCK_PATH
);
355 /* create the consumer instance with and assign the callbacks */
356 ctx
= lttng_kconsumerd_create(read_subbuffer
, on_recv_fd
, NULL
);
361 lttng_kconsumerd_set_command_sock_path(ctx
, command_sock_path
);
362 if (strlen(error_sock_path
) == 0) {
363 snprintf(error_sock_path
, PATH_MAX
,
364 KCONSUMERD_ERR_SOCK_PATH
);
367 if (set_signal_handler() < 0) {
371 /* Connect to the socket created by ltt-sessiond to report errors */
372 DBG("Connecting to error socket %s", error_sock_path
);
373 ret
= lttcomm_connect_unix_sock(error_sock_path
);
374 /* not a fatal error, but all communication with ltt-sessiond will fail */
376 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
378 lttng_kconsumerd_set_error_sock(ctx
, ret
);
380 /* Create the thread to manage the receive of fd */
381 ret
= pthread_create(&threads
[0], NULL
, lttng_kconsumerd_thread_receive_fds
,
384 perror("pthread_create");
388 /* Create thread to manage the polling/writing of traces */
389 ret
= pthread_create(&threads
[1], NULL
, lttng_kconsumerd_thread_poll_fds
,
392 perror("pthread_create");
396 for (i
= 0; i
< 2; i
++) {
397 ret
= pthread_join(threads
[i
], &status
);
399 perror("pthread_join");
404 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_SUCCESS
);
409 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_FAILURE
);
412 lttng_kconsumerd_destroy(ctx
);
413 lttng_kconsumerd_cleanup();