]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include: add close_fd() for noticing write errors before close()
authorSami Kerola <kerolasa@iki.fi>
Sat, 13 Apr 2013 19:54:44 +0000 (20:54 +0100)
committerKarel Zak <kzak@redhat.com>
Fri, 26 Apr 2013 11:26:06 +0000 (13:26 +0200)
Essentially this helper function is similar to close_stream(), but for
file descriptors.

When a file descriptors are close()'d status of write is often
overlooked.  The close_fd() will try to determine what happen to writes
with fsync() before closing the file descriptor.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/closestream.h

index d61b83b5ec4c7da22abad642d3612943940000d8..2535c8b22eb1cbc8b8c76d31297e50b2421ba465 100644 (file)
@@ -48,4 +48,23 @@ close_stdout(void)
                _exit(EXIT_FAILURE);
 }
 
+#ifndef HAVE_FSYNC
+static inline int
+fsync(int fd __attribute__((__unused__)))
+{
+       return 0;
+}
+#endif
+
+static inline int
+close_fd(int fd)
+{
+       const int fsync_fail = (fsync(fd) != 0);
+       const int close_fail = (close(fd) != 0);
+
+       if (fsync_fail || close_fail)
+               return EOF;
+       return 0;
+}
+
 #endif /* UTIL_LINUX_CLOSESTREAM_H */