use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::time::{Duration, Instant};
use tokio::io::{AsyncRead, AsyncReadExt};
use tokio::process::{Child, Command};
use tokio_util::sync::CancellationToken;
use crate::Host;
#[derive(Debug, Clone)]
pub struct ExecRequest {
pub command: String,
pub cwd: PathBuf,
pub timeout: Option<Duration>,
pub env: Vec<(String, String)>,
}
#[derive(Debug, Clone)]
pub struct ExecOutput {
pub stdout: String,
pub stderr: String,
pub combined: String,
pub exit_code: Option<i32>,
pub timed_out: bool,
pub cancelled: bool,
pub truncated: bool,
pub duration: Duration,
}
#[derive(Debug, thiserror::Error)]
pub enum ExecError {
#[error("failed to spawn shell: {0}")]
Spawn(String),
}
impl Host {
pub async fn exec(
&self,
req: ExecRequest,
cancel: &CancellationToken,
) -> Result<ExecOutput, ExecError> {
let timeout = req
.timeout
.unwrap_or(self.limits.default_timeout)
.min(self.limits.max_timeout);
let cmd = self.build_shell_command(&req);
self.run_captured(cmd, timeout, cancel).await
}
pub async fn run_capture(
&self,
program: &Path,
args: &[String],
cwd: &Path,
timeout: Option<Duration>,
cancel: &CancellationToken,
) -> Result<ExecOutput, ExecError> {
let timeout = timeout
.unwrap_or(self.limits.default_timeout)
.min(self.limits.max_timeout);
let mut cmd = Command::new(program);
cmd.args(args);
cmd.current_dir(cwd);
cmd.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
#[cfg(unix)]
cmd.process_group(0);
self.run_captured(cmd, timeout, cancel).await
}
async fn run_captured(
&self,
mut cmd: Command,
timeout: Duration,
cancel: &CancellationToken,
) -> Result<ExecOutput, ExecError> {
let started = Instant::now();
let cap = self.limits.max_output_bytes;
let mut child = cmd.spawn().map_err(|e| ExecError::Spawn(e.to_string()))?;
let pid = child.id();
let stdout = child.stdout.take();
let stderr = child.stderr.take();
let out_task = tokio::spawn(read_capped(stdout, cap));
let err_task = tokio::spawn(read_capped(stderr, cap));
let mut exit_code = None;
let mut timed_out = false;
let mut cancelled = false;
tokio::select! {
status = child.wait() => {
exit_code = status.ok().and_then(|s| s.code());
}
() = tokio::time::sleep(timeout) => {
timed_out = true;
terminate(&mut child, pid, self.limits.kill_grace).await;
}
() = cancel.cancelled() => {
cancelled = true;
terminate(&mut child, pid, self.limits.kill_grace).await;
}
}
let (stdout, out_trunc) = out_task.await.unwrap_or_else(|_| (Vec::new(), false));
let (stderr, err_trunc) = err_task.await.unwrap_or_else(|_| (Vec::new(), false));
let stdout = String::from_utf8_lossy(&stdout).into_owned();
let stderr = String::from_utf8_lossy(&stderr).into_owned();
let combined = combine(&stdout, &stderr);
Ok(ExecOutput {
stdout,
stderr,
combined,
exit_code,
timed_out,
cancelled,
truncated: out_trunc || err_trunc,
duration: started.elapsed(),
})
}
fn build_shell_command(&self, req: &ExecRequest) -> Command {
let mut cmd = Command::new(&self.shell_program);
cmd.arg(if self.login_shell { "-lc" } else { "-c" });
cmd.arg(&req.command);
cmd.current_dir(&req.cwd);
cmd.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped());
for (key, value) in &req.env {
cmd.env(key, value);
}
#[cfg(unix)]
cmd.process_group(0);
cmd
}
}
async fn read_capped<R: AsyncRead + Unpin>(reader: Option<R>, cap: usize) -> (Vec<u8>, bool) {
let Some(mut reader) = reader else {
return (Vec::new(), false);
};
let mut buf = Vec::new();
let mut chunk = [0u8; 8192];
let mut truncated = false;
loop {
match reader.read(&mut chunk).await {
Ok(0) | Err(_) => break,
Ok(n) => {
buf.extend_from_slice(&chunk[..n]);
if buf.len() > cap {
let excess = buf.len() - cap;
buf.drain(..excess);
truncated = true;
}
}
}
}
(buf, truncated)
}
async fn terminate(child: &mut Child, pid: Option<u32>, grace: Duration) {
if group_kill(child, pid, grace).await {
return;
}
let _ = child.start_kill();
let _ = child.wait().await;
}
#[cfg(unix)]
async fn group_kill(child: &mut Child, pid: Option<u32>, grace: Duration) -> bool {
use nix::sys::signal::{Signal, killpg};
use nix::unistd::Pid;
let Some(pid) = pid else { return false };
let Ok(raw) = i32::try_from(pid) else {
return false;
};
if raw <= 1 {
return false;
}
let leader = Pid::from_raw(raw);
let _ = killpg(leader, Signal::SIGTERM);
if tokio::time::timeout(grace, child.wait()).await.is_err() {
let _ = killpg(leader, Signal::SIGKILL);
let _ = child.wait().await;
}
true
}
#[cfg(not(unix))]
async fn group_kill(_child: &mut Child, _pid: Option<u32>, _grace: Duration) -> bool {
false
}
fn combine(stdout: &str, stderr: &str) -> String {
match (stdout.is_empty(), stderr.is_empty()) {
(false, false) => format!("{stdout}\n{stderr}"),
(false, true) => stdout.to_string(),
(true, false) => stderr.to_string(),
(true, true) => String::new(),
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ExecLimits, Host, HostConfig, PathPolicy, test_host};
use std::time::Duration;
use tempfile::tempdir;
fn req(command: &str, cwd: PathBuf) -> ExecRequest {
ExecRequest {
command: command.to_string(),
cwd,
timeout: None,
env: Vec::new(),
}
}
fn shell_host(root: &std::path::Path) -> Host {
test_host(root, PathPolicy::Jailed, false)
}
#[tokio::test]
async fn captures_stdout_and_exit() {
let dir = tempdir().unwrap();
let host = shell_host(dir.path());
let out = host
.exec(
req("echo hello", dir.path().into()),
&CancellationToken::new(),
)
.await
.unwrap();
assert!(out.stdout.contains("hello"));
assert_eq!(out.exit_code, Some(0));
assert!(!out.timed_out && !out.cancelled);
}
#[tokio::test]
async fn captures_stderr_and_nonzero_exit() {
let dir = tempdir().unwrap();
let host = shell_host(dir.path());
let out = host
.exec(
req(">&2 echo boom; exit 3", dir.path().into()),
&CancellationToken::new(),
)
.await
.unwrap();
assert!(out.stderr.contains("boom"));
assert_eq!(out.exit_code, Some(3));
}
#[tokio::test]
async fn timeout_kills_sleeper() {
let dir = tempdir().unwrap();
let mut config = HostConfig::new(dir.path());
config.login_shell = false;
config.exec = ExecLimits {
default_timeout: Duration::from_millis(200),
..ExecLimits::default()
};
let host = Host::new(config).unwrap();
let started = Instant::now();
let out = host
.exec(
req("sleep 30", dir.path().into()),
&CancellationToken::new(),
)
.await
.unwrap();
assert!(out.timed_out);
assert!(out.exit_code.is_none());
assert!(
started.elapsed() < Duration::from_secs(5),
"should not wait for the sleep"
);
}
#[tokio::test]
async fn cancellation_kills_running_command() {
let dir = tempdir().unwrap();
let host = shell_host(dir.path());
let cancel = CancellationToken::new();
let child_cancel = cancel.clone();
tokio::spawn(async move {
tokio::time::sleep(Duration::from_millis(50)).await;
child_cancel.cancel();
});
let started = Instant::now();
let out = host
.exec(req("sleep 30", dir.path().into()), &cancel)
.await
.unwrap();
assert!(out.cancelled);
assert!(started.elapsed() < Duration::from_secs(5));
}
#[tokio::test]
async fn output_over_cap_is_truncated() {
let dir = tempdir().unwrap();
let mut config = HostConfig::new(dir.path());
config.login_shell = false;
config.exec = ExecLimits {
max_output_bytes: 1000,
..ExecLimits::default()
};
let host = Host::new(config).unwrap();
let out = host
.exec(
req(
"for i in $(seq 1 20000); do echo 0123456789; done",
dir.path().into(),
),
&CancellationToken::new(),
)
.await
.unwrap();
assert!(out.truncated);
assert!(
out.stdout.len() <= 1000 + 16,
"kept ~cap bytes, got {}",
out.stdout.len()
);
}
#[tokio::test]
async fn null_stdin_does_not_hang() {
let dir = tempdir().unwrap();
let host = shell_host(dir.path());
let out = host
.exec(req("cat", dir.path().into()), &CancellationToken::new())
.await
.unwrap();
assert_eq!(out.exit_code, Some(0));
}
}