use std::process::ExitStatus;
use std::sync::{Arc, Mutex};
use async_channel::{Receiver, Sender};
use super::ssh::RemoteJob;
use crate::process_tree::{JobProcess, JobTree, KillTreeError};
use crate::stdin_watch::StdinWatchSignal;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChildExit {
Exited(ExitStatus),
Killed,
Failed(String),
}
impl ChildExit {
#[must_use]
pub fn success(&self) -> bool {
matches!(self, Self::Exited(status) if status.success())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Descendants {
Killed(Vec<JobProcess>),
Unobserved,
}
pub enum KillHandle {
Tree(JobTree),
Remote(RemoteJob),
Switch(Sender<()>),
}
pub struct ExitWatch {
result: Mutex<Option<(ChildExit, Descendants)>>,
done: Receiver<()>,
}
pub struct ExitSetter {
watch: Arc<ExitWatch>,
_done: Sender<()>,
}
pub fn exit_watch() -> (ExitSetter, Arc<ExitWatch>) {
let (done_tx, done_rx) = async_channel::bounded(1);
let watch = Arc::new(ExitWatch {
result: Mutex::new(None),
done: done_rx,
});
(
ExitSetter {
watch: Arc::clone(&watch),
_done: done_tx,
},
watch,
)
}
impl ExitSetter {
pub(crate) fn finish(self, exit: ChildExit, descendants: Descendants) {
*self
.watch
.result
.lock()
.expect("the exit result lock is never held across a panic") =
Some((exit, descendants));
}
}
pub struct SandboxedChild {
id: u32,
stdin: Option<Sender<Vec<u8>>>,
stdout: Option<Receiver<Vec<u8>>>,
stderr: Option<Receiver<Vec<u8>>>,
stdin_blocked: Option<Receiver<StdinWatchSignal>>,
kill: KillHandle,
exit: Arc<ExitWatch>,
}
impl std::fmt::Debug for SandboxedChild {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SandboxedChild")
.field("id", &self.id)
.finish_non_exhaustive()
}
}
impl SandboxedChild {
pub(crate) fn new(id: u32, streams: Streams, kill: KillHandle, exit: Arc<ExitWatch>) -> Self {
let Streams {
stdin,
stdout,
stderr,
stdin_blocked,
} = streams;
Self {
id,
stdin,
stdout,
stderr,
stdin_blocked,
kill,
exit,
}
}
#[must_use]
pub const fn id(&self) -> u32 {
self.id
}
pub const fn take_stdin(&mut self) -> Option<Sender<Vec<u8>>> {
self.stdin.take()
}
pub const fn take_stdout(&mut self) -> Option<Receiver<Vec<u8>>> {
self.stdout.take()
}
pub const fn take_stderr(&mut self) -> Option<Receiver<Vec<u8>>> {
self.stderr.take()
}
pub const fn take_stdin_blocked(&mut self) -> Option<Receiver<StdinWatchSignal>> {
self.stdin_blocked.take()
}
pub async fn kill(&self) -> Result<(), KillTreeError> {
let killed = match &self.kill {
KillHandle::Tree(tree) => tree.kill().await,
KillHandle::Remote(job) => job.kill().await,
KillHandle::Switch(switch) => {
let _ = switch.send(()).await;
Ok(())
}
};
match killed {
#[cfg(unix)]
Err(KillTreeError::Ended) => Ok(()),
other => other,
}
}
pub async fn wait(&self) -> ChildExit {
self.outcome().await.0
}
pub async fn descendants_killed(&self) -> Descendants {
self.outcome().await.1
}
async fn outcome(&self) -> (ChildExit, Descendants) {
let _ = self.exit.done.recv().await;
self.exit
.result
.lock()
.expect("the exit result lock is never held across a panic")
.clone()
.unwrap_or_else(|| {
(
ChildExit::Failed("the task waiting for the process ended".into()),
Descendants::Unobserved,
)
})
}
}
#[derive(Default)]
pub struct Streams {
pub(crate) stdin: Option<Sender<Vec<u8>>>,
pub(crate) stdout: Option<Receiver<Vec<u8>>>,
pub(crate) stderr: Option<Receiver<Vec<u8>>>,
pub(crate) stdin_blocked: Option<Receiver<StdinWatchSignal>>,
}