4764838c82d6c7138c1880d96b0abeac4f8b8e09
[ltt-control.git] / ltt-control / lttd / lttd.c
1 /* lttd
2 *
3 * Linux Trace Toolkit Daemon
4 *
5 * This is a simple daemon that reads a few relay+debugfs channels and save
6 * them in a trace.
7 *
8 *
9 * Copyright 2005 -
10 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 */
12
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #define _REENTRANT
18 #define _GNU_SOURCE
19 #include <features.h>
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <errno.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <stdlib.h>
26 #include <dirent.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <sys/poll.h>
30 #include <sys/mman.h>
31 #include <signal.h>
32 #include <pthread.h>
33 #include <sys/syscall.h>
34 #include <unistd.h>
35 #include <asm/ioctls.h>
36
37 #include <linux/version.h>
38
39 /* Relayfs IOCTL */
40 #include <asm/ioctl.h>
41 #include <asm/types.h>
42
43 /* Get the next sub buffer that can be read. */
44 #define RELAY_GET_SUBBUF _IOR(0xF5, 0x00,__u32)
45 /* Release the oldest reserved (by "get") sub buffer. */
46 #define RELAY_PUT_SUBBUF _IOW(0xF5, 0x01,__u32)
47 /* returns the number of sub buffers in the per cpu channel. */
48 #define RELAY_GET_N_SUBBUFS _IOR(0xF5, 0x02,__u32)
49 /* returns the size of the sub buffers. */
50 #define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32)
51
52 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
53 #include <linux/inotify.h>
54 /* From the inotify-tools 2.6 package */
55 static inline int inotify_init (void)
56 {
57 return syscall (__NR_inotify_init);
58 }
59
60 static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
61 {
62 return syscall (__NR_inotify_add_watch, fd, name, mask);
63 }
64
65 static inline int inotify_rm_watch (int fd, __u32 wd)
66 {
67 return syscall (__NR_inotify_rm_watch, fd, wd);
68 }
69 #define HAS_INOTIFY
70 #else
71 static inline int inotify_init (void)
72 {
73 return -1;
74 }
75
76 static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
77 {
78 return 0;
79 }
80
81 static inline int inotify_rm_watch (int fd, __u32 wd)
82 {
83 return 0;
84 }
85 #undef HAS_INOTIFY
86 #endif
87
88 enum {
89 GET_SUBBUF,
90 PUT_SUBBUF,
91 GET_N_BUBBUFS,
92 GET_SUBBUF_SIZE
93 };
94
95 struct fd_pair {
96 int channel;
97 int trace;
98 unsigned int n_subbufs;
99 unsigned int subbuf_size;
100 void *mmap;
101 pthread_mutex_t mutex;
102 };
103
104 struct channel_trace_fd {
105 struct fd_pair *pair;
106 int num_pairs;
107 };
108
109 struct inotify_watch {
110 int wd;
111 char path_channel[PATH_MAX];
112 char path_trace[PATH_MAX];
113 };
114
115 struct inotify_watch_array {
116 struct inotify_watch *elem;
117 int num;
118 };
119
120 static char *trace_name = NULL;
121 static char *channel_name = NULL;
122 static int daemon_mode = 0;
123 static int append_mode = 0;
124 static unsigned long num_threads = 1;
125 volatile static int quit_program = 0; /* For signal handler */
126 static int dump_flight_only = 0;
127 static int dump_normal_only = 0;
128
129 /* Args :
130 *
131 * -t directory Directory name of the trace to write to. Will be created.
132 * -c directory Root directory of the debugfs trace channels.
133 * -d Run in background (daemon).
134 * -a Trace append mode.
135 * -s Send SIGUSR1 to parent when ready for IO.
136 */
137 void show_arguments(void)
138 {
139 printf("Please use the following arguments :\n");
140 printf("\n");
141 printf("-t directory Directory name of the trace to write to.\n"
142 " It will be created.\n");
143 printf("-c directory Root directory of the debugfs trace channels.\n");
144 printf("-d Run in background (daemon).\n");
145 printf("-a Append to an possibly existing trace.\n");
146 printf("-N Number of threads to start.\n");
147 printf("-f Dump only flight recorder channels.\n");
148 printf("-n Dump only normal channels.\n");
149 printf("\n");
150 }
151
152
153 /* parse_arguments
154 *
155 * Parses the command line arguments.
156 *
157 * Returns 1 if the arguments were correct, but doesn't ask for program
158 * continuation. Returns -1 if the arguments are incorrect, or 0 if OK.
159 */
160 int parse_arguments(int argc, char **argv)
161 {
162 int ret = 0;
163 int argn = 1;
164
165 if(argc == 2) {
166 if(strcmp(argv[1], "-h") == 0) {
167 return 1;
168 }
169 }
170
171 while(argn < argc) {
172
173 switch(argv[argn][0]) {
174 case '-':
175 switch(argv[argn][1]) {
176 case 't':
177 if(argn+1 < argc) {
178 trace_name = argv[argn+1];
179 argn++;
180 }
181 break;
182 case 'c':
183 if(argn+1 < argc) {
184 channel_name = argv[argn+1];
185 argn++;
186 }
187 break;
188 case 'd':
189 daemon_mode = 1;
190 break;
191 case 'a':
192 append_mode = 1;
193 break;
194 case 'N':
195 if(argn+1 < argc) {
196 num_threads = strtoul(argv[argn+1], NULL, 0);
197 argn++;
198 }
199 break;
200 case 'f':
201 dump_flight_only = 1;
202 break;
203 case 'n':
204 dump_normal_only = 1;
205 break;
206 default:
207 printf("Invalid argument '%s'.\n", argv[argn]);
208 printf("\n");
209 ret = -1;
210 }
211 break;
212 default:
213 printf("Invalid argument '%s'.\n", argv[argn]);
214 printf("\n");
215 ret = -1;
216 }
217 argn++;
218 }
219
220 if(trace_name == NULL) {
221 printf("Please specify a trace name.\n");
222 printf("\n");
223 ret = -1;
224 }
225
226 if(channel_name == NULL) {
227 printf("Please specify a channel name.\n");
228 printf("\n");
229 ret = -1;
230 }
231
232 return ret;
233 }
234
235 void show_info(void)
236 {
237 printf("Linux Trace Toolkit Trace Daemon " VERSION "\n");
238 printf("\n");
239 printf("Reading from debugfs directory : %s\n", channel_name);
240 printf("Writing to trace directory : %s\n", trace_name);
241 printf("\n");
242 }
243
244
245 /* signal handling */
246
247 static void handler(int signo)
248 {
249 printf("Signal %d received : exiting cleanly\n", signo);
250 quit_program = 1;
251 }
252
253
254 int open_buffer_file(char *filename, char *path_channel, char *path_trace,
255 struct channel_trace_fd *fd_pairs)
256 {
257 int open_ret = 0;
258 int ret = 0;
259 struct stat stat_buf;
260
261 if(strncmp(filename, "flight-", sizeof("flight-")-1) != 0) {
262 if(dump_flight_only) {
263 printf("Skipping normal channel %s\n", path_channel);
264 return 0;
265 }
266 } else {
267 if(dump_normal_only) {
268 printf("Skipping flight channel %s\n", path_channel);
269 return 0;
270 }
271 }
272 printf("Opening file.\n");
273
274 fd_pairs->pair = realloc(fd_pairs->pair,
275 ++fd_pairs->num_pairs * sizeof(struct fd_pair));
276
277 /* Open the channel in read mode */
278 fd_pairs->pair[fd_pairs->num_pairs-1].channel =
279 open(path_channel, O_RDONLY | O_NONBLOCK);
280 if(fd_pairs->pair[fd_pairs->num_pairs-1].channel == -1) {
281 perror(path_channel);
282 fd_pairs->num_pairs--;
283 return 0; /* continue */
284 }
285 /* Open the trace in write mode, only append if append_mode */
286 ret = stat(path_trace, &stat_buf);
287 if(ret == 0) {
288 if(append_mode) {
289 printf("Appending to file %s as requested\n", path_trace);
290
291 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
292 open(path_trace, O_WRONLY|O_APPEND,
293 S_IRWXU|S_IRWXG|S_IRWXO);
294
295 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
296 perror(path_trace);
297 }
298 } else {
299 printf("File %s exists, cannot open. Try append mode.\n", path_trace);
300 open_ret = -1;
301 goto end;
302 }
303 } else {
304 if(errno == ENOENT) {
305 fd_pairs->pair[fd_pairs->num_pairs-1].trace =
306 open(path_trace, O_WRONLY|O_CREAT|O_EXCL,
307 S_IRWXU|S_IRWXG|S_IRWXO);
308 if(fd_pairs->pair[fd_pairs->num_pairs-1].trace == -1) {
309 perror(path_trace);
310 }
311 }
312 }
313 end:
314 return open_ret;
315 }
316
317 int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name,
318 struct channel_trace_fd *fd_pairs, int *inotify_fd,
319 struct inotify_watch_array *iwatch_array)
320 {
321 DIR *channel_dir = opendir(subchannel_name);
322 struct dirent *entry;
323 struct stat stat_buf;
324 int ret;
325 char path_channel[PATH_MAX];
326 int path_channel_len;
327 char *path_channel_ptr;
328 char path_trace[PATH_MAX];
329 int path_trace_len;
330 char *path_trace_ptr;
331 int open_ret = 0;
332
333 if(channel_dir == NULL) {
334 perror(subchannel_name);
335 open_ret = ENOENT;
336 goto end;
337 }
338
339 printf("Creating trace subdirectory %s\n", subtrace_name);
340 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
341 if(ret == -1) {
342 if(errno != EEXIST) {
343 perror(subtrace_name);
344 open_ret = -1;
345 goto end;
346 }
347 }
348
349 strncpy(path_channel, subchannel_name, PATH_MAX-1);
350 path_channel_len = strlen(path_channel);
351 path_channel[path_channel_len] = '/';
352 path_channel_len++;
353 path_channel_ptr = path_channel + path_channel_len;
354
355 strncpy(path_trace, subtrace_name, PATH_MAX-1);
356 path_trace_len = strlen(path_trace);
357 path_trace[path_trace_len] = '/';
358 path_trace_len++;
359 path_trace_ptr = path_trace + path_trace_len;
360
361 #ifdef HAS_INOTIFY
362 iwatch_array->elem = realloc(iwatch_array->elem,
363 ++iwatch_array->num * sizeof(struct inotify_watch));
364
365 printf("Adding inotify for channel %s\n", path_channel);
366 iwatch_array->elem[iwatch_array->num-1].wd = inotify_add_watch(*inotify_fd, path_channel, IN_CREATE);
367 strcpy(iwatch_array->elem[iwatch_array->num-1].path_channel, path_channel);
368 strcpy(iwatch_array->elem[iwatch_array->num-1].path_trace, path_trace);
369 printf("Added inotify for channel %s, wd %u\n", iwatch_array->elem[iwatch_array->num-1].path_channel,
370 iwatch_array->elem[iwatch_array->num-1].wd);
371 #endif
372
373 while((entry = readdir(channel_dir)) != NULL) {
374
375 if(entry->d_name[0] == '.') continue;
376
377 strncpy(path_channel_ptr, entry->d_name, PATH_MAX - path_channel_len);
378 strncpy(path_trace_ptr, entry->d_name, PATH_MAX - path_trace_len);
379
380 ret = stat(path_channel, &stat_buf);
381 if(ret == -1) {
382 perror(path_channel);
383 continue;
384 }
385
386 printf("Channel file : %s\n", path_channel);
387
388 if(S_ISDIR(stat_buf.st_mode)) {
389
390 printf("Entering channel subdirectory...\n");
391 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs,
392 inotify_fd, iwatch_array);
393 if(ret < 0) continue;
394 } else if(S_ISREG(stat_buf.st_mode)) {
395 open_ret = open_buffer_file(entry->d_name, path_channel, path_trace,
396 fd_pairs);
397 if(open_ret)
398 goto end;
399 }
400 }
401
402 end:
403 closedir(channel_dir);
404
405 return open_ret;
406 }
407
408
409 int read_subbuffer(struct fd_pair *pair)
410 {
411 unsigned int consumed_old;
412 int err, ret=0;
413
414
415 err = ioctl(pair->channel, RELAY_GET_SUBBUF,
416 &consumed_old);
417 printf("cookie : %u\n", consumed_old);
418 if(err != 0) {
419 ret = errno;
420 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
421 goto get_error;
422 }
423
424 err = TEMP_FAILURE_RETRY(write(pair->trace,
425 pair->mmap
426 + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
427 pair->subbuf_size));
428
429 if(err < 0) {
430 ret = errno;
431 perror("Error in writing to file");
432 goto write_error;
433 }
434 #if 0
435 err = fsync(pair->trace);
436 if(err < 0) {
437 ret = errno;
438 perror("Error in writing to file");
439 goto write_error;
440 }
441 #endif //0
442 write_error:
443 err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old);
444 if(err != 0) {
445 ret = errno;
446 if(errno == EFAULT) {
447 perror("Error in unreserving sub buffer\n");
448 } else if(errno == EIO) {
449 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
450 /* FIXME : we may delete the last written buffer if we wish. */
451 }
452 goto get_error;
453 }
454
455 get_error:
456 return ret;
457 }
458
459
460
461 int map_channels(struct channel_trace_fd *fd_pairs,
462 int idx_begin, int idx_end)
463 {
464 int i,j;
465 int ret=0;
466
467 if(fd_pairs->num_pairs <= 0) {
468 printf("No channel to read\n");
469 goto end;
470 }
471
472 /* Get the subbuf sizes and number */
473
474 for(i=idx_begin;i<idx_end;i++) {
475 struct fd_pair *pair = &fd_pairs->pair[i];
476
477 ret = ioctl(pair->channel, RELAY_GET_N_SUBBUFS,
478 &pair->n_subbufs);
479 if(ret != 0) {
480 perror("Error in getting the number of subbuffers");
481 goto end;
482 }
483 ret = ioctl(pair->channel, RELAY_GET_SUBBUF_SIZE,
484 &pair->subbuf_size);
485 if(ret != 0) {
486 perror("Error in getting the size of the subbuffers");
487 goto end;
488 }
489 ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */
490 if(ret != 0) {
491 perror("Error in mutex init");
492 goto end;
493 }
494 }
495
496 /* Mmap each FD */
497 for(i=idx_begin;i<idx_end;i++) {
498 struct fd_pair *pair = &fd_pairs->pair[i];
499
500 pair->mmap = mmap(0, pair->subbuf_size * pair->n_subbufs, PROT_READ,
501 MAP_SHARED, pair->channel, 0);
502 if(pair->mmap == MAP_FAILED) {
503 perror("Mmap error");
504 goto munmap;
505 }
506 }
507
508 goto end; /* success */
509
510 /* Error handling */
511 /* munmap only the successfully mmapped indexes */
512 munmap:
513 /* Munmap each FD */
514 for(j=idx_begin;j<i;j++) {
515 struct fd_pair *pair = &fd_pairs->pair[j];
516 int err_ret;
517
518 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
519 if(err_ret != 0) {
520 perror("Error in munmap");
521 }
522 ret |= err_ret;
523 }
524
525 end:
526 return ret;
527
528
529 }
530
531
532 int unmap_channels(struct channel_trace_fd *fd_pairs)
533 {
534 int j;
535 int ret=0;
536
537 /* Munmap each FD */
538 for(j=0;j<fd_pairs->num_pairs;j++) {
539 struct fd_pair *pair = &fd_pairs->pair[j];
540 int err_ret;
541
542 err_ret = munmap(pair->mmap, pair->subbuf_size * pair->n_subbufs);
543 if(err_ret != 0) {
544 perror("Error in munmap");
545 }
546 ret |= err_ret;
547 err_ret = pthread_mutex_destroy(&pair->mutex);
548 if(err_ret != 0) {
549 perror("Error in mutex destroy");
550 }
551 ret |= err_ret;
552 }
553
554 return ret;
555 }
556
557 #ifdef HAS_INOTIFY
558 /* Inotify event arrived.
559 *
560 * Only support add file for now.
561 */
562
563 int read_inotify(int inotify_fd,
564 struct channel_trace_fd *fd_pairs,
565 struct inotify_watch_array *iwatch_array)
566 {
567 char buf[sizeof(struct inotify_event) + PATH_MAX];
568 char path_channel[PATH_MAX];
569 char path_trace[PATH_MAX];
570 ssize_t len;
571 struct inotify_event *ievent;
572 size_t offset;
573 unsigned int i;
574 int ret;
575 int old_num;
576
577 offset = 0;
578 len = read(inotify_fd, buf, sizeof(struct inotify_event) + PATH_MAX);
579 if(len < 0) {
580 printf("Error in read from inotify FD %s.\n", strerror(len));
581 return -1;
582 }
583 while(offset < len) {
584 ievent = (struct inotify_event *)&(buf[offset]);
585 for(i=0; i<iwatch_array->num; i++) {
586 if(iwatch_array->elem[i].wd == ievent->wd &&
587 ievent->mask == IN_CREATE) {
588 printf("inotify wd %u event mask : %u for %s%s\n",
589 ievent->wd, ievent->mask,
590 iwatch_array->elem[i].path_channel, ievent->name);
591 old_num = fd_pairs->num_pairs;
592 strcpy(path_channel, iwatch_array->elem[i].path_channel);
593 strcat(path_channel, ievent->name);
594 strcpy(path_trace, iwatch_array->elem[i].path_trace);
595 strcat(path_trace, ievent->name);
596 if(ret = open_buffer_file(ievent->name, path_channel,
597 path_trace, fd_pairs)) {
598 printf("Error opening buffer file\n");
599 return -1;
600 }
601 if(ret = map_channels(fd_pairs, old_num, fd_pairs->num_pairs)) {
602 printf("Error mapping channel\n");
603 return -1;
604 }
605
606 }
607 }
608 offset += sizeof(*ievent) + ievent->len;
609 }
610 }
611 #endif //HAS_INOTIFY
612
613 /* read_channels
614 *
615 * Thread worker.
616 *
617 * Read the debugfs channels and write them in the paired tracefiles.
618 *
619 * @fd_pairs : paired channels and trace files.
620 *
621 * returns 0 on success, -1 on error.
622 *
623 * Note that the high priority polled channels are consumed first. We then poll
624 * again to see if these channels are still in priority. Only when no
625 * high priority channel is left, we start reading low priority channels.
626 *
627 * Note that a channel is considered high priority when the buffer is almost
628 * full.
629 */
630
631 int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
632 int inotify_fd, struct inotify_watch_array *iwatch_array)
633 {
634 struct pollfd *pollfd = NULL;
635 int i,j;
636 int num_rdy, num_hup;
637 int high_prio;
638 int ret = 0;
639 int inotify_fds;
640 unsigned int old_num;
641
642 #ifdef HAS_INOTIFY
643 inotify_fds = 1;
644 #else
645 inotify_fds = 0;
646 #endif
647
648 /* Start polling the FD. Keep one fd for inotify */
649 pollfd = malloc((inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd));
650
651 #ifdef HAS_INOTIFY
652 pollfd[0].fd = inotify_fd;
653 pollfd[0].events = POLLIN|POLLPRI;
654 #endif
655
656 for(i=0;i<fd_pairs->num_pairs;i++) {
657 pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
658 pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
659 }
660
661 while(1) {
662 high_prio = 0;
663 num_hup = 0;
664 #ifdef DEBUG
665 printf("Press a key for next poll...\n");
666 char buf[1];
667 read(STDIN_FILENO, &buf, 1);
668 printf("Next poll (polling %d fd) :\n", 1+fd_pairs->num_pairs);
669 #endif //DEBUG
670
671 /* Have we received a signal ? */
672 if(quit_program) break;
673
674 num_rdy = poll(pollfd, inotify_fds+fd_pairs->num_pairs, -1);
675 if(num_rdy == -1) {
676 perror("Poll error");
677 goto free_fd;
678 }
679
680 printf("Data received\n");
681 #ifdef HAS_INOTIFY
682 switch(pollfd[0].revents) {
683 case POLLERR:
684 printf("Error returned in polling inotify fd %d.\n", pollfd[0].fd);
685 break;
686 case POLLHUP:
687 printf("Polling inotify fd %d tells it has hung up.\n", pollfd[0].fd);
688 break;
689 case POLLNVAL:
690 printf("Polling inotify fd %d tells fd is not open.\n", pollfd[0].fd);
691 break;
692 case POLLPRI:
693 case POLLIN:
694 printf("Polling inotify fd %d : data ready.\n", pollfd[0].fd);
695 old_num = fd_pairs->num_pairs;
696 read_inotify(inotify_fd, fd_pairs, iwatch_array);
697 pollfd = realloc(pollfd,
698 (inotify_fds + fd_pairs->num_pairs) * sizeof(struct pollfd));
699 for(i=old_num;i<fd_pairs->num_pairs;i++) {
700 pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
701 pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
702 }
703 break;
704 }
705 #endif
706
707 for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
708 switch(pollfd[i].revents) {
709 case POLLERR:
710 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
711 num_hup++;
712 break;
713 case POLLHUP:
714 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
715 num_hup++;
716 break;
717 case POLLNVAL:
718 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
719 num_hup++;
720 break;
721 case POLLPRI:
722 if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
723 printf("Urgent read on fd %d\n", pollfd[i].fd);
724 /* Take care of high priority channels first. */
725 high_prio = 1;
726 /* it's ok to have an unavailable subbuffer */
727 ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
728 if(ret == EAGAIN) ret = 0;
729
730 ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
731 if(ret)
732 printf("Error in mutex unlock : %s\n", strerror(ret));
733 }
734 break;
735 }
736 }
737 /* If every buffer FD has hung up, we end the read loop here */
738 if(num_hup == fd_pairs->num_pairs) break;
739
740 if(!high_prio) {
741 for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
742 switch(pollfd[i].revents) {
743 case POLLIN:
744 if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
745 /* Take care of low priority channels. */
746 printf("Normal read on fd %d\n", pollfd[i].fd);
747 /* it's ok to have an unavailable subbuffer */
748 ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
749 if(ret == EAGAIN) ret = 0;
750
751 ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
752 if(ret)
753 printf("Error in mutex unlock : %s\n", strerror(ret));
754 }
755 break;
756 }
757 }
758 }
759
760 }
761
762 free_fd:
763 free(pollfd);
764
765 end:
766 return ret;
767 }
768
769
770 void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd,
771 struct inotify_watch_array *iwatch_array)
772 {
773 int i;
774 int ret;
775
776 for(i=0;i<fd_pairs->num_pairs;i++) {
777 ret = close(fd_pairs->pair[i].channel);
778 if(ret == -1) perror("Close error on channel");
779 ret = close(fd_pairs->pair[i].trace);
780 if(ret == -1) perror("Close error on trace");
781 }
782 free(fd_pairs->pair);
783 free(iwatch_array->elem);
784 }
785
786 /* Thread worker */
787 void * thread_main(void *arg)
788 {
789 struct channel_trace_fd fd_pairs = { NULL, 0 };
790 int inotify_fd = -1;
791 struct inotify_watch_array inotify_watch_array = { NULL, 0 };
792 int ret = 0;
793 unsigned int thread_num = (unsigned int)arg;
794
795 inotify_fd = inotify_init();
796
797 if(ret = open_channel_trace_pairs(channel_name, trace_name, &fd_pairs,
798 &inotify_fd, &inotify_watch_array))
799 goto close_channel;
800
801 if(ret = map_channels(&fd_pairs, 0, fd_pairs.num_pairs))
802 goto close_channel;
803
804 ret = read_channels(thread_num, &fd_pairs, inotify_fd, &inotify_watch_array);
805
806 ret |= unmap_channels(&fd_pairs);
807
808 close_channel:
809 close_channel_trace_pairs(&fd_pairs, inotify_fd, &inotify_watch_array);
810 if(inotify_fd >= 0)
811 close(inotify_fd);
812
813 return (void*)ret;
814 }
815
816 int main(int argc, char ** argv)
817 {
818 int ret = 0;
819 struct sigaction act;
820 pthread_t *tids;
821 unsigned int i;
822 void *tret;
823
824 ret = parse_arguments(argc, argv);
825
826 if(ret != 0) show_arguments();
827 if(ret < 0) return EINVAL;
828 if(ret > 0) return 0;
829
830 show_info();
831
832 if(daemon_mode) {
833 ret = daemon(0, 0);
834
835 if(ret == -1) {
836 perror("An error occured while daemonizing.");
837 exit(-1);
838 }
839 }
840
841 /* Connect the signal handlers */
842 act.sa_handler = handler;
843 act.sa_flags = 0;
844 sigemptyset(&(act.sa_mask));
845 sigaddset(&(act.sa_mask), SIGTERM);
846 sigaddset(&(act.sa_mask), SIGQUIT);
847 sigaddset(&(act.sa_mask), SIGINT);
848 sigaction(SIGTERM, &act, NULL);
849 sigaction(SIGQUIT, &act, NULL);
850 sigaction(SIGINT, &act, NULL);
851
852 tids = malloc(sizeof(pthread_t) * num_threads);
853 for(i=0; i<num_threads; i++) {
854
855 ret = pthread_create(&tids[i], NULL, thread_main, (void*)i);
856 if(ret) {
857 perror("Error creating thread");
858 break;
859 }
860 }
861
862 for(i=0; i<num_threads; i++) {
863 ret = pthread_join(tids[i], &tret);
864 if(ret) {
865 perror("Error joining thread");
866 break;
867 }
868 if((int)tret != 0) {
869 printf("Error %s occured in thread %u\n", strerror((int)tret), i);
870 }
871 }
872
873 free(tids);
874
875 return ret;
876 }
This page took 0.044278 seconds and 3 git commands to generate.