Processing (Windows): support timeout

This commit is contained in:
李通洲
2023-07-10 21:02:13 +08:00
parent 9c9ba2f1a1
commit d53b584ba2
3 changed files with 29 additions and 113 deletions
+13 -2
View File
@@ -5,7 +5,18 @@
#include "util/FFstrbuf.h"
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]);
const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[]);
#define FF_WAIT_TIMEOUT 1000
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr);
static inline const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
{
return ffProcessAppendOutput(buffer, argv, false);
}
static inline const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[])
{
return ffProcessAppendOutput(buffer, argv, true);
}
#endif
+3 -47
View File
@@ -14,8 +14,6 @@
#include <poll.h>
#endif
#define FF_WAIT_TIMEOUT 1000
int waitpid_timeout(pid_t pid, int* status)
{
if (FF_WAIT_TIMEOUT <= 0)
@@ -55,7 +53,7 @@ int waitpid_timeout(pid_t pid, int* status)
}
}
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
{
int pipes[2];
@@ -69,52 +67,10 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
//Child
if(childPid == 0)
{
dup2(pipes[1], STDOUT_FILENO);
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
close(pipes[0]);
close(pipes[1]);
close(STDERR_FILENO);
execvp(argv[0], argv);
exit(901);
}
//Parent
close(pipes[1]);
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
int status = -1;
if(waitpid_timeout(childPid, &status) < 0)
return "waitpid(childPid, &status) failed";
if (!WIFEXITED(status))
return "WIFEXITED(status) == false";
if(WEXITSTATUS(status) == 901)
return "WEXITSTATUS(status) == 901 ( execvp failed )";
if(!ffAppendFDBuffer(childPipeFd, buffer))
return "ffAppendFDBuffer(childPipeFd, buffer) failed";
return NULL;
}
const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[])
{
int pipes[2];
if(pipe(pipes) == -1)
return "pipe() failed";
pid_t childPid = fork();
if(childPid == -1)
return "fork() failed";
//Child
if(childPid == 0)
{
dup2(pipes[1], STDERR_FILENO);
close(pipes[0]);
close(pipes[1]);
close(STDOUT_FILENO);
close(useStdErr ? STDOUT_FILENO : STDERR_FILENO);
execvp(argv[0], argv);
exit(901);
}
+13 -64
View File
@@ -1,9 +1,10 @@
#include "fastfetch.h"
#include "common/processing.h"
#include "common/io/io.h"
#include <Windows.h>
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
{
SECURITY_ATTRIBUTES saAttr = {
.nLength = sizeof(SECURITY_ATTRIBUTES),
@@ -11,7 +12,8 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
.bInheritHandle = TRUE,
};
HANDLE hChildPipeRead, hChildPipeWrite;
FF_AUTO_CLOSE_FD HANDLE hChildPipeRead = NULL;
HANDLE hChildPipeWrite = NULL;
if (!CreatePipe(&hChildPipeRead, &hChildPipeWrite, &saAttr, 0))
return "CreatePipe() failed";
@@ -22,8 +24,11 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
STARTUPINFOA siStartInfo = {
.cb = sizeof(siStartInfo),
.dwFlags = STARTF_USESTDHANDLES,
.hStdOutput = hChildPipeWrite,
};
if (useStdErr)
siStartInfo.hStdError = hChildPipeWrite;
else
siStartInfo.hStdOutput = hChildPipeWrite;
BOOL success;
@@ -51,71 +56,16 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
CloseHandle(hChildPipeWrite);
if(!success)
{
CloseHandle(hChildPipeRead);
return "CreateProcessA() failed";
}
char str[1024];
DWORD nRead;
while(ReadFile(hChildPipeRead, str, sizeof(str), &nRead, NULL) && nRead > 0)
ffStrbufAppendNS(buffer, nRead, str);
CloseHandle(hChildPipeRead);
return NULL;
}
const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[])
{
SECURITY_ATTRIBUTES saAttr = {
.nLength = sizeof(SECURITY_ATTRIBUTES),
.lpSecurityDescriptor = NULL,
.bInheritHandle = TRUE,
};
HANDLE hChildPipeRead, hChildPipeWrite;
if (!CreatePipe(&hChildPipeRead, &hChildPipeWrite, &saAttr, 0))
return "CreatePipe() failed";
if (!SetHandleInformation(hChildPipeRead, HANDLE_FLAG_INHERIT, 0))
return "SetHandleInformation(hChildPipeRead) failed";
PROCESS_INFORMATION piProcInfo = {0};
STARTUPINFOA siStartInfo = {
.cb = sizeof(siStartInfo),
.dwFlags = STARTF_USESTDHANDLES,
.hStdError = hChildPipeWrite,
};
BOOL success;
if (FF_WAIT_TIMEOUT > 0)
{
FF_STRBUF_AUTO_DESTROY cmdline = ffStrbufCreateF("\"%s\"", argv[0]);
for(char* const* parg = &argv[1]; *parg; ++parg)
DWORD ret = WaitForSingleObjectEx(piProcInfo.hProcess, FF_WAIT_TIMEOUT, TRUE);
if (ret == WAIT_TIMEOUT)
{
ffStrbufAppendC(&cmdline, ' ');
ffStrbufAppendS(&cmdline, *parg);
TerminateProcess(piProcInfo.hProcess, 1);
return "Waiting process timeout";
}
success = CreateProcessA(
NULL, // application name
cmdline.chars, // command line
NULL, // process security attributes
NULL, // primary thread security attributes
TRUE, // handles are inherited
0, // creation flags
NULL, // use parent's environment
NULL, // use parent's current directory
&siStartInfo, // STARTUPINFO pointer
&piProcInfo // receives PROCESS_INFORMATION
);
}
CloseHandle(hChildPipeWrite);
if(!success)
{
CloseHandle(hChildPipeRead);
return "CreateProcessA() failed";
}
char str[1024];
@@ -123,6 +73,5 @@ const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[])
while(ReadFile(hChildPipeRead, str, sizeof(str), &nRead, NULL) && nRead > 0)
ffStrbufAppendNS(buffer, nRead, str);
CloseHandle(hChildPipeRead);
return NULL;
}