aboutsummaryrefslogtreecommitdiff
path: root/butl/fdstream.cxx
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-01-28 01:06:36 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-01-30 20:20:07 +0300
commit20fe8fea93ad2165d3f44453c648179c6ed6bd53 (patch)
tree32f07a56c7306a04f9c58b6fea9663254ce7bfa3 /butl/fdstream.cxx
parentea97d24afdc13d6f8d94918c7f2919b943bba04d (diff)
Make fdopen_pipe(), fdopen(), fdnull() and fddup() to set FD_CLOEXEC flag
Diffstat (limited to 'butl/fdstream.cxx')
-rw-r--r--butl/fdstream.cxx23
1 files changed, 21 insertions, 2 deletions
diff --git a/butl/fdstream.cxx b/butl/fdstream.cxx
index 370a75e..e28eb0f 100644
--- a/butl/fdstream.cxx
+++ b/butl/fdstream.cxx
@@ -658,7 +658,7 @@ namespace butl
of |= O_LARGEFILE;
#endif
- int fd (open (f, of, pf));
+ int fd (open (f, of | O_CLOEXEC, pf));
#else
@@ -745,6 +745,18 @@ namespace butl
{
#ifndef _WIN32
int nfd (dup (fd));
+
+ int f (fcntl (fd, F_GETFD));
+ if (f == -1)
+ throw_ios_failure (errno);
+
+ if ((f & FD_CLOEXEC) != 0)
+ {
+ f = fcntl (nfd, F_GETFD);
+ if (f == -1 || fcntl (nfd, F_SETFD, f | FD_CLOEXEC) == -1)
+ throw_ios_failure (errno);
+ }
+
#else
int nfd (_dup (fd));
#endif
@@ -766,7 +778,7 @@ namespace butl
int
fdnull () noexcept
{
- return open ("/dev/null", O_RDWR);
+ return open ("/dev/null", O_RDWR | O_CLOEXEC);
}
fdstream_mode
@@ -829,6 +841,13 @@ namespace butl
if (pipe (pd) == -1)
throw_ios_failure (errno);
+ for (size_t i (0); i < 2; ++i)
+ {
+ int f (fcntl (pd[i], F_GETFD));
+ if (f == -1 || fcntl (pd[i], F_SETFD, f | FD_CLOEXEC) == -1)
+ throw_ios_failure (errno);
+ }
+
return {auto_fd (pd[0]), auto_fd (pd[1])};
}