From ea97d24afdc13d6f8d94918c7f2919b943bba04d Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Fri, 27 Jan 2017 19:43:58 +0300 Subject: Check for HANDLE_FLAG_INHERIT presence before setting it for handle --- butl/process.cxx | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'butl/process.cxx') 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; }; -- cgit v1.1