26 #include <system_error>
32 void redirect_stream_to_fd(
int fd,
int stream)
34 auto rc = ::dup2(fd, stream);
36 throw std::system_error(errno, std::system_category());
39 void print_backtrace(std::ostream& out,
const std::string& line_prefix)
43 out << line_prefix << std::dec << std::setw(2) << frame.
depth() <<
"@" << std::hex << std::setw(14) << frame.
frame_pointer() <<
": "
60 ChildProcess::Pipe stdin_pipe{ChildProcess::Pipe::invalid()};
61 ChildProcess::Pipe stdout_pipe{ChildProcess::Pipe::invalid()};
62 ChildProcess::Pipe stderr_pipe{ChildProcess::Pipe::invalid()};
65 stdin_pipe = ChildProcess::Pipe();
67 stdout_pipe = ChildProcess::Pipe();
69 stderr_pipe = ChildProcess::Pipe();
74 throw std::system_error(errno, std::system_category());
82 stdin_pipe.close_write_fd();
83 stdout_pipe.close_read_fd();
84 stderr_pipe.close_read_fd();
87 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
89 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
91 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
94 }
catch(
const std::exception& e)
96 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
97 <<
" what(): " << e.what() << std::endl;
101 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
110 ::_exit(
static_cast<int>(result));
115 stdin_pipe.close_read_fd();
116 stdout_pipe.close_write_fd();
117 stderr_pipe.close_write_fd();
128 ChildProcess::Pipe stdin_pipe, stdout_pipe, stderr_pipe;
133 throw std::system_error(errno, std::system_category());
142 stdin_pipe.close_write_fd();
143 stdout_pipe.close_read_fd();
144 stderr_pipe.close_read_fd();
147 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
149 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
151 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
154 }
catch(
const std::exception& e)
156 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
157 <<
" what(): " << e.what() << std::endl;
161 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
170 ::_exit(
static_cast<int>(result));
176 stdin_pipe.close_read_fd();
177 stdout_pipe.close_write_fd();
178 stderr_pipe.close_write_fd();