tests: investigating catch2
[awesomized/libmemcached] / testing / lib / WaitForExec.cpp
1 #include "WaitForExec.hpp"
2
3 #include <cerrno>
4 #include <cstdio>
5
6 #include <fcntl.h>
7 #include <unistd.h>
8
9 #include <system_error>
10
11 using namespace std;
12
13 WaitForExec::WaitForExec(Poll &&poll_)
14 : poll{poll_}
15 {
16 if (pipe2(pipes, O_CLOEXEC|O_NONBLOCK)) {
17 int error = errno;
18 perror("Server::start pipe2()");
19 throw system_error(error, system_category());
20 }
21 }
22
23 WaitForExec::~WaitForExec() {
24 if (pipes[0] != -1) {
25 close(pipes[0]);
26 }
27 if (pipes[1] != -1) {
28 close(pipes[1]);
29 }
30 }
31
32 bool WaitForExec::operator()() {
33 if (pipes[0] == -1) {
34 return false;
35 }
36 if (pipes[1] != -1) {
37 close(pipes[1]);
38 pipes[1] = -1;
39 }
40
41 return poll(initializer_list<int>{pipes[0]});
42 }