[PATCH] Enable use of FIFO as input
authorJens Axboe <jens.axboe@oracle.com>
Wed, 20 Dec 2006 13:00:54 +0000 (14:00 +0100)
committerJens Axboe <jens.axboe@oracle.com>
Wed, 20 Dec 2006 13:00:54 +0000 (14:00 +0100)
Just reuse the stdin stuff for handling a named pipe.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
blkparse.c

index 0d2ea1281fcbfa1c61d8c3fd065dab12b4b64aa0..cbb422f59a076cc230221ce3e743853e04810e6a 100644 (file)
@@ -282,6 +282,7 @@ static unsigned int bit_alloc_cache;
 static unsigned int rb_batch = RB_BATCH_DEFAULT;
 
 static int pipeline;
+static char *pipename;
 
 static int text_output = 1;
 
@@ -2331,18 +2332,12 @@ static int do_file(void)
        return 0;
 }
 
-static int do_stdin(void)
+static void do_pipe(int fd)
 {
        unsigned long long youngest;
-       int fd, events, fdblock;
+       int events, fdblock;
 
        last_allowed_time = -1ULL;
-       fd = dup(STDIN_FILENO);
-       if (fd == -1) {
-               perror("dup stdin");
-               return -1;
-       }
-
        fdblock = -1;
        while ((events = read_events(fd, 0, &fdblock)) > 0) {
                read_sequence++;
@@ -2362,7 +2357,23 @@ static int do_stdin(void)
 
        if (rb_sort_entries)
                show_entries_rb(1);
+}
+
+static int do_fifo(void)
+{
+       int fd;
+
+       if (!strcmp(pipename, "-"))
+               fd = dup(STDIN_FILENO);
+       else
+               fd = open(pipename, O_RDONLY);
+
+       if (fd == -1) {
+               perror("dup stdin");
+               return -1;
+       }
 
+       do_pipe(fd);
        close(fd);
        return 0;
 }
@@ -2428,6 +2439,18 @@ static int find_stopwatch_interval(char *string)
        return 0;
 }
 
+static int is_pipe(const char *str)
+{
+       struct stat st;
+
+       if (!strcmp(str, "-"))
+               return 1;
+       if (!stat(str, &st) && S_ISFIFO(st.st_mode))
+               return 1;
+
+       return 0;
+}
+
 #define S_OPTS  "a:A:b:D:d:f:F:hi:o:Oqstw:vV"
 static char usage_str[] =    "\n\n" \
        "-i <file>           | --input=<file>\n" \
@@ -2501,9 +2524,10 @@ int main(int argc, char *argv[])
                        act_mask_tmp = i;
                        break;
                case 'i':
-                       if (!strcmp(optarg, "-") && !pipeline)
+                       if (is_pipe(optarg) && !pipeline) {
                                pipeline = 1;
-                       else if (resize_devices(optarg) != 0)
+                               pipename = strdup(optarg);
+                       } else if (resize_devices(optarg) != 0)
                                return 1;
                        break;
                case 'D':
@@ -2559,9 +2583,10 @@ int main(int argc, char *argv[])
        }
 
        while (optind < argc) {
-               if (!strcmp(argv[optind], "-") && !pipeline)
+               if (is_pipe(argv[optind]) && !pipeline) {
                        pipeline = 1;
-               else if (resize_devices(argv[optind]) != 0)
+                       pipename = strdup(optarg);
+               } else if (resize_devices(argv[optind]) != 0)
                        return 1;
                optind++;
        }
@@ -2621,7 +2646,7 @@ int main(int argc, char *argv[])
        }
 
        if (pipeline)
-               ret = do_stdin();
+               ret = do_fifo();
        else
                ret = do_file();