diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 9f1d439e..09b187e6 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -48,6 +48,24 @@ bool set_realtime_priority(bool sched_fifo, int priority) { ¶ms) == 0; } +bool pid_running(pid_t pid) { + // With regular individually hosted plugins we can simply check whether the + // process is still running, however Boost.Process does not allow you to do + // the same thing for a process that's not a direct child if this process. + // When using plugin groups we'll have to manually check whether the PID + // returned by the group host process is still active. We sadly can't use + // `kill()` for this as that provides no way to distinguish between active + // processes and zombies, and a terminated group host process will always be + // left as a zombie process. If the process is active, then + // `/proc//{cwd,exe,root}` will be valid symlinks. + try { + fs::canonical("/proc/" + std::to_string(pid) + "/exe"); + return true; + } catch (const fs::filesystem_error&) { + return false; + } +} + ScopedFlushToZero::ScopedFlushToZero() { old_ftz_mode = _MM_GET_FLUSH_ZERO_MODE(); _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); diff --git a/src/common/utils.h b/src/common/utils.h index 7dc15413..0bae536c 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -84,6 +84,12 @@ std::optional get_realtime_priority(); */ bool set_realtime_priority(bool sched_fifo, int priority = 5); +/** + * Check whether a process with the given PID is still active (and not a + * zombie). + */ +bool pid_running(pid_t pid); + /** * A RAII wrapper that will temporarily enable the FTZ flag so that denormals * are automatically flushed to zero, returning to whatever the flag was diff --git a/src/plugin/host-process.cpp b/src/plugin/host-process.cpp index 96e5982e..7158cd32 100644 --- a/src/plugin/host-process.cpp +++ b/src/plugin/host-process.cpp @@ -26,12 +26,6 @@ namespace bp = boost::process; namespace fs = boost::filesystem; -/** - * Check whether a process with the given PID is still active (and not a - * zombie). - */ -bool pid_running(pid_t pid); - /** * Simple helper function around `boost::process::child` that launches the host * application (`*.exe`) wrapped in winedbg if compiling with @@ -234,21 +228,3 @@ void GroupHost::terminate() { // the sockets will cause the associated plugin to exit. sockets.close(); } - -bool pid_running(pid_t pid) { - // With regular individually hosted plugins we can simply check whether the - // process is still running, however Boost.Process does not allow you to do - // the same thing for a process that's not a direct child if this process. - // When using plugin groups we'll have to manually check whether the PID - // returned by the group host process is still active. We sadly can't use - // `kill()` for this as that provides no way to distinguish between active - // processes and zombies, and a terminated group host process will always be - // left as a zombie process. If the process is active, then - // `/proc//{cwd,exe,root}` will be valid symlinks. - try { - fs::canonical("/proc/" + std::to_string(pid) + "/exe"); - return true; - } catch (const fs::filesystem_error&) { - return false; - } -}