-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
proc.rs
314 lines (285 loc) · 10.4 KB
/
proc.rs
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
use futures_util::FutureExt;
use nix::sys::signal::{self, Signal};
use nix::unistd::Pid;
use rebuilderd_common::errors::*;
use std::cmp;
use std::collections::HashMap;
use std::ffi::OsStr;
use std::fmt;
use std::path::Path;
use std::process::Stdio;
use std::time::{Duration, Instant};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::process::{Command, Child};
use tokio::select;
use tokio::time;
const SIGKILL_DELAY: u64 = 10;
pub struct Options {
pub timeout: Duration,
pub size_limit: Option<usize>,
pub kill_at_size_limit: bool,
pub passthrough: bool,
pub envs: HashMap<String, String>,
}
pub struct Capture<'a> {
output: &'a mut Vec<u8>,
timeout: Duration,
size_limit: Option<usize>,
kill_at_size_limit: bool,
start: Instant,
sigterm_sent: Option<Instant>,
truncated: bool,
}
pub fn capture(output: &mut Vec<u8>, opts: Options) -> Capture<'_> {
let start = Instant::now();
Capture {
output,
timeout: opts.timeout,
size_limit: opts.size_limit,
kill_at_size_limit: opts.kill_at_size_limit,
start,
sigterm_sent: None,
truncated: false,
}
}
impl Capture<'_> {
pub async fn push_bytes(&mut self, child: &mut Child, mut slice: &[u8]) -> Result<()> {
if !self.truncated {
if let Some(size_limit) = &self.size_limit {
let n = cmp::min(size_limit - self.output.len(), slice.len());
if n < 1 {
warn!("Exceeding output limit: output={}, slice={}, limit={}", self.output.len(), slice.len(), size_limit);
let msg = format!("TRUNCATED DUE TO SIZE LIMIT: {} bytes", size_limit);
self.truncate(child, &msg, self.kill_at_size_limit).await?;
return Ok(());
} else {
// truncate to stay within the limit
slice = &slice[..n];
}
}
self.output.extend(slice);
}
Ok(())
}
fn kill(pid: u32, signal: Signal) -> Result<()> {
// convert 1234 to -1234 to kill grand-children too
let pid = -(pid as i32);
info!("Sending {} to child(pid={})", signal, pid);
signal::kill(Pid::from_raw(pid), signal)?;
Ok(())
}
async fn truncate(&mut self, child: &mut Child, reason: &str, kill: bool) -> Result<()> {
if kill {
if let Some(pid) = child.id() {
Self::kill(pid, Signal::SIGTERM)?;
}
self.sigterm_sent = Some(Instant::now());
}
self.output.extend(format!("\n\n{}\n\n", reason).as_bytes());
self.truncated = true;
Ok(())
}
pub async fn next_wakeup(&mut self, child: &mut Child, stdout_open: &mut bool, stderr_open: &mut bool) -> Result<Duration> {
// check if we need to SIGKILL due to SIGTERM timeout
if let Some(sigterm_sent) = self.sigterm_sent {
if sigterm_sent.elapsed() > Duration::from_secs(SIGKILL_DELAY) {
if let Some(pid) = child.id() {
warn!("child(pid={}) didn't terminate {}s after SIGTERM, sending SIGKILL", pid, SIGKILL_DELAY);
// child.id is going to return None after this
Self::kill(pid, Signal::SIGKILL)?;
*stdout_open = false;
*stderr_open = false;
}
}
}
// check if the process timed out and we need to SIGTERM
if let Some(remaining) = self.timeout.checked_sub(self.start.elapsed()) {
return Ok(remaining);
} else if self.sigterm_sent.is_none() {
// the process has timed out, sending SIGTERM
warn!("child timed out, killing...");
let msg = format!("TRUNCATED DUE TO TIMEOUT: {} seconds", self.timeout.as_secs());
self.truncate(child, &msg, true).await?;
}
// if we don't need any timeouts anymore we just return any value
Ok(Duration::from_secs(SIGKILL_DELAY))
}
}
pub async fn run<I, S>(bin: &Path, args: I, opts: Options, log: &mut Vec<u8>) -> Result<bool>
where I: IntoIterator<Item = S> + fmt::Debug,
S: AsRef<OsStr>,
{
info!("Running {:?} {:?}", bin, args);
let mut cmd = Command::new(bin);
cmd
.args(args)
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.envs(&opts.envs);
unsafe {
cmd.pre_exec(|| {
// create a new process group
let pid = nix::unistd::getpid();
if let Err(err) = nix::unistd::setpgid(pid, Pid::from_raw(0)) {
warn!("Failed to create new process group: {:#?}", err);
}
Ok(())
});
}
let mut child = cmd.spawn()?;
let mut child_stdout = child.stdout.take().unwrap();
let mut child_stderr = child.stderr.take().unwrap();
let mut buf_stdout = [0u8; 4096];
let mut buf_stderr = [0u8; 4096];
let mut stdout = tokio::io::stdout();
let mut stderr = tokio::io::stderr();
let passthrough = opts.passthrough;
let mut stdout_open = true;
let mut stderr_open = true;
let mut cap = capture(log, opts);
let success = loop {
let remaining = cap.next_wakeup(&mut child, &mut stdout_open, &mut stderr_open).await?;
if stdout_open || stderr_open {
select! {
n = child_stdout.read(&mut buf_stdout).fuse() => {
let n = n?;
trace!("read stdout: {}", n);
if n == 0 {
stdout_open = false;
} else {
cap.push_bytes(&mut child, &buf_stdout[..n]).await?;
if passthrough {
stdout.write_all(&buf_stdout[..n]).await?;
}
}
},
n = child_stderr.read(&mut buf_stderr).fuse() => {
let n = n?;
trace!("read stderr: {}", n);
if n == 0 {
stderr_open = false;
} else {
cap.push_bytes(&mut child, &buf_stderr[..n]).await?;
if passthrough {
stderr.write_all(&buf_stderr[..n]).await?;
}
}
},
_ = time::sleep(remaining).fuse() => continue,
}
} else {
select! {
status = child.wait().fuse() => {
let status = status?;
info!("{:?} exited with exit={}, captured {} bytes", bin, status, log.len());
break status.success();
}
_ = time::sleep(remaining).fuse() => continue,
}
}
};
Ok(success)
}
#[cfg(test)]
mod tests {
use super::*;
async fn script(script: &str, opts: Options) -> Result<(bool, String, Duration)> {
let start = Instant::now();
let path = Path::new("sh");
let mut output = Vec::new();
let success = run(path, &["-c", script], opts, &mut output).await?;
let duration = start.elapsed();
let output = String::from_utf8_lossy(&output).into_owned();
Ok((success, output, duration))
}
#[tokio::test]
async fn hello_world() {
let (success, output, _) = script("/bin/echo hello world", Options {
timeout: Duration::from_secs(600),
size_limit: None,
kill_at_size_limit: false,
passthrough: false,
envs: HashMap::new(),
}).await.unwrap();
assert!(success);
assert_eq!(output, "hello world\n");
}
#[tokio::test]
async fn size_limit_no_kill() {
let (success, output, _) = script("
for x in `seq 100`; do
/bin/echo AAAAAAAAAAAAAAAAAAAAAAAA
done
", Options {
timeout: Duration::from_secs(600),
size_limit: Some(50),
kill_at_size_limit: false,
passthrough: false,
envs: HashMap::new(),
}).await.unwrap();
assert!(success);
assert_eq!(output,
"AAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAA\n\n\nTRUNCATED DUE TO SIZE LIMIT: 50 bytes\n\n");
}
#[tokio::test]
async fn size_limit_kill() {
let (success, output, duration) = script("
for x in `seq 100`; do
/bin/echo AAAAAAAAAAAAAAAAAAAAAAAA
sleep 0.5
done
", Options {
timeout: Duration::from_secs(600),
size_limit: Some(50),
kill_at_size_limit: true,
passthrough: false,
envs: HashMap::new(),
}).await.unwrap();
assert!(!success);
assert_eq!(output,
"AAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAA\n\n\nTRUNCATED DUE TO SIZE LIMIT: 50 bytes\n\n");
assert!(duration > Duration::from_secs(1));
assert!(duration < Duration::from_secs(5));
}
#[tokio::test]
async fn timeout() {
let (success, output, duration) = script("
for x in `seq 100`; do
/bin/echo AAAAAAAAAAAAAAAAAAAAAAAA
sleep 1
done
", Options {
timeout: Duration::from_millis(1500),
size_limit: None,
kill_at_size_limit: false,
passthrough: false,
envs: HashMap::new(),
}).await.unwrap();
assert!(!success);
assert_eq!(output,
"AAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAA\n\n\nTRUNCATED DUE TO TIMEOUT: 1 seconds\n\n");
assert!(duration > Duration::from_secs(1));
assert!(duration < Duration::from_secs(3));
}
#[tokio::test]
async fn size_limit_no_kill_but_timeout() {
let (success, output, duration) = script("
for x in `seq 100`; do
/bin/echo AAAAAAAAAAAAAAAAAAAAAAAA
sleep 0.1
done
", Options {
timeout: Duration::from_millis(1500),
size_limit: Some(50),
kill_at_size_limit: false,
passthrough: false,
envs: HashMap::new(),
}).await.unwrap();
assert!(!success);
assert_eq!(output,
"AAAAAAAAAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAA\n\n\nTRUNCATED DUE TO SIZE LIMIT: 50 bytes\n\n\n\nTRUNCATED DUE TO TIMEOUT: 1 seconds\n\n");
assert!(duration > Duration::from_secs(1));
assert!(duration < Duration::from_secs(2));
}
}