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
This commit is contained in:
Jahdiel Alvarez 2023-10-25 17:16:38 -07:00
parent b5c02ec925
commit d7c42966b9

View file

@ -173,6 +173,7 @@ int KillProcessesWithOpenFiles(const std::string& prefix, int signal, bool killF
} }
} }
} }
int totalKilledPids = pids.size();
if (signal != 0) { if (signal != 0) {
for (const auto& pid : pids) { for (const auto& pid : pids) {
std::string comm; 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 LOG(WARNING) << "Sending " << strsignal(signal) << " to pid " << pid << " (" << comm
<< ", " << exe << ")"; << ", " << 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 } // namespace vold