feat: add approval bound full maintenance script console and executors
Rust development slice / linux (push) Failing after 6s
Rust development slice / linux (push) Failing after 6s
This commit is contained in:
+232
-108
@@ -15,11 +15,19 @@ pub struct Output {
|
||||
pub stderr: Vec<u8>,
|
||||
pub truncated: bool,
|
||||
pub timed_out: bool,
|
||||
pub interrupted: bool,
|
||||
}
|
||||
|
||||
// Nonblocking pipes avoid both unbounded output and reader threads held open by
|
||||
// grandchildren. The child has a separate process group, cleaned even on success.
|
||||
pub(crate) fn run(program: &str, args: &[String], timeout: Duration, cap: usize) -> Result<Output> {
|
||||
run_controlled(program, args, timeout, cap, || Ok(()))
|
||||
}
|
||||
pub(crate) fn run_controlled(
|
||||
program: &str,
|
||||
args: &[String],
|
||||
timeout: Duration,
|
||||
cap: usize,
|
||||
mut observe: impl FnMut() -> Result<()>,
|
||||
) -> Result<Output> {
|
||||
ensure!(!timeout.is_zero() && cap > 0, "invalid process limits");
|
||||
let mut command = Command::new(program);
|
||||
command
|
||||
@@ -34,144 +42,260 @@ pub(crate) fn run(program: &str, args: &[String], timeout: Duration, cap: usize)
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.process_group(0);
|
||||
observe()?;
|
||||
let mut child = command
|
||||
.spawn()
|
||||
.with_context(|| format!("spawn {program}"))?;
|
||||
struct Group(u32);
|
||||
// Keep the group leader unreaped until AFTER the last signal and cleanup.
|
||||
// Its reserved PID prevents the numerical PGID from becoming a foreign group.
|
||||
struct Group {
|
||||
pid: u32,
|
||||
reserved: bool,
|
||||
}
|
||||
impl Group {
|
||||
fn kill(&self) {
|
||||
if self.reserved {
|
||||
unsafe {
|
||||
libc::kill(-(self.pid as i32), libc::SIGKILL);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Drop for Group {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: negative PID addresses only the group created for this child.
|
||||
unsafe {
|
||||
libc::kill(-(self.0 as i32), libc::SIGKILL);
|
||||
}
|
||||
self.kill();
|
||||
}
|
||||
}
|
||||
let group = Group(child.id());
|
||||
let mut group = Group {
|
||||
pid: child.id(),
|
||||
reserved: true,
|
||||
};
|
||||
let mut stdout = child.stdout.take().context("missing stdout")?;
|
||||
let mut stderr = child.stderr.take().context("missing stderr")?;
|
||||
for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] {
|
||||
// SAFETY: these are live owned pipe descriptors; flags are not pointers.
|
||||
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
||||
if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
|
||||
drop(group);
|
||||
let _ = child.wait();
|
||||
return Err(std::io::Error::last_os_error().into());
|
||||
}
|
||||
}
|
||||
fn drain(
|
||||
pipe: &mut impl Read,
|
||||
target: &mut Vec<u8>,
|
||||
remaining: &mut usize,
|
||||
truncated: &mut bool,
|
||||
) -> Result<bool> {
|
||||
let mut bytes = [0; 4096];
|
||||
// Bound each pass too: a continuously writing child cannot starve timeout.
|
||||
for _ in 0..16 {
|
||||
match pipe.read(&mut bytes) {
|
||||
Ok(0) => return Ok(true),
|
||||
Ok(n) => {
|
||||
let keep = n.min(*remaining);
|
||||
target.extend_from_slice(&bytes[..keep]);
|
||||
*remaining -= keep;
|
||||
*truncated |= keep < n;
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
let started = Instant::now();
|
||||
let mut result = Output {
|
||||
let began = Instant::now();
|
||||
let mut output = Output {
|
||||
code: None,
|
||||
stdout: vec![],
|
||||
stderr: vec![],
|
||||
truncated: false,
|
||||
timed_out: false,
|
||||
interrupted: false,
|
||||
};
|
||||
let mut remaining = cap;
|
||||
let status = loop {
|
||||
let drained = drain(
|
||||
&mut stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)
|
||||
.and_then(|_| {
|
||||
let mut failure = None;
|
||||
for fd in [stdout.as_raw_fd(), stderr.as_raw_fd()] {
|
||||
let flags = unsafe { libc::fcntl(fd, libc::F_GETFL) };
|
||||
if flags < 0 || unsafe { libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
|
||||
failure = Some(anyhow::Error::from(std::io::Error::last_os_error()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if failure.is_none() {
|
||||
loop {
|
||||
if let Err(e) = drain(
|
||||
&mut stdout,
|
||||
&mut output.stdout,
|
||||
&mut remaining,
|
||||
&mut output.truncated,
|
||||
)
|
||||
.and_then(|_| {
|
||||
drain(
|
||||
&mut stderr,
|
||||
&mut output.stderr,
|
||||
&mut remaining,
|
||||
&mut output.truncated,
|
||||
)
|
||||
}) {
|
||||
failure = Some(e);
|
||||
break;
|
||||
}
|
||||
// WNOWAIT deliberately observes without releasing this child's PID.
|
||||
let mut info = std::mem::MaybeUninit::<libc::siginfo_t>::zeroed();
|
||||
let state = unsafe {
|
||||
libc::waitid(
|
||||
libc::P_PID,
|
||||
child.id(),
|
||||
info.as_mut_ptr(),
|
||||
libc::WEXITED | libc::WNOHANG | libc::WNOWAIT,
|
||||
)
|
||||
};
|
||||
if state < 0 {
|
||||
let e = std::io::Error::last_os_error();
|
||||
if e.kind() == std::io::ErrorKind::Interrupted {
|
||||
continue;
|
||||
}
|
||||
if e.raw_os_error() == Some(libc::ECHILD) {
|
||||
group.reserved = false;
|
||||
}
|
||||
failure = Some(e.into());
|
||||
break;
|
||||
}
|
||||
if unsafe { info.assume_init().si_pid() } != 0 {
|
||||
break;
|
||||
}
|
||||
let fenced = observe().is_err();
|
||||
if began.elapsed() >= timeout || fenced {
|
||||
output.timed_out = began.elapsed() >= timeout;
|
||||
output.interrupted = fenced;
|
||||
break;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
}
|
||||
if !group.reserved {
|
||||
// An unexpected external reap destroyed the group identity proof. Never
|
||||
// signal/adopt a numerical replacement; caller records Unknown.
|
||||
return Err(failure.unwrap_or_else(|| anyhow::anyhow!("OWNED_GROUP_IDENTITY_LOST")));
|
||||
}
|
||||
group.kill();
|
||||
let cleanup = Instant::now();
|
||||
loop {
|
||||
// A setup error may have left a blocking pipe. It must not become an
|
||||
// unbounded cleanup read; retain only group ownership in that path.
|
||||
let out_closed = if failure.is_some() {
|
||||
Ok(true)
|
||||
} else {
|
||||
drain(
|
||||
&mut stdout,
|
||||
&mut output.stdout,
|
||||
&mut remaining,
|
||||
&mut output.truncated,
|
||||
)
|
||||
};
|
||||
let err_closed = if failure.is_some() {
|
||||
Ok(true)
|
||||
} else {
|
||||
drain(
|
||||
&mut stderr,
|
||||
&mut result.stderr,
|
||||
&mut output.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
&mut output.truncated,
|
||||
)
|
||||
});
|
||||
if let Err(e) = drained {
|
||||
drop(group);
|
||||
let _ = child.wait();
|
||||
return Err(e);
|
||||
};
|
||||
if out_closed.is_err() || err_closed.is_err() {
|
||||
output.truncated = true;
|
||||
}
|
||||
match child.try_wait() {
|
||||
Ok(Some(status)) => break status,
|
||||
Ok(None) => {}
|
||||
Err(e) => {
|
||||
drop(group);
|
||||
let _ = child.wait();
|
||||
return Err(e.into());
|
||||
// Only zombies may remain while the *owned* leader stays unreaped.
|
||||
// They cannot execute or retain pipe writers. Unclear /proc state keeps
|
||||
// this owner and the caller's executor lock instead of a replacement.
|
||||
if group_has_no_live_members(group.pid) {
|
||||
if out_closed.unwrap_or(true) && err_closed.unwrap_or(true) {
|
||||
break;
|
||||
}
|
||||
// A privileged script may deliberately give a pipe to an unrelated
|
||||
// process. That process is outside our ownership; never kill it.
|
||||
if cleanup.elapsed() >= Duration::from_millis(250) {
|
||||
output.truncated = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if started.elapsed() >= timeout {
|
||||
result.timed_out = true;
|
||||
// SAFETY: this is the dedicated group created for this child.
|
||||
unsafe {
|
||||
libc::kill(-(child.id() as i32), libc::SIGKILL);
|
||||
}
|
||||
break child.wait()?;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
};
|
||||
let pgid = child.id();
|
||||
drop(group);
|
||||
// SIGKILL delivery is asynchronous. A single WouldBlock is not EOF and
|
||||
// does not prove that descendants have stopped. Bound both cleanup waiting
|
||||
// and output draining, and withhold a successful exit code if unconfirmed.
|
||||
let cleanup_started = Instant::now();
|
||||
loop {
|
||||
let stdout_closed = drain(
|
||||
&mut stdout,
|
||||
&mut result.stdout,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
let stderr_closed = drain(
|
||||
&mut stderr,
|
||||
&mut result.stderr,
|
||||
&mut remaining,
|
||||
&mut result.truncated,
|
||||
)?;
|
||||
// SAFETY: signal zero only probes the dedicated process group.
|
||||
let absent = unsafe { libc::kill(-(pgid as i32), 0) } == -1
|
||||
&& std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH);
|
||||
if stdout_closed && stderr_closed && absent {
|
||||
result.code = status.code();
|
||||
break;
|
||||
}
|
||||
if cleanup_started.elapsed() >= Duration::from_millis(250) {
|
||||
result.truncated |= !stdout_closed || !stderr_closed;
|
||||
// None explicitly means no confirmed successful completion. This
|
||||
// can also occur for lingering zombies: never guess their state.
|
||||
result.code = None;
|
||||
break;
|
||||
if cleanup.elapsed() >= Duration::from_millis(250) {
|
||||
output.timed_out = true;
|
||||
}
|
||||
thread::sleep(Duration::from_millis(5));
|
||||
}
|
||||
Ok(result)
|
||||
// Disarm BEFORE releasing the reserved leader PID: no later Drop can signal
|
||||
// a reused PGID. All assigned living members have already ended.
|
||||
group.reserved = false;
|
||||
let status = child.wait()?;
|
||||
if let Some(e) = failure {
|
||||
return Err(e);
|
||||
}
|
||||
output.interrupted |= observe().is_err();
|
||||
output.code = if output.timed_out || began.elapsed() >= timeout {
|
||||
None
|
||||
} else {
|
||||
status.code()
|
||||
};
|
||||
Ok(output)
|
||||
}
|
||||
fn drain(
|
||||
pipe: &mut impl Read,
|
||||
target: &mut Vec<u8>,
|
||||
remaining: &mut usize,
|
||||
truncated: &mut bool,
|
||||
) -> Result<bool> {
|
||||
let mut bytes = [0; 4096];
|
||||
for _ in 0..16 {
|
||||
match pipe.read(&mut bytes) {
|
||||
Ok(0) => return Ok(true),
|
||||
Ok(n) => {
|
||||
let keep = n.min(*remaining);
|
||||
target.extend_from_slice(&bytes[..keep]);
|
||||
*remaining -= keep;
|
||||
*truncated |= keep < n;
|
||||
}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => break,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => continue,
|
||||
Err(e) => return Err(e.into()),
|
||||
}
|
||||
}
|
||||
Ok(false)
|
||||
}
|
||||
fn group_has_no_live_members(pgid: u32) -> bool {
|
||||
let Ok(entries) = std::fs::read_dir("/proc") else {
|
||||
return false;
|
||||
};
|
||||
for entry in entries {
|
||||
let Ok(entry) = entry else {
|
||||
return false;
|
||||
};
|
||||
if entry.file_name().to_string_lossy().parse::<u32>().is_err() {
|
||||
continue;
|
||||
}
|
||||
let bytes = match std::fs::read_to_string(entry.path().join("stat")) {
|
||||
Ok(v) => v,
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => continue,
|
||||
Err(_) => return false,
|
||||
};
|
||||
let Some((_, rest)) = bytes.rsplit_once(") ") else {
|
||||
return false;
|
||||
};
|
||||
let fields: Vec<_> = rest.split_whitespace().take(3).collect();
|
||||
if fields.len() != 3 {
|
||||
return false;
|
||||
}
|
||||
let Ok(group) = fields[2].parse::<u32>() else {
|
||||
return false;
|
||||
};
|
||||
if group == pgid && fields[0] != "Z" && fields[0] != "X" {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn cancellation_observation_terminates_only_owned_group_and_keeps_raw_bounded_output() {
|
||||
let began = Instant::now();
|
||||
let output = run_controlled(
|
||||
"/bin/sh",
|
||||
&[
|
||||
"-c".into(),
|
||||
"printf before; printf err >&2; sleep 10".into(),
|
||||
],
|
||||
Duration::from_secs(5),
|
||||
64,
|
||||
|| {
|
||||
ensure!(
|
||||
began.elapsed() < Duration::from_millis(80),
|
||||
"TEST_CANCELLED"
|
||||
);
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert!(output.interrupted);
|
||||
assert!(!output.timed_out);
|
||||
assert_eq!(output.stdout, b"before");
|
||||
assert_eq!(output.stderr, b"err");
|
||||
assert_ne!(output.code, Some(0));
|
||||
assert!(began.elapsed() < Duration::from_secs(2));
|
||||
}
|
||||
#[test]
|
||||
fn exited_leader_does_not_leave_background_effects_or_open_pipes() {
|
||||
let directory =
|
||||
|
||||
Reference in New Issue
Block a user