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

Thiago Macieira thiago.macieira at intel.com
Wed Sep 25 16:33:35 CEST 2024


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

> 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.

> 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.

>      });
>      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?


-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Principal Engineer - Intel DCAI Platform & System Engineering
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 5152 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20240925/a1a40f87/attachment.bin>


More information about the Interest mailing list