diff --git a/src/System/Process.cpp b/src/System/Process.cpp index a2eeb19..722c810 100644 --- a/src/System/Process.cpp +++ b/src/System/Process.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -11,8 +12,10 @@ #include #include #include +#include #include #include +#include #include #include #include @@ -347,6 +350,49 @@ void write_child_failure(int err_fd, int phase, int err) noexcept return st; } +#if defined(__linux__) +// kill(-pgid, 0) succeeds while any member exists, including zombies. A +// supervising ancestor that is a child subreaper can hold zombie descendants +// until it reaps them, which would otherwise keep a dead group "alive" +// forever. Scan /proc for a member that is not a zombie. +[[nodiscard]] bool process_group_has_live_member(pid_t pgid) noexcept +{ + std::error_code ec; + for (std::filesystem::directory_iterator entry{"/proc", ec}; + !ec && entry != std::filesystem::directory_iterator{}; + entry.increment(ec)) { + const std::string name = entry->path().filename().string(); + if (name.empty() || + !std::all_of(name.begin(), name.end(), [](unsigned char c) { + return std::isdigit(c) != 0; + })) { + continue; + } + std::ifstream stat{entry->path() / "stat"}; + std::string line; + if (!std::getline(stat, line)) { + continue; + } + // "pid (comm) state ppid pgrp ..." - comm may contain ')' or spaces. + const std::size_t close = line.rfind(')'); + if (close == std::string::npos) { + continue; + } + std::istringstream fields{line.substr(close + 1)}; + char state = 0; + long parent = 0; + long group = 0; + if (!(fields >> state >> parent >> group)) { + continue; + } + if (group == static_cast(pgid) && state != 'Z' && state != 'X') { + return true; + } + } + return false; +} +#endif + [[nodiscard]] bool is_process_group_alive(pid_t pgid) noexcept { if (pgid <= 0) { @@ -354,7 +400,11 @@ void write_child_failure(int err_fd, int phase, int err) noexcept } for (;;) { if (::kill(-pgid, 0) == 0) { +#if defined(__linux__) + return process_group_has_live_member(pgid); +#else return true; +#endif } if (errno == EINTR) { continue; diff --git a/tests/standalone/System/SystemHardening.cpp b/tests/standalone/System/SystemHardening.cpp index ca55b8e..4608459 100644 --- a/tests/standalone/System/SystemHardening.cpp +++ b/tests/standalone/System/SystemHardening.cpp @@ -296,6 +296,43 @@ Void::Testing::Test system_replace_file_temp_ownership_and_metrics() expect(sample.ratio >= 0.0); } +Void::Testing::Test system_process_group_alive_ignores_unreaped_zombies() +{ + // A group whose only members are zombies is dead, even though + // kill(-pgid, 0) still succeeds. This happens whenever a supervising + // ancestor is a child subreaper (e.g. a build system running the tests). + Void::Process::SpawnOptions opts; + opts.executable = VOID_PROCESS_HELPER_PATH; + opts.arguments = {"exit", "0"}; + opts.new_process_group = true; + opts.stdin_pipe = false; + opts.stdout_pipe = false; + opts.stderr_pipe = false; + auto spawned = Void::Process::spawn(opts); + require(spawned.has_value()); + + const auto pid = static_cast(spawned->pid()); + bool zombie = false; + for (int attempt = 0; attempt < 400; ++attempt) { + std::ifstream stat{"/proc/" + std::to_string(pid) + "/stat"}; + std::string line; + if (std::getline(stat, line)) { + const std::size_t close = line.rfind(')'); + if (close != std::string::npos && close + 2 < line.size() && + line[close + 2] == 'Z') { + zombie = true; + break; + } + } + std::this_thread::sleep_for(std::chrono::milliseconds{5}); + } + require(zombie); + expect(!spawned->process_group_alive()); + + // terminate() reaps the zombie leader and cleans up. + expect(spawned->terminate(std::chrono::milliseconds{50}).has_value()); +} + Void::Testing::Test system_terminate_reaps_entire_process_group_after_leader_exits() { Void::FileSystem::TempDir tmp{"void_sys_pgid_"};