update
[ltt-control.git] / ltt-control / lttd / lttd.c
CommitLineData
617de8e1 1/* lttd
2 *
3 * Linux Trace Toolkit Daemon
4 *
c2ffa20f 5 * This is a simple daemon that reads a few relay+debugfs channels and save
6 * them in a trace.
617de8e1 7 *
8 *
9 * Copyright 2005 -
10 * Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
11 */
12
0bb647f5 13#ifdef HAVE_CONFIG_H
469206ed 14#include <config.h>
0bb647f5 15#endif
16
e54e1d5d 17#define _REENTRANT
0bb647f5 18#define _GNU_SOURCE
e54e1d5d 19#include <features.h>
617de8e1 20#include <stdio.h>
1d483eea 21#include <unistd.h>
617de8e1 22#include <errno.h>
23#include <sys/types.h>
24#include <sys/stat.h>
617de8e1 25#include <stdlib.h>
26#include <dirent.h>
27#include <string.h>
90ccaa9a 28#include <fcntl.h>
29#include <sys/poll.h>
1d483eea 30#include <sys/mman.h>
31#include <signal.h>
e54e1d5d 32#include <pthread.h>
357915bb 33#include <sys/syscall.h>
34#include <unistd.h>
35#include <asm/ioctls.h>
36
37#include <linux/version.h>
1d483eea 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. */
766632ac 44#define RELAY_GET_SUBBUF _IOR(0xF5, 0x00,__u32)
1d483eea 45/* Release the oldest reserved (by "get") sub buffer. */
766632ac 46#define RELAY_PUT_SUBBUF _IOW(0xF5, 0x01,__u32)
1d483eea 47/* returns the number of sub buffers in the per cpu channel. */
766632ac 48#define RELAY_GET_N_SUBBUFS _IOR(0xF5, 0x02,__u32)
1d483eea 49/* returns the size of the sub buffers. */
766632ac 50#define RELAY_GET_SUBBUF_SIZE _IOR(0xF5, 0x03,__u32)
1d483eea 51
357915bb 52#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,14)
53#include <linux/inotify.h>
54/* From the inotify-tools 2.6 package */
55static inline int inotify_init (void)
56{
57 return syscall (__NR_inotify_init);
58}
59
60static 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
65static 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
71static inline int inotify_init (void)
72{
73 return -1;
74}
1d483eea 75
357915bb 76static inline int inotify_add_watch (int fd, const char *name, __u32 mask)
77{
78 return 0;
79}
80
81static inline int inotify_rm_watch (int fd, __u32 wd)
82{
83 return 0;
84}
85#undef HAS_INOTIFY
86#endif
617de8e1 87
88enum {
89 GET_SUBBUF,
90 PUT_SUBBUF,
91 GET_N_BUBBUFS,
92 GET_SUBBUF_SIZE
93};
94
95struct fd_pair {
96 int channel;
97 int trace;
1d483eea 98 unsigned int n_subbufs;
99 unsigned int subbuf_size;
100 void *mmap;
5ffb77aa 101 pthread_mutex_t mutex;
617de8e1 102};
103
104struct channel_trace_fd {
105 struct fd_pair *pair;
106 int num_pairs;
107};
108
357915bb 109struct inotify_watch {
110 int wd;
111 char path_channel[PATH_MAX];
112 char path_trace[PATH_MAX];
113};
114
115struct inotify_watch_array {
116 struct inotify_watch *elem;
117 int num;
118};
119
89565b43 120static char *trace_name = NULL;
121static char *channel_name = NULL;
122static int daemon_mode = 0;
123static int append_mode = 0;
124static unsigned long num_threads = 1;
1d483eea 125volatile static int quit_program = 0; /* For signal handler */
89565b43 126static int dump_flight_only = 0;
127static int dump_normal_only = 0;
617de8e1 128
129/* Args :
130 *
131 * -t directory Directory name of the trace to write to. Will be created.
c2ffa20f 132 * -c directory Root directory of the debugfs trace channels.
617de8e1 133 * -d Run in background (daemon).
90ccaa9a 134 * -a Trace append mode.
ce2b5176 135 * -s Send SIGUSR1 to parent when ready for IO.
617de8e1 136 */
137void 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");
c2ffa20f 143 printf("-c directory Root directory of the debugfs trace channels.\n");
617de8e1 144 printf("-d Run in background (daemon).\n");
90ccaa9a 145 printf("-a Append to an possibly existing trace.\n");
5ffb77aa 146 printf("-N Number of threads to start.\n");
89565b43 147 printf("-f Dump only flight recorder channels.\n");
148 printf("-n Dump only normal channels.\n");
617de8e1 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 */
160int 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
90ccaa9a 171 while(argn < argc) {
617de8e1 172
173 switch(argv[argn][0]) {
174 case '-':
175 switch(argv[argn][1]) {
176 case 't':
90ccaa9a 177 if(argn+1 < argc) {
178 trace_name = argv[argn+1];
179 argn++;
180 }
617de8e1 181 break;
182 case 'c':
90ccaa9a 183 if(argn+1 < argc) {
184 channel_name = argv[argn+1];
185 argn++;
186 }
617de8e1 187 break;
188 case 'd':
189 daemon_mode = 1;
190 break;
90ccaa9a 191 case 'a':
192 append_mode = 1;
193 break;
5ffb77aa 194 case 'N':
e54e1d5d 195 if(argn+1 < argc) {
196 num_threads = strtoul(argv[argn+1], NULL, 0);
197 argn++;
198 }
199 break;
89565b43 200 case 'f':
201 dump_flight_only = 1;
202 break;
203 case 'n':
204 dump_normal_only = 1;
205 break;
617de8e1 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
235void show_info(void)
236{
15061ecb 237 printf("Linux Trace Toolkit Trace Daemon " VERSION "\n");
617de8e1 238 printf("\n");
c2ffa20f 239 printf("Reading from debugfs directory : %s\n", channel_name);
617de8e1 240 printf("Writing to trace directory : %s\n", trace_name);
241 printf("\n");
242}
243
244
1d483eea 245/* signal handling */
246
247static void handler(int signo)
248{
249 printf("Signal %d received : exiting cleanly\n", signo);
250 quit_program = 1;
251}
252
253
357915bb 254int 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 }
313end:
314 return open_ret;
315}
1d483eea 316
617de8e1 317int open_channel_trace_pairs(char *subchannel_name, char *subtrace_name,
357915bb 318 struct channel_trace_fd *fd_pairs, int *inotify_fd,
319 struct inotify_watch_array *iwatch_array)
617de8e1 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;
002f91bb 331 int open_ret = 0;
617de8e1 332
333 if(channel_dir == NULL) {
334 perror(subchannel_name);
d304b1dd 335 open_ret = ENOENT;
002f91bb 336 goto end;
617de8e1 337 }
338
617de8e1 339 printf("Creating trace subdirectory %s\n", subtrace_name);
340 ret = mkdir(subtrace_name, S_IRWXU|S_IRWXG|S_IRWXO);
341 if(ret == -1) {
b1e3e7c7 342 if(errno != EEXIST) {
90ccaa9a 343 perror(subtrace_name);
002f91bb 344 open_ret = -1;
d304b1dd 345 goto end;
90ccaa9a 346 }
617de8e1 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
357915bb 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
617de8e1 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");
357915bb 391 ret = open_channel_trace_pairs(path_channel, path_trace, fd_pairs,
392 inotify_fd, iwatch_array);
617de8e1 393 if(ret < 0) continue;
90ccaa9a 394 } else if(S_ISREG(stat_buf.st_mode)) {
357915bb 395 open_ret = open_buffer_file(entry->d_name, path_channel, path_trace,
396 fd_pairs);
397 if(open_ret)
398 goto end;
617de8e1 399 }
617de8e1 400 }
401
d304b1dd 402end:
617de8e1 403 closedir(channel_dir);
404
d304b1dd 405 return open_ret;
617de8e1 406}
407
1d483eea 408
409int read_subbuffer(struct fd_pair *pair)
410{
4f31148b 411 unsigned int consumed_old;
5ffb77aa 412 int err, ret=0;
1d483eea 413
414
c2ffa20f 415 err = ioctl(pair->channel, RELAY_GET_SUBBUF,
4f31148b 416 &consumed_old);
417 printf("cookie : %u\n", consumed_old);
469206ed 418 if(err != 0) {
5ffb77aa 419 ret = errno;
30478a4d 420 perror("Reserving sub buffer failed (everything is normal, it is due to concurrency)");
469206ed 421 goto get_error;
1d483eea 422 }
469206ed 423
424 err = TEMP_FAILURE_RETRY(write(pair->trace,
e4bed64a 425 pair->mmap
426 + (consumed_old & ((pair->n_subbufs * pair->subbuf_size)-1)),
1d483eea 427 pair->subbuf_size));
469206ed 428
429 if(err < 0) {
5ffb77aa 430 ret = errno;
1d483eea 431 perror("Error in writing to file");
469206ed 432 goto write_error;
1d483eea 433 }
a7eb8aa2 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
469206ed 442write_error:
c2ffa20f 443 err = ioctl(pair->channel, RELAY_PUT_SUBBUF, &consumed_old);
469206ed 444 if(err != 0) {
5ffb77aa 445 ret = errno;
30478a4d 446 if(errno == EFAULT) {
5ffb77aa 447 perror("Error in unreserving sub buffer\n");
30478a4d 448 } else if(errno == EIO) {
4f31148b 449 perror("Reader has been pushed by the writer, last subbuffer corrupted.");
ec8cce5a 450 /* FIXME : we may delete the last written buffer if we wish. */
4f31148b 451 }
469206ed 452 goto get_error;
1d483eea 453 }
454
469206ed 455get_error:
456 return ret;
1d483eea 457}
458
459
617de8e1 460
357915bb 461int map_channels(struct channel_trace_fd *fd_pairs,
462 int idx_begin, int idx_end)
617de8e1 463{
1d483eea 464 int i,j;
e54e1d5d 465 int ret=0;
1d483eea 466
469206ed 467 if(fd_pairs->num_pairs <= 0) {
468 printf("No channel to read\n");
469 goto end;
470 }
471
1d483eea 472 /* Get the subbuf sizes and number */
473
357915bb 474 for(i=idx_begin;i<idx_end;i++) {
1d483eea 475 struct fd_pair *pair = &fd_pairs->pair[i];
90ccaa9a 476
c2ffa20f 477 ret = ioctl(pair->channel, RELAY_GET_N_SUBBUFS,
1d483eea 478 &pair->n_subbufs);
479 if(ret != 0) {
480 perror("Error in getting the number of subbuffers");
481 goto end;
482 }
c2ffa20f 483 ret = ioctl(pair->channel, RELAY_GET_SUBBUF_SIZE,
1d483eea 484 &pair->subbuf_size);
485 if(ret != 0) {
486 perror("Error in getting the size of the subbuffers");
487 goto end;
488 }
5ffb77aa 489 ret = pthread_mutex_init(&pair->mutex, NULL); /* Fast mutex */
490 if(ret != 0) {
491 perror("Error in mutex init");
492 goto end;
493 }
1d483eea 494 }
495
496 /* Mmap each FD */
357915bb 497 for(i=idx_begin;i<idx_end;i++) {
1d483eea 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
5ffb77aa 508 goto end; /* success */
1d483eea 509
e54e1d5d 510 /* Error handling */
511 /* munmap only the successfully mmapped indexes */
512munmap:
513 /* Munmap each FD */
357915bb 514 for(j=idx_begin;j<i;j++) {
e54e1d5d 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
525end:
526 return ret;
527
528
529}
530
531
532int 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;
5ffb77aa 547 err_ret = pthread_mutex_destroy(&pair->mutex);
548 if(err_ret != 0) {
549 perror("Error in mutex destroy");
550 }
551 ret |= err_ret;
e54e1d5d 552 }
553
554 return ret;
555}
556
357915bb 557#ifdef HAS_INOTIFY
558/* Inotify event arrived.
559 *
560 * Only support add file for now.
561 */
562
563int 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
e54e1d5d 612
613/* read_channels
5ffb77aa 614 *
615 * Thread worker.
e54e1d5d 616 *
c2ffa20f 617 * Read the debugfs channels and write them in the paired tracefiles.
e54e1d5d 618 *
619 * @fd_pairs : paired channels and trace files.
620 *
357915bb 621 * returns 0 on success, -1 on error.
e54e1d5d 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
357915bb 631int read_channels(unsigned int thread_num, struct channel_trace_fd *fd_pairs,
632 int inotify_fd, struct inotify_watch_array *iwatch_array)
e54e1d5d 633{
357915bb 634 struct pollfd *pollfd = NULL;
e54e1d5d 635 int i,j;
636 int num_rdy, num_hup;
637 int high_prio;
5ffb77aa 638 int ret = 0;
357915bb 639 int inotify_fds;
640 unsigned int old_num;
e54e1d5d 641
357915bb 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
90ccaa9a 655
656 for(i=0;i<fd_pairs->num_pairs;i++) {
357915bb 657 pollfd[inotify_fds+i].fd = fd_pairs->pair[i].channel;
658 pollfd[inotify_fds+i].events = POLLIN|POLLPRI;
90ccaa9a 659 }
357915bb 660
90ccaa9a 661 while(1) {
4f45ea55 662 high_prio = 0;
1d483eea 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);
357915bb 668 printf("Next poll (polling %d fd) :\n", 1+fd_pairs->num_pairs);
1d483eea 669#endif //DEBUG
357915bb 670
1d483eea 671 /* Have we received a signal ? */
672 if(quit_program) break;
673
357915bb 674 num_rdy = poll(pollfd, inotify_fds+fd_pairs->num_pairs, -1);
90ccaa9a 675 if(num_rdy == -1) {
676 perror("Poll error");
1d483eea 677 goto free_fd;
90ccaa9a 678 }
679
680 printf("Data received\n");
357915bb 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
90ccaa9a 706
357915bb 707 for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
90ccaa9a 708 switch(pollfd[i].revents) {
709 case POLLERR:
710 printf("Error returned in polling fd %d.\n", pollfd[i].fd);
1d483eea 711 num_hup++;
90ccaa9a 712 break;
713 case POLLHUP:
714 printf("Polling fd %d tells it has hung up.\n", pollfd[i].fd);
1d483eea 715 num_hup++;
90ccaa9a 716 break;
717 case POLLNVAL:
718 printf("Polling fd %d tells fd is not open.\n", pollfd[i].fd);
1d483eea 719 num_hup++;
90ccaa9a 720 break;
721 case POLLPRI:
357915bb 722 if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
5ffb77aa 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 */
357915bb 727 ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
30478a4d 728 if(ret == EAGAIN) ret = 0;
cdad9787 729
357915bb 730 ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
5ffb77aa 731 if(ret)
732 printf("Error in mutex unlock : %s\n", strerror(ret));
733 }
90ccaa9a 734 break;
4f45ea55 735 }
90ccaa9a 736 }
357915bb 737 /* If every buffer FD has hung up, we end the read loop here */
1d483eea 738 if(num_hup == fd_pairs->num_pairs) break;
90ccaa9a 739
4f45ea55 740 if(!high_prio) {
357915bb 741 for(i=inotify_fds;i<inotify_fds+fd_pairs->num_pairs;i++) {
4f45ea55 742 switch(pollfd[i].revents) {
743 case POLLIN:
357915bb 744 if(pthread_mutex_trylock(&fd_pairs->pair[i-inotify_fds].mutex) == 0) {
5ffb77aa 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 */
357915bb 748 ret = read_subbuffer(&fd_pairs->pair[i-inotify_fds]);
30478a4d 749 if(ret == EAGAIN) ret = 0;
cdad9787 750
357915bb 751 ret = pthread_mutex_unlock(&fd_pairs->pair[i-inotify_fds].mutex);
5ffb77aa 752 if(ret)
753 printf("Error in mutex unlock : %s\n", strerror(ret));
754 }
4f45ea55 755 break;
756 }
757 }
90ccaa9a 758 }
759
760 }
761
1d483eea 762free_fd:
90ccaa9a 763 free(pollfd);
764
1d483eea 765end:
357915bb 766 return ret;
617de8e1 767}
768
769
357915bb 770void close_channel_trace_pairs(struct channel_trace_fd *fd_pairs, int inotify_fd,
771 struct inotify_watch_array *iwatch_array)
617de8e1 772{
90ccaa9a 773 int i;
774 int ret;
617de8e1 775
90ccaa9a 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);
357915bb 783 free(iwatch_array->elem);
784}
785
786/* Thread worker */
787void * 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
808close_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;
617de8e1 814}
815
816int main(int argc, char ** argv)
817{
e54e1d5d 818 int ret = 0;
1d483eea 819 struct sigaction act;
5ffb77aa 820 pthread_t *tids;
821 unsigned int i;
822 void *tret;
617de8e1 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) {
2253bb8e 833 ret = daemon(0, 0);
002f91bb 834
835 if(ret == -1) {
836 perror("An error occured while daemonizing.");
837 exit(-1);
838 }
839 }
617de8e1 840
1d483eea 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
5ffb77aa 852 tids = malloc(sizeof(pthread_t) * num_threads);
853 for(i=0; i<num_threads; i++) {
ae410d24 854
357915bb 855 ret = pthread_create(&tids[i], NULL, thread_main, (void*)i);
5ffb77aa 856 if(ret) {
857 perror("Error creating thread");
858 break;
859 }
860 }
617de8e1 861
5ffb77aa 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) {
a7eb8aa2 869 printf("Error %s occured in thread %u\n", strerror((int)tret), i);
5ffb77aa 870 }
871 }
872
873 free(tids);
874
617de8e1 875 return ret;
876}
This page took 0.060751 seconds and 4 git commands to generate.