[Interest] Enforce only stdin/out/err inherited with QProcess

christoph at cullmann.io christoph at cullmann.io
Wed Sep 25 17:36:47 CEST 2024


On 2024-09-25 16:33, Thiago Macieira wrote:
> On Wednesday 25 September 2024 06:17:11 GMT-7 christoph at cullmann.io 
> wrote:
>> Hi,
>> 
>> I want to ensure only the standard handles are inherited.
> 
> Hello Cristoph

Hi Thiago,

thanks for the fast response!

> 
>> For Unix this seems to work fine with:
>> 
>>      QProcess::UnixProcessParameters params;
>>      params.flags = QProcess::UnixProcessFlag::CloseFileDescriptors;
>>      params.lowestFileDescriptorToClose = 0;
>>      m_process.setUnixProcessParameters(params);
>>      m_process.start(....
> 
> lowestFileDescriptorToClose defaults to 0, so you don't have to set it. 
> If
> you're going to set, I expect you to set it to something higher than
> STDERR_FILENO + 1 (which is the minimum), for whatever your need is.

You are right, I could have kept the default, like promised in the docs 
even
with the 0 the standard handles survive and the normal Qt QProcess 
read/... stuff
works like intended.

> 
>> For Windows I tried something like:
>> 
>> https://devblogs.microsoft.com/oldnewthing/20111216-00/?p=8873
>> 
>> via: (the UR_ENSURE stuff just checks if return value is sane)
>> 
>>      STARTUPINFOEX info;
>>      ZeroMemory(&info, sizeof(info));
>> 
>> m_process.setCreateProcessArgumentsModifier([&info](QProcess::CreateProcessA
>> rguments *args) {
> ...
>>          args->startupInfo = &info.StartupInfo;
> 
> You don't need to pass your own startupInfo here. At least you made 
> sure it's
> valid around the CreateProcess call, so no dangling pointer, which was 
> my first
> worry. But args should have a valid startupInfo pointer you can modify.

You are right, there is a valid startupInfo, but that is no 
STARTUPINFOEX struct
and only that one allows to have the explicit list of handles to inherit 
that
I tried to use. (at least if I didn't read the types wrong)

> 
>>      });
>>      m_process.start(....
>> 
>> But with that I end up without any valid handles that can be used to 
>> get
>> output from the QProcess m_process.
>> 
>> Any pointers how to do that correctly would be very welcome. (or if I
>> just missed to used the proper Qt API for that)
> 
> Since you want no handles but the standard three, would setting
>   args->inheritHandles = false
> suffice?

I tried that but for me that fails, I have no handles and unlike on 
Linux
or Mac with the other code path I can no longer read any output from the 
QProcess.

I did read a bit the Windows docs and the Qt code and its seems 
inheritHandles = TRUE
is required if you use the STARTF_USESTDHANDLES flag in the startup info 
like Qt does.

https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfoa

Greetings
Christoph

> 
> 
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> https://lists.qt-project.org/listinfo/interest


More information about the Interest mailing list