about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc12
-rw-r--r--src/libutil/util.hh3
2 files changed, 15 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 31322f9c4894..842cf3ea47b7 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -8,6 +8,7 @@
 #include <cstring>
 
 #include <sys/wait.h>
+#include <unistd.h>
 #include <fcntl.h>
 #include <limits.h>
 
@@ -683,6 +684,8 @@ void Pipe::create()
     if (pipe(fds) != 0) throw SysError("creating pipe");
     readSide = fds[0];
     writeSide = fds[1];
+    closeOnExec(readSide);
+    closeOnExec(writeSide);
 }
 
 
@@ -934,6 +937,15 @@ void closeMostFDs(const set<int> & exceptions)
 }
 
 
+void closeOnExec(int fd)
+{
+    int prev;
+    if ((prev = fcntl(fd, F_GETFD, 0)) == -1 ||
+        fcntl(fd, F_SETFD, prev | FD_CLOEXEC) == -1)
+        throw SysError("setting close-on-exec flag");
+}
+
+
 void quickExit(int status)
 {
     _exit(status);
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index a1cf68e69d1c..ee0f3862a872 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -258,6 +258,9 @@ string runProgram(Path program, bool searchPath = false,
    listed in the given set.  Good practice in child processes. */
 void closeMostFDs(const set<int> & exceptions);
 
+/* Set the close-on-exec flag for the given file descriptor. */
+void closeOnExec(int fd);
+
 /* Wrapper around _exit() on Unix and ExitProcess() on Windows.  (On
    Cygwin, _exit() doesn't seem to do the right thing.) */
 void quickExit(int status);