Switch to boost::process

This commit is contained in:
badaix 2020-01-07 21:03:57 +01:00
parent 87d4c00d21
commit 4a802d73ba
2 changed files with 24 additions and 12 deletions

View file

@ -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<stream_descriptor>(ioc_, process_->getStdout());
stream_stderr_ = make_unique<stream_descriptor>(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<stream_descriptor>(ioc_, pipe_stdout_.native_source());
stream_stderr_ = make_unique<stream_descriptor>(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

View file

@ -19,13 +19,17 @@
#ifndef PROCESS_STREAM_HPP
#define PROCESS_STREAM_HPP
#include <boost/process.hpp>
#include <memory>
#include <string>
#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> process_;
bp::pipe pipe_stdout_;
bp::pipe pipe_stderr_;
bp::child process_;
bool logStderr_;
boost::asio::streambuf streambuf_stderr_;