[PATCH] Formalize input/output pipe checking
authorJens Axboe <axboe@suse.de>
Wed, 26 Apr 2006 13:28:10 +0000 (15:28 +0200)
committerJens Axboe <axboe@suse.de>
Wed, 26 Apr 2006 13:28:10 +0000 (15:28 +0200)
ktee-net.c
ktee.c
splice-in.c
splice-net.c
splice-out.c
splice.h
vmsplice.c

index 766531eaa68f020505d6e684af46b3f8e81ecec0..3ae25296e5f9b2bd9de1aabbb67e815cea539b09 100644 (file)
@@ -10,7 +10,6 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <netdb.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <limits.h>
@@ -41,18 +40,13 @@ int main(int argc, char *argv[])
 {
        struct sockaddr_in addr;
        char *p, *hname;
-       struct stat sb;
        int fd;
 
        if (argc < 2)
                return usage(argv[0]);
 
-       if (fstat(STDIN_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdin must be a pipe\n");
+       if (check_input_pipe())
                return usage(argv[0]);
-       }
 
        hname = strdup(argv[1]);
        p = strstr(hname, ":");
diff --git a/ktee.c b/ktee.c
index dda5c086ca21bfff61f5bde6ae6484fde16f7351..a01211b0e832194f1c04c45188a899481443ed2c 100644 (file)
--- a/ktee.c
+++ b/ktee.c
@@ -8,7 +8,6 @@
 #include <string.h>
 #include <fcntl.h>
 #include <string.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <errno.h>
 #include <assert.h>
@@ -38,18 +37,13 @@ static int usage(char *name)
 
 int main(int argc, char *argv[])
 {
-       struct stat sb;
        int fd;
 
        if (argc < 2)
                return usage(argv[0]);
 
-       if (fstat(STDIN_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdout must be a pipe\n");
+       if (check_input_pipe())
                return usage(argv[0]);
-       }
 
        fd = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0644);
        if (fd < 0)
index 82c47c44587e953105a9771eae6a7fd7b74340f7..14b31f198619deec2ec031eb634cbedc4cf2e258 100644 (file)
@@ -24,12 +24,8 @@ int main(int argc, char *argv[])
        if (argc < 2)
                return usage(argv[0]);
 
-       if (fstat(STDOUT_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdout must be a pipe\n");
+       if (check_output_pipe())
                return usage(argv[0]);
-       }
 
        fd = open(argv[1], O_RDONLY);
        if (fd < 0)
index c7df53487e8fa2372fea36cc093a82cb33bf18f2..2c117d9d379d7f6205becfcba693302b7cb7442d 100644 (file)
@@ -12,7 +12,6 @@
 #include <arpa/inet.h>
 #include <string.h>
 #include <sys/time.h>
-#include <sys/stat.h>
 #include <errno.h>
 
 #include "splice.h"
@@ -28,17 +27,12 @@ int main(int argc, char *argv[])
        struct sockaddr_in addr;
        unsigned short port;
        int fd, ret;
-       struct stat sb;
 
        if (argc < 3)
                return usage(argv[0]);
 
-       if (fstat(STDIN_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdin must be a pipe\n");
+       if (check_input_pipe())
                return usage(argv[0]);
-       }
 
        port = atoi(argv[2]);
 
index b388b86b3b56cb4781c5b5e42caaf42dba9f15aa..fe62b5191f32961c4a11af2d4edb3e4424db9b1b 100644 (file)
@@ -5,7 +5,6 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <sys/stat.h>
 
 #include "splice.h"
 
@@ -37,15 +36,10 @@ static int parse_options(int argc, char *argv[])
 
 int main(int argc, char *argv[])
 {
-       struct stat sb;
        int fd, index;
 
-       if (fstat(STDIN_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdin must be a pipe\n");
+       if (check_input_pipe())
                return usage(argv[0]);
-       }
 
        index = parse_options(argc, argv);
        if (index == -1 || index + 1 > argc)
index 5b8c2541ce4d75ac27f74de5ba2ddfa707c087b3..7befe3d5755559de4ea376300064b1a2565336f9 100644 (file)
--- a/splice.h
+++ b/splice.h
@@ -2,6 +2,7 @@
 #define SPLICE_H
 
 #include <sys/uio.h>
+#include <sys/stat.h>
 
 #if defined(__i386__)
 #define __NR_splice    313
@@ -74,4 +75,34 @@ static inline int error(const char *n)
        return -1;
 }
 
+static int __check_pipe(int pfd)
+{
+       struct stat sb;
+
+       if (fstat(pfd, &sb) < 0)
+               return error("stat");
+       if (!S_ISFIFO(sb.st_mode))
+               return 1;
+
+       return 0;
+}
+
+static inline int check_input_pipe(void)
+{
+       if (!__check_pipe(STDIN_FILENO))
+               return 0;
+
+       fprintf(stderr, "stdin must be a pipe\n");
+       return 1;
+}
+
+static inline int check_output_pipe(void)
+{
+       if (!__check_pipe(STDOUT_FILENO))
+               return 0;
+
+       fprintf(stderr, "stdout must be a pipe\n");
+       return 1;
+}
+
 #endif
index 72befbe3869430569612bf4a336e49f4374e9f05..5c0b1edf1b433989e048dca5cba9ad3233149764 100644 (file)
@@ -9,7 +9,6 @@
 #include <string.h>
 #include <getopt.h>
 #include <sys/poll.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 
 #include "splice.h"
@@ -95,17 +94,12 @@ static int parse_options(int argc, char *argv[])
 int main(int argc, char *argv[])
 {
        unsigned char *b1, *b2;
-       struct stat sb;
 
        if (parse_options(argc, argv) < 0)
                return usage(argv[0]);
 
-       if (fstat(STDOUT_FILENO, &sb) < 0)
-               return error("stat");
-       if (!S_ISFIFO(sb.st_mode)) {
-               fprintf(stderr, "stdout must be a pipe\n");
+       if (check_output_pipe())
                return usage(argv[0]);
-       }
 
        b1 = ALIGN(malloc(SPLICE_SIZE + align_mask));
        b2 = ALIGN(malloc(SPLICE_SIZE + align_mask));