[llvm] r334293 - Add a file open flag that disables O_CLOEXEC.

Zachary Turner via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 8 08:15:56 PDT 2018


Author: zturner
Date: Fri Jun  8 08:15:56 2018
New Revision: 334293

URL: http://llvm.org/viewvc/llvm-project?rev=334293&view=rev
Log:
Add a file open flag that disables O_CLOEXEC.

O_CLOEXEC is the right default, but occasionally you don't
want this.  This is especially true for tools like debuggers
where you might need to spawn the child process with specific
files already open, but it's occasionally useful in other
scenarios as well, like when you want to do some IPC between
parent and child.

Modified:
    llvm/trunk/include/llvm/Support/FileSystem.h
    llvm/trunk/lib/Support/Unix/Path.inc
    llvm/trunk/lib/Support/Windows/Path.inc

Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=334293&r1=334292&r2=334293&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Fri Jun  8 08:15:56 2018
@@ -717,7 +717,11 @@ enum OpenFlags : unsigned {
   F_Append = 2, // For compatibility
 
   /// Delete the file on close. Only makes a difference on windows.
-  OF_Delete = 4
+  OF_Delete = 4,
+
+  /// When a child process is launched, this file should remain open in the
+  /// child process.
+  OF_ChildInherit = 8,
 };
 
 /// Create a uniquely named file.

Modified: llvm/trunk/lib/Support/Unix/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Unix/Path.inc?rev=334293&r1=334292&r2=334293&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Unix/Path.inc (original)
+++ llvm/trunk/lib/Support/Unix/Path.inc Fri Jun  8 08:15:56 2018
@@ -753,7 +753,8 @@ static int nativeOpenFlags(CreationDispo
     Result |= O_APPEND;
 
 #ifdef O_CLOEXEC
-  Result |= O_CLOEXEC;
+  if (!(Flags & OF_ChildInherit))
+    Result |= O_CLOEXEC;
 #endif
 
   return Result;
@@ -770,9 +771,11 @@ std::error_code openFile(const Twine &Na
       0)
     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");
+  if (!(Flags & OF_ChildInherit)) {
+    int r = fcntl(ResultFD, F_SETFD, FD_CLOEXEC);
+    (void)r;
+    assert(r == 0 && "fcntl(F_SETFD, FD_CLOEXEC) failed");
+  }
 #endif
   return std::error_code();
 }

Modified: llvm/trunk/lib/Support/Windows/Path.inc
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Windows/Path.inc?rev=334293&r1=334292&r2=334293&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Windows/Path.inc (original)
+++ llvm/trunk/lib/Support/Windows/Path.inc Fri Jun  8 08:15:56 2018
@@ -1104,15 +1104,21 @@ static DWORD nativeAccess(FileAccess Acc
 
 static std::error_code openNativeFileInternal(const Twine &Name,
                                               file_t &ResultFile, DWORD Disp,
-                                              DWORD Access, DWORD Flags) {
+                                              DWORD Access, DWORD Flags,
+                                              bool Inherit = false) {
   SmallVector<wchar_t, 128> PathUTF16;
   if (std::error_code EC = widenPath(Name, PathUTF16))
     return EC;
 
+  SECURITY_ATTRIBUTES SA;
+  SA.nLength = sizeof(SA);
+  SA.lpSecurityDescriptor = nullptr;
+  SA.bInheritHandle = Inherit;
+
   HANDLE H =
       ::CreateFileW(PathUTF16.begin(), Access,
-                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
-                    NULL, Disp, Flags, NULL);
+                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &SA,
+                    Disp, Flags, NULL);
   if (H == INVALID_HANDLE_VALUE) {
     DWORD LastError = ::GetLastError();
     std::error_code EC = mapWindowsError(LastError);
@@ -1140,9 +1146,13 @@ Expected<file_t> openNativeFile(const Tw
   DWORD NativeDisp = nativeDisposition(Disp, Flags);
   DWORD NativeAccess = nativeAccess(Access, Flags);
 
+  bool Inherit = false;
+  if (Flags & OF_ChildInherit)
+    Inherit = true;
+
   file_t Result;
-  std::error_code EC = openNativeFileInternal(Name, Result, NativeDisp,
-                                              NativeAccess, NativeFlags);
+  std::error_code EC = openNativeFileInternal(
+      Name, Result, NativeDisp, NativeAccess, NativeFlags, Inherit);
   if (EC)
     return errorCodeToError(EC);
   return Result;




More information about the llvm-commits mailing list