use std::collections::VecDeque;
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, Listing};
use crate::containment::{Containment, Root, TerminateOutcome, TreeKill, TreeStop};
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>,
unwalked: bool,
kill_answers: Mutex<VecDeque<TreeKillOutcome>>,
terminate_answers: Mutex<VecDeque<TerminateOutcome>>,
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(),
unwalked: false,
kill_answers: Mutex::default(),
terminate_answers: Mutex::default(),
sends: sends.clone(),
};
(fake, sends)
}
#[cfg_attr(
not(target_os = "macos"),
allow(dead_code, reason = "only a macOS row states what each sweep of a tree answered")
)]
pub(crate) fn answering_kills(self, outcomes: impl IntoIterator<Item = TreeKillOutcome>) -> FakeContains {
*self
.kill_answers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = outcomes.into_iter().collect();
self
}
#[cfg_attr(
not(target_os = "linux"),
allow(dead_code, reason = "only a Linux row states what the cooperative route answered")
)]
pub(crate) fn answering_terminates(self, outcomes: impl IntoIterator<Item = TerminateOutcome>) -> FakeContains {
*self
.terminate_answers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = outcomes.into_iter().collect();
self
}
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
}
#[cfg_attr(windows, allow(dead_code, reason = "only a Unix row states an unwalked walk"))]
pub(crate) fn unwalked(mut self) -> FakeContains {
self.unwalked = true;
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 send_kill(&self, root: Option<&dyn Root>) -> TreeKillOutcome {
let stated = self
.kill_answers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.pop_front();
match stated {
Some(answer) => {
self.record("kill");
answer
}
None => self.kill(root).outcome(),
}
}
fn kill(&self, _root: Option<&dyn Root>) -> TreeKill<'_> {
self.record("kill");
if self.unwalked {
let error = crate::error::Error::Unassessable {
detail: "the process table could not be read".into(),
source: None,
};
return TreeKill::of(TreeKillOutcome::Unwalked(error));
}
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>,
how: TreeStop,
) -> (TerminateOutcome, Option<Listing>) {
self.record("terminate");
let stated = self
.terminate_answers
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.pop_front();
let sent = stated.unwrap_or_else(|| TerminateOutcome::Sent {
result: Ok(()),
earlier: Some(how.earlier()),
});
(sent, None)
}
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<'_>>) -> Result<bool, crate::error::Error> {
Ok(self.reaches)
}
#[cfg(unix)]
fn reached(&self, _child: &crate::child::owned::OwnedChild) -> Result<bool, crate::error::Error> {
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
}
}