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