raw_fd_ostream: Make file handles non-inheritable by default

Summary:
This makes the file descriptors on unix platform non-inheritable (O_CLOEXEC).

There is no change in behavior on windows, as the handles were already
non-inheritable there.

Reviewers: rnk, rafael

Subscribers: llvm-commits, mgorny

Differential Revision: https://reviews.llvm.org/D28854

llvm-svn: 292401
diff --git a/llvm/lib/Support/Unix/Path.inc b/llvm/lib/Support/Unix/Path.inc
index e0b11aa..0bf9dba 100644
--- a/llvm/lib/Support/Unix/Path.inc
+++ b/llvm/lib/Support/Unix/Path.inc
@@ -577,7 +577,7 @@
                                 SmallVectorImpl<char> *RealPath) {
   SmallString<128> Storage;
   StringRef P = Name.toNullTerminatedStringRef(Storage);
-  while ((ResultFD = open(P.begin(), O_RDONLY)) < 0) {
+  while ((ResultFD = open(P.begin(), O_RDONLY | O_CLOEXEC)) < 0) {
     if (errno != EINTR)
       return std::error_code(errno, std::generic_category());
   }
@@ -614,7 +614,7 @@
   assert((!(Flags & sys::fs::F_Excl) || !(Flags & sys::fs::F_Append)) &&
          "Cannot specify both 'excl' and 'append' file creation flags!");
 
-  int OpenFlags = O_CREAT;
+  int OpenFlags = O_CREAT | O_CLOEXEC;
 
   if (Flags & F_RW)
     OpenFlags |= O_RDWR;