mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Processing (Linux): support timeout
This commit is contained in:
@@ -1,10 +1,60 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/processing.h"
|
||||
#include "common/io/io.h"
|
||||
#include "common/time.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/time.h>
|
||||
#include <signal.h>
|
||||
|
||||
#ifdef __linux__
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/poll.h>
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#define FF_WAIT_TIMEOUT 1000
|
||||
|
||||
int waitpid_timeout(pid_t pid, int* status)
|
||||
{
|
||||
if (FF_WAIT_TIMEOUT <= 0)
|
||||
return waitpid(pid, status, 0);
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
FF_AUTO_CLOSE_FD int pidfd = (int) syscall(SYS_pidfd_open, pid, 0);
|
||||
if (pidfd >= 0)
|
||||
{
|
||||
int res = poll(&(struct pollfd) { .events = POLLIN, .fd = pidfd }, 1, FF_WAIT_TIMEOUT);
|
||||
if (res > 0)
|
||||
return (int) waitpid(pid, status, WNOHANG);
|
||||
else if (res == 0)
|
||||
{
|
||||
kill(pid, SIGTERM);
|
||||
return -ETIME;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
uint64_t start = ffTimeGetTick();
|
||||
while (true)
|
||||
{
|
||||
int res = (int) waitpid(pid, status, WNOHANG);
|
||||
if (res != 0)
|
||||
return res;
|
||||
if (ffTimeGetTick() - start < FF_WAIT_TIMEOUT)
|
||||
ffTimeSleep(FF_WAIT_TIMEOUT / 10);
|
||||
else
|
||||
{
|
||||
kill(pid, SIGTERM);
|
||||
return -ETIME;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
|
||||
{
|
||||
@@ -33,8 +83,8 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
|
||||
|
||||
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
|
||||
int status = -1;
|
||||
if(waitpid(childPid, &status, 0) < 0)
|
||||
return "waitpid(childPid, &status, 0) failed";
|
||||
if(waitpid_timeout(childPid, &status) < 0)
|
||||
return "waitpid(childPid, &status) failed";
|
||||
|
||||
if (!WIFEXITED(status))
|
||||
return "WIFEXITED(status) == false";
|
||||
@@ -75,8 +125,8 @@ const char* ffProcessAppendStdErr(FFstrbuf* buffer, char* const argv[])
|
||||
|
||||
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
|
||||
int status = -1;
|
||||
if(waitpid(childPid, &status, WNOHANG) < 0)
|
||||
return "waitpid(childPid, &status, 0) failed";
|
||||
if(waitpid_timeout(childPid, &status) < 0)
|
||||
return "waitpid(childPid, &status) failed";
|
||||
|
||||
if (!WIFEXITED(status))
|
||||
return "WIFEXITED(status) == false";
|
||||
|
||||
Reference in New Issue
Block a user