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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "spawner.hh"
#include "image_processor.hh"
#include "io.hh"
#include "unique_fd.hh"
#include <cassert>
#include <csignal>
#include <expected>
#include <linux/prctl.h>
#include <memory>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <utility>
namespace {
int pidfd_getfd(int pidfd, int targetfd, unsigned int flags) {
return static_cast<int>(syscall(SYS_pidfd_getfd, pidfd, targetfd, flags));
}
// NOLINTNEXTLINE(misc-include-cleaner)
int pidfd_open(pid_t pid, unsigned int flags) {
return static_cast<int>(syscall(SYS_pidfd_open, pid, flags));
}
class ProcessImpl : public Process {
public:
ProcessImpl(
pid_t pid,
std::pair<std::unique_ptr<io::Reader>, std::unique_ptr<io::Writer>> pipe)
: pid_(pid), pipe_(std::move(pipe)) {
assert(pid_);
}
[[nodiscard]]
pid_t pid() const { return pid_; }
[[nodiscard]]
io::Reader& reader() const override { return *pipe_.first; }
[[nodiscard]]
io::Writer& writer() const override { return *pipe_.second; }
void kill() {
if (pid_ != 0) {
::kill(pid_, SIGTERM); // NOLINT(misc-include-cleaner)
pid_ = 0;
}
}
private:
pid_t pid_;
std::pair<std::unique_ptr<io::Reader>, std::unique_ptr<io::Writer>> pipe_;
};
struct Request {
Spawner::Exec exec;
int reader;
int writer;
};
struct Response {
pid_t pid;
};
void spawner_runner(unique_fd parent, std::unique_ptr<io::Reader> reader,
std::unique_ptr<io::Writer> writer) {
while (true) {
Request request;
{
auto ret = reader->repeat_read(&request, sizeof(request));
if (!ret.has_value() || ret.value() != sizeof(request)) {
break;
}
}
// Need to not return a response before child has duped fds.
// So use a short-lived pipe to sync child start.
auto sync_pipe = io::pipe();
pid_t child_pid = 0;
if (sync_pipe.has_value()) {
child_pid = fork();
if (child_pid == 0) {
// Child process
sync_pipe->first.reset();
reader.reset();
writer.reset();
unique_fd child_reader_fd{pidfd_getfd(parent.get(), request.reader, 0)};
unique_fd child_writer_fd{pidfd_getfd(parent.get(), request.writer, 0)};
parent.reset();
if (child_reader_fd && child_writer_fd) {
char c = 1;
if (sync_pipe->second->write(&c, sizeof(c)).has_value()) {
auto child_reader = io::reader_from_raw(child_reader_fd.release());
auto child_writer = io::writer_from_raw(child_writer_fd.release());
switch (request.exec) {
case Spawner::Exec::kImageProcessor:
_exit(image_processor::run(std::move(child_reader),
std::move(child_writer)));
}
}
}
_exit(1);
// _exit obviously never returns but to help tools and compilers…
return;
}
// Parent process
if (child_pid == -1) {
// fork() failed, we use zero as error value.
child_pid = 0;
} else {
sync_pipe->second.reset();
char c = 1;
std::ignore = sync_pipe->first->read(&c, sizeof(c));
}
}
Response response{child_pid};
{
auto ret = writer->repeat_write(&response, sizeof(response));
if (!ret.has_value() || ret.value() != sizeof(response)) {
break;
}
}
}
}
class SpawnerImpl : public Spawner {
public:
explicit SpawnerImpl(std::unique_ptr<ProcessImpl> process)
: process_(std::move(process)) {}
~SpawnerImpl() override {
if (process_) {
process_->kill();
}
}
[[nodiscard]]
std::expected<std::unique_ptr<Process>, Error> run(Exec exec) override {
if (!process_) {
return std::unexpected(Error::kError);
}
auto reader_pipe = io::pipe();
auto writer_pipe = io::pipe();
if (!reader_pipe.has_value() || !writer_pipe.has_value()) {
return std::unexpected(Error::kError);
}
Request req{
.exec = exec,
.reader = writer_pipe->first->raw_fd(),
.writer = reader_pipe->second->raw_fd(),
};
{
auto ret = process_->writer().repeat_write(&req, sizeof(req));
if (!ret.has_value() || ret.value() != sizeof(req)) {
process_->kill();
process_.reset();
return std::unexpected(Error::kError);
}
}
Response resp;
{
auto ret = process_->reader().repeat_read(&resp, sizeof(resp));
if (!ret.has_value() || ret.value() != sizeof(resp)) {
process_->kill();
process_.reset();
return std::unexpected(Error::kError);
}
}
if (resp.pid == 0)
return std::unexpected(Error::kError);
return std::make_unique<ProcessImpl>(
resp.pid, std::make_pair(std::move(reader_pipe->first),
std::move(writer_pipe->second)));
}
private:
std::unique_ptr<ProcessImpl> process_;
};
} // namespace
std::expected<std::unique_ptr<Spawner>, Spawner::Error> Spawner::create() {
auto reader_pipe = io::pipe();
auto writer_pipe = io::pipe();
if (!reader_pipe.has_value() || !writer_pipe.has_value()) {
return std::unexpected(Error::kError);
}
auto pid = getpid();
unique_fd pidfd{pidfd_open(pid, 0)};
if (!pidfd) {
return std::unexpected(Error::kError);
}
// Needed for pidfd_getfd to work in child.
if (prctl(PR_SET_PTRACER, pid) != 0) {
return std::unexpected(Error::kError);
}
pid = fork();
if (pid == 0) {
// Child process
reader_pipe->first.reset();
writer_pipe->second.reset();
spawner_runner(std::move(pidfd), std::move(writer_pipe->first),
std::move(reader_pipe->second));
_exit(0);
}
// Parent process
if (pid == -1) {
return std::unexpected(Error::kError);
}
return std::make_unique<SpawnerImpl>(std::make_unique<ProcessImpl>(
pid, std::make_pair(std::move(reader_pipe->first),
std::move(writer_pipe->second))));
}
|