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