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

christoph at cullmann.io christoph at cullmann.io
Wed Sep 25 15:17:11 CEST 2024


Hi,

I want to ensure only the standard handles are inherited.

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

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::CreateProcessArguments 
*args) {
         // only keep the handles that we have in args->startupInfo
         SIZE_T size = 0;
         LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList = NULL;
         UR_ENSURE(InitializeProcThreadAttributeList(NULL, 1, 0, &size) 
|| (GetLastError() == ERROR_INSUFFICIENT_BUFFER));
         UR_ENSURE((lpAttributeList = 
reinterpret_cast<LPPROC_THREAD_ATTRIBUTE_LIST>(HeapAlloc(GetProcessHeap(), 
0, size))));
         UR_ENSURE(InitializeProcThreadAttributeList(lpAttributeList, 1, 
0, &size));
         HANDLE handlesToInherit[] = {args->startupInfo->hStdInput, 
args->startupInfo->hStdOutput, args->startupInfo->hStdError};
         UR_ENSURE(UpdateProcThreadAttribute(lpAttributeList, 0, 
PROC_THREAD_ATTRIBUTE_HANDLE_LIST, handlesToInherit, 
sizeof(handlesToInherit), NULL, NULL));

         info.StartupInfo = *(args->startupInfo);
         info.StartupInfo.cb = sizeof(info);
         info.lpAttributeList = lpAttributeList;
         args->flags |= EXTENDED_STARTUPINFO_PRESENT;
         args->startupInfo = &info.StartupInfo;
     });
     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)

Greetings
Christoph


More information about the Interest mailing list