Skip to main content

orch/
cmd.rs

1//! Thin wrapper around `std::process::Command` / `tokio::process::Command`
2//! that attaches the binary path to any spawn error so "No such file or
3//! directory" always says *which* file was missing.
4
5use std::ffi::OsStr;
6use std::path::PathBuf;
7
8use tokio::process;
9
10use std::future::Future;
11use std::process::Command;
12use std::process::ExitStatus;
13use std::process::Output;
14
15/// Extension trait that maps spawn errors to include the program path.
16pub trait CommandErrorContext {
17    /// Like `.output()` but the error includes the program name.
18    fn output_with_context(&mut self) -> impl Future<Output = anyhow::Result<Output>>;
19}
20
21impl CommandErrorContext for process::Command {
22    async fn output_with_context(&mut self) -> anyhow::Result<Output> {
23        let prog = program_name(self.as_std());
24        self.output()
25            .await
26            .map_err(|e| anyhow::anyhow!("failed to execute `{prog}`: {e}"))
27    }
28}
29
30/// Sync version for `std::process::Command`.
31pub trait SyncCommandErrorContext {
32    fn output_with_context(&mut self) -> anyhow::Result<Output>;
33    fn status_with_context(&mut self) -> anyhow::Result<ExitStatus>;
34}
35
36impl SyncCommandErrorContext for Command {
37    fn output_with_context(&mut self) -> anyhow::Result<Output> {
38        let prog = program_name(self);
39        self.output()
40            .map_err(|e| anyhow::anyhow!("failed to execute `{prog}`: {e}"))
41    }
42
43    fn status_with_context(&mut self) -> anyhow::Result<ExitStatus> {
44        let prog = program_name(self);
45        self.status()
46            .map_err(|e| anyhow::anyhow!("failed to execute `{prog}`: {e}"))
47    }
48}
49
50/// `kill(2)`. A negative `pid` addresses the process group with that id
51#[cfg(unix)]
52pub fn raw_kill(pid: i32, sig: i32) -> i32 {
53    unsafe extern "C" {
54        fn kill(pid: i32, sig: i32) -> i32;
55    }
56    // SAFETY: both arguments are plain integers and kill has no other effect on this process
57    unsafe { kill(pid, sig) }
58}
59
60/// SIGKILL the process group led by `pgid`, a child started with `process_group(0)`. The group is
61/// the only handle on grandchildren, which outlive a child killed alone
62#[cfg(unix)]
63pub fn kill_process_group(pgid: u32) {
64    const SIGKILL: i32 = 9;
65    let Ok(pgid) = i32::try_from(pgid) else {
66        return;
67    };
68    let _ = raw_kill(-pgid, SIGKILL);
69}
70
71/// Extract the program name from a `std::process::Command`.
72fn program_name(cmd: &Command) -> String {
73    let prog: &OsStr = cmd.get_program();
74    let path = PathBuf::from(prog);
75    path.display().to_string()
76}