about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-06-06 07:19:55 +0000
committerEric Wong <normalperson@yhbt.net>2010-06-06 07:21:30 +0000
commitcd76457a8eec40a0de8cb2bc779c5bfb08b9d7b3 (patch)
treecc99b6f8a0b70b558a568a7b32eb357db929f712
parent8794037db3e88fc03d8bc33d603a0b39a69ddf97 (diff)
downloadclogger-cd76457a8eec40a0de8cb2bc779c5bfb08b9d7b3.tar.gz
Sometimes programmers can blindly set O_NONBLOCK on everything
in sight.  However, O_NONBLOCK has no effect on regular files,
so ignore it and allow the raw file descriptor to be used.
-rw-r--r--ext/clogger_ext/clogger.c12
1 files changed, 11 insertions, 1 deletions
diff --git a/ext/clogger_ext/clogger.c b/ext/clogger_ext/clogger.c
index 838f37c..834debe 100644
--- a/ext/clogger_ext/clogger.c
+++ b/ext/clogger_ext/clogger.c
@@ -260,7 +260,17 @@ static int raw_fd(VALUE my_fd)
         if (flags < 0)
                 rb_sys_fail("fcntl");
 
-        return (flags & O_NONBLOCK) ? -1 : fd;
+        if (flags & O_NONBLOCK) {
+                struct stat sb;
+
+                if (fstat(fd, &sb) < 0)
+                        return -1;
+
+                /* O_NONBLOCK is no-op for regular files: */
+                if (! S_ISREG(sb.st_mode))
+                        return -1;
+        }
+        return fd;
 #else /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */
         return -1;
 #endif /* platforms w/o fcntl/F_GETFL/O_NONBLOCK */