+
+bool ForkAndExec::prepareExecReadyPipe() {
+ if (ready[mode::READ] == -1) {
+ if (pipe2(ready, O_CLOEXEC | O_NONBLOCK)) {
+ perror("ForkAndExec pipe()");
+ return false;
+ }
+ closePipe(ready[mode::WRITE]);
+ }
+
+ return true;
+}
+
+void ForkAndExec::prepareOutputPipe() {
+ if (pipes[mode::WRITE] != -1) {
+ if (0 > dup2(pipes[mode::WRITE], STDERR_FILENO) ||
+ 0 > dup2(pipes[mode::WRITE], STDOUT_FILENO)) {
+ perror("ForkAndExec dup()");
+ }
+ }
+}
+
+void ForkAndExec::closePipe(int &fd) {
+ if (fd != -1) {
+ close(fd);
+ fd = -1;
+ }
+}
+
+void ForkAndExec::pollExecReadyPipe() {
+ pollfd fd{ready[mode::READ], 0, 0};
+ if (1 > poll(&fd, 1, 5000)) {
+ cerr << "exec() timed out" << endl;
+ }
+}