From d7c42966b95e8540c8166cd8689cca5fe0a53e0c Mon Sep 17 00:00:00 2001 From: Jahdiel Alvarez Date: Wed, 25 Oct 2023 17:16:38 -0700 Subject: [PATCH] Verify if pid actually killed for processes with open files Its possible for vold to read a pid from procfs, the pid is killed externally and then vold tries to kill it. In this scenario, we sleep for 5s without needing it. Verify the return value from the kill syscall and validate that the pid was killed, if the pid didn't exist at the moment of the kill call, then don't count the pid as being killed. Test: Boots successfully Bug: 307801020 Change-Id: Ie127108b85be7249cf8b2881f4917d653d032186 --- Process.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Process.cpp b/Process.cpp index c1d55ee..426a425 100644 --- a/Process.cpp +++ b/Process.cpp @@ -173,6 +173,7 @@ int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killF } } } + int totalKilledPids = pids.size(); if (signal != 0) { for (const auto& pid : pids) { std::string comm; @@ -184,10 +185,17 @@ int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killF LOG(WARNING) << "Sending " << strsignal(signal) << " to pid " << pid << " (" << comm << ", " << exe << ")"; - kill(pid, signal); + if (kill(pid, signal) < 0) { + if (errno == ESRCH) { + totalKilledPids--; + LOG(WARNING) << "The target pid " << pid << " was already killed"; + continue; + } + LOG(ERROR) << "Unable to send signal " << strsignal(signal) << " to pid " << pid; + } } } - return pids.size(); + return totalKilledPids; } } // namespace vold