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 <ltt-kconsumerd.h>
42 #include <lttng-kernel-ctl.h>
43 #include <lttng-sessiond-comm.h>
44 #include <lttng/lttng-kconsumerd.h>
47 /* the two threads (receive fd and poll) */
48 static pthread_t threads
[2];
50 /* to count the number of time the user pressed ctrl+c */
51 static int sigintcount
= 0;
53 /* Argument variables */
56 static int opt_daemon
;
57 static const char *progname
;
58 static char command_sock_path
[PATH_MAX
]; /* Global command socket path */
59 static char error_sock_path
[PATH_MAX
]; /* Global error path */
61 /* the liblttngkconsumerd context */
62 static struct lttng_kconsumerd_local_data
*ctx
;
65 * Signal handler for the daemon
67 static void sighandler(int sig
)
69 if (sig
== SIGINT
&& sigintcount
++ == 0) {
70 DBG("ignoring first SIGINT");
74 lttng_kconsumerd_should_exit(ctx
);
78 * Setup signal handler for :
79 * SIGINT, SIGTERM, SIGPIPE
81 static int set_signal_handler(void)
87 if ((ret
= sigemptyset(&sigset
)) < 0) {
88 perror("sigemptyset");
92 sa
.sa_handler
= sighandler
;
95 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
100 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
105 if ((ret
= sigaction(SIGPIPE
, &sa
, NULL
)) < 0) {
114 * usage function on stderr
116 static void usage(void)
118 fprintf(stderr
, "Usage: %s OPTIONS\n\nOptions:\n", progname
);
119 fprintf(stderr
, " -h, --help "
120 "Display this usage.\n");
121 fprintf(stderr
, " -c, --kconsumerd-cmd-sock PATH "
122 "Specify path for the command socket\n");
123 fprintf(stderr
, " -e, --kconsumerd-err-sock PATH "
124 "Specify path for the error socket\n");
125 fprintf(stderr
, " -d, --daemonize "
126 "Start as a daemon.\n");
127 fprintf(stderr
, " -q, --quiet "
128 "No output at all.\n");
129 fprintf(stderr
, " -v, --verbose "
130 "Verbose mode. Activate DBG() macro.\n");
131 fprintf(stderr
, " -V, --version "
132 "Show version number.\n");
136 * daemon argument parsing
138 static void parse_args(int argc
, char **argv
)
142 static struct option long_options
[] = {
143 { "kconsumerd-cmd-sock", 1, 0, 'c' },
144 { "kconsumerd-err-sock", 1, 0, 'e' },
145 { "daemonize", 0, 0, 'd' },
146 { "help", 0, 0, 'h' },
147 { "quiet", 0, 0, 'q' },
148 { "verbose", 0, 0, 'v' },
149 { "version", 0, 0, 'V' },
154 int option_index
= 0;
155 c
= getopt_long(argc
, argv
, "dhqvV" "c:e:", long_options
, &option_index
);
162 fprintf(stderr
, "option %s", long_options
[option_index
].name
);
164 fprintf(stderr
, " with arg %s\n", optarg
);
168 snprintf(command_sock_path
, PATH_MAX
, "%s", optarg
);
171 snprintf(error_sock_path
, PATH_MAX
, "%s", optarg
);
186 fprintf(stdout
, "%s\n", VERSION
);
196 * Consume data on a file descriptor and write it on a trace file.
198 static int read_subbuffer(struct lttng_kconsumerd_fd
*kconsumerd_fd
)
203 int infd
= kconsumerd_fd
->consumerd_fd
;
205 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd
);
206 /* Get the next subbuffer */
207 err
= kernctl_get_next_subbuf(infd
);
211 * This is a debug message even for single-threaded consumer,
212 * because poll() have more relaxed criterions than get subbuf,
213 * so get_subbuf may fail for short race windows where poll()
214 * would issue wakeups.
216 DBG("Reserving sub buffer failed (everything is normal, "
217 "it is due to concurrency)");
221 switch (kconsumerd_fd
->output
) {
222 case LTTNG_EVENT_SPLICE
:
223 /* read the whole subbuffer */
224 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
227 perror("Getting sub-buffer len failed.");
231 /* splice the subbuffer to the tracefile */
232 ret
= lttng_kconsumerd_on_read_subbuffer_splice(ctx
, kconsumerd_fd
, len
);
235 * display the error but continue processing to try
236 * to release the subbuffer
238 ERR("Error splicing to tracefile");
241 case LTTNG_EVENT_MMAP
:
242 /* read the used subbuffer size */
243 err
= kernctl_get_padded_subbuf_size(infd
, &len
);
246 perror("Getting sub-buffer len failed.");
249 /* write the subbuffer to the tracefile */
250 ret
= lttng_kconsumerd_on_read_subbuffer_mmap(ctx
, kconsumerd_fd
, len
);
253 * display the error but continue processing to try
254 * to release the subbuffer
256 ERR("Error writing to tracefile");
260 ERR("Unknown output method");
264 err
= kernctl_put_next_subbuf(infd
);
267 if (errno
== EFAULT
) {
268 perror("Error in unreserving sub buffer\n");
269 } else if (errno
== EIO
) {
270 /* Should never happen with newer LTTng versions */
271 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
280 static int on_recv_fd(struct lttng_kconsumerd_fd
*kconsumerd_fd
)
284 /* Opening the tracefile in write mode */
285 if (kconsumerd_fd
->path_name
!= NULL
) {
286 ret
= open(kconsumerd_fd
->path_name
,
287 O_WRONLY
|O_CREAT
|O_TRUNC
, S_IRWXU
|S_IRWXG
|S_IRWXO
);
289 ERR("Opening %s", kconsumerd_fd
->path_name
);
293 kconsumerd_fd
->out_fd
= ret
;
296 if (kconsumerd_fd
->output
== LTTNG_EVENT_MMAP
) {
297 /* get the len of the mmap region */
298 ret
= kernctl_get_mmap_len(kconsumerd_fd
->consumerd_fd
, &kconsumerd_fd
->mmap_len
);
301 perror("kernctl_get_mmap_len");
305 kconsumerd_fd
->mmap_base
= mmap(NULL
, kconsumerd_fd
->mmap_len
,
306 PROT_READ
, MAP_PRIVATE
, kconsumerd_fd
->consumerd_fd
, 0);
307 if (kconsumerd_fd
->mmap_base
== MAP_FAILED
) {
308 perror("Error mmaping");
314 /* we return 0 to let the library handle the FD internally */
321 err
= close(kconsumerd_fd
->out_fd
);
331 int main(int argc
, char **argv
)
337 /* Parse arguments */
339 parse_args(argc
, argv
);
350 if (strlen(command_sock_path
) == 0) {
351 snprintf(command_sock_path
, PATH_MAX
,
352 KCONSUMERD_CMD_SOCK_PATH
);
354 /* create the consumer instance with and assign the callbacks */
355 ctx
= lttng_kconsumerd_create(read_subbuffer
, on_recv_fd
, NULL
);
360 lttng_kconsumerd_set_command_sock_path(ctx
, command_sock_path
);
361 if (strlen(error_sock_path
) == 0) {
362 snprintf(error_sock_path
, PATH_MAX
,
363 KCONSUMERD_ERR_SOCK_PATH
);
366 if (set_signal_handler() < 0) {
370 /* Connect to the socket created by ltt-sessiond to report errors */
371 DBG("Connecting to error socket %s", error_sock_path
);
372 ret
= lttcomm_connect_unix_sock(error_sock_path
);
373 /* not a fatal error, but all communication with ltt-sessiond will fail */
375 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
377 lttng_kconsumerd_set_error_sock(ctx
, ret
);
379 /* Create the thread to manage the receive of fd */
380 ret
= pthread_create(&threads
[0], NULL
, lttng_kconsumerd_thread_receive_fds
,
383 perror("pthread_create");
387 /* Create thread to manage the polling/writing of traces */
388 ret
= pthread_create(&threads
[1], NULL
, lttng_kconsumerd_thread_poll_fds
,
391 perror("pthread_create");
395 for (i
= 0; i
< 2; i
++) {
396 ret
= pthread_join(threads
[i
], &status
);
398 perror("pthread_join");
403 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_SUCCESS
);
408 lttng_kconsumerd_send_error(ctx
, KCONSUMERD_EXIT_FAILURE
);
411 lttng_kconsumerd_destroy(ctx
);
412 lttng_kconsumerd_cleanup();