10 if (pipe(stdin_pipe) < 0 || pipe(stdout_pipe) < 0) {
11 throw std::runtime_error(
"pipe() failed");
16 throw std::runtime_error(
"fork() failed");
21 close(stdout_pipe[0]);
23 dup2(stdin_pipe[0], STDIN_FILENO);
24 dup2(stdout_pipe[1], STDOUT_FILENO);
27 close(stdout_pipe[1]);
29 execl(
"/bin/sh",
"sh",
"-c", command.c_str(),
nullptr);
35 close(stdout_pipe[1]);
46 waitpid(
pid,
nullptr, 0);
51 std::string command = line +
"\n";
60 ssize_t bytes_read = 0;
63 response.append(
buffer,
static_cast<size_t>(bytes_read));
64 if (response.find(
'\n') != std::string::npos) {
68 if (bytes_read < 0 && errno != EINTR) {
69 throw std::runtime_error(
"read() error: " + std::string(
std::strerror(errno)));
std::string read_line() const
Reads a line from the process.
Process(const std::string &command)
void write_line(const std::string &line) const
Ends line with a newline character, sends to the process.
uint8_t buffer[RANDOM_BUFFER_SIZE]
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept