From 86a8509b26238479b5b240a6c1edcd5727de1eca Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 20 Oct 2020 17:06:36 -0700 Subject: [PATCH] ircd::m::exec: Add options structure; add logging/detach related options. --- include/ircd/exec.h | 26 ++++++++++++++++++++++++++ ircd/exec.cc | 19 +++++++++++++++---- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/include/ircd/exec.h b/include/ircd/exec.h index b07482287..893cce90a 100644 --- a/include/ircd/exec.h +++ b/include/ircd/exec.h @@ -34,6 +34,7 @@ namespace boost::process struct ircd::exec :instance_list { + struct opts; using args = vector_view; using const_buffers = vector_view; using mutable_buffers = vector_view; @@ -42,6 +43,7 @@ struct ircd::exec static uint64_t id_ctr; uint64_t id {0}; + std::unique_ptr opt; std::string path; std::vector argv; std::unique_ptr> pipe; @@ -55,6 +57,7 @@ struct ircd::exec bool signal(const int &sig); long join(const int &sig = 0); + exec(const args &, const opts &); exec(const args &); exec(exec &&) = delete; exec(const exec &) = delete; @@ -63,6 +66,29 @@ struct ircd::exec ~exec() noexcept; }; +/// Exec options +/// +struct ircd::exec::opts +{ + /// When false (default) the child is terminated by the dtor of + /// ircd::exec. Note setting this to true does not detach the boost + /// handles until destruction time; it also does not affect calling + /// the interface join() manually. + bool detach {false}; + + /// Child executions will be logged at this level (use DEBUG to quiet) + ircd::log::level exec_log_level = ircd::log::level::NOTICE; + + /// Child exits will be logged at this level (use DEBUG to quiet); note + /// non-zero exits are still logged with level::ERROR. + ircd::log::level exit_log_level = ircd::log::level::INFO; +}; + +inline +ircd::exec::exec(const args &args) +:exec{args, opts{}} +{} + inline ircd::const_buffer ircd::write(exec &p, const const_buffer &buf) diff --git a/ircd/exec.cc b/ircd/exec.cc index 92b90e926..ef2a6cec0 100644 --- a/ircd/exec.cc +++ b/ircd/exec.cc @@ -30,11 +30,16 @@ ircd::util::instance_list::list allocator }; -ircd::exec::exec(const args &args) +ircd::exec::exec(const args &args, + const opts &opt) :id { ++id_ctr } +,opt +{ + std::make_unique(opt) +} ,path ( args.at(0) @@ -66,9 +71,10 @@ ircd::exec::exec(const args &args) child->id() } { - log::notice + log::logf { - log, "id:%lu pid:%ld `%s' exec argc:%zu", + log, this->opt->exec_log_level, + "id:%lu pid:%ld `%s' exec argc:%zu", id, pid, path, @@ -79,6 +85,10 @@ ircd::exec::exec(const args &args) ircd::exec::~exec() noexcept try { + assert(opt); + if(opt->detach) + return; + join(SIGKILL); } catch(const std::exception &e) @@ -126,10 +136,11 @@ try assert(!child->running()); code = child->exit_code(); + assert(opt); const auto &level { code == 0? - log::level::INFO: + opt->exit_log_level: log::level::ERROR };