From 57b56f20894a982bbc37e7784f7d0159bece5d5b Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Thu, 22 Aug 2024 10:58:02 +0200 Subject: safer calls to IsProcessRunning (#131) * safer calls to IsProcessRunning to handle cases where we can't check status of processes --- src/zenutil/zenserverprocess.cpp | 54 ++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 11 deletions(-) (limited to 'src/zenutil/zenserverprocess.cpp') diff --git a/src/zenutil/zenserverprocess.cpp b/src/zenutil/zenserverprocess.cpp index 84544bac8..29d590d00 100644 --- a/src/zenutil/zenserverprocess.cpp +++ b/src/zenutil/zenserverprocess.cpp @@ -249,7 +249,8 @@ ZenServerState::Lookup(int DesiredListenPort) const { if (DesiredListenPort == 0 || (EntryPort == DesiredListenPort)) { - if (IsProcessRunning(m_Data[i].Pid)) + std::error_code _; + if (IsProcessRunning(m_Data[i].Pid, _)) { return &m_Data[i]; } @@ -270,7 +271,8 @@ ZenServerState::LookupByEffectivePort(int Port) const { if (EntryPort == Port) { - if (IsProcessRunning(m_Data[i].Pid)) + std::error_code _; + if (IsProcessRunning(m_Data[i].Pid, _)) { return &m_Data[i]; } @@ -337,11 +339,21 @@ ZenServerState::Sweep() if (Entry.DesiredListenPort) { - if (Entry.Pid != 0 && IsProcessRunning(Entry.Pid) == false) + std::error_code ErrorCode; + if (Entry.Pid != 0 && IsProcessRunning(Entry.Pid, ErrorCode) == false) { - ZEN_DEBUG("Sweep - pid {} not running, reclaiming entry (port {})", Entry.Pid.load(), Entry.DesiredListenPort.load()); - - Entry.Reset(); + if (ErrorCode) + { + ZEN_WARN("Sweep - can not determine running state for pid {}, skipping entry (port {}). Reason: '{}'", + Entry.Pid.load(), + Entry.DesiredListenPort.load(), + ErrorCode.message()); + } + else + { + ZEN_DEBUG("Sweep - pid {} not running, reclaiming entry (port {})", Entry.Pid.load(), Entry.DesiredListenPort.load()); + Entry.Reset(); + } } } } @@ -361,9 +373,20 @@ ZenServerState::Snapshot(std::function&& Callback) if (Entry.Pid != 0 && Entry.DesiredListenPort) { - if (IsProcessRunning(Entry.Pid.load())) + std::error_code ErrorCode; + if (IsProcessRunning(Entry.Pid.load(), ErrorCode)) { - Callback(Entry); + if (ErrorCode) + { + ZEN_WARN("Snapshot - can not determine running state for pid {}, skipping entry (port {}). Reason: '{}'", + Entry.Pid.load(), + Entry.DesiredListenPort.load(), + ErrorCode.message()); + } + else + { + Callback(Entry); + } } } } @@ -415,7 +438,8 @@ ZenServerState::ZenServerEntry::AddSponsorProcess(uint32_t PidToAdd, uint64_t Ti { return false; } - if (!IsProcessRunning(ServerPid)) + std::error_code _; + if (!IsProcessRunning(ServerPid, _)) { return false; } @@ -995,9 +1019,17 @@ ValidateLockFileInfo(const LockFileInfo& Info, std::string& OutReason) OutReason = fmt::format("process ({}) is invalid", Info.Pid); return false; } - if (!IsProcessRunning(Info.Pid)) + std::error_code ErrorCode; + if (!IsProcessRunning(Info.Pid, ErrorCode)) { - OutReason = fmt::format("process ({}) is not running", Info.Pid); + if (ErrorCode) + { + OutReason = fmt::format("process ({}) can not be checked. Reason: '{}'", Info.Pid, ErrorCode.message()); + } + else + { + OutReason = fmt::format("process ({}) is not running", Info.Pid); + } return false; } if (Info.SessionId == Oid::Zero) -- cgit v1.2.3