From 521ad55a61cbdf915acd179ba4eea385663bdddb Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 6 May 2023 00:41:01 -0400 Subject: [PATCH] SystemServer: Handle `waitpid`'s status correctly We used to call `did_exit()` directly with the status returned from `waitpid` but the function expected an exit code. We now use several of `wait`-related macros to deduce the correct information. --- Userland/Services/SystemServer/Service.cpp | 9 ++++++--- Userland/Services/SystemServer/Service.h | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Userland/Services/SystemServer/Service.cpp b/Userland/Services/SystemServer/Service.cpp index 3893c6eaaa..da4cc357ee 100644 --- a/Userland/Services/SystemServer/Service.cpp +++ b/Userland/Services/SystemServer/Service.cpp @@ -241,14 +241,17 @@ ErrorOr Service::spawn(int socket_fd) return {}; } -ErrorOr Service::did_exit(int exit_code) +ErrorOr Service::did_exit(int status) { using namespace AK::TimeLiterals; VERIFY(m_pid > 0); VERIFY(!m_multi_instance); - dbgln("Service {} has exited with exit code {}", name(), exit_code); + if (WIFEXITED(status)) + dbgln("Service {} has exited with exit code {}", name(), WEXITSTATUS(status)); + if (WIFSIGNALED(status)) + dbgln("Service {} terminated due to signal {}", name(), WTERMSIG(status)); s_service_map.remove(m_pid); m_pid = -1; @@ -257,7 +260,7 @@ ErrorOr Service::did_exit(int exit_code) return {}; auto run_time = m_run_timer.elapsed_time(); - bool exited_successfully = exit_code == 0; + bool exited_successfully = WIFEXITED(status) == 0; if (!exited_successfully && run_time < 1_sec) { switch (m_restart_attempts) { diff --git a/Userland/Services/SystemServer/Service.h b/Userland/Services/SystemServer/Service.h index c82aabcea8..c26ac8378d 100644 --- a/Userland/Services/SystemServer/Service.h +++ b/Userland/Services/SystemServer/Service.h @@ -22,7 +22,8 @@ public: bool is_enabled() const; ErrorOr activate(); - ErrorOr did_exit(int exit_code); + // Note: This is a `status` as in POSIX's wait syscall, not an exit-code. + ErrorOr did_exit(int status); static Service* find_by_pid(pid_t);