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;
107 ::exit(static_cast<int>(result));
112 stdin_pipe.close_read_fd();
113 stdout_pipe.close_write_fd();
114 stderr_pipe.close_write_fd();
125 ChildProcess::Pipe stdin_pipe, stdout_pipe, stderr_pipe;
130 throw std::system_error(errno, std::system_category());
139 stdin_pipe.close_write_fd();
140 stdout_pipe.close_read_fd();
141 stderr_pipe.close_read_fd();
144 redirect_stream_to_fd(stdin_pipe.read_fd(), STDIN_FILENO);
146 redirect_stream_to_fd(stdout_pipe.write_fd(), STDOUT_FILENO);
148 redirect_stream_to_fd(stderr_pipe.write_fd(), STDERR_FILENO);
151 }
catch(
const std::exception& e)
153 std::cerr <<
"core::posix::fork(): An unhandled std::exception occured in the child process:" << std::endl
154 <<
" what(): " << e.what() << std::endl;
158 std::cerr <<
"core::posix::fork(): An unhandled exception occured in the child process." << std::endl;
164 ::exit(static_cast<int>(result));
170 stdin_pipe.close_read_fd();
171 stdout_pipe.close_write_fd();
172 stderr_pipe.close_write_fd();