summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Support
diff options
context:
space:
mode:
authorPavel Labath <labath@google.com>2017-01-24 10:57:01 +0000
committerPavel Labath <labath@google.com>2017-01-24 10:57:01 +0000
commitf726dfa65edebb6827eb5c8f17d028ad7e588b49 (patch)
tree28fe5cb52337e28a3cc1987208f7388b771aa6aa /llvm/lib/Support
parent992ac2d5c2af380ad094bcf6fc38927f7d539194 (diff)
downloadbcm5719-llvm-f726dfa65edebb6827eb5c8f17d028ad7e588b49.tar.gz
bcm5719-llvm-f726dfa65edebb6827eb5c8f17d028ad7e588b49.zip
[Support] Use O_CLOEXEC only when declared
Summary: Use the O_CLOEXEC flag only when it is available. Some old systems (e.g. SLES10) do not support this flag. POSIX explicitly guarantees that this flag can be checked for using #if, so there is no need for a CMake check. In case O_CLOEXEC is not supported, fall back to fcntl(FD_CLOEXEC) instead. Reviewers: rnk, rafael, mgorny Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D28894 llvm-svn: 292912
Diffstat (limited to 'llvm/lib/Support')
-rw-r--r--llvm/lib/Support/Unix/Path.inc22
1 files changed, 20 insertions, 2 deletions
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index f7334245a00..ecc9a2ea8e2 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -587,10 +587,19 @@ std::error_code openFileForRead(const Twine &Name, int &ResultFD,
SmallVectorImpl<char> *RealPath) {
SmallString<128> Storage;
StringRef P = Name.toNullTerminatedStringRef(Storage);
- while ((ResultFD = open(P.begin(), O_RDONLY | O_CLOEXEC)) < 0) {
+ int OpenFlags = O_RDONLY;
+#ifdef O_CLOEXEC
+ OpenFlags |= O_CLOEXEC;
+#endif
+ while ((ResultFD = open(P.begin(), OpenFlags)) < 0) {
if (errno != EINTR)
return std::error_code(errno, std::generic_category());
}
+#ifndef O_CLOEXEC
+ int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
+ (void)r;
+ assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
+#endif
// Attempt to get the real name of the file, if the user asked
if(!RealPath)
return std::error_code();
@@ -624,7 +633,11 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
"Cannot specify both 'excl' and 'append' file creation flags!");
- int OpenFlags = O_CREAT | O_CLOEXEC;
+ int OpenFlags = O_CREAT;
+
+#ifdef O_CLOEXEC
+ OpenFlags |= O_CLOEXEC;
+#endif
if (Flags & F_RW)
OpenFlags |= O_RDWR;
@@ -645,6 +658,11 @@ std::error_code openFileForWrite(const Twine &Name, int &ResultFD,
if (errno != EINTR)
return std::error_code(errno, std::generic_category());
}
+#ifndef O_CLOEXEC
+ int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
+ (void)r;
+ assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
+#endif
return std::error_code();
}
OpenPOWER on IntegriCloud