mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Processing (Windows): properly implement timed reading
This commit is contained in:
@@ -4,35 +4,55 @@
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
enum { FF_PIPE_BUFSIZ = 4096 };
|
||||
|
||||
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
|
||||
{
|
||||
SECURITY_ATTRIBUTES saAttr = {
|
||||
.nLength = sizeof(SECURITY_ATTRIBUTES),
|
||||
.lpSecurityDescriptor = NULL,
|
||||
.bInheritHandle = TRUE,
|
||||
};
|
||||
int timeout = instance.config.processingTimeout;
|
||||
|
||||
FF_AUTO_CLOSE_FD HANDLE hChildPipeRead = NULL;
|
||||
HANDLE hChildPipeWrite = NULL;
|
||||
if (!CreatePipe(&hChildPipeRead, &hChildPipeWrite, &saAttr, 0))
|
||||
return "CreatePipe() failed";
|
||||
FF_AUTO_CLOSE_FD HANDLE hChildPipeRead = CreateNamedPipeW(
|
||||
L"\\\\.\\pipe\\LOCAL\\",
|
||||
PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE | (timeout < 0 ? 0 : FILE_FLAG_OVERLAPPED),
|
||||
0,
|
||||
1,
|
||||
FF_PIPE_BUFSIZ,
|
||||
FF_PIPE_BUFSIZ,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
if (hChildPipeRead == INVALID_HANDLE_VALUE)
|
||||
return "CreateNamedPipeW(L\"\\\\.\\pipe\\LOCAL\\\") failed";
|
||||
|
||||
if (!SetHandleInformation(hChildPipeRead, HANDLE_FLAG_INHERIT, 0))
|
||||
return "SetHandleInformation(hChildPipeRead) failed";
|
||||
HANDLE hChildPipeWrite = CreateFileW(
|
||||
L"\\\\.\\pipe\\LOCAL\\",
|
||||
GENERIC_WRITE,
|
||||
0,
|
||||
&(SECURITY_ATTRIBUTES){
|
||||
.nLength = sizeof(SECURITY_ATTRIBUTES),
|
||||
.lpSecurityDescriptor = NULL,
|
||||
.bInheritHandle = TRUE,
|
||||
},
|
||||
OPEN_EXISTING,
|
||||
0,
|
||||
NULL
|
||||
);
|
||||
if (hChildPipeWrite == INVALID_HANDLE_VALUE)
|
||||
return "CreateFileW(L\"\\\\.\\pipe\\LOCAL\\\") failed";
|
||||
|
||||
PROCESS_INFORMATION piProcInfo = {0};
|
||||
STARTUPINFOA siStartInfo = {
|
||||
.cb = sizeof(siStartInfo),
|
||||
.dwFlags = STARTF_USESTDHANDLES,
|
||||
};
|
||||
if (useStdErr)
|
||||
siStartInfo.hStdError = hChildPipeWrite;
|
||||
else
|
||||
siStartInfo.hStdOutput = hChildPipeWrite;
|
||||
|
||||
BOOL success;
|
||||
|
||||
{
|
||||
STARTUPINFOA siStartInfo = {
|
||||
.cb = sizeof(siStartInfo),
|
||||
.dwFlags = STARTF_USESTDHANDLES,
|
||||
};
|
||||
if (useStdErr)
|
||||
siStartInfo.hStdError = hChildPipeWrite;
|
||||
else
|
||||
siStartInfo.hStdOutput = hChildPipeWrite;
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY cmdline = ffStrbufCreateF("\"%s\"", argv[0]);
|
||||
for(char* const* parg = &argv[1]; *parg; ++parg)
|
||||
{
|
||||
@@ -58,20 +78,35 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
|
||||
if(!success)
|
||||
return "CreateProcessA() failed";
|
||||
|
||||
if (instance.config.processingTimeout > 0)
|
||||
char str[FF_PIPE_BUFSIZ];
|
||||
DWORD nRead = 0;
|
||||
OVERLAPPED overlapped = {};
|
||||
// ReadFile always completes synchronously if the pipe is not created with FILE_FLAG_OVERLAPPED
|
||||
if (!ReadFile(hChildPipeRead, str, sizeof(str), &nRead, &overlapped))
|
||||
{
|
||||
DWORD ret = WaitForSingleObjectEx(piProcInfo.hProcess, (DWORD) instance.config.processingTimeout, TRUE);
|
||||
if (ret == WAIT_TIMEOUT)
|
||||
if (!GetOverlappedResultEx(hChildPipeRead, &overlapped, &nRead, (DWORD) timeout, TRUE))
|
||||
{
|
||||
if (GetLastError() == ERROR_BROKEN_PIPE)
|
||||
return "Child process closed its end (nothing to read)";
|
||||
CancelIo(hChildPipeRead);
|
||||
TerminateProcess(piProcInfo.hProcess, 1);
|
||||
return "Waiting process timeout";
|
||||
return "GetOverlappedResultEx(hChildPipeRead) failed or timeout";
|
||||
}
|
||||
}
|
||||
while (nRead > 0)
|
||||
{
|
||||
ffStrbufAppendNS(buffer, nRead, str);
|
||||
if (!ReadFile(hChildPipeRead, str, sizeof(str), &nRead, &overlapped))
|
||||
{
|
||||
if (!GetOverlappedResult(hChildPipeRead, &overlapped, &nRead, TRUE))
|
||||
{
|
||||
if (GetLastError() == ERROR_BROKEN_PIPE)
|
||||
return NULL;
|
||||
CancelIo(hChildPipeRead);
|
||||
return "GetOverlappedResult(hChildPipeRead) failed";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char str[1024];
|
||||
DWORD nRead;
|
||||
while(ReadFile(hChildPipeRead, str, sizeof(str), &nRead, NULL) && nRead > 0)
|
||||
ffStrbufAppendNS(buffer, nRead, str);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user