blob: 20110414e81760eaa08cf0988d923fa00917e3da (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
* This source code is released into the public domain.
*/
#include "generic_error.hh"
#include "process.hh"
namespace lfjail {
/*
* wait_result
*/
wait_result::wait_result(int status)
: _status(status)
{}
auto wait_result::okay(this wait_result const &self) -> bool {
if (self.signal())
return false;
return *self.status() == 0;
}
wait_result::operator bool(this wait_result const &self) {
return self.okay();
}
// Return the exit status, if any.
auto wait_result::status(this wait_result const &self) -> std::optional<int> {
if (WIFEXITED(self._status))
return WEXITSTATUS(self._status);
return {};
}
// Return the exit signal, if any.
auto wait_result::signal(this wait_result const &self) -> std::optional<int> {
if (WIFSIGNALED(self._status))
return WTERMSIG(self._status);
return {};
}
/*
* process
*/
process::process(::pid_t pid)
: _pid(pid)
{}
// Wait for the child process to exit and return its exit status.
auto process::wait(this process &&self) -> wait_result {
auto status = int{};
auto ret = waitpid(self._pid, &status, WEXITED);
self._pid = -1;
switch (ret) {
case -1:
throw generic_error("waitpid({}): failed: {}",
self._pid, strerror(errno));
case 0:
throw generic_error("waitpid({}): no child to wait",
self._pid);
}
return wait_result(status);
}
auto process::release(this process &&self) -> ::pid_t {
auto const ret = self._pid;
self._pid = -1;
return ret;
}
process::~process() {
if (_pid == -1)
return;
auto status = int{};
std::ignore = waitpid(_pid, &status, WEXITED);
}
}
|