|
| 1 | +#include "NonInheritingProcess.hpp" |
| 2 | + |
| 3 | +#ifdef Q_OS_WIN |
| 4 | +#ifndef WIN32_LEAN_AND_MEAN |
| 5 | +#define WIN32_LEAN_AND_MEAN |
| 6 | +#endif |
| 7 | +#ifndef NOMINMAX |
| 8 | +#define NOMINMAX |
| 9 | +#endif |
| 10 | +#ifndef _UNICODE |
| 11 | +#define _UNICODE |
| 12 | +#endif |
| 13 | +#ifdef _WIN32_WINNT |
| 14 | +#undef _WIN32_WINNT |
| 15 | +#endif |
| 16 | +#define _WIN32_WINNT 0x0601 |
| 17 | +#include <windows.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +#include <memory> |
| 21 | +#include <functional> |
| 22 | + |
| 23 | +#include "pimpl_impl.hpp" |
| 24 | + |
| 25 | +namespace |
| 26 | +{ |
| 27 | +#ifdef Q_OS_WIN |
| 28 | + struct start_info_deleter |
| 29 | + { |
| 30 | + void operator () (STARTUPINFOEXW * si) |
| 31 | + { |
| 32 | + if (si->lpAttributeList) |
| 33 | + { |
| 34 | + ::DeleteProcThreadAttributeList (si->lpAttributeList); |
| 35 | + } |
| 36 | + delete si; |
| 37 | + } |
| 38 | + }; |
| 39 | +#endif |
| 40 | +} |
| 41 | + |
| 42 | +class NonInheritingProcess::impl |
| 43 | +{ |
| 44 | +public: |
| 45 | +#ifdef Q_OS_WIN |
| 46 | + void extend_CreateProcessArguments (QProcess::CreateProcessArguments * args) |
| 47 | + { |
| 48 | + // |
| 49 | + // Here we modify the CreateProcessArguments structure to use a |
| 50 | + // STARTUPINFOEX extended argument to CreateProcess. In that we |
| 51 | + // set up a list of handles for the new process to inherit. By |
| 52 | + // doing this we stop all inherited handles from being |
| 53 | + // inherited. Unfortunately UpdateProcThreadAttribute does not let |
| 54 | + // us set up an empty handle list, so we populate the list with |
| 55 | + // the three standard stream handles that QProcess::start has set |
| 56 | + // up as Pipes to do IPC. Even though these Pipe handles are |
| 57 | + // created with inheritance disabled, UpdateProcThreadAtribute and |
| 58 | + // CreateProcess don't seem to mind, which suits us fine. |
| 59 | + // |
| 60 | + // Note: that we cannot just clear the inheritHandles flag as that |
| 61 | + // stops the standard stream handles being inherited which breaks |
| 62 | + // our IPC using std(in|out|err). Only be using a |
| 63 | + // PROC_THREAD_ATTRIBUTE_HANDLE_LIST attribute in a STARTUPINFOEX |
| 64 | + // structure can we avoid the all or nothing behaviour of |
| 65 | + // CreateProcess /w respect to handle inheritance. |
| 66 | + // |
| 67 | + BOOL fSuccess; |
| 68 | + SIZE_T size {0}; |
| 69 | + LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = nullptr; |
| 70 | + ::InitializeProcThreadAttributeList (nullptr, 1, 0, &size); |
| 71 | + lpAttributeList = reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST> (::HeapAlloc (::GetProcessHeap (), 0, size)); |
| 72 | + fSuccess = !!lpAttributeList; |
| 73 | + if (fSuccess) |
| 74 | + { |
| 75 | + fSuccess = ::InitializeProcThreadAttributeList (lpAttributeList, 1, 0, &size); |
| 76 | + } |
| 77 | + if (fSuccess) |
| 78 | + { |
| 79 | + // empty list of handles |
| 80 | + fSuccess = ::UpdateProcThreadAttribute (lpAttributeList, 0, |
| 81 | + PROC_THREAD_ATTRIBUTE_HANDLE_LIST, |
| 82 | + &args->startupInfo->hStdInput, 3 * sizeof (HANDLE), |
| 83 | + nullptr, 0); |
| 84 | + } |
| 85 | + if (fSuccess) |
| 86 | + { |
| 87 | + start_info_.reset (new STARTUPINFOEXW); |
| 88 | + start_info_->StartupInfo = *args->startupInfo; |
| 89 | + start_info_->StartupInfo.cb = sizeof (STARTUPINFOEXW); |
| 90 | + start_info_->lpAttributeList = lpAttributeList; |
| 91 | + args->startupInfo = reinterpret_cast<Q_STARTUPINFO*> (start_info_.get ()); |
| 92 | + args->flags |= EXTENDED_STARTUPINFO_PRESENT; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + using start_info_type = std::unique_ptr<STARTUPINFOEXW, start_info_deleter>; |
| 97 | + start_info_type start_info_; |
| 98 | +#endif |
| 99 | +}; |
| 100 | + |
| 101 | +NonInheritingProcess::NonInheritingProcess (QObject * parent) |
| 102 | + : QProcess {parent} |
| 103 | +{ |
| 104 | +#ifdef Q_OS_WIN |
| 105 | + using namespace std::placeholders; |
| 106 | + |
| 107 | + // enable cleanup after process starts or fails to start |
| 108 | + connect (this, &QProcess::started, [this] {m_->start_info_.reset ();}); |
| 109 | + connect (this, &QProcess::errorOccurred, [this] (QProcess::ProcessError) {m_->start_info_.reset ();}); |
| 110 | + setCreateProcessArgumentsModifier (std::bind (&NonInheritingProcess::impl::extend_CreateProcessArguments, &*m_, _1)); |
| 111 | +#endif |
| 112 | +} |
| 113 | + |
| 114 | +NonInheritingProcess::~NonInheritingProcess () |
| 115 | +{ |
| 116 | +} |
0 commit comments