about summary refs log tree commit diff
path: root/src/libutil/util.cc
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2012-03-05T19·29+0100
committerEelco Dolstra <e.dolstra@tudelft.nl>2012-03-05T19·29+0100
commit35355fc1fcffbe859395e360c0a6a1463f137d63 (patch)
tree0d5ed221e64ca64303c2f88dcf68cc054ba5e8e6 /src/libutil/util.cc
parent7b22bec252a155514d811a8e2acc21562ac02bd4 (diff)
Set the close-on-exec flag on file descriptors
Diffstat (limited to 'src/libutil/util.cc')
-rw-r--r--src/libutil/util.cc12
1 files changed, 12 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);