use std::{
io,
process::{ExitStatus, Stdio},
sync::{Arc, Mutex},
time::Duration,
};
use tokio::{
io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
process::{Child, ChildStdin, Command},
task::JoinHandle,
time::Instant,
};
pub const DEFAULT_KILL_AFTER: Duration = Duration::from_secs(5);
pub const DEFAULT_PIPE_DRAIN: Duration = Duration::from_secs(5);
#[derive(Debug, Clone, Default)]
pub struct ExecutionResult {
pub exit_code: i32,
pub signal: Option<String>,
pub stdout: String,
pub stderr: String,
pub command: String,
}
const SIGNALS: &[(i32, &str)] = &[
(1, "SIGHUP"),
(2, "SIGINT"),
(3, "SIGQUIT"),
(4, "SIGILL"),
(5, "SIGTRAP"),
(6, "SIGABRT"),
(8, "SIGFPE"),
(9, "SIGKILL"),
(11, "SIGSEGV"),
(13, "SIGPIPE"),
(14, "SIGALRM"),
(15, "SIGTERM"),
#[cfg(target_os = "linux")]
(7, "SIGBUS"),
#[cfg(target_os = "linux")]
(10, "SIGUSR1"),
#[cfg(target_os = "linux")]
(12, "SIGUSR2"),
];
pub fn signal_name(number: i32) -> Option<&'static str> {
SIGNALS
.iter()
.find(|(value, _)| *value == number)
.map(|(_, name)| *name)
}
pub fn signal_from_exit_code(exit_code: i32) -> Option<&'static str> {
if exit_code > 128 {
signal_name(exit_code - 128)
} else {
None
}
}
pub fn normalize_exit(status: ExitStatus) -> (i32, Option<&'static str>) {
#[cfg(unix)]
{
use std::os::unix::process::ExitStatusExt;
if let Some(number) = status.signal() {
return (128 + number, signal_name(number));
}
}
let code = status.code().unwrap_or(1);
(code, signal_from_exit_code(code))
}
type Buffer = Arc<Mutex<Vec<u8>>>;
type Reader = JoinHandle<io::Result<()>>;
async fn join_readers(
readers: &mut [Option<Reader>],
deadline: Option<Instant>,
) -> io::Result<bool> {
for slot in readers.iter_mut() {
let Some(reader) = slot.as_mut() else {
continue;
};
let joined = match deadline {
None => reader.await,
Some(deadline) => match tokio::time::timeout_at(deadline, reader).await {
Ok(joined) => joined,
Err(_) => return Ok(false),
},
};
*slot = None;
joined.map_err(io::Error::other)??;
}
Ok(true)
}
fn read_buffer(buffer: &Buffer) -> String {
let bytes = buffer.lock().unwrap_or_else(|e| e.into_inner());
String::from_utf8_lossy(&bytes).into_owned()
}
async fn drain<R: AsyncRead + Unpin>(
mut pipe: R,
buffer: Buffer,
attached: bool,
stderr: bool,
) -> io::Result<()> {
let mut chunk = [0; 8192];
loop {
let count = pipe.read(&mut chunk).await?;
if count == 0 {
break;
}
buffer
.lock()
.unwrap_or_else(|e| e.into_inner())
.extend_from_slice(&chunk[..count]);
if attached {
if stderr {
tokio::io::stderr().write_all(&chunk[..count]).await?;
} else {
tokio::io::stdout().write_all(&chunk[..count]).await?;
}
}
}
Ok(())
}
#[cfg(unix)]
async fn kill_group(pgid: u32, signal: &str) -> io::Result<std::process::Output> {
let signal = signal.trim_start_matches("SIG");
Command::new("bash")
.arg("-c")
.arg(format!("kill -{signal} -- -{pgid}"))
.stdin(Stdio::null())
.output()
.await
}
pub async fn is_process_group_alive(pgid: u32) -> bool {
#[cfg(unix)]
{
match kill_group(pgid, "0").await {
Ok(output) if output.status.success() => true,
Ok(output) => String::from_utf8_lossy(&output.stderr)
.to_lowercase()
.contains("not permitted"),
Err(_) => false,
}
}
#[cfg(not(unix))]
{
let _ = pgid;
false
}
}
pub async fn execute_command(
command: &str,
dry_run: bool,
attached: bool,
) -> io::Result<ExecutionResult> {
if dry_run {
println!("Dry run - command that would be executed:\n{}", command);
return Ok(ExecutionResult {
command: command.into(),
..Default::default()
});
}
let mut handle = start_command(command, attached).await?;
let exit_code = handle.wait_for_exit().await?;
let (stdout, stderr, _) = handle.get_output();
Ok(ExecutionResult {
exit_code,
signal: handle.signal().map(String::from),
stdout: stdout.into(),
stderr: stderr.into(),
command: command.into(),
})
}
pub struct ProcessHandle {
pub command: String,
child: Child,
pid: Option<u32>,
readers: Option<(Reader, Reader)>,
stdout_buffer: Buffer,
stderr_buffer: Buffer,
pipe_drain: Option<Duration>,
stdout: String,
stderr: String,
exit_code: Option<i32>,
signal: Option<&'static str>,
stdin: Option<ChildStdin>,
}
impl ProcessHandle {
#[must_use]
pub const fn with_pipe_drain(mut self, pipe_drain: Option<Duration>) -> Self {
self.pipe_drain = pipe_drain;
self
}
pub const fn pid(&self) -> Option<u32> {
self.pid
}
pub async fn wait_for_exit(&mut self) -> io::Result<i32> {
if let Some(code) = self.exit_code {
return Ok(code);
}
self.end_stdin();
let status = self.child.wait().await?;
if let Some((stdout, stderr)) = self.readers.take() {
let mut readers = [Some(stdout), Some(stderr)];
let deadline = self.pipe_drain.map(|limit| Instant::now() + limit);
if !join_readers(&mut readers, deadline).await? {
#[cfg(unix)]
if let Some(pid) = self.pid {
if is_process_group_alive(pid).await {
let _ = kill_group(pid, "KILL").await;
}
}
let grace = Some(Instant::now() + Duration::from_millis(500));
if !join_readers(&mut readers, grace).await? {
for reader in readers.iter().flatten() {
reader.abort();
}
}
}
}
self.stdout = read_buffer(&self.stdout_buffer);
self.stderr = read_buffer(&self.stderr_buffer);
let (code, signal) = normalize_exit(status);
self.exit_code = Some(code);
self.signal = signal;
Ok(code)
}
pub async fn terminate(&mut self) -> io::Result<()> {
self.terminate_with("SIGTERM", Some(DEFAULT_KILL_AFTER))
.await
.map(|_| ())
}
pub async fn terminate_with(
&mut self,
signal: &str,
kill_after: Option<Duration>,
) -> io::Result<bool> {
#[cfg(unix)]
{
let Some(pid) = self.pid else {
return Ok(false);
};
if !is_process_group_alive(pid).await {
return Ok(false);
}
let output = kill_group(pid, signal).await?;
if std::env::var_os("AGENT_COMMANDER_DEBUG").is_some() {
eprintln!(
"Cancel process group {pid}: {} {}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
}
if !output.status.success() && is_process_group_alive(pid).await {
return Err(io::Error::other(
String::from_utf8_lossy(&output.stderr).into_owned(),
));
}
let is_kill = matches!(signal.trim_start_matches("SIG"), "KILL" | "9");
if let (Some(delay), false) = (kill_after, is_kill) {
tokio::spawn(async move {
tokio::time::sleep(delay).await;
if is_process_group_alive(pid).await {
let _ = kill_group(pid, "KILL").await;
}
});
}
Ok(true)
}
#[cfg(not(unix))]
{
let _ = (signal, kill_after);
if self.exit_code.is_some() || self.child.try_wait()?.is_some() {
return Ok(false);
}
self.child.start_kill()?;
Ok(true)
}
}
pub fn get_output(&self) -> (&str, &str, Option<i32>) {
(&self.stdout, &self.stderr, self.exit_code)
}
pub fn peek_stdout(&self) -> String {
read_buffer(&self.stdout_buffer)
}
pub const fn signal(&self) -> Option<&'static str> {
self.signal
}
pub fn try_wait(&mut self) -> io::Result<bool> {
Ok(self.exit_code.is_some() || self.child.try_wait()?.is_some())
}
pub const fn has_exited(&self) -> bool {
self.exit_code.is_some()
}
pub const fn has_stdin(&self) -> bool {
self.stdin.is_some()
}
pub async fn write_stdin(&mut self, data: &str) -> io::Result<()> {
let stdin = self
.stdin
.as_mut()
.ok_or_else(|| io::Error::new(io::ErrorKind::BrokenPipe, "stdin is not open"))?;
stdin.write_all(data.as_bytes()).await?;
stdin.flush().await
}
pub fn end_stdin(&mut self) -> bool {
self.stdin.take().is_some()
}
}
pub async fn start_command(command: &str, attached: bool) -> io::Result<ProcessHandle> {
spawn_command(command, attached, false, None)
}
pub async fn start_command_with_stdin(command: &str, attached: bool) -> io::Result<ProcessHandle> {
spawn_command(command, attached, true, None)
}
pub async fn start_command_with_env(
command: &str,
attached: bool,
pipe_stdin: bool,
env: &[(String, String)],
) -> io::Result<ProcessHandle> {
spawn_command(command, attached, pipe_stdin, Some(env))
}
fn apply_env(command_builder: &mut Command, env: Option<&[(String, String)]>) {
if let Some(env) = env {
command_builder.env_clear();
command_builder.envs(env.iter().map(|(k, v)| (k.as_str(), v.as_str())));
}
}
fn spawn_command(
command: &str,
attached: bool,
pipe_stdin: bool,
env: Option<&[(String, String)]>,
) -> io::Result<ProcessHandle> {
let mut command_builder = Command::new("bash");
apply_env(&mut command_builder, env);
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command_builder.as_std_mut().process_group(0);
}
let mut child = command_builder
.arg("-c")
.arg(command)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stdin(if pipe_stdin {
Stdio::piped()
} else {
Stdio::null()
})
.kill_on_drop(true)
.spawn()?;
let stdin = child.stdin.take();
let stdout = child.stdout.take().expect("piped stdout");
let stderr = child.stderr.take().expect("piped stderr");
let stdout_buffer = Buffer::default();
let stderr_buffer = Buffer::default();
Ok(ProcessHandle {
command: command.into(),
pid: child.id(),
child,
readers: Some((
tokio::spawn(drain(stdout, Arc::clone(&stdout_buffer), attached, false)),
tokio::spawn(drain(stderr, Arc::clone(&stderr_buffer), attached, true)),
)),
stdout_buffer,
stderr_buffer,
pipe_drain: Some(DEFAULT_PIPE_DRAIN),
stdout: String::new(),
stderr: String::new(),
exit_code: None,
signal: None,
stdin,
})
}
pub async fn execute_detached(command: &str) -> io::Result<Option<u32>> {
execute_detached_with_env(command, None).await
}
pub async fn execute_detached_with_env(
command: &str,
env: Option<&[(String, String)]>,
) -> io::Result<Option<u32>> {
let mut command_builder = Command::new("bash");
apply_env(&mut command_builder, env);
#[cfg(unix)]
{
use std::os::unix::process::CommandExt;
command_builder.as_std_mut().process_group(0);
}
let child = command_builder
.arg("-c")
.arg(command)
.stdout(Stdio::null())
.stderr(Stdio::null())
.stdin(Stdio::null())
.spawn()?;
Ok(child.id())
}
pub type CleanupFn = Box<dyn Fn() + Send + Sync>;
pub fn setup_signal_handler<F>(cleanup_fn: F) -> impl Fn()
where
F: Fn() + Send + Sync + 'static,
{
let task = tokio::runtime::Handle::try_current().ok().map(|runtime| {
runtime.spawn(async move {
if tokio::signal::ctrl_c().await.is_ok() {
cleanup_fn();
}
})
});
move || {
if let Some(task) = &task {
task.abort();
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_execute_command_dry_run() {
let result = execute_command("echo hello", true, false).await.unwrap();
assert_eq!(result.exit_code, 0);
assert_eq!(result.command, "echo hello");
}
#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn test_execute_command_real() {
let result = execute_command("echo hello", false, false).await.unwrap();
assert_eq!(result.exit_code, 0);
assert!(result.stdout.contains("hello"));
}
#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn test_start_command_and_wait() {
let mut handle = start_command("echo hello", false).await.unwrap();
let exit_code = handle.wait_for_exit().await.unwrap();
assert_eq!(exit_code, 0);
let (stdout, _, _) = handle.get_output();
assert!(stdout.contains("hello"));
}
#[tokio::test]
#[cfg(not(target_os = "windows"))]
async fn test_execute_detached() {
let pid = execute_detached("sleep 0.1").await.unwrap();
assert!(pid.is_some());
}
#[tokio::test]
#[cfg(unix)]
async fn test_execute_detached_leads_its_own_process_group() {
let pid = execute_detached("sleep 1").await.unwrap().unwrap();
let output = tokio::process::Command::new("ps")
.args(["-o", "pgid=", "-p", &pid.to_string()])
.output()
.await
.unwrap();
assert_eq!(
String::from_utf8_lossy(&output.stdout).trim(),
pid.to_string()
);
}
}