diff options
author | Oliver Reiche <oliver.reiche@huawei.com> | 2023-12-19 15:59:56 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2024-04-10 16:07:46 +0200 |
commit | a7c897a29fd8b65e110018e27186f6b7b6983f9c (patch) | |
tree | cbc07c365dae57d743c6533d907d03d878c02700 | |
parent | c77a3408e925c7bd9f1ca6a7e27ad8784680628e (diff) | |
download | justbuild-a7c897a29fd8b65e110018e27186f6b7b6983f9c.tar.gz |
SystemCommand: Fix handling of child status
(cherry-picked from commit c3d28a4cc3115644414ddba41d4b7ada5fd74fc2)
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | src/buildtool/system/system_command.hpp | 26 |
2 files changed, 24 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e499691c..87d4f607 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Bug fixes on top of release `1.2.4`. - On errors reading expressions, the full import path is reported in the error message. +- Child processes are now properly waited for in all circumstances. ## Release `1.2.4` (2023-12-19) diff --git a/src/buildtool/system/system_command.hpp b/src/buildtool/system/system_command.hpp index 660d9202..b9145301 100644 --- a/src/buildtool/system/system_command.hpp +++ b/src/buildtool/system/system_command.hpp @@ -201,10 +201,30 @@ class SystemCommand { // wait for child to finish and obtain return value int status{}; - ::waitpid(pid, &status, 0); + std::optional<int> retval{std::nullopt}; + do { + if (::waitpid(pid, &status, 0) == -1) { + // this should never happen + logger_.Emit(LogLevel::Error, + "Waiting for child failed with: {}", + strerror(errno)); + break; + } + + if (WIFEXITED(status)) { // NOLINT(hicpp-signed-bitwise) + retval = WEXITSTATUS(status); // NOLINT(hicpp-signed-bitwise) + } + else if (WIFSIGNALED(status)) { // NOLINT(hicpp-signed-bitwise) + constexpr auto kSignalBit = 128; + auto sig = WTERMSIG(status); // NOLINT(hicpp-signed-bitwise) + retval = kSignalBit + sig; + logger_.Emit( + LogLevel::Debug, "Child got killed by signal {}", sig); + } + // continue waitpid() in case we got STOPSIG from child + } while (not retval); - // NOLINTNEXTLINE(hicpp-signed-bitwise) - return WEXITSTATUS(status); + return retval; } static auto UnwrapStrings(std::vector<std::string>* v) noexcept |