aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-01-27 19:43:58 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-01-27 19:43:58 +0300
commitea97d24afdc13d6f8d94918c7f2919b943bba04d (patch)
tree3c14f88d4c18bc326c54b42c372f0c2d7181e2e2
parentb2a76e026d69385779e15febe1516abc911522c9 (diff)
Check for HANDLE_FLAG_INHERIT presence before setting it for handle
-rw-r--r--butl/process.cxx20
1 files changed, 12 insertions, 8 deletions
diff --git a/butl/process.cxx b/butl/process.cxx
index 6a0dfa2..526966e 100644
--- a/butl/process.cxx
+++ b/butl/process.cxx
@@ -874,15 +874,19 @@ namespace butl
if (h == INVALID_HANDLE_VALUE)
fail ("unable to obtain file handle");
- // SetHandleInformation() fails for standard handles. We assume they are
- // inherited by default.
+ // Make the handle inheritable by the child unless it is already
+ // inheritable.
//
- if (fd != STDIN_FILENO && fd != STDOUT_FILENO && fd != STDERR_FILENO)
- {
- if (!SetHandleInformation (
- h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
- fail ();
- }
+ DWORD f;
+ if (!GetHandleInformation (h, &f))
+ fail ();
+
+ // Note that the flag check is essential as SetHandleInformation() fails
+ // for standard handles and their duplicates.
+ //
+ if ((f & HANDLE_FLAG_INHERIT) == 0 &&
+ !SetHandleInformation (h, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
+ fail ();
return h;
};