diff --git a/init/sigchld_handler.cpp b/init/sigchld_handler.cpp index 9d4c7c8e3..02795080a 100644 --- a/init/sigchld_handler.cpp +++ b/init/sigchld_handler.cpp @@ -118,8 +118,23 @@ static pid_t ReapOneProcess() { return pid; } -void ReapAnyOutstandingChildren() { - while (ReapOneProcess() != 0) { +std::set ReapAnyOutstandingChildren() { + std::set reaped_pids; + for (;;) { + const pid_t pid = ReapOneProcess(); + if (pid <= 0) { + return reaped_pids; + } + reaped_pids.emplace(pid); + } +} + +static void ReapAndRemove(std::vector& alive_pids) { + for (auto pid : ReapAnyOutstandingChildren()) { + const auto it = std::find(alive_pids.begin(), alive_pids.end(), pid); + if (it != alive_pids.end()) { + alive_pids.erase(it); + } } } @@ -142,13 +157,7 @@ void WaitToBeReaped(int sigchld_fd, const std::vector& pids, } std::vector alive_pids(pids.begin(), pids.end()); while (!alive_pids.empty() && t.duration() < timeout) { - pid_t pid; - while ((pid = ReapOneProcess()) != 0) { - auto it = std::find(alive_pids.begin(), alive_pids.end(), pid); - if (it != alive_pids.end()) { - alive_pids.erase(it); - } - } + ReapAndRemove(alive_pids); if (alive_pids.empty()) { break; } diff --git a/init/sigchld_handler.h b/init/sigchld_handler.h index e07a7d66a..535130296 100644 --- a/init/sigchld_handler.h +++ b/init/sigchld_handler.h @@ -18,12 +18,13 @@ #define _INIT_SIGCHLD_HANDLER_H_ #include +#include #include namespace android { namespace init { -void ReapAnyOutstandingChildren(); +std::set ReapAnyOutstandingChildren(); void WaitToBeReaped(int sigchld_fd, const std::vector& pids, std::chrono::milliseconds timeout);