#[cfg(target_os = "linux")]
use std::os::fd::OwnedFd;
use std::time::Duration;
use super::Diagnosis;
#[cfg(target_os = "linux")]
use super::FrontFate;
use crate::child::drop_teardown::Torn;
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::unreaped::DropKind;
use crate::child::owned::OwnedChild;
use crate::child::spawn::policy::SpawnPolicy;
#[cfg(target_os = "linux")]
use crate::containment::cgroup::Subtree;
#[cfg(target_os = "linux")]
use crate::elevation::front::Front;
use crate::identity::ProcessId;
#[cfg(target_os = "linux")]
use crate::os_process::linux::LinuxProcess;
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) policy: SpawnPolicy,
}
#[cfg(target_os = "linux")]
pub(crate) fn tear_down_held(child: OwnedChild, how: HeldTeardown<'_>, why: &mut Diagnosis) -> (Option<String>, Torn) {
let HeldTeardown {
front,
id,
subtree,
disposition,
policy,
} = how;
let pid = child.pid();
if front.is_none() {
let torn = tear_down_failed_spawn(child, id, Past::WaitMayFail, disposition, policy, why);
return (None, torn);
}
let reach = Ok(subtree.map(|subtree| subtree as &dyn Reach));
let (front_fate, torn) = look_at_failed_spawn_front(&child, id, reach, disposition, policy.teardown_warn_after);
let torn = HandleTeardown::new(torn, Expect::of(&child, Past::WaitMayFail));
let torn = say_failed_spawn(child, id, true, torn, disposition, policy.unreaped_drop, why);
let detail = if torn.unreaped.is_some() {
front_fate.detail_handed_back(front, pid)
} else {
front_fate.detail(front, pid)
};
(detail, torn)
}
pub(crate) fn tear_down_failed_spawn(
child: OwnedChild,
id: Option<ProcessId>,
past: Past,
disposition: Disposition,
policy: SpawnPolicy,
why: &mut Diagnosis,
) -> Torn {
let kill = Kill {
disposition,
tree: None,
};
let torn = tear_down_expecting(&child, id, kill, past, policy.teardown_warn_after);
say_failed_spawn(child, id, false, torn, disposition, policy.unreaped_drop, why)
}
pub(crate) fn tear_down_expecting(
child: &OwnedChild,
id: Option<ProcessId>,
kill: Kill<'_>,
past: Past,
warn_after: Option<Duration>,
) -> HandleTeardown {
HandleTeardown::new(child.tear_down(id, kill, warn_after), 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,
warn_after: Option<Duration>,
) -> (FrontFate, TeardownOutcome) {
let look = child.look_at_front(id, reach, warn_after);
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,
unreaped_drop: DropKind,
why: &mut Diagnosis,
) -> Torn {
let remains = torn.say_as_failed_spawn(why);
let unreaped = disposition
.hands_back(&remains)
.then(|| Unreaped::new(child, id, front, unreaped_drop));
Torn {
fate: remains.fate,
unreaped,
cleanup: remains.failures,
}
}
#[cfg(target_os = "linux")]
pub(crate) fn tear_down_pidfd(
pid: Option<u32>,
pidfd: OwnedFd,
id: Option<ProcessId>,
past: Past,
warn_after: Option<Duration>,
) -> 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, warn_after)
}