-
Notifications
You must be signed in to change notification settings - Fork 0
/
popen2.h
185 lines (158 loc) · 5.14 KB
/
popen2.h
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
// NOTE(dkorolev: https://github.com/dkorolev/c5t_april2024_demo has the most recent version of this code as of now!!!
#pragma once
#include <atomic>
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/eventfd.h>
#include <poll.h>
#include <string>
#include <thread>
#include <functional>
#include "bricks/strings/group_by_lines.h"
template <typename F>
static void MutableCStyleVectorStringsArg(const std::vector<std::string>& in, F&& f) {
std::vector<std::vector<char>> mutable_in;
mutable_in.resize(in.size());
for (size_t i = 0u; i < in.size(); ++i) {
mutable_in[i].assign(in[i].c_str(), in[i].c_str() + in[i].length() + 1u);
}
std::vector<char*> out;
for (auto& e : mutable_in) {
out.push_back(&e[0]);
}
out.push_back(nullptr);
f(&out[0]);
}
inline void pipe_or_fail(int r[2]) {
if (pipe(r)) {
std::cerr << "FATAL: " << __LINE__ << std::endl;
::abort();
}
}
struct Popen2Runtime {
virtual ~Popen2Runtime() = default;
virtual void operator()(std::string const& write) = 0;
virtual void Kill() = 0;
// TODO(dkorolev): Add `.Close()` as well? And test it?
};
inline int popen2(
std::vector<std::string> const& cmdline,
std::function<void(const std::string&)> cb_line,
std::function<void(Popen2Runtime&)> cb_runtime = [](Popen2Runtime&) {},
std::vector<std::string> const& env = {}) {
pid_t pid;
int pipe_stdin[2];
int pipe_stdout[2];
pipe_or_fail(pipe_stdin);
pipe_or_fail(pipe_stdout);
int const efd = eventfd(0, 0);
if (efd < 0) {
std::cerr << "FATAL: " << __LINE__ << std::endl;
::abort();
}
pid = fork();
if (pid < 0) {
std::cerr << "FATAL: " << __LINE__ << std::endl;
::abort();
}
if (pid == 0) {
// Child.
::close(pipe_stdin[1]);
dup2(pipe_stdin[0], 0);
::close(pipe_stdout[0]);
dup2(pipe_stdout[1], 1);
dup2(pipe_stdout[1], 2); // Capture `stderr` too.
if (env.empty()) {
MutableCStyleVectorStringsArg(cmdline, [&](char* const argv[]) {
int r = execvp(cmdline[0].c_str(), argv);
std::cerr << "FATAL: " << __LINE__ << " R=" << r << ", errno=" << errno << std::endl;
perror("execvp");
});
} else {
MutableCStyleVectorStringsArg(cmdline, [&](char* const argv[]) {
MutableCStyleVectorStringsArg(env, [&](char* const envp[]) {
int r = execvpe(cmdline[0].c_str(), argv, envp);
std::cerr << "FATAL: " << __LINE__ << " R=" << r << ", errno=" << errno << std::endl;
perror("execvpe");
});
});
}
}
::close(pipe_stdin[0]);
::close(pipe_stdout[1]);
struct TrivialPopen2Runtime final : Popen2Runtime {
std::function<void(std::string const&)> write_;
std::function<void()> kill_;
void operator()(std::string const& data) override { write_(data); }
void Kill() override { kill_(); }
};
TrivialPopen2Runtime runtime_context;
std::shared_ptr<std::atomic_bool> already_done = std::make_shared<std::atomic_bool>(false);
std::thread thread_user_code(
[copy_already_done_or_killed = already_done, &runtime_context](
std::function<void(Popen2Runtime&)> cb_code, int write_fd, int pid) {
runtime_context.write_ = [write_fd](std::string const& s) {
ssize_t const n = write(write_fd, s.c_str(), s.length());
if (n < 0 || static_cast<size_t>(n) != s.length()) {
return false;
} else {
return true;
}
};
runtime_context.kill_ = [pid, moved_already_done_or_killed = std::move(copy_already_done_or_killed)]() {
if (!*moved_already_done_or_killed) {
*moved_already_done_or_killed = true;
kill(pid, SIGTERM);
}
};
cb_code(runtime_context);
},
std::move(cb_runtime),
pipe_stdin[1],
pid);
std::thread thread_reader(
[](std::function<void(const std::string&)> cb_line, int read_fd, int efd) {
struct pollfd fds[2];
fds[0].fd = read_fd;
fds[0].events = POLLIN;
fds[1].fd = efd;
fds[1].events = POLLIN;
char buf[1000];
auto grouper = current::strings::CreateStatefulGroupByLines(std::move(cb_line));
while (true) {
::poll(fds, 2, -1);
if (fds[0].revents & POLLIN) {
ssize_t const n = read(read_fd, buf, sizeof(buf) - 1);
if (n < 0) {
// NOTE(dkorolev): This may or may not be a major issue.
// std::cerr << __LINE__ << std::endl;
break;
}
buf[n] = '\0';
grouper.Feed(buf);
} else if (fds[1].revents & POLLIN) {
// NOTE(dkorolev): Termination signaled.
// std::cerr << __LINE__ << std::endl;
break;
}
}
},
std::move(cb_line),
pipe_stdout[0],
efd);
::waitpid(pid, NULL, 0);
*already_done = true;
uint64_t u = 1;
if (write(efd, &u, sizeof(uint64_t)) != sizeof(uint64_t)) {
std::cerr << "FATAL: " << __LINE__ << std::endl;
::abort();
}
thread_user_code.join();
thread_reader.join();
::close(efd);
::close(pipe_stdin[1]);
::close(pipe_stdout[0]);
return 0;
}