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
|
|
|
|
|
|
|
|
|
|
#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);
|
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;
|
|
|
|
|
if (ffTimeGetTick() - start < FF_WAIT_TIMEOUT)
|
|
|
|
|
ffTimeSleep(FF_WAIT_TIMEOUT / 10);
|
|
|
|
|
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
|
|
|
|
2022-09-21 16:03:51 +08:00
|
|
|
const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[])
|
2021-04-18 15:19:21 +02:00
|
|
|
{
|
|
|
|
|
int pipes[2];
|
|
|
|
|
|
|
|
|
|
if(pipe(pipes) == -1)
|
2022-09-21 16:03:51 +08:00
|
|
|
return "pipe() failed";
|
2021-04-18 15:19:21 +02:00
|
|
|
|
|
|
|
|
pid_t childPid = fork();
|
|
|
|
|
if(childPid == -1)
|
2022-09-21 16:03:51 +08:00
|
|
|
return "fork() failed";
|
2021-04-18 15:19:21 +02:00
|
|
|
|
2022-03-17 16:52:19 +01:00
|
|
|
//Child
|
2021-04-18 15:19:21 +02:00
|
|
|
if(childPid == 0)
|
|
|
|
|
{
|
|
|
|
|
dup2(pipes[1], STDOUT_FILENO);
|
|
|
|
|
close(pipes[0]);
|
|
|
|
|
close(pipes[1]);
|
2021-05-01 21:27:46 +02:00
|
|
|
close(STDERR_FILENO);
|
2021-04-18 15:19:21 +02:00
|
|
|
execvp(argv[0], argv);
|
|
|
|
|
exit(901);
|
|
|
|
|
}
|
2022-03-17 16:52:19 +01:00
|
|
|
|
|
|
|
|
//Parent
|
|
|
|
|
close(pipes[1]);
|
2022-09-21 16:03:51 +08:00
|
|
|
|
2023-02-23 11:13:50 +08:00
|
|
|
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-02-23 11:13:50 +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;
|
2021-04-18 15:19:21 +02:00
|
|
|
}
|
2023-07-10 09:36:33 +08:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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;
|
|
|
|
|
}
|