Callbacks on receive and update FD
[lttng-tools.git] / ltt-kconsumerd / ltt-kconsumerd.c
1 /*
2 * Copyright (C) 2011 - Julien Desfossez <julien.desfossez@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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
8 * of the License.
9 *
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.
14 *
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.
18 */
19
20 #define _GNU_SOURCE
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <grp.h>
24 #include <limits.h>
25 #include <pthread.h>
26 #include <signal.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/ipc.h>
31 #include <sys/shm.h>
32 #include <sys/socket.h>
33 #include <sys/stat.h>
34 #include <sys/types.h>
35 #include <urcu/list.h>
36 #include <poll.h>
37 #include <unistd.h>
38 #include <sys/mman.h>
39 #include <assert.h>
40
41 #include <lttng/lttng-kconsumerd.h>
42
43 #include "lttngerr.h"
44 #include "kernelctl.h"
45 #include "ltt-kconsumerd.h"
46 #include "lttng-sessiond-comm.h"
47
48 /* the two threads (receive fd and poll) */
49 static pthread_t threads[2];
50
51 /* to count the number of time the user pressed ctrl+c */
52 static int sigintcount = 0;
53
54 /* Argument variables */
55 int opt_quiet;
56 int opt_verbose;
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 */
61
62 /* the liblttngkconsumerd context */
63 static struct lttng_kconsumerd_local_data *ctx;
64
65 /*
66 * Signal handler for the daemon
67 */
68 static void sighandler(int sig)
69 {
70 if (sig == SIGINT && sigintcount++ == 0) {
71 DBG("ignoring first SIGINT");
72 return;
73 }
74
75 lttng_kconsumerd_should_exit(ctx);
76 }
77
78 /*
79 * Setup signal handler for :
80 * SIGINT, SIGTERM, SIGPIPE
81 */
82 static int set_signal_handler(void)
83 {
84 int ret = 0;
85 struct sigaction sa;
86 sigset_t sigset;
87
88 if ((ret = sigemptyset(&sigset)) < 0) {
89 perror("sigemptyset");
90 return ret;
91 }
92
93 sa.sa_handler = sighandler;
94 sa.sa_mask = sigset;
95 sa.sa_flags = 0;
96 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
97 perror("sigaction");
98 return ret;
99 }
100
101 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
102 perror("sigaction");
103 return ret;
104 }
105
106 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
107 perror("sigaction");
108 return ret;
109 }
110
111 return ret;
112 }
113
114 /*
115 * usage function on stderr
116 */
117 static void usage(void)
118 {
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");
134 }
135
136 /*
137 * daemon argument parsing
138 */
139 static void parse_args(int argc, char **argv)
140 {
141 int c;
142
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' },
151 { NULL, 0, 0, 0 }
152 };
153
154 while (1) {
155 int option_index = 0;
156 c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index);
157 if (c == -1) {
158 break;
159 }
160
161 switch (c) {
162 case 0:
163 fprintf(stderr, "option %s", long_options[option_index].name);
164 if (optarg) {
165 fprintf(stderr, " with arg %s\n", optarg);
166 }
167 break;
168 case 'c':
169 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
170 break;
171 case 'e':
172 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
173 break;
174 case 'd':
175 opt_daemon = 1;
176 break;
177 case 'h':
178 usage();
179 exit(EXIT_FAILURE);
180 case 'q':
181 opt_quiet = 1;
182 break;
183 case 'v':
184 opt_verbose = 1;
185 break;
186 case 'V':
187 fprintf(stdout, "%s\n", VERSION);
188 exit(EXIT_SUCCESS);
189 default:
190 usage();
191 exit(EXIT_FAILURE);
192 }
193 }
194 }
195
196 /*
197 * Consume data on a file descriptor and write it on a trace file.
198 */
199 static int read_subbuffer(struct lttng_kconsumerd_fd *kconsumerd_fd)
200 {
201 unsigned long len;
202 int err;
203 long ret = 0;
204 int infd = kconsumerd_fd->consumerd_fd;
205
206 DBG("In kconsumerd_read_subbuffer (infd : %d)", infd);
207 /* Get the next subbuffer */
208 err = kernctl_get_next_subbuf(infd);
209 if (err != 0) {
210 ret = errno;
211 /*
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.
216 */
217 DBG("Reserving sub buffer failed (everything is normal, "
218 "it is due to concurrency)");
219 goto end;
220 }
221
222 switch (kconsumerd_fd->output) {
223 case LTTNG_EVENT_SPLICE:
224 /* read the whole subbuffer */
225 err = kernctl_get_padded_subbuf_size(infd, &len);
226 if (err != 0) {
227 ret = errno;
228 perror("Getting sub-buffer len failed.");
229 goto end;
230 }
231
232 /* splice the subbuffer to the tracefile */
233 ret = lttng_kconsumerd_on_read_subbuffer_splice(ctx, kconsumerd_fd, len);
234 if (ret < 0) {
235 /*
236 * display the error but continue processing to try
237 * to release the subbuffer
238 */
239 ERR("Error splicing to tracefile");
240 }
241 break;
242 case LTTNG_EVENT_MMAP:
243 /* read the used subbuffer size */
244 err = kernctl_get_padded_subbuf_size(infd, &len);
245 if (err != 0) {
246 ret = errno;
247 perror("Getting sub-buffer len failed.");
248 goto end;
249 }
250 /* write the subbuffer to the tracefile */
251 ret = lttng_kconsumerd_on_read_subbuffer_mmap(ctx, kconsumerd_fd, len);
252 if (ret < 0) {
253 /*
254 * display the error but continue processing to try
255 * to release the subbuffer
256 */
257 ERR("Error writing to tracefile");
258 }
259 break;
260 default:
261 ERR("Unknown output method");
262 ret = -1;
263 }
264
265 err = kernctl_put_next_subbuf(infd);
266 if (err != 0) {
267 ret = errno;
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.");
273 }
274 goto end;
275 }
276
277 end:
278 return ret;
279 }
280
281 static int on_recv_fd(struct lttng_kconsumerd_fd *kconsumerd_fd)
282 {
283 int ret;
284
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);
289 if (ret < 0) {
290 ERR("Opening %s", kconsumerd_fd->path_name);
291 perror("open");
292 goto error;
293 }
294 kconsumerd_fd->out_fd = ret;
295 }
296
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);
300 if (ret != 0) {
301 ret = errno;
302 perror("kernctl_get_mmap_len");
303 goto error_close_fd;
304 }
305
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");
310 ret = -1;
311 goto error_close_fd;
312 }
313 }
314
315 /* we return 0 to let the library handle the FD internally */
316 return 0;
317
318 error_close_fd:
319 {
320 int err;
321
322 err = close(kconsumerd_fd->out_fd);
323 assert(!err);
324 }
325 error:
326 return ret;
327 }
328
329 /*
330 * main
331 */
332 int main(int argc, char **argv)
333 {
334 int i;
335 int ret = 0;
336 void *status;
337
338 /* Parse arguments */
339 progname = argv[0];
340 parse_args(argc, argv);
341
342 /* Daemonize */
343 if (opt_daemon) {
344 ret = daemon(0, 0);
345 if (ret < 0) {
346 perror("daemon");
347 goto error;
348 }
349 }
350
351 if (strlen(command_sock_path) == 0) {
352 snprintf(command_sock_path, PATH_MAX,
353 KCONSUMERD_CMD_SOCK_PATH);
354 }
355 /* create the consumer instance with and assign the callbacks */
356 ctx = lttng_kconsumerd_create(read_subbuffer, on_recv_fd, NULL);
357 if (ctx == NULL) {
358 goto error;
359 }
360
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);
365 }
366
367 if (set_signal_handler() < 0) {
368 goto error;
369 }
370
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 */
375 if (ret < 0) {
376 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
377 }
378 lttng_kconsumerd_set_error_sock(ctx, ret);
379
380 /* Create the thread to manage the receive of fd */
381 ret = pthread_create(&threads[0], NULL, lttng_kconsumerd_thread_receive_fds,
382 (void *) ctx);
383 if (ret != 0) {
384 perror("pthread_create");
385 goto error;
386 }
387
388 /* Create thread to manage the polling/writing of traces */
389 ret = pthread_create(&threads[1], NULL, lttng_kconsumerd_thread_poll_fds,
390 (void *) ctx);
391 if (ret != 0) {
392 perror("pthread_create");
393 goto error;
394 }
395
396 for (i = 0; i < 2; i++) {
397 ret = pthread_join(threads[i], &status);
398 if (ret != 0) {
399 perror("pthread_join");
400 goto error;
401 }
402 }
403 ret = EXIT_SUCCESS;
404 lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_SUCCESS);
405 goto end;
406
407 error:
408 ret = EXIT_FAILURE;
409 lttng_kconsumerd_send_error(ctx, KCONSUMERD_EXIT_FAILURE);
410
411 end:
412 lttng_kconsumerd_destroy(ctx);
413 lttng_kconsumerd_cleanup();
414
415 return ret;
416 }
This page took 0.045091 seconds and 4 git commands to generate.