[PATCH] D28894: [Support] Use O_CLOEXEC only when declared

Pavel Labath via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 23 03:16:26 PST 2017


labath updated this revision to Diff 85338.
labath edited the summary of this revision.
labath added a comment.

Add FD_CLOEXEC fallback.


https://reviews.llvm.org/D28894

Files:
  lib/Support/Unix/Path.inc


Index: lib/Support/Unix/Path.inc
===================================================================
--- lib/Support/Unix/Path.inc
+++ lib/Support/Unix/Path.inc
@@ -577,10 +577,19 @@
                                 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();
@@ -614,7 +623,11 @@
   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;
@@ -635,6 +648,11 @@
     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();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28894.85338.patch
Type: text/x-patch
Size: 1536 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170123/826aeb39/attachment.bin>


More information about the llvm-commits mailing list