From a5ded46f278d0e4635473b8d053f7e0fbfcd2034 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Thu, 14 Apr 2022 16:43:18 +0200 Subject: [PATCH] Add a detach function to the process handles --- src/common/process.cpp | 10 +++++++--- src/common/process.h | 10 ++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/common/process.cpp b/src/common/process.cpp index 817f706d..54b308c8 100644 --- a/src/common/process.cpp +++ b/src/common/process.cpp @@ -96,18 +96,18 @@ char* const* ProcessEnvironment::make_environ() const { Process::Handle::Handle(pid_t pid) : pid_(pid) {} Process::Handle::~Handle() { - if (!is_moved_) { + if (!detached_) { // If this function has already been called then that's okay terminate(); } } Process::Handle::Handle(Handle&& o) noexcept : pid_(o.pid_) { - o.is_moved_ = true; + o.detached_ = true; } Process::Handle& Process::Handle::operator=(Handle&& o) noexcept { - o.is_moved_ = true; + o.detached_ = true; pid_ = o.pid_; @@ -122,6 +122,10 @@ bool Process::Handle::running() const noexcept { return pid_running(pid_); } +void Process::Handle::detach() noexcept { + detached_ = true; +} + void Process::Handle::terminate() const noexcept { kill(pid_, SIGINT); wait(); diff --git a/src/common/process.h b/src/common/process.h index 05354078..922d8231 100644 --- a/src/common/process.h +++ b/src/common/process.h @@ -135,6 +135,11 @@ class Process { */ bool running() const noexcept; + /** + * Don't terminate the process when this object gets dropped. + */ + void detach() noexcept; + /** * Forcefully terminate the process by sending `SIGKILL`. Will reap the * process zombie after sending the signal. @@ -149,9 +154,10 @@ class Process { private: /** - * If `true`, don't terminate the process + * If `true`, don't terminate the process when this object gets dropped. + * Also set when this object gets moved from. */ - bool is_moved_ = false; + bool detached_ = false; pid_t pid_ = 0; };