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>
|
2023-07-10 16:14:04 +08:00
|
|
|
#include <signal.h>
|
2023-07-11 11:10:10 +08:00
|
|
|
#include <poll.h>
|
2023-07-11 20:28:59 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <errno.h>
|
2023-07-15 08:25:57 +08:00
|
|
|
#include <sys/wait.h>
|
2023-07-11 20:28:59 +08:00
|
|
|
|
2023-12-04 15:10:58 +08:00
|
|
|
enum { FF_PIPE_BUFSIZ = 8192 };
|
2021-04-18 15:19:21 +02:00
|
|
|
|
2023-11-17 22:49:28 +01:00
|
|
|
static inline void waitpid_wrapper(const pid_t* pid)
|
2023-07-15 00:33:18 +08:00
|
|
|
{
|
|
|
|
|
// remove zombie processes
|
|
|
|
|
if (*pid > 0)
|
|
|
|
|
waitpid(*pid, NULL, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-29 19:28:28 +09:00
|
|
|
static inline int ffPipe2(int *fds, int flags)
|
|
|
|
|
{
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
|
if(pipe(fds) == -1)
|
|
|
|
|
return -1;
|
|
|
|
|
fcntl(fds[0], F_SETFL, fcntl(fds[0], F_GETFL) | flags);
|
|
|
|
|
fcntl(fds[1], F_SETFL, fcntl(fds[1], F_GETFL) | flags);
|
|
|
|
|
return 0;
|
|
|
|
|
#else
|
|
|
|
|
return pipe2(fds, flags);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
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];
|
2023-11-29 19:28:28 +09:00
|
|
|
const int timeout = instance.config.general.processingTimeout;
|
2023-07-10 09:36:33 +08:00
|
|
|
|
2023-11-29 19:28:28 +09:00
|
|
|
if(ffPipe2(pipes, O_CLOEXEC) == -1)
|
2023-07-10 09:36:33 +08:00
|
|
|
return "pipe() failed";
|
|
|
|
|
|
2023-07-15 00:33:18 +08:00
|
|
|
__attribute__((__cleanup__(waitpid_wrapper))) pid_t childPid = fork();
|
2023-07-10 09:36:33 +08:00
|
|
|
if(childPid == -1)
|
2023-11-29 19:28:28 +09:00
|
|
|
{
|
|
|
|
|
close(pipes[0]);
|
|
|
|
|
close(pipes[1]);
|
2023-07-10 09:36:33 +08:00
|
|
|
return "fork() failed";
|
2023-11-29 19:28:28 +09:00
|
|
|
}
|
2023-07-10 09:36:33 +08:00
|
|
|
|
|
|
|
|
//Child
|
|
|
|
|
if(childPid == 0)
|
|
|
|
|
{
|
2023-12-04 15:10:58 +08:00
|
|
|
int nullFile = open("/dev/null", O_WRONLY | O_CLOEXEC);
|
2023-07-10 21:02:13 +08:00
|
|
|
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
|
2023-10-31 14:03:24 +08:00
|
|
|
dup2(nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
|
2023-09-27 15:02:55 +08:00
|
|
|
setenv("LANG", "C", 1);
|
2023-07-10 09:36:33 +08:00
|
|
|
execvp(argv[0], argv);
|
2023-11-29 19:28:28 +09:00
|
|
|
_exit(127);
|
2023-07-10 09:36:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Parent
|
|
|
|
|
close(pipes[1]);
|
|
|
|
|
|
|
|
|
|
int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
|
2023-11-29 19:28:28 +09:00
|
|
|
char str[FF_PIPE_BUFSIZ];
|
2023-07-11 20:28:59 +08:00
|
|
|
|
2023-11-29 19:28:28 +09:00
|
|
|
while(1)
|
2023-07-11 11:10:10 +08:00
|
|
|
{
|
2023-07-11 20:28:59 +08:00
|
|
|
if (timeout >= 0)
|
2023-07-11 11:10:10 +08:00
|
|
|
{
|
2023-07-11 20:28:59 +08:00
|
|
|
struct pollfd pollfd = { childPipeFd, POLLIN, 0 };
|
|
|
|
|
if (poll(&pollfd, 1, timeout) == 0)
|
|
|
|
|
{
|
|
|
|
|
kill(childPid, SIGTERM);
|
|
|
|
|
return "poll(&pollfd, 1, timeout) timeout";
|
|
|
|
|
}
|
|
|
|
|
else if (pollfd.revents & POLLERR)
|
|
|
|
|
{
|
|
|
|
|
kill(childPid, SIGTERM);
|
|
|
|
|
return "poll(&pollfd, 1, timeout) error";
|
|
|
|
|
}
|
2023-07-11 11:10:10 +08:00
|
|
|
}
|
2023-07-11 20:28:59 +08:00
|
|
|
|
2023-11-29 19:28:28 +09:00
|
|
|
ssize_t nRead = read(childPipeFd, str, FF_PIPE_BUFSIZ);
|
|
|
|
|
if (nRead > 0)
|
|
|
|
|
ffStrbufAppendNS(buffer, (uint32_t) nRead, str);
|
|
|
|
|
else if (nRead == 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
else if (nRead < 0)
|
|
|
|
|
break;
|
|
|
|
|
};
|
2023-07-10 09:36:33 +08:00
|
|
|
|
2023-11-29 19:28:28 +09:00
|
|
|
return "read(childPipeFd, str, FF_PIPE_BUFSIZ) failed";
|
2023-07-10 09:36:33 +08:00
|
|
|
}
|