#[cfg(target_os = "linux")]
use std::os::fd::OwnedFd;
use super::Diagnosis;
#[cfg(target_os = "linux")]
use super::FrontFate;
use crate::child::owned::teardown::{Disposition, Expect, HandleTeardown, Kill, Past};
#[cfg(target_os = "linux")]
use crate::child::owned::teardown::{Reach, TeardownOutcome};
use crate::child::owned::OwnedChild;
#[cfg(target_os = "linux")]
use crate::containment::cgroup::Subtree;
#[cfg(target_os = "linux")]
use crate::elevation::front::Front;
use crate::error::ChildFate;
#[cfg(target_os = "linux")]
use crate::error::Error;
use crate::identity::ProcessId;
#[cfg(target_os = "linux")]
use crate::os_process::linux::LinuxProcess;
use crate::wait::teardown_warn::Due;
use crate::Unreaped;
#[cfg(target_os = "linux")]
pub(crate) struct HeldTeardown<'a> {
pub(crate) front: Option<Front>,
pub(crate) id: Option<ProcessId>,
pub(crate) subtree: Option<&'a Subtree>,
pub(crate) disposition: Disposition,
pub(crate) due: Option<Due>,
}
#[cfg(target_os = "linux")]
pub(crate) fn tear_down_held(
child: OwnedChild,
how: HeldTeardown<'_>,
why: &mut Diagnosis,
error: Error,
) -> (Error, ChildFate, Option<Unreaped>) {
let HeldTeardown {
front,
id,
subtree,
disposition,
due,
} = how;
let pid = child.pid();
if front.is_none() {
let (fate, unreaped) = tear_down_failed_spawn(child, id, Past::WaitMayFail, disposition, due, why);
return (error, fate, unreaped);
}
let reach = Ok(subtree.map(|subtree| subtree as &dyn Reach));
let (front_fate, torn) = look_at_failed_spawn_front(&child, id, reach, disposition, due);
let torn = HandleTeardown::new(torn, Expect::of(&child, Past::WaitMayFail));
let (fate, unreaped) = say_failed_spawn(child, id, true, torn, disposition, why);
let error = if unreaped.is_some() {
front_fate.note_handed_back(error, front, pid)
} else {
front_fate.note(error, front, pid)
};
(error, fate, unreaped)
}
pub(crate) fn tear_down_failed_spawn(
child: OwnedChild,
id: Option<ProcessId>,
past: Past,
disposition: Disposition,
due: Option<Due>,
why: &mut Diagnosis,
) -> (ChildFate, Option<Unreaped>) {
let kill = Kill {
disposition,
tree: None,
};
let torn = tear_down_expecting(&child, id, kill, past, due);
say_failed_spawn(child, id, false, torn, disposition, why)
}
pub(crate) fn tear_down_expecting(
child: &OwnedChild,
id: Option<ProcessId>,
kill: Kill<'_>,
past: Past,
due: Option<Due>,
) -> HandleTeardown {
HandleTeardown::new(child.tear_down(id, kill, due), Expect::of(child, past))
}
#[cfg(target_os = "linux")]
pub(crate) fn look_at_failed_spawn_front(
child: &OwnedChild,
id: Option<ProcessId>,
reach: Result<Option<&dyn Reach>, std::io::Error>,
disposition: Disposition,
due: Option<Due>,
) -> (FrontFate, TeardownOutcome) {
let look = child.look_at_front(id, reach, due);
let front_fate = FrontFate::of_look(&look.look);
let torn = crate::child::word_front(child.pid(), &"its spawn failed", disposition, look);
(front_fate, torn)
}
fn say_failed_spawn(
child: OwnedChild,
id: Option<ProcessId>,
front: bool,
torn: HandleTeardown,
disposition: Disposition,
why: &mut Diagnosis,
) -> (ChildFate, Option<Unreaped>) {
let remains = torn.say_as_failed_spawn(why);
let unreaped = disposition
.hands_back(&remains)
.then(|| Unreaped::new(child, id, front));
(remains.fate, unreaped)
}
#[cfg(target_os = "linux")]
pub(crate) fn tear_down_pidfd(
pid: Option<u32>,
pidfd: OwnedFd,
id: Option<ProcessId>,
past: Past,
due: Option<Due>,
) -> HandleTeardown {
let child = OwnedChild::new(Box::new(LinuxProcess::new(pid, pidfd)));
let kill = Kill {
disposition: Disposition::Leave,
tree: None,
};
tear_down_expecting(&child, id, kill, past, due)
}