From 52c304ecae2829cab8bf10bdebbeff83f0a963f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 23 Feb 2023 11:13:50 +0800 Subject: [PATCH] IO (Linux): add detailed error messages for ffProcessAppendStdOut --- src/common/io/io.h | 19 +++++++++++++++++++ src/common/processing_linux.c | 19 +++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/common/io/io.h b/src/common/io/io.h index ca800dbe6..ed8d38b10 100644 --- a/src/common/io/io.h +++ b/src/common/io/io.h @@ -101,4 +101,23 @@ static inline void ffUnsuppressIO(bool* suppressed) void ffListFilesRecursively(const char* path); +static inline bool wrapClose(FFNativeFD* pfd) +{ + assert(pfd); + + #ifndef WIN32 + if (*pfd < 0) + return false; + close(*pfd); + #else + // https://devblogs.microsoft.com/oldnewthing/20040302-00/?p=40443 + if (*pfd == NULL || *pfd == INVALID_HANDLE_VALUE) + return false; + CloseHandle(*pfd); + #endif + + return true; +} +#define FF_AUTO_CLOSE_FD __attribute__((__cleanup__(wrapClose))) + #endif // FF_INCLUDED_common_io_io diff --git a/src/common/processing_linux.c b/src/common/processing_linux.c index 74502ef52..b9bf20c1b 100644 --- a/src/common/processing_linux.c +++ b/src/common/processing_linux.c @@ -30,9 +30,20 @@ const char* ffProcessAppendStdOut(FFstrbuf* buffer, char* const argv[]) //Parent close(pipes[1]); - waitpid(childPid, NULL, 0); - bool ok = ffAppendFDBuffer(pipes[0], buffer); - close(pipes[0]); - return ok ? NULL : "ffAppendFDBuffer() failed"; + int FF_AUTO_CLOSE_FD childPipeFd = pipes[0]; + int status = -1; + if(waitpid(childPid, &status, 0) < 0) + return "waitpid(childPid, &status, 0) failed"; + + 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; }