2021-04-18 15:19:21 +02:00
|
|
|
#include "fastfetch.h"
|
2022-07-25 14:48:59 +02:00
|
|
|
#include "common/processing.h"
|
2023-01-28 12:40:12 +01:00
|
|
|
#include "common/io/io.h"
|
2023-07-10 16:14:04 +08:00
|
|
|
#include "common/time.h"
|
2021-04-18 15:19:21 +02:00
|
|
|
|
2022-06-18 14:25:31 +02:00
|
|
|
#include <stdlib.h>
|
2021-04-18 15:19:21 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/wait.h>
|
2023-07-10 16:14:04 +08:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
|
|
|
|
#ifdef __linux__
|
|
|
|
|
#include <sys/syscall.h>
|
2023-07-10 18:04:21 +08:00
|
|
|
#include <poll.h>
|
2023-07-10 16:14:04 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int waitpid_timeout(pid_t pid, int* status)
|
|
|
|
|
{
|
2023-07-10 22:12:07 +08:00
|
|
|
if (instance.config.processingTimeout <= 0)
|
2023-07-10 16:14:04 +08:00
|
|
|
return waitpid(pid, status, 0);
|
|
|
|
|
|
2023-07-10 22:12:07 +08:00
|
|
|
uint32_t timeout = (uint32_t) instance.config.processingTimeout;
|
|
|
|
|
|
2023-07-10 18:10:59 +08:00
|
|
|
#if defined(__linux__) && defined(SYS_pidfd_open) // musl don't define SYS_pidfd_open
|
2023-07-10 16:14:04 +08:00
|
|
|
|
|
|
|
|
FF_AUTO_CLOSE_FD int pidfd = (int) syscall(SYS_pidfd_open, pid, 0);
|
|
|
|
|
if (pidfd >= 0)
|
|
|
|
|
{
|
2023-07-10 22:12:07 +08:00
|
|
|
int res = poll(&(struct pollfd) { .events = POLLIN, .fd = pidfd }, 1, (int) timeout);
|
2023-07-10 16:14:04 +08:00
|
|
|
if (res > 0)
|
|
|
|
|
return (int) waitpid(pid, status, WNOHANG);
|
|
|
|
|
else if (res == 0)
|
|
|
|
|
{
|
|
|
|
|
kill(pid, SIGTERM);
|
2023-07-10 18:04:21 +08:00
|
|
|
return -62; // -ETIME
|
2023-07-10 16:14:04 +08:00
|
|
|
}
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uint64_t start = ffTimeGetTick();
|
|
|
|
|
while (true)
|
|
|
|
|
{
|
|
|
|
|
int res = (int) waitpid(pid, status, WNOHANG);
|
|
|
|
|
if (res != 0)
|
|
|
|
|
return res;
|
2023-07-10 22:12:07 +08:00
|
|
|
if (ffTimeGetTick() - start < timeout)
|
|
|
|
|
ffTimeSleep(timeout / 10);
|
2023-07-10 16:14:04 +08:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
kill(pid, SIGTERM);
|
2023-07-10 18:04:21 +08:00
|
|
|
return -62; // -ETIME
|
2023-07-10 16:14:04 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-18 15:19:21 +02:00
|
|
|
|
2023-07-10 21:02:13 +08:00
|
|
|
const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
|
2023-07-10 09:36:33 +08:00
|
|
|
{
|
|
|
|
|
int pipes[2];
|
|
|
|
|
|
|
|
|
|
if(pipe(pipes) == -1)
|
|
|
|
|
return "pipe() failed";
|
|
|
|
|
|
|
|
|
|
pid_t childPid = fork();
|
|
|
|
|
if(childPid == -1)
|
|
|
|
|
return "fork() failed";
|
|
|
|
|
|
|
|
|
|
//Child
|
|
|
|
|
if(childPid == 0)
|
|
|
|
|
{
|
2023-07-10 21:02:13 +08:00
|
|
|
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
|
2023-07-10 09:36:33 +08:00
|
|
|
close(pipes[0]);
|
|
|
|
|
close(pipes[1]);
|
2023-07-10 21:02:13 +08:00
|
|
|
close(useStdErr ? STDOUT_FILENO : STDERR_FILENO);
|
2023-07-10 09:36:33 +08:00
|
|
|
execvp(argv[0], argv);
|
|
|
|
|
exit(901);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Parent
|
|
|
|
|
close(pipes[1]);
|
|
|
|
|
|
|
|
|
|
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
|
|
|
|
|
int status = -1;
|
2023-07-10 16:14:04 +08:00
|
|
|
if(waitpid_timeout(childPid, &status) < 0)
|
|
|
|
|
return "waitpid(childPid, &status) failed";
|
2023-07-10 09:36:33 +08:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|