From 76797253acd5061c0c734b1f28f0ad1deb862a74 Mon Sep 17 00:00:00 2001 From: Jens Axboe Date: Wed, 26 Apr 2006 15:28:10 +0200 Subject: [PATCH] [PATCH] Formalize input/output pipe checking --- ktee-net.c | 8 +------- ktee.c | 8 +------- splice-in.c | 6 +----- splice-net.c | 8 +------- splice-out.c | 8 +------- splice.h | 31 +++++++++++++++++++++++++++++++ vmsplice.c | 8 +------- 7 files changed, 37 insertions(+), 40 deletions(-) diff --git a/ktee-net.c b/ktee-net.c index 766531e..3ae2529 100644 --- a/ktee-net.c +++ b/ktee-net.c @@ -10,7 +10,6 @@ #include #include #include -#include #include #include #include @@ -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 dda5c08..a01211b 100644 --- a/ktee.c +++ b/ktee.c @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -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) diff --git a/splice-in.c b/splice-in.c index 82c47c4..14b31f1 100644 --- a/splice-in.c +++ b/splice-in.c @@ -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) diff --git a/splice-net.c b/splice-net.c index c7df534..2c117d9 100644 --- a/splice-net.c +++ b/splice-net.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #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]); diff --git a/splice-out.c b/splice-out.c index b388b86..fe62b51 100644 --- a/splice-out.c +++ b/splice-out.c @@ -5,7 +5,6 @@ #include #include #include -#include #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) diff --git a/splice.h b/splice.h index 5b8c254..7befe3d 100644 --- a/splice.h +++ b/splice.h @@ -2,6 +2,7 @@ #define SPLICE_H #include +#include #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 diff --git a/vmsplice.c b/vmsplice.c index 72befbe..5c0b1ed 100644 --- a/vmsplice.c +++ b/vmsplice.c @@ -9,7 +9,6 @@ #include #include #include -#include #include #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)); -- 2.25.1