aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/process.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2024-03-21 10:50:44 +0100
committerGitHub Enterprise <[email protected]>2024-03-21 10:50:44 +0100
commitd7f422f02976b8d48cce533f9292400f2f85a141 (patch)
tree74032ff63b5308ef6012526959ebd0fcde7ff4ba /src/zencore/process.cpp
parent5.4.2-pre5 (diff)
downloadzen-d7f422f02976b8d48cce533f9292400f2f85a141.tar.xz
zen-d7f422f02976b8d48cce533f9292400f2f85a141.zip
improved process monitoring behaviour with invalid pids (#16)
Diffstat (limited to 'src/zencore/process.cpp')
-rw-r--r--src/zencore/process.cpp41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/zencore/process.cpp b/src/zencore/process.cpp
index 2d0ec2de6..e0a395494 100644
--- a/src/zencore/process.cpp
+++ b/src/zencore/process.cpp
@@ -75,8 +75,9 @@ ProcessHandle::~ProcessHandle()
}
void
-ProcessHandle::Initialize(int Pid)
+ProcessHandle::Initialize(int Pid, std::error_code& OutEc)
{
+ OutEc.clear();
ZEN_ASSERT(m_ProcessHandle == nullptr);
#if ZEN_PLATFORM_WINDOWS
@@ -90,7 +91,21 @@ ProcessHandle::Initialize(int Pid)
if (!m_ProcessHandle)
{
- ThrowLastError(fmt::format("ProcessHandle::Initialize(pid: {}) failed", Pid));
+ OutEc = MakeErrorCodeFromLastError();
+ }
+
+ m_Pid = Pid;
+}
+
+void
+ProcessHandle::Initialize(int Pid)
+{
+ std::error_code Ec;
+ Initialize(Pid, Ec);
+
+ if (Ec)
+ {
+ throw std::system_error(Ec, fmt::format("ProcessHandle::Initialize(pid: {}) failed", Pid));
}
m_Pid = Pid;
@@ -576,12 +591,19 @@ ProcessMonitor::IsRunning()
#if ZEN_PLATFORM_WINDOWS
DWORD ExitCode = 0;
- GetExitCodeProcess(Proc, &ExitCode);
- ProcIsActive = (ExitCode == STILL_ACTIVE);
- if (!ProcIsActive)
+ if (Proc)
+ {
+ GetExitCodeProcess(Proc, &ExitCode);
+ ProcIsActive = (ExitCode == STILL_ACTIVE);
+ if (!ProcIsActive)
+ {
+ CloseHandle(Proc);
+ }
+ }
+ else
{
- CloseHandle(Proc);
+ ProcIsActive = false;
}
#else
int Pid = int(intptr_t(Proc));
@@ -613,11 +635,8 @@ ProcessMonitor::AddPid(int Pid)
ProcessHandle = HandleType(intptr_t(Pid));
#endif
- if (ProcessHandle)
- {
- RwLock::ExclusiveLockScope _(m_Lock);
- m_ProcessHandles.push_back(ProcessHandle);
- }
+ RwLock::ExclusiveLockScope _(m_Lock);
+ m_ProcessHandles.push_back(ProcessHandle);
}
bool