diff --git a/fairmq/tools/Process.cxx b/fairmq/tools/Process.cxx index 05be0aae..3486e0a0 100644 --- a/fairmq/tools/Process.cxx +++ b/fairmq/tools/Process.cxx @@ -64,15 +64,16 @@ class LinePrinter namespace fair::mq::tools { -/** - * Execute given command in forked process and capture stdout output - * and exit code. - * - * @param[in] cmd Command to execute - * @param[in] log_prefix How to prefix each captured output line with - * @return Captured stdout output and exit code - */ execute_result execute(const string& cmd, const string& prefix, const string& input, int sig) +{ + return execute(cmd, prefix, input, sig, chrono::milliseconds::zero()); +} + +execute_result execute(const string& cmd, + const string& prefix, + const string& input, + int sig, + chrono::milliseconds timeout) { execute_result result; stringstream out; @@ -104,6 +105,25 @@ execute_result execute(const string& cmd, const string& prefix, const string& in int pid = c.id(); p.Print(ToString("fair::mq::tools::execute: pid: ", pid)); + // kill the child when the deadline expires; cancel the deadline when both + // output pipes are closed (the child has exited) + ba::steady_timer deadlineTimer(ioc); + int openPipes = 2; + auto onPipeClosed = [&]() { + if (--openPipes == 0) { + deadlineTimer.cancel(); + } + }; + if (timeout > chrono::milliseconds::zero()) { + deadlineTimer.expires_after(timeout); + deadlineTimer.async_wait([&](const bs::error_code& ec) { + if (!ec) { + p.Print(ToString("fair::mq::tools::execute: timeout after ", timeout.count(), " ms, killing pid ", pid)); + kill(pid, SIGKILL); + } + }); + } + // handle std_in with a delay if (!input.empty()) { inputTimer.async_wait([&](const bs::error_code& ec1) { @@ -142,11 +162,10 @@ execute_result execute(const string& cmd, const string& prefix, const string& in ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut); } else { - if (ec == ba::error::eof) { - // outputPipe.async_close(); - } else { + if (ec != ba::error::eof) { p.Print(ec.message()); } + onPipeClosed(); } }; ba::async_read_until(outputPipe, outputBuffer, delimiter, onStdOut); @@ -162,11 +181,10 @@ execute_result execute(const string& cmd, const string& prefix, const string& in ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr); } else { - if (ec == ba::error::eof) { - // errorPipe.async_close(); - } else { + if (ec != ba::error::eof) { p.Print(ec.message()); } + onPipeClosed(); } }; ba::async_read_until(errorPipe, errorBuffer, delimiter, onStdErr); diff --git a/fairmq/tools/Process.h b/fairmq/tools/Process.h index 839b345d..e1b2b38a 100644 --- a/fairmq/tools/Process.h +++ b/fairmq/tools/Process.h @@ -9,6 +9,7 @@ #ifndef FAIR_MQ_TOOLS_PROCESS_H #define FAIR_MQ_TOOLS_PROCESS_H +#include #include namespace fair::mq::tools @@ -37,6 +38,16 @@ execute_result execute(const std::string& cmd, const std::string& input = "", int sig = -1); +/** + * Same as above, but kill the child process (SIGKILL) if it runs longer than + * timeout. A timeout of zero means no limit. + */ +execute_result execute(const std::string& cmd, + const std::string& prefix, + const std::string& input, + int sig, + std::chrono::milliseconds timeout); + } // namespace fair::mq::tools #endif /* FAIR_MQ_TOOLS_PROCESS_H */