Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Common): fixes for ffProcessAppendOutput #640

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 33 additions & 24 deletions src/common/processing_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <errno.h>
#include <sys/wait.h>

enum { FF_PIPE_BUFSIZ = 4096 };
#define FF_PIPE_BUFSIZ 4096

static inline void waitpid_wrapper(const pid_t* pid)
{
Expand All @@ -20,40 +20,53 @@ static inline void waitpid_wrapper(const pid_t* pid)
waitpid(*pid, NULL, 0);
}

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
}

const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool useStdErr)
{
int pipes[2];
const int timeout = instance.config.general.processingTimeout;

if(pipe(pipes) == -1)
if(ffPipe2(pipes, O_CLOEXEC) == -1)
return "pipe() failed";

__attribute__((__cleanup__(waitpid_wrapper))) pid_t childPid = fork();
if(childPid == -1)
{
close(pipes[0]);
close(pipes[1]);
return "fork() failed";
}

//Child
if(childPid == 0)
{
int nullFile = open("/dev/null", O_WRONLY);
int nullFile = open("/dev/null", O_WRONLY|O_CLOEXEC);
dup2(pipes[1], useStdErr ? STDERR_FILENO : STDOUT_FILENO);
dup2(nullFile, useStdErr ? STDOUT_FILENO : STDERR_FILENO);
close(pipes[0]);
close(pipes[1]);
setenv("LANG", "C", 1);
execvp(argv[0], argv);
exit(901);
_exit(127);
}

//Parent
close(pipes[1]);

int FF_AUTO_CLOSE_FD childPipeFd = pipes[0];
char str[FF_PIPE_BUFSIZ];

int timeout = instance.config.general.processingTimeout;
if (timeout >= 0)
fcntl(childPipeFd, F_SETFL, fcntl(childPipeFd, F_GETFL) | O_NONBLOCK);

do
while(1)
{
if (timeout >= 0)
{
Expand All @@ -70,18 +83,14 @@ const char* ffProcessAppendOutput(FFstrbuf* buffer, char* const argv[], bool use
}
}

char str[FF_PIPE_BUFSIZ];
while (true)
{
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;
}
} while (errno == EAGAIN);
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;
};

return NULL;
return "read(childPipeFd, str, FF_PIPE_BUFSIZ) failed";
}