Fix ExecFile waitpid error handling.

This commit is contained in:
rakshasa
2025-06-01 18:21:10 +02:00
committed by Jari Sundell
parent a9196b43d4
commit 231606afc1
+15 -9
View File
@@ -1,6 +1,7 @@
#include "config.h" #include "config.h"
#include <cerrno> #include <cerrno>
#include <cstring>
#include <fcntl.h> #include <fcntl.h>
#include <string> #include <string>
#include <unistd.h> #include <unistd.h>
@@ -18,7 +19,7 @@ namespace rpc {
int int
ExecFile::execute(const char* file, char* const* argv, int flags) { ExecFile::execute(const char* file, char* const* argv, int flags) {
// Write the execued command and its parameters to the log fd. // Write the executed command and its parameters to the log fd.
[[maybe_unused]] int result; [[maybe_unused]] int result;
if (m_log_fd != -1) { if (m_log_fd != -1) {
@@ -117,18 +118,23 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
} }
int status; int status;
int wpid;
do { while (waitpid(childPid, &status, 0) == -1) {
wpid = waitpid(childPid, &status, 0); switch (errno) {
} while (wpid == -1 && WIFEXITED(status) == 0); case EINTR:
continue;
if (wpid != childPid) case ECHILD:
throw torrent::internal_error("ExecFile::execute(...) waitpid failed."); throw torrent::internal_error("ExecFile::execute(...) waitpid failed with ECHILD, child process not found.");
case EINVAL:
throw torrent::internal_error("ExecFile::execute(...) waitpid failed with EINVAL.");
default:
throw torrent::internal_error("ExecFile::execute(...) waitpid failed with unexpected error: " + std::string(std::strerror(errno)));
}
};
// Check return value? // Check return value?
if (m_log_fd != -1) { if (m_log_fd != -1) {
if (status == 0) if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
result = write(m_log_fd, "\n--- Success ---\n", sizeof("\n--- Success ---\n")); result = write(m_log_fd, "\n--- Success ---\n", sizeof("\n--- Success ---\n"));
else else
result = write(m_log_fd, "\n--- Error ---\n", sizeof("\n--- Error ---\n")); result = write(m_log_fd, "\n--- Error ---\n", sizeof("\n--- Error ---\n"));