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