From 257ff9ff793e7f8f69731c91630540088c43c349 Mon Sep 17 00:00:00 2001 From: Philip Pronin Date: Sat, 17 Aug 2013 02:06:35 -0700 Subject: [PATCH] retry flock() if interrupted (EINTR) Test Plan: fbconfig folly/test:file_test && fbmake runtests_opt Reviewed By: soren@fb.com FB internal diff: D932782 --- folly/File.cpp | 10 +++++----- folly/FileUtil.cpp | 5 +++++ folly/FileUtil.h | 1 + 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/folly/File.cpp b/folly/File.cpp index aaf109f0..3c961aaa 100644 --- a/folly/File.cpp +++ b/folly/File.cpp @@ -16,12 +16,12 @@ #include "folly/File.h" -#include #include #include -#include "folly/Format.h" #include "folly/Exception.h" +#include "folly/FileUtil.h" +#include "folly/Format.h" #include "folly/ScopeGuard.h" #include @@ -112,11 +112,11 @@ void File::lock_shared() { doLock(LOCK_SH); } bool File::try_lock_shared() { return doTryLock(LOCK_SH); } void File::doLock(int op) { - checkUnixError(flock(fd_, op), "flock() failed (lock)"); + checkUnixError(flockNoInt(fd_, op), "flock() failed (lock)"); } bool File::doTryLock(int op) { - int r = flock(fd_, op | LOCK_NB); + int r = flockNoInt(fd_, op | LOCK_NB); // flock returns EWOULDBLOCK if already locked if (r == -1 && errno == EWOULDBLOCK) return false; checkUnixError(r, "flock() failed (try_lock)"); @@ -124,7 +124,7 @@ bool File::doTryLock(int op) { } void File::unlock() { - checkUnixError(flock(fd_, LOCK_UN), "flock() failed (unlock)"); + checkUnixError(flockNoInt(fd_, LOCK_UN), "flock() failed (unlock)"); } void File::unlock_shared() { unlock(); } diff --git a/folly/FileUtil.cpp b/folly/FileUtil.cpp index 4a4ca6b6..45c313ff 100644 --- a/folly/FileUtil.cpp +++ b/folly/FileUtil.cpp @@ -20,6 +20,7 @@ #ifdef __APPLE__ #include #endif +#include #include "folly/detail/FileUtilDetail.h" @@ -70,6 +71,10 @@ int truncateNoInt(const char* path, off_t len) { return wrapNoInt(truncate, path, len); } +int flockNoInt(int fd, int operation) { + return wrapNoInt(flock, fd, operation); +} + ssize_t readNoInt(int fd, void* buf, size_t count) { return wrapNoInt(read, fd, buf, count); } diff --git a/folly/FileUtil.h b/folly/FileUtil.h index 81974871..a542e1c9 100644 --- a/folly/FileUtil.h +++ b/folly/FileUtil.h @@ -39,6 +39,7 @@ int fsyncNoInt(int fd); int fdatasyncNoInt(int fd); int ftruncateNoInt(int fd, off_t len); int truncateNoInt(const char* path, off_t len); +int flockNoInt(int fd, int operation); ssize_t readNoInt(int fd, void* buf, size_t n); ssize_t preadNoInt(int fd, void* buf, size_t n, off_t offset); -- 2.34.1