diff --git a/server/streamreader/process_stream.cpp b/server/streamreader/process_stream.cpp index 036a45a7..fcae1341 100644 --- a/server/streamreader/process_stream.cpp +++ b/server/streamreader/process_stream.cpp @@ -35,8 +35,7 @@ namespace streamreader static constexpr auto LOG_TAG = "ProcessStream"; -ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri) - : PosixStream(pcmListener, ioc, uri), path_(""), process_(nullptr) +ProcessStream::ProcessStream(PcmListener* pcmListener, boost::asio::io_context& ioc, const StreamUri& uri) : PosixStream(pcmListener, ioc, uri) { params_ = uri_.getQuery("params"); wd_timeout_sec_ = cpt::stoul(uri_.getQuery("wd_timeout", "0")); @@ -103,11 +102,18 @@ void ProcessStream::do_connect() return; initExeAndPath(uri_.path); LOG(DEBUG, LOG_TAG) << "Launching: '" << path_ + exe_ << "', with params: '" << params_ << "', in path: '" << path_ << "'\n"; - process_.reset(new Process(path_ + exe_ + " " + params_, path_)); - int flags = fcntl(process_->getStdout(), F_GETFL, 0); - fcntl(process_->getStdout(), F_SETFL, flags | O_NONBLOCK); - stream_ = make_unique(ioc_, process_->getStdout()); - stream_stderr_ = make_unique(ioc_, process_->getStderr()); + + pipe_stdout_ = bp::pipe(); + // could use bp::async_pipe, but this is broken in boost 1.72: + // https://github.com/boostorg/process/issues/116 + pipe_stderr_ = bp::pipe(); + // stdout pipe should not block + int flags = fcntl(pipe_stdout_.native_source(), F_GETFL, 0); + fcntl(pipe_stdout_.native_source(), F_SETFL, flags | O_NONBLOCK); + + process_ = bp::child(path_ + exe_ + " " + params_, bp::std_out > pipe_stdout_, bp::std_err > pipe_stderr_, bp::start_dir = path_); + stream_ = make_unique(ioc_, pipe_stdout_.native_source()); + stream_stderr_ = make_unique(ioc_, pipe_stderr_.native_source()); on_connect(); if (wd_timeout_sec_ > 0) { @@ -124,8 +130,8 @@ void ProcessStream::do_connect() void ProcessStream::do_disconnect() { - if (process_) - process_->kill(); + if (process_.running()) + ::kill(-process_.native_handle(), SIGINT); } @@ -171,7 +177,7 @@ void ProcessStream::onTimeout(const Watchdog& /*watchdog*/, std::chrono::millise { LOG(ERROR, LOG_TAG) << "Watchdog timeout: " << ms.count() / 1000 << "s\n"; if (process_) - process_->kill(); + ::kill(-process_.native_handle(), SIGINT); } } // namespace streamreader diff --git a/server/streamreader/process_stream.hpp b/server/streamreader/process_stream.hpp index 5cdea921..d81eab16 100644 --- a/server/streamreader/process_stream.hpp +++ b/server/streamreader/process_stream.hpp @@ -19,13 +19,17 @@ #ifndef PROCESS_STREAM_HPP #define PROCESS_STREAM_HPP +#include #include #include #include "posix_stream.hpp" -#include "process.hpp" #include "watchdog.hpp" + +namespace bp = boost::process; + + namespace streamreader { @@ -49,7 +53,9 @@ protected: std::string exe_; std::string path_; std::string params_; - std::unique_ptr process_; + bp::pipe pipe_stdout_; + bp::pipe pipe_stderr_; + bp::child process_; bool logStderr_; boost::asio::streambuf streambuf_stderr_;