]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
io: open pipes in non-blocking mode
authorDave Chinner <dchinner@redhat.com>
Wed, 12 Dec 2018 17:42:40 +0000 (11:42 -0600)
committerEric Sandeen <sandeen@redhat.com>
Wed, 12 Dec 2018 17:42:40 +0000 (11:42 -0600)
So that O_RDONLY open commands (such as from copy_range) do not
block forever waiting on a non-existent writer.

Signed-off-by: Dave Chinner <dchinner@redhat.com>
Reviewed-by: Jan Tulak <jtulak@redhat.com>
[sandeen: initialize st per djwong's suggestion]
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
io/open.c

index 6ea3e9a2019f5fd73430933e1641d1c3f1369b01..21c0e054f8d2c604498b16dbffcca2cb281d1e28 100644 (file)
--- a/io/open.c
+++ b/io/open.c
@@ -56,6 +56,7 @@ openfile(
        struct fs_path  *fs_path)
 {
        struct fs_path  *fsp;
+       struct stat     st = { 0 };
        int             fd;
        int             oflags;
 
@@ -79,6 +80,18 @@ openfile(
        if (flags & IO_NOFOLLOW)
                oflags |= O_NOFOLLOW;
 
+       /*
+        * if we've been passed a pipe to open, don't block waiting for a
+        * reader or writer to appear. We want to either succeed or error out
+        * immediately.
+        */
+       if (stat(path, &st) < 0 && errno != ENOENT) {
+               perror("stat");
+               return -1;
+       }
+       if (S_ISFIFO(st.st_mode))
+               oflags |= O_NONBLOCK;
+
        fd = open(path, oflags, mode);
        if (fd < 0) {
                if (errno == EISDIR &&