forked from fuzziqersoftware/phosg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Process.cc
471 lines (404 loc) · 12.2 KB
/
Process.cc
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#include "Process.hh"
#ifndef WINDOWS
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef MACOSX
#include <libproc.h>
#include <sys/proc_info.h>
#else
#include <signal.h>
#endif
#include "Filesystem.hh"
#include "Strings.hh"
#include "Time.hh"
using namespace std;
unique_ptr<FILE, void(*)(FILE*)> popen_unique(const string& command,
const string& mode) {
unique_ptr<FILE, void(*)(FILE*)> f(
popen(command.c_str(), mode.c_str()),
[](FILE* f) {
pclose(f);
});
if (!f.get()) {
throw cannot_open_file(command);
}
return f;
}
string name_for_pid(pid_t pid) {
string command = string_printf("ps -ax -c -o pid -o command | grep ^\\ *%u\\ | sed s/[0-9]*\\ //g", pid);
auto f = popen_unique(command.c_str(), "r");
string name;
int ch;
while ((ch = fgetc(f.get())) != EOF) {
if (ch >= 0x20 && ch < 0x7F) {
name += ch;
}
}
return name;
}
pid_t pid_for_name(const string& name, bool search_commands, bool exclude_self) {
pid_t self_pid = exclude_self ? getpid() : 0;
pid_t pid = 0;
for (const auto& it : list_processes(search_commands)) {
if (!strcasestr(it.second.c_str(), name.c_str())) {
continue;
}
if (pid == self_pid) {
continue;
}
if (pid) {
throw runtime_error("multiple processes found");
}
pid = it.first;
}
if (pid) {
return pid;
}
throw out_of_range("no processes found");
}
// calls the specified callback once for each process
unordered_map<pid_t, string> list_processes(bool with_commands) {
auto f = popen_unique(with_commands ? "ps -ax -o pid -o command | grep [0-9]" :
"ps -ax -c -o pid -o command | grep [0-9]", "r");
unordered_map<pid_t, string> ret;
while (!feof(f.get())) {
pid_t pid;
fscanf(f.get(), "%u", &pid);
int ch;
while ((ch = fgetc(f.get())) == ' ');
ungetc(ch, f.get());
string name;
while (ch != '\n') {
ch = fgetc(f.get());
if (ch == EOF) {
break;
}
if (ch >= 0x20 && ch < 0x7F) {
name += (char)ch;
}
}
if (!name.empty()) {
ret.emplace(pid, name);
}
}
return ret;
}
bool pid_exists(pid_t pid) {
if (!kill(pid, 0)) {
return true;
}
if (errno == ESRCH) {
return false;
}
return true;
}
#ifdef LINUX
bool pid_is_zombie(pid_t pid) {
// so many syscalls... sigh
char status_data[2048]; // this is probably big enough
try {
string filename = string_printf("/proc/%d/status", pid);
scoped_fd fd(filename, O_RDONLY);
ssize_t bytes_read = read(fd, status_data, 2047);
if (bytes_read < 0) {
throw runtime_error("can\'t read stat file for pid " + to_string(pid));
}
status_data[bytes_read] = '\0';
} catch (const cannot_open_file& e) {
return false; // non-running processes are not zombies
}
char* state = strstr(status_data, "\nState:");
if (!state) {
return false;
}
state += skip_whitespace(state + 7, 0) + 7; // +7 to skip over "\nState:"
return (*state == 'Z');
}
#endif
uint64_t start_time_for_pid(pid_t pid, bool allow_zombie) {
#ifdef MACOSX
struct proc_taskallinfo ti;
int ret = proc_pidinfo(pid, PROC_PIDTASKALLINFO, 0, &ti, sizeof(ti));
if (ret <= 0) {
if (errno == ESRCH) {
return 0;
}
throw runtime_error("can\'t get start time for pid " + to_string(pid) +
": " + string_for_error(errno));
}
if (ret < sizeof(ti)) {
throw runtime_error("can\'t get start time for pid " + to_string(pid) +
": " + string_for_error(errno));
}
return (uint64_t)ti.pbsd.pbi_start_tvsec * 1000000 +
(uint64_t)ti.pbsd.pbi_start_tvusec;
#else
uint64_t start_time;
try {
struct stat st = stat(string_printf("/proc/%d", pid));
start_time = (uint64_t)st.st_mtim.tv_sec * 1000000000 +
(uint64_t)st.st_mtim.tv_nsec;
} catch (const cannot_stat_file& e) {
return 0;
}
if (!allow_zombie && pid_is_zombie(pid)) {
return 0;
}
return start_time;
#endif
}
static bool atfork_handler_added = false;
static pid_t cached_this_process_pid = 0;
static uint64_t cached_this_process_start_time = 0;
static void clear_cached_pid_vars() {
cached_this_process_pid = 0;
cached_this_process_start_time = 0;
// TODO: do pthread_atfork() handlers survive in the child process? if not,
// we should set atfork_handler_added to false here
}
static void maybe_add_atfork_handler() {
if (!atfork_handler_added) {
pthread_atfork(NULL, NULL, clear_cached_pid_vars);
atfork_handler_added = true;
}
}
pid_t getpid_cached() {
if (!cached_this_process_pid) {
maybe_add_atfork_handler();
cached_this_process_pid = getpid();
}
return cached_this_process_pid;
}
uint64_t this_process_start_time() {
if (!cached_this_process_start_time) {
// don't need to call maybe_add_atfork_handler; getpid_cached will do it
cached_this_process_start_time = start_time_for_pid(getpid_cached());
}
return cached_this_process_start_time;
}
static void replace_fd(int oldfd, int newfd) {
if (oldfd != newfd) {
dup2(oldfd, newfd);
close(oldfd);
}
}
Subprocess::Subprocess(const vector<string>& cmd, int stdin_fd, int stdout_fd,
int stderr_fd, const string* cwd, const unordered_map<string, string>* env)
: stdin_write_fd(-1), stdout_read_fd(-1), stderr_read_fd(-1), child_pid(0),
exit_status(-1) {
vector<int> parent_fds_to_close;
if (stdin_fd == -1) {
auto pipefds = pipe();
stdin_fd = pipefds.first;
this->stdin_write_fd = pipefds.second;
parent_fds_to_close.emplace_back(stdin_fd);
}
if (stdout_fd == -1) {
auto pipefds = pipe();
this->stdout_read_fd = pipefds.first;
stdout_fd = pipefds.second;
parent_fds_to_close.emplace_back(stdout_fd);
}
if (stderr_fd == -1) {
auto pipefds = pipe();
this->stderr_read_fd = pipefds.first;
stderr_fd = pipefds.second;
parent_fds_to_close.emplace_back(stderr_fd);
}
this->child_pid = fork();
if (this->child_pid == -1) {
throw runtime_error("fork failed: " + string_for_error(errno));
}
if (!this->child_pid) {
replace_fd(stdin_fd, 0);
replace_fd(stdout_fd, 1);
replace_fd(stderr_fd, 2);
close(this->stdin_write_fd);
close(this->stdout_read_fd);
close(this->stderr_read_fd);
if (cwd) {
chdir(cwd->c_str());
}
// make the argv list. this is ugly but it will be blown away by execve
vector<const char*> argv;
for (const string& s : cmd) {
argv.emplace_back(s.c_str());
}
argv.emplace_back(nullptr);
if (env) {
vector<string> environ;
vector<const char*> envp;
for (const auto& it : *env) {
environ.emplace_back(string_printf("%s=%s", it.first.c_str(), it.second.c_str()));
envp.emplace_back(environ.back().c_str());
}
envp.emplace_back(nullptr);
execve(cmd[0].c_str(), (char* const *)argv.data(), (char* const *)envp.data());
} else {
execvp(cmd[0].c_str(), (char* const *)argv.data());
}
}
for (int fd : parent_fds_to_close) {
close(fd);
}
}
Subprocess::~Subprocess() {
if (this->wait(true) == -1) {
this->kill(SIGKILL);
this->wait();
}
}
int Subprocess::stdin() {
return this->stdin_write_fd;
}
int Subprocess::stdout() {
return this->stdout_read_fd;
}
int Subprocess::stderr() {
return this->stderr_read_fd;
}
pid_t Subprocess::pid() {
return this->child_pid;
}
int Subprocess::wait(bool poll) {
if (this->exit_status >= 0) {
return this->exit_status;
}
int ret = waitpid(this->child_pid, &this->exit_status, poll ? WNOHANG : 0);
if (ret == -1) {
throw runtime_error("waitpid failed: " + string_for_error(errno));
}
if (ret == 0) {
return -1; // not terminated yet
}
assert(ret == this->child_pid);
return this->exit_status;
}
void Subprocess::kill(int signum) {
if (::kill(this->child_pid, signum)) {
throw runtime_error("kill failed: " + string_for_error(errno));
}
}
SubprocessResult::SubprocessResult() : elapsed_time(now()) { }
static const size_t READ_BLOCK_SIZE = 128 * 1024;
SubprocessResult run_process(const vector<string>& cmd, const string* stdin_data,
bool check, const std::string* cwd,
const std::unordered_map<std::string, std::string>* env,
size_t timeout_usecs) {
SubprocessResult ret;
bool terminated = false;
uint64_t start_time = now();
Subprocess sp(cmd, -1, -1, -1, cwd, env);
make_fd_nonblocking(sp.stdin());
make_fd_nonblocking(sp.stdout());
make_fd_nonblocking(sp.stderr());
struct Buffer {
const string* buf;
size_t offset;
Buffer(const string* buf) : buf(buf), offset(0) { }
};
unordered_map<int, Buffer> write_fd_to_buffer;
unordered_map<int, string*> read_fd_to_buffer;
Poll p;
if (stdin_data) {
write_fd_to_buffer.emplace(sp.stdin(), stdin_data);
p.add(sp.stdin(), POLLOUT);
} else {
close(sp.stdin());
}
read_fd_to_buffer.emplace(sp.stdout(), &ret.stdout_contents);
p.add(sp.stdout(), POLLIN);
read_fd_to_buffer.emplace(sp.stderr(), &ret.stderr_contents);
p.add(sp.stderr(), POLLIN);
// read/write to pipes as long as the process is running
while ((ret.exit_status = sp.wait(true)) == -1) {
for (const auto& pfd : p.poll(1000)) {
if (pfd.second & POLLIN) {
string* buf = read_fd_to_buffer.at(pfd.first);
size_t read_offset = buf->size();
buf->resize(read_offset + READ_BLOCK_SIZE);
ssize_t bytes_read = read(pfd.first,
const_cast<char*>(buf->data()) + read_offset, READ_BLOCK_SIZE);
if (bytes_read > 0) {
buf->resize(read_offset + bytes_read);
} else if (bytes_read < 0) {
buf->resize(read_offset);
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
continue;
}
throw runtime_error("read failed: " + string_for_error(errno));
} else { // bytes_read == 0; usually means the pipe is broken
buf->resize(read_offset);
p.remove(pfd.first, true);
read_fd_to_buffer.erase(pfd.first);
}
}
if (pfd.second & POLLOUT) {
auto& buf = write_fd_to_buffer.at(pfd.first);
size_t bytes_to_write = buf.buf->size() - buf.offset;
ssize_t bytes_written = write(pfd.first,
buf.buf->data() + buf.offset, bytes_to_write);
if (bytes_written > 0) {
buf.offset += bytes_written;
if (buf.offset == buf.buf->size()) {
p.remove(sp.stdin(), true);
write_fd_to_buffer.erase(pfd.first);
}
} else if (bytes_written < 0) {
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
continue;
}
throw runtime_error("write failed: " + string_for_error(errno));
} else { // bytes_written == 0; usually means the pipe is broken
p.remove(pfd.first, true);
write_fd_to_buffer.erase(pfd.first);
}
}
}
if (timeout_usecs && (start_time < now() - timeout_usecs)) {
if (!terminated) {
sp.kill(SIGTERM);
terminated = true;
timeout_usecs = 5000000;
start_time = now();
} else {
sp.kill(SIGKILL);
}
}
}
ret.elapsed_time = now() - ret.elapsed_time;
// read any leftover data after termination
for (auto& it : read_fd_to_buffer) {
for (;;) {
size_t read_offset = it.second->size();
it.second->resize(read_offset + READ_BLOCK_SIZE);
ssize_t bytes_read = read(it.first,
const_cast<char*>(it.second->data()) + read_offset, READ_BLOCK_SIZE);
if (bytes_read > 0) {
it.second->resize(it.second->size() - READ_BLOCK_SIZE + bytes_read);
} else if (bytes_read < 0) {
it.second->resize(read_offset);
if (errno == EAGAIN || errno == EINTR || errno == EWOULDBLOCK) {
break;
}
throw runtime_error("read failed: " + string_for_error(errno));
} else { // bytes_read == 0; usually means the pipe is broken
it.second->resize(read_offset);
break;
}
}
}
if (check && sp.wait()) {
throw runtime_error(string_printf("command returned code %d\nstdout:\n%s\nstderr:\n%s",
sp.wait(), ret.stdout_contents.c_str(), ret.stderr_contents.c_str()));
}
return ret;
}
#endif