Fatal error causes kconsumerd to exit
[lttng-tools.git] / kconsumerd / 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; either version 2
8 * of the License, or (at your option) any later version.
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
39 #include "lttngerr.h"
40 #include "libkernelctl.h"
41 #include "liblttsessiondcomm.h"
42 #include "kconsumerd.h"
43
44 /* Init the list of FDs */
45 static struct ltt_kconsumerd_fd_list kconsumerd_fd_list = {
46 .head = CDS_LIST_HEAD_INIT(kconsumerd_fd_list.head),
47 };
48
49 /* Number of element for the list below. */
50 static unsigned int fds_count;
51
52 /* If the local array of FDs needs update in the poll function */
53 static unsigned int update_fd_array = 1;
54
55 /* lock the fd array and structures */
56 static pthread_mutex_t kconsumerd_lock_fds;
57
58 /* the two threads (receive fd and poll) */
59 static pthread_t threads[2];
60
61 /* communication with splice */
62 static int thread_pipe[2];
63
64 /* socket to communicate errors with sessiond */
65 static int error_socket = -1;
66
67 /* Argument variables */
68 int opt_quiet;
69 int opt_verbose;
70 static int opt_daemon;
71 static const char *progname;
72 static char command_sock_path[PATH_MAX]; /* Global command socket path */
73 static char error_sock_path[PATH_MAX]; /* Global error path */
74
75 /*
76 * del_fd
77 *
78 * Remove a fd from the global list protected by a mutex
79 */
80 static void del_fd(struct ltt_kconsumerd_fd *lcf)
81 {
82 pthread_mutex_lock(&kconsumerd_lock_fds);
83 cds_list_del(&lcf->list);
84 if (fds_count > 0) {
85 fds_count--;
86 DBG("Removed ltt_kconsumerd_fd");
87 if (lcf != NULL) {
88 close(lcf->out_fd);
89 close(lcf->consumerd_fd);
90 free(lcf);
91 lcf = NULL;
92 }
93 }
94 pthread_mutex_unlock(&kconsumerd_lock_fds);
95 }
96
97 /*
98 * cleanup
99 *
100 * Cleanup the daemon's socket on exit
101 */
102 static void cleanup()
103 {
104 struct ltt_kconsumerd_fd *iter;
105
106
107 /* remove the socket file */
108 unlink(command_sock_path);
109
110 /* unblock the threads */
111 WARN("Terminating the threads before exiting");
112 pthread_cancel(threads[0]);
113 pthread_cancel(threads[1]);
114
115 /* close all outfd */
116 cds_list_for_each_entry(iter, &kconsumerd_fd_list.head, list) {
117 del_fd(iter);
118 }
119 }
120
121 /* send_error
122 *
123 * send return code to ltt-sessiond
124 */
125 static int send_error(enum lttcomm_return_code cmd)
126 {
127 if (error_socket > 0) {
128 return lttcomm_send_unix_sock(error_socket, &cmd,
129 sizeof(enum lttcomm_sessiond_command));
130 } else {
131 return 0;
132 }
133 }
134
135 /*
136 * add_fd
137 *
138 * Add a fd to the global list protected by a mutex
139 */
140 static int add_fd(struct lttcomm_kconsumerd_msg *buf, int consumerd_fd)
141 {
142 struct ltt_kconsumerd_fd *tmp_fd;
143 int ret;
144
145 tmp_fd = malloc(sizeof(struct ltt_kconsumerd_fd));
146 tmp_fd->sessiond_fd = buf->fd;
147 tmp_fd->consumerd_fd = consumerd_fd;
148 tmp_fd->state = buf->state;
149 tmp_fd->max_sb_size = buf->max_sb_size;
150 strncpy(tmp_fd->path_name, buf->path_name, PATH_MAX);
151
152 /* Opening the tracefile in write mode */
153 DBG("Opening %s for writing", tmp_fd->path_name);
154 ret = open(tmp_fd->path_name,
155 O_WRONLY|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
156 if (ret < 0) {
157 ERR("Opening %s", tmp_fd->path_name);
158 perror("open");
159 goto end;
160 }
161 tmp_fd->out_fd = ret;
162 tmp_fd->out_fd_offset = 0;
163
164 DBG("Adding %s (%d, %d, %d)", tmp_fd->path_name,
165 tmp_fd->sessiond_fd, tmp_fd->consumerd_fd, tmp_fd->out_fd);
166
167 pthread_mutex_lock(&kconsumerd_lock_fds);
168 cds_list_add(&tmp_fd->list, &kconsumerd_fd_list.head);
169 fds_count++;
170 pthread_mutex_unlock(&kconsumerd_lock_fds);
171
172 end:
173 return ret;
174 }
175
176
177 /*
178 * sighandler
179 *
180 * Signal handler for the daemon
181 */
182 static void sighandler(int sig)
183 {
184 cleanup();
185
186 return;
187 }
188
189 /*
190 * set_signal_handler
191 *
192 * Setup signal handler for :
193 * SIGINT, SIGTERM, SIGPIPE
194 */
195 static int set_signal_handler(void)
196 {
197 int ret = 0;
198 struct sigaction sa;
199 sigset_t sigset;
200
201 if ((ret = sigemptyset(&sigset)) < 0) {
202 perror("sigemptyset");
203 return ret;
204 }
205
206 sa.sa_handler = sighandler;
207 sa.sa_mask = sigset;
208 sa.sa_flags = 0;
209 if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) {
210 perror("sigaction");
211 return ret;
212 }
213
214 if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) {
215 perror("sigaction");
216 return ret;
217 }
218
219 if ((ret = sigaction(SIGPIPE, &sa, NULL)) < 0) {
220 perror("sigaction");
221 return ret;
222 }
223
224 return ret;
225 }
226
227 /*
228 * on_read_subbuffer
229 *
230 * Splice the data from the ring buffer to the tracefile.
231 * Returns the number of bytes spliced
232 */
233 static int on_read_subbuffer(struct ltt_kconsumerd_fd *kconsumerd_fd,
234 unsigned long len)
235 {
236 long ret = 0;
237 loff_t offset = 0;
238 off_t orig_offset = kconsumerd_fd->out_fd_offset;
239 int fd = kconsumerd_fd->consumerd_fd;
240 int outfd = kconsumerd_fd->out_fd;
241
242 while (len > 0) {
243 DBG("splice chan to pipe offset %lu (fd : %d)",
244 (unsigned long)offset, fd);
245 ret = splice(fd, &offset, thread_pipe[1], NULL, len,
246 SPLICE_F_MOVE | SPLICE_F_MORE);
247 DBG("splice chan to pipe ret %ld", ret);
248 if (ret < 0) {
249 ret = errno;
250 perror("Error in relay splice");
251 goto splice_error;
252 }
253
254 ret = splice(thread_pipe[0], NULL, outfd, NULL, ret,
255 SPLICE_F_MOVE | SPLICE_F_MORE);
256 DBG("splice pipe to file %ld", ret);
257 if (ret < 0) {
258 ret = errno;
259 perror("Error in file splice");
260 goto splice_error;
261 }
262 if (ret >= len) {
263 len = 0;
264 }
265 /* This won't block, but will start writeout asynchronously */
266 sync_file_range(outfd, kconsumerd_fd->out_fd_offset, ret,
267 SYNC_FILE_RANGE_WRITE);
268 kconsumerd_fd->out_fd_offset += ret;
269 }
270
271 /*
272 * This does a blocking write-and-wait on any page that belongs to the
273 * subbuffer prior to the one we just wrote.
274 * Don't care about error values, as these are just hints and ways to
275 * limit the amount of page cache used.
276 */
277 if (orig_offset >= kconsumerd_fd->max_sb_size) {
278 sync_file_range(outfd, orig_offset - kconsumerd_fd->max_sb_size,
279 kconsumerd_fd->max_sb_size,
280 SYNC_FILE_RANGE_WAIT_BEFORE
281 | SYNC_FILE_RANGE_WRITE
282 | SYNC_FILE_RANGE_WAIT_AFTER);
283 /*
284 * Give hints to the kernel about how we access the file:
285 * POSIX_FADV_DONTNEED : we won't re-access data in a near
286 * future after we write it.
287 * We need to call fadvise again after the file grows because
288 * the kernel does not seem to apply fadvise to non-existing
289 * parts of the file.
290 * Call fadvise _after_ having waited for the page writeback to
291 * complete because the dirty page writeback semantic is not
292 * well defined. So it can be expected to lead to lower
293 * throughput in streaming.
294 */
295 posix_fadvise(outfd, orig_offset - kconsumerd_fd->max_sb_size,
296 kconsumerd_fd->max_sb_size, POSIX_FADV_DONTNEED);
297 }
298 goto end;
299
300 splice_error:
301 /* send the appropriate error description to sessiond */
302 switch(ret) {
303 case EBADF:
304 send_error(KCONSUMERD_SPLICE_EBADF);
305 break;
306 case EINVAL:
307 send_error(KCONSUMERD_SPLICE_EINVAL);
308 break;
309 case ENOMEM:
310 send_error(KCONSUMERD_SPLICE_ENOMEM);
311 break;
312 case ESPIPE:
313 send_error(KCONSUMERD_SPLICE_ESPIPE);
314 break;
315 }
316
317 end:
318 return ret;
319 }
320
321 /*
322 * read_subbuffer
323 *
324 * Consume data on a file descriptor and write it on a trace file
325 */
326 static int read_subbuffer(struct ltt_kconsumerd_fd *kconsumerd_fd)
327 {
328 unsigned long len;
329 int err;
330 long ret = 0;
331 int infd = kconsumerd_fd->consumerd_fd;
332
333 DBG("In read_subbuffer");
334 /* Get the next subbuffer */
335 err = kernctl_get_next_subbuf(infd);
336 if (err != 0) {
337 ret = errno;
338 perror("Reserving sub buffer failed (everything is normal, "
339 "it is due to concurrency)");
340 goto end;
341 }
342
343 /* read the whole subbuffer */
344 err = kernctl_get_padded_subbuf_size(infd, &len);
345 if (err != 0) {
346 ret = errno;
347 perror("Getting sub-buffer len failed.");
348 goto end;
349 }
350
351 /* splice the subbuffer to the tracefile */
352 ret = on_read_subbuffer(kconsumerd_fd, len);
353 if (ret < 0) {
354 /*
355 * display the error but continue processing to try
356 * to release the subbuffer
357 */
358 ERR("Error splicing to tracefile");
359 }
360
361 err = kernctl_put_next_subbuf(infd);
362 if (err != 0) {
363 ret = errno;
364 if (errno == EFAULT) {
365 perror("Error in unreserving sub buffer\n");
366 } else if (errno == EIO) {
367 /* Should never happen with newer LTTng versions */
368 perror("Reader has been pushed by the writer, last sub-buffer corrupted.");
369 }
370 goto end;
371 }
372
373 end:
374 return ret;
375 }
376
377 /*
378 * change_fd_state
379 *
380 * Update a fd according to what we just received
381 */
382 static void change_fd_state(int sessiond_fd,
383 enum lttcomm_kconsumerd_fd_state state)
384 {
385 struct ltt_kconsumerd_fd *iter;
386 cds_list_for_each_entry(iter, &kconsumerd_fd_list.head, list) {
387 if (iter->sessiond_fd == sessiond_fd) {
388 iter->state = state;
389 break;
390 }
391 }
392 }
393
394 /*
395 * consumerd_recv_fd
396 *
397 * Receives an array of file descriptors and the associated
398 * structures describing each fd (path name).
399 * Returns the size of received data
400 */
401 static int consumerd_recv_fd(int sfd, int size,
402 enum lttcomm_consumerd_command cmd_type)
403 {
404 struct msghdr msg;
405 struct iovec iov[1];
406 int ret, i;
407 struct cmsghdr *cmsg;
408 int nb_fd;
409 char tmp[CMSG_SPACE(size)];
410 struct lttcomm_kconsumerd_msg *buf;
411 /* the number of fds we are about to receive */
412 nb_fd = size/sizeof(struct lttcomm_kconsumerd_msg);
413
414 buf = malloc(size);
415
416 memset(&msg, 0, sizeof(msg));
417
418 /* Prepare to receive the structures */
419 iov[0].iov_base = buf;
420 iov[0].iov_len = size;
421 msg.msg_iov = iov;
422 msg.msg_iovlen = 1;
423
424 msg.msg_control = tmp;
425 msg.msg_controllen = sizeof(tmp);
426
427 DBG("Waiting to receive fds");
428 if ((ret = recvmsg(sfd, &msg, 0)) < 0) {
429 perror("recvmsg");
430 }
431 if (ret != size) {
432 ERR("Received only %d, expected %d", ret, size);
433 send_error(KCONSUMERD_ERROR_RECV_FD);
434 goto end;
435 }
436
437 cmsg = CMSG_FIRSTHDR(&msg);
438 if (!cmsg) {
439 ERR("Invalid control message header");
440 ret = -1;
441 send_error(KCONSUMERD_ERROR_RECV_FD);
442 goto end;
443 }
444
445 /* if we received fds */
446 if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
447 DBG("Receive : expecting %d fds", nb_fd);
448 for (i = 0; i < nb_fd; i++) {
449 switch (cmd_type) {
450 case LTTCOMM_ADD_STREAM:
451 DBG("add_fd %s (%d)", buf[i].path_name, ((int *)CMSG_DATA(cmsg))[i]);
452 ret = add_fd(&buf[i], ((int *)CMSG_DATA(cmsg))[i]);
453 if (ret < 0) {
454 send_error(KCONSUMERD_OUTFD_ERROR);
455 goto end;
456 }
457 break;
458 case LTTCOMM_UPDATE_STREAM:
459 change_fd_state(buf[i].fd, buf[i].state);
460 break;
461 default:
462 break;
463 }
464 }
465 /* flag to tell the polling thread to update its fd array */
466 update_fd_array = 1;
467 send_error(KCONSUMERD_SUCCESS_RECV_FD);
468 } else {
469 ERR("Didn't received any fd");
470 send_error(KCONSUMERD_ERROR_RECV_FD);
471 ret = -1;
472 goto end;
473 }
474
475 end:
476 if (buf != NULL) {
477 free(buf);
478 buf = NULL;
479 }
480 return ret;
481 }
482
483 /*
484 * thread_receive_fds
485 *
486 * This thread listens on the consumerd socket and
487 * receives the file descriptors from ltt-sessiond
488 */
489 static void *thread_receive_fds(void *data)
490 {
491 int sock, client_socket, ret;
492 struct lttcomm_kconsumerd_header tmp;
493
494 DBG("Creating command socket %s", command_sock_path);
495 unlink(command_sock_path);
496 client_socket = lttcomm_create_unix_sock(command_sock_path);
497 if (client_socket < 0) {
498 ERR("Cannot create command socket");
499 goto error;
500 }
501
502 ret = lttcomm_listen_unix_sock(client_socket);
503 if (ret < 0) {
504 goto error;
505 }
506
507 DBG("Sending ready command to ltt-sessiond");
508 ret = send_error(KCONSUMERD_COMMAND_SOCK_READY);
509 if (ret < 0) {
510 ERR("Error sending ready command to ltt-sessiond");
511 goto error;
512 }
513
514 while (1) {
515 /* Blocking call, waiting for transmission */
516 sock = lttcomm_accept_unix_sock(client_socket);
517 if (sock <= 0) {
518 WARN("On accept, retrying");
519 continue;
520 }
521
522 /* We first get the number of fd we are about to receive */
523 ret = lttcomm_recv_unix_sock(sock, &tmp,
524 sizeof(struct lttcomm_kconsumerd_header));
525 if (ret < 0) {
526 ERR("Receiving the lttcomm_kconsumerd_header, exiting");
527 goto error;
528 }
529 ret = consumerd_recv_fd(sock, tmp.payload_size, tmp.cmd_type);
530 if (ret < 0) {
531 ERR("Receiving the FD, exiting");
532 goto error;
533 }
534 }
535
536 error:
537 cleanup();
538 return NULL;
539 }
540
541 /*
542 * update_poll_array
543 *
544 * Allocate the pollfd structure and the local view of the out fds
545 * to avoid doing a lookup in the linked list and concurrency issues
546 * when writing is needed.
547 * Returns the number of fds in the structures
548 */
549 static int update_poll_array(struct pollfd **pollfd,
550 struct ltt_kconsumerd_fd **local_kconsumerd_fd)
551 {
552 struct ltt_kconsumerd_fd *iter;
553 int i = 0;
554
555 if (*pollfd != NULL) {
556 free(*pollfd);
557 *pollfd = NULL;
558 }
559
560 if (*local_kconsumerd_fd != NULL) {
561 free(*local_kconsumerd_fd);
562 *local_kconsumerd_fd = NULL;
563 }
564
565 *pollfd = malloc(fds_count * sizeof(struct pollfd));
566 if (*pollfd == NULL) {
567 perror("pollfd malloc");
568 goto error_mem;
569 }
570
571 *local_kconsumerd_fd = malloc(fds_count * sizeof(struct ltt_kconsumerd_fd));
572 if (*local_kconsumerd_fd == NULL) {
573 perror("local_kconsumerd_fd malloc");
574 goto error_mem;
575 }
576
577 DBG("Updating poll fd array");
578 pthread_mutex_lock(&kconsumerd_lock_fds);
579
580 cds_list_for_each_entry(iter, &kconsumerd_fd_list.head, list) {
581 DBG("Inside for each");
582 if (iter->state == ACTIVE_FD) {
583 DBG("Active FD %d", iter->consumerd_fd);
584 pollfd[i]->fd = iter->consumerd_fd;
585 pollfd[i]->events = POLLIN | POLLPRI;
586 local_kconsumerd_fd[i] = iter;
587 i++;
588 } else if (iter->state == DELETE_FD) {
589 del_fd(iter);
590 }
591 }
592 update_fd_array = 0;
593 pthread_mutex_unlock(&kconsumerd_lock_fds);
594 return i;
595
596 error_mem:
597 return -ENOMEM;
598 }
599
600 /*
601 * thread_poll_fds
602 *
603 * This thread polls the fds in the ltt_fd_list to consume the data
604 * and write it to tracefile if necessary.
605 */
606 static void *thread_poll_fds(void *data)
607 {
608 int num_rdy, num_hup, high_prio, ret, i;
609 struct pollfd *pollfd = NULL;
610 /* local view of the fds */
611 struct ltt_kconsumerd_fd *local_kconsumerd_fd = NULL;
612 /* local view of fds_count */
613 int nb_fd = 0;
614
615 ret = pipe(thread_pipe);
616 if (ret < 0) {
617 perror("Error creating pipe");
618 goto end;
619 }
620
621 while (1) {
622 high_prio = 0;
623 num_hup = 0;
624
625 /*
626 * the ltt_fd_list has been updated, we need to update our
627 * local array as well
628 */
629 if (update_fd_array) {
630 ret = update_poll_array(&pollfd, &local_kconsumerd_fd);
631 if (ret < 0) {
632 ERR("Error in allocating pollfd or local_outfds");
633 send_error(KCONSUMERD_POLL_ERROR);
634 goto end;
635 }
636 nb_fd = ret;
637 }
638
639 /* poll on the array of fds */
640 DBG("polling on %d fd", nb_fd);
641 num_rdy = poll(pollfd, nb_fd, POLL_TIMEOUT);
642 DBG("poll num_rdy : %d", num_rdy);
643 if (num_rdy == -1) {
644 perror("Poll error");
645 send_error(KCONSUMERD_POLL_ERROR);
646 goto end;
647 }
648
649 /* Take care of high priority channels first. */
650 for (i = 0; i < nb_fd; i++) {
651 switch(pollfd[i].revents) {
652 case POLLERR:
653 ERR("Error returned in polling fd %d.", pollfd[i].fd);
654 num_hup++;
655 send_error(KCONSUMERD_POLL_ERROR);
656 break;
657 case POLLHUP:
658 ERR("Polling fd %d tells it has hung up.", pollfd[i].fd);
659 num_hup++;
660 break;
661 case POLLNVAL:
662 ERR("Polling fd %d tells fd is not open.", pollfd[i].fd);
663 send_error(KCONSUMERD_POLL_NVAL);
664 num_hup++;
665 break;
666 case POLLPRI:
667 DBG("Urgent read on fd %d", pollfd[i].fd);
668 high_prio = 1;
669 ret = read_subbuffer(&local_kconsumerd_fd[i]);
670 /* it's ok to have an unavailable sub-buffer (FIXME : is it ?) */
671 if (ret == EAGAIN) {
672 ret = 0;
673 }
674 break;
675 }
676 }
677
678 /* If every buffer FD has hung up, we end the read loop here */
679 if (nb_fd > 0 && num_hup == nb_fd) {
680 DBG("every buffer FD has hung up\n");
681 send_error(KCONSUMERD_POLL_HUP);
682 continue;
683 }
684
685 /* Take care of low priority channels. */
686 if (!high_prio) {
687 for (i = 0; i < nb_fd; i++) {
688 switch(pollfd[i].revents) {
689 case POLLIN:
690 DBG("Normal read on fd %d", pollfd[i].fd);
691 ret = read_subbuffer(&local_kconsumerd_fd[i]);
692 /* it's ok to have an unavailable subbuffer (FIXME : is it ?) */
693 if (ret == EAGAIN) {
694 ret = 0;
695 }
696 break;
697 }
698 }
699 }
700 }
701 end:
702 if (pollfd != NULL) {
703 free(pollfd);
704 pollfd = NULL;
705 }
706 if (local_kconsumerd_fd != NULL) {
707 free(local_kconsumerd_fd);
708 local_kconsumerd_fd = NULL;
709 }
710 cleanup();
711 return NULL;
712 }
713
714 /*
715 * usage function on stderr
716 */
717 static void usage(void)
718 {
719 fprintf(stderr, "Usage: %s OPTIONS\n\nOptions:\n", progname);
720 fprintf(stderr, " -h, --help "
721 "Display this usage.\n");
722 fprintf(stderr, " -c, --kconsumerd-cmd-sock PATH "
723 "Specify path for the command socket\n");
724 fprintf(stderr, " -e, --kconsumerd-err-sock PATH "
725 "Specify path for the error socket\n");
726 fprintf(stderr, " -d, --daemonize "
727 "Start as a daemon.\n");
728 fprintf(stderr, " -q, --quiet "
729 "No output at all.\n");
730 fprintf(stderr, " -v, --verbose "
731 "Verbose mode. Activate DBG() macro.\n");
732 fprintf(stderr, " -V, --version "
733 "Show version number.\n");
734 }
735
736 /*
737 * daemon argument parsing
738 */
739 static void parse_args(int argc, char **argv)
740 {
741 int c;
742
743 static struct option long_options[] = {
744 { "kconsumerd-cmd-sock", 1, 0, 'c' },
745 { "kconsumerd-err-sock", 1, 0, 'e' },
746 { "daemonize", 0, 0, 'd' },
747 { "help", 0, 0, 'h' },
748 { "quiet", 0, 0, 'q' },
749 { "verbose", 0, 0, 'v' },
750 { "version", 0, 0, 'V' },
751 { NULL, 0, 0, 0 }
752 };
753
754 while (1) {
755 int option_index = 0;
756 c = getopt_long(argc, argv, "dhqvV" "c:e:", long_options, &option_index);
757 if (c == -1) {
758 break;
759 }
760
761 switch (c) {
762 case 0:
763 fprintf(stderr, "option %s", long_options[option_index].name);
764 if (optarg) {
765 fprintf(stderr, " with arg %s\n", optarg);
766 }
767 break;
768 case 'c':
769 snprintf(command_sock_path, PATH_MAX, "%s", optarg);
770 break;
771 case 'e':
772 snprintf(error_sock_path, PATH_MAX, "%s", optarg);
773 break;
774 case 'd':
775 opt_daemon = 1;
776 break;
777 case 'h':
778 usage();
779 exit(EXIT_FAILURE);
780 case 'q':
781 opt_quiet = 1;
782 break;
783 case 'v':
784 opt_verbose = 1;
785 break;
786 case 'V':
787 fprintf(stdout, "%s\n", VERSION);
788 exit(EXIT_SUCCESS);
789 default:
790 usage();
791 exit(EXIT_FAILURE);
792 }
793 }
794 }
795
796
797 /*
798 * main
799 */
800 int main(int argc, char **argv)
801 {
802 int i;
803 int ret = 0;
804 void *status;
805
806 /* Parse arguments */
807 progname = argv[0];
808 parse_args(argc, argv);
809
810 /* Daemonize */
811 if (opt_daemon) {
812 ret = daemon(0, 0);
813 if (ret < 0) {
814 perror("daemon");
815 goto error;
816 }
817 }
818
819 if (strlen(command_sock_path) == 0) {
820 snprintf(command_sock_path, PATH_MAX,
821 KCONSUMERD_CMD_SOCK_PATH);
822 }
823 if (strlen(error_sock_path) == 0) {
824 snprintf(error_sock_path, PATH_MAX,
825 KCONSUMERD_ERR_SOCK_PATH);
826 }
827
828 if (set_signal_handler() < 0) {
829 goto error;
830 }
831
832 /* Connect to the socket created by ltt-sessiond to report errors */
833 DBG("Connecting to error socket %s", error_sock_path);
834 error_socket = lttcomm_connect_unix_sock(error_sock_path);
835 /* not a fatal error, but all communication with ltt-sessiond will fail */
836 if (error_socket < 0) {
837 WARN("Cannot connect to error socket, is ltt-sessiond started ?");
838 }
839
840 /* Create the thread to manage the receive of fd */
841 ret = pthread_create(&threads[0], NULL, thread_receive_fds, (void *) NULL);
842 if (ret != 0) {
843 perror("pthread_create");
844 goto error;
845 }
846
847 /* Create thread to manage the polling/writing of traces */
848 ret = pthread_create(&threads[1], NULL, thread_poll_fds, (void *) NULL);
849 if (ret != 0) {
850 perror("pthread_create");
851 goto error;
852 }
853
854 for (i = 0; i < 2; i++) {
855 ret = pthread_join(threads[i], &status);
856 if (ret != 0) {
857 perror("pthread_join");
858 goto error;
859 }
860 }
861 ret = EXIT_SUCCESS;
862 send_error(KCONSUMERD_EXIT_SUCCESS);
863 goto end;
864
865 error:
866 ret = EXIT_FAILURE;
867 send_error(KCONSUMERD_EXIT_FAILURE);
868
869 end:
870 cleanup();
871
872 return ret;
873 }
This page took 0.07557 seconds and 4 git commands to generate.