use std::sync::{Arc, Mutex};
use crate::containment::dispatch::{TreeKillOutcome, Unkilled};
use crate::containment::drain_edge::DrainEdge;
#[cfg(unix)]
use crate::containment::mechanism::Release;
use crate::containment::mechanism::{nothing_held, Contains, KillLeftOutcome};
#[cfg(unix)]
use crate::containment::tree_kill::refused_unobserved;
use crate::containment::treewalk::Left;
use crate::containment::{Containment, Root, TerminateOutcome, TreeKill};
use crate::identity::ProcessId;
pub(crate) type Sends = Arc<Mutex<Vec<&'static str>>>;
#[derive(Debug)]
pub(crate) struct FakeContains {
kind: Containment,
#[cfg_attr(windows, allow(dead_code, reason = "only Unix asks whether a kill reaches"))]
reaches: bool,
killed: Vec<ProcessId>,
killed_again: Vec<ProcessId>,
#[cfg(unix)]
unobserved: Vec<u32>,
sends: Sends,
}
impl FakeContains {
pub(crate) fn new(kind: Containment, reaches: bool) -> (FakeContains, Sends) {
let sends = Sends::default();
let fake = FakeContains {
kind,
reaches,
killed: Vec::new(),
killed_again: Vec::new(),
#[cfg(unix)]
unobserved: Vec::new(),
sends: sends.clone(),
};
(fake, sends)
}
pub(crate) fn killing_again(mut self, members: impl IntoIterator<Item = ProcessId>) -> FakeContains {
self.killed_again = members.into_iter().collect();
self
}
pub(crate) fn reaching(mut self, members: impl IntoIterator<Item = ProcessId>) -> FakeContains {
self.killed = members.into_iter().collect();
self
}
#[cfg(unix)]
pub(crate) fn reaching_unobserved(mut self, pid: u32) -> FakeContains {
self.unobserved.push(pid);
self
}
fn record(&self, send: &'static str) {
self.sends
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.push(send);
}
}
impl Contains for FakeContains {
fn kill(&self, _root: Option<&dyn Root>) -> TreeKill<'_> {
self.record("kill");
let kill = TreeKill::of(TreeKillOutcome::Ran(Ok(()))).reaching(self.killed.iter().copied());
#[cfg(unix)]
let kill = self
.unobserved
.iter()
.fold(kill, |kill, &pid| kill.unobserved(refused_unobserved(pid)));
kill
}
fn kill_left(&self, result: Result<(), Unkilled>, _left: Vec<Left>) -> KillLeftOutcome {
KillLeftOutcome {
killed: self.killed_again.clone(),
..nothing_held(result)
}
}
fn send_terminate(&self, _pid: u32, _root: Option<&dyn Root>) -> TerminateOutcome {
self.record("terminate");
TerminateOutcome::Sent {
result: Ok(()),
earlier: Some("the SIGTERM"),
}
}
fn drain_edge(&self) -> Option<DrainEdge<'_>> {
None
}
fn kind(&self) -> Containment {
self.kind
}
fn disarm(&self) -> std::io::Result<()> {
self.record("disarm");
Ok(())
}
#[cfg(unix)]
fn subject(&self) -> String {
format!("{:?}", self.kind)
}
#[cfg(unix)]
fn would_reach(&self, _pid: u32, _pidfd: Option<std::os::fd::BorrowedFd<'_>>) -> std::io::Result<bool> {
Ok(self.reaches)
}
#[cfg(unix)]
fn reached(&self, _child: &crate::child::owned::OwnedChild) -> std::io::Result<bool> {
Ok(self.reaches)
}
#[cfg(unix)]
fn release(self) -> (Release, Option<FakeContains>) {
self.record("release");
(Release::nothing(), Some(self))
}
#[cfg(all(feature = "tokio", target_os = "linux"))]
fn awaitable_release(&self) -> Option<crate::containment::mechanism::AwaitableRelease<'_>> {
None
}
}