From d167ac7305f8a13cb8529a74b7239cc0801fa6ad Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Sun, 15 Sep 2013 08:58:56 +0000 Subject: 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. --- compat-util.h | 14 ++++++++++++++ fadvise.c | 8 +------- mincore.c | 2 +- 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; } -- cgit v1.2.3-24-ge0c7