about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2013-09-15 08:58:56 +0000
committerEric Wong <normalperson@yhbt.net>2013-09-15 08:58:56 +0000
commitd167ac7305f8a13cb8529a74b7239cc0801fa6ad (patch)
treee836c61642d009fc5c3710360bfbd408843eb43e
parenta0eb4997e3fe992f8600fde7c59be033afd36adf (diff)
downloadpcu-d167ac7305f8a13cb8529a74b7239cc0801fa6ad.tar.gz
open_noatime wrapper to deal with EPERM on O_NOATIME
We favor O_NOATIME in a variety of places, use it when we can,
but fall back gracefully when we cannot.
-rw-r--r--compat-util.h14
-rw-r--r--fadvise.c8
-rw-r--r--mincore.c2
3 files changed, 16 insertions, 8 deletions
diff --git a/compat-util.h b/compat-util.h
index d3520e1..60616e2 100644
--- a/compat-util.h
+++ b/compat-util.h
@@ -56,4 +56,18 @@ static inline off_t cstr_to_off_t(const char *nptr, char **endptr, int base)
         exit(1);
 }
 
+static inline int open_noatime(const char *path)
+{
+        int flags = O_RDONLY | O_NOATIME;
+        int fd = open(path, flags);
+
+        if (fd < 0) {
+                if (errno == EPERM && O_NOATIME != 0)
+                        fd = open(path, O_RDONLY);
+                return fd;
+        }
+
+        return fd;
+}
+
 #endif /* OS_COMPAT_H */
diff --git a/fadvise.c b/fadvise.c
index b4b85d7..72ef3a3 100644
--- a/fadvise.c
+++ b/fadvise.c
@@ -30,16 +30,10 @@ static void apply_fadvise(const char *path, off_t offset, off_t len, int advice)
 {
         int fd;
 
-        if ((fd = open(path, O_RDONLY|O_NOATIME)) < 0) {
-                if (errno == EPERM && O_NOATIME != 0) {
-                        fd = open(path, O_RDONLY);
-                        if (fd >= 0)
-                                goto ok;
-                }
+        if ((fd = open_noatime(path) < 0)) {
                 fprintf(stderr, "%s: open(): %s\n", path, strerror(errno));
                 return;
         }
-ok:
 
         if (len <= 0) {
                 /* for compatibility with kernels < 2.6.6 */
diff --git a/mincore.c b/mincore.c
index cafb63d..0028aea 100644
--- a/mincore.c
+++ b/mincore.c
@@ -20,7 +20,7 @@ static void mincore_stats(const char *path, off_t offset, off_t len)
         static const char *fmt = sizeof(void *) == 8 ?
                                  "%s: %016lx %x\n": "%s: %08lx %x\n";
 
-        if ((fd = open(path, O_RDONLY|O_NOATIME)) < 0) {
+        if ((fd = open_noatime(path) < 0)) {
                 fprintf(stderr, "%s: open(): %s\n", path, strerror(errno));
                 return;
         }