-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.hpp
264 lines (237 loc) · 6.71 KB
/
process.hpp
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#pragma once
#include <sys/wait.h>
#include <sys/poll.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cerrno>
#include <future>
#include <optional>
namespace kiq {
struct proc_result_t
{
std::string output;
bool error{false};
int termination_code{0};
std::string err_msg;
};
using args_t = std::vector<std::string>;
using proc_fut_t = std::future<proc_result_t>;
using opt_fut_t = std::optional<proc_fut_t>;
struct proc_wrap_t
{
proc_result_t result;
opt_fut_t fut;
};
//--------------------------------------------------------------------
static const std::string get_current_working_directory()
{
std::string ret;
char buffer[PATH_MAX];
if (getcwd(buffer, sizeof(buffer)) != nullptr)
ret = std::string(buffer);
return ret;
}
//--------------------------------------------------------------------
static const uint32_t buf_size{32768};
//--------------------------------------------------------------------
static std::string read_fd(int fd)
{
char buffer[buf_size];
std::string s;
ssize_t r;
while ((r = read(fd, buffer, buf_size)) > 0)
s.append(buffer, r);
if (r < 0)
std::cerr << "Reading from fd returned error: " << std::strerror(errno) << std::endl;
return s;
}
//--------------------------------------------------------------------
static proc_wrap_t qx(const args_t& args,
int timeout_sec = 30,
bool kill_on_timeout = false,
bool handle_process = true,
const std::string& working_directory = "")
{
int stdout_fds[2];
int stderr_fds[2];
pipe(stdout_fds);
pipe(stderr_fds);
const pid_t pid = fork();
if (!pid) // Child
{
if (!working_directory.empty()) chdir(working_directory.c_str());
close(stdout_fds[0]);
dup2 (stdout_fds[1], 1);
close(stdout_fds[1]);
close(stderr_fds[0]);
dup2 (stderr_fds[1], 2);
close(stderr_fds[1]);
std::vector<char*> vc(args.size() + 1, 0);
for (size_t i = 0; i < args.size(); ++i)
vc[i] = const_cast<char*>(args[i].c_str());
execvp(vc[0], &vc[0]);
exit(0);
}
// Parent
close(stdout_fds[1]);
close(stderr_fds[1]);
auto handler = [pid, stdout_fds, stderr_fds, timeout_sec, kill_on_timeout]
{
proc_result_t result;
pollfd poll_fds[2] {
pollfd{
.fd = stdout_fds[0] & 0xFF,
.events = POLL_OUT | POLL_ERR | POLL_IN,
.revents = short{0}
},
pollfd{
.fd = stderr_fds[0] & 0xFF,
.events = POLLHUP | POLLERR | POLLIN,
.revents = short{0}
}};
int retries = 0;
const auto timeout = timeout_sec * 1000;
do
{
if (const int poll_result = poll(poll_fds, 2, timeout); !poll_result)
{
if (timeout)
{
if (kill_on_timeout)
kill(pid, SIGKILL);
result.error = true;
result.err_msg = "Child process timed out";
}
else
result.output = "Non-block poll() returned immediately with no results";
break;
}
else
if (poll_result == -1)
{
result.error = true;
result.err_msg = "Errno while calling poll(): " + std::string(std::strerror(errno));
break;
}
else
if (poll_result)
{
if (poll_fds[1].revents & POLLIN)
{
result.err_msg += read_fd(poll_fds[1].fd);
result.error = true;
std::cout << "Error was: " << result.err_msg << std::endl;
}
if (poll_fds[0].revents & POLLIN)
{
result.output = read_fd(poll_fds[0].fd);
break;
}
else
if (poll_fds[0].revents & POLLHUP)
{
result.err_msg += "Lost connection to forked process";
result.error = true;
break;
}
}
}
while (++retries < 2);
close(stdout_fds[0]);
close(stderr_fds[0]);
int status;
if (const pid_t wait_result = waitpid(pid, &status, (WNOHANG | WUNTRACED | WCONTINUED)); wait_result == -1)
result.err_msg = "Error waiting for " + pid + std::string(". Error: " + std::string(std::strerror(errno)));
else
if (!wait_result) // unchanged
{
if (result.output.empty())
result.output = "Forked process may not have completed";
}
else
{
if (WIFEXITED(status))
result.termination_code = WEXITSTATUS(status);
else
if (WIFSIGNALED(status))
result.output += "Child process terminated by signal: " + std::to_string(WTERMSIG(status));
}
return result;
};
proc_wrap_t process;
if (handle_process)
process.result = handler();
else
process.fut = opt_fut_t{std::async(std::launch::async, handler)};
return process;
}
//--------------------------------
//--------------------------------
//--------------------------------
struct process
{
process(const args_t& args,
int timeout_sec = 30,
bool kill_on_timeout = false,
bool handle_process = true,
const std::string& working_directory = "")
{
process_ = qx(args, timeout_sec, kill_on_timeout, handle_process, working_directory);
}
//--------------------------------
bool has_error() const
{
return process_.result.error || !process_.result.err_msg.empty();
}
//--------------------------------
bool has_work() const
{
return process_.result.output.empty() && process_.fut.has_value();
}
//--------------------------------
bool is_ready() const
{
return !process_.result.output.empty();
}
//--------------------------------
int exit_code() const
{
return process_.result.termination_code;
}
//--------------------------------
void do_work(int wait_time = 30) const
{
if (process_.fut.has_value())
process_.fut.value().wait_for(std::chrono::milliseconds(wait_time));
}
//-------------------------------
std::string get_error() const
{
return process_.result.err_msg;
}
//-------------------------------
proc_result_t get() const
{
return process_.result;
}
//-------------------------------
std::string preview() const
{
if (!is_ready())
return "";
if (process_.result.output.size() > 1000)
return process_.result.output.substr(0, 1000);
return process_.result.output;
}
//--------------------------------
//--------------------------------
private:
proc_wrap_t process_;
pid_t pid;
int stdout_fds[2];
int stderr_fds[2];
};
} //ns kiq