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";
52 const char*
data = command.c_str();
53 size_t remaining = command.size();
57 while (remaining > 0) {
63 throw std::runtime_error(
"write() error: " + std::string(
std::strerror(errno)));
66 remaining -=
static_cast<size_t>(written);
74 ssize_t bytes_read = 0;
77 const char* newline_pos =
static_cast<const char*
>(memchr(
buffer,
'\n',
static_cast<size_t>(bytes_read)));
78 if (newline_pos !=
nullptr) {
80 response.append(
buffer,
static_cast<size_t>(newline_pos -
buffer + 1));
83 response.append(
buffer,
static_cast<size_t>(bytes_read));
85 if (bytes_read < 0 && errno != EINTR) {
86 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.
const std::vector< MemoryValue > data
uint8_t buffer[RANDOM_BUFFER_SIZE]
constexpr decltype(auto) get(::tuplet::tuple< T... > &&t) noexcept