ScopedFd: Don't use TEMP_FAILURE_RETRY() with close()
According to the comments in Posix_close(), TEMP_FAILURE_RETRY() should
not be used with close():
https://android.googlesource.com/platform/libcore/+/462bdac45c10f43d88d8f07f6994e272a27c14a2%5E%21/#F12
Bug: http://b/20501816
Change-Id: Ie283f848c4fe50fcde9358c8ed307ec048e70892
Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
(cherry picked from commit 315ac19f69d5ca232ddc4a73b4d4088ac6c65e47)
diff --git a/include/nativehelper/ScopedFd.h b/include/nativehelper/ScopedFd.h
index 5ef41f8..d14e62b 100644
--- a/include/nativehelper/ScopedFd.h
+++ b/include/nativehelper/ScopedFd.h
@@ -18,7 +18,7 @@
#define SCOPED_FD_H_included
#include <unistd.h>
-#include "JNIHelp.h" // for TEMP_FAILURE_RETRY
+#include "JNIHelp.h" // for DISALLOW_COPY_AND_ASSIGN.
// A smart pointer that closes the given fd on going out of scope.
// Use this when the fd is incidental to the purpose of your function,
@@ -44,7 +44,10 @@
void reset(int new_fd = -1) {
if (fd_ != -1) {
- TEMP_FAILURE_RETRY(close(fd_));
+ // Even if close(2) fails with EINTR, the fd will have been closed.
+ // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone else's fd.
+ // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
+ close(fd_);
}
fd_ = new_fd;
}