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 "kernelctl.h"
42 #include "lttkconsumerd.h"
44 /* the two threads (receive fd and poll) */
47 /* to count the number of time the user pressed ctrl+c */
48 static int sigintcount
= 0;
50 /* Argument variables */
53 static int opt_daemon
;
54 static const char *progname
;
55 char command_sock_path
[PATH_MAX
]; /* Global command socket path */
56 char error_sock_path
[PATH_MAX
]; /* Global error path */
58 /* the liblttkconsumerd context */
59 struct kconsumerd_local_data
*ctx
;
64 * Signal handler for the daemon
66 static void sighandler(int sig
)
68 if (sig
== SIGINT
&& sigintcount
++ == 0) {
69 DBG("ignoring first SIGINT");
73 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
);
199 * Consume data on a file descriptor and write it on a trace file
201 static int read_subbuffer(struct kconsumerd_fd
*kconsumerd_fd
)
206 int infd
= kconsumerd_fd
->consumerd_fd
;
208 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd
);
209 /* Get the next subbuffer */
210 err
= kernctl_get_next_subbuf(infd
);
213 perror("Reserving sub buffer failed (everything is normal, "
214 "it is due to concurrency)");
218 switch (DEFAULT_KERNEL_CHANNEL_OUTPUT
) {
219 case LTTNG_EVENT_SPLICE
:
220 /* read the whole subbuffer */
221 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
224 perror("Getting sub-buffer len failed.");
228 /* splice the subbuffer to the tracefile */
229 ret
= kconsumerd_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
232 * display the error but continue processing to try
233 * to release the subbuffer
235 ERR("Error splicing to tracefile");
238 case LTTNG_EVENT_MMAP
:
239 /* read the used subbuffer size */
240 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
243 perror("Getting sub-buffer len failed.");
246 /* write the subbuffer to the tracefile */
247 ret
= kconsumerd_on_read_subbuffer_mmap(ctx
, kconsumerd_fd
, len
);
250 * display the error but continue processing to try
251 * to release the subbuffer
253 ERR("Error writing to tracefile");
257 ERR("Unknown output method");
261 err
= kernctl_put_next_subbuf(infd
);
264 if (errno
== EFAULT
) {
265 perror("Error in unreserving sub buffer\n");
266 } else if (errno
== EIO
) {
267 /* Should never happen with newer LTTng versions */
268 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
280 int main(int argc
, char **argv
)
286 /* Parse arguments */
288 parse_args(argc
, argv
);
299 if (strlen(command_sock_path
) == 0) {
300 snprintf(command_sock_path
, PATH_MAX
,
301 KCONSUMERD_CMD_SOCK_PATH
);
303 /* create the pipe to wake to receiving thread when needed */
304 ctx
= kconsumerd_create(read_subbuffer
);
309 kconsumerd_set_command_socket_path(ctx
, command_sock_path
);
310 if (strlen(error_sock_path
) == 0) {
311 snprintf(error_sock_path
, PATH_MAX
,
312 KCONSUMERD_ERR_SOCK_PATH
);
315 if (set_signal_handler() < 0) {
319 /* Connect to the socket created by ltt-sessiond to report errors */
320 DBG("Connecting to error socket %s", error_sock_path
);
321 ret
= lttcomm_connect_unix_sock(error_sock_path
);
322 /* not a fatal error, but all communication with ltt-sessiond will fail */
324 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
326 kconsumerd_set_error_socket(ctx
, ret
);
328 /* Create the thread to manage the receive of fd */
329 ret
= pthread_create(&threads
[0], NULL
, kconsumerd_thread_receive_fds
,
332 perror("pthread_create");
336 /* Create thread to manage the polling/writing of traces */
337 ret
= pthread_create(&threads
[1], NULL
, kconsumerd_thread_poll_fds
,
340 perror("pthread_create");
344 for (i
= 0; i
< 2; i
++) {
345 ret
= pthread_join(threads
[i
], &status
);
347 perror("pthread_join");
352 kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_SUCCESS
);
357 kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_FAILURE
);
360 kconsumerd_destroy(ctx
);
361 kconsumerd_cleanup();