use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Containment {
CgroupV2,
JobObject,
ProcessGroup,
Session,
FdMarker,
TreeWalk,
Delegated,
None,
}
impl Containment {
pub fn can_teardown(&self) -> bool {
match self {
Containment::CgroupV2
| Containment::JobObject
| Containment::ProcessGroup
| Containment::Session
| Containment::FdMarker
| Containment::TreeWalk => true,
Containment::None | Containment::Delegated => false,
}
}
pub fn can_observe_drain(&self) -> bool {
matches!(
self,
Containment::CgroupV2 | Containment::JobObject | Containment::FdMarker
)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TreeDrain {
AllMembersExited,
AllMarkersClosed,
MembersRemain,
}
impl TreeDrain {
pub(crate) fn permits_skipping_sweep(&self) -> bool {
match self {
TreeDrain::AllMembersExited => true,
TreeDrain::AllMarkersClosed | TreeDrain::MembersRemain => false,
}
}
}
pub(crate) fn require_contained(containment: Containment, attached: &Attached) -> Result<(), crate::error::Error> {
debug_assert_eq!(
containment.can_teardown(),
attached.is_actionable(),
"Containment/Attached actionability diverged"
);
if !attached.is_actionable() {
return Err(crate::error::Error::Unsupported {
op: "tree teardown (kill_tree / terminate_tree)".into(),
platform: std::env::consts::OS,
detail: "this child holds no actionable tree-teardown mechanism (uncontained, \
or a nested member of an ancestor's containment group). Use kill() for a \
lone process, or tear down the tree via the outermost .contain()ed handle."
.into(),
});
}
Ok(())
}
pub(crate) fn require_drainable(containment: Containment, attached: &Attached) -> Result<(), crate::error::Error> {
debug_assert_eq!(
containment.can_observe_drain(),
attached.can_observe_drain(),
"Containment/Attached drain-observability diverged"
);
if !attached.can_observe_drain() {
return Err(crate::error::Error::Unsupported {
op: "wait for the contained tree to drain (wait_tree / wait_tree_timeout)".into(),
platform: std::env::consts::OS,
detail: format!(
"this child's containment mechanism ({containment}) exposes no kernel edge for \
tree drain — only cgroup v2, a Windows job object, and the macOS fd marker do. \
Use kill_tree()/terminate_tree() for teardown, or wait() to watch the root alone."
),
});
}
Ok(())
}
impl fmt::Display for Containment {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Containment::CgroupV2 => "cgroup v2",
Containment::JobObject => "job object",
Containment::ProcessGroup => "process group",
Containment::Session => "session",
Containment::FdMarker => "inherited-fd marker",
Containment::TreeWalk => "process-tree walk",
Containment::Delegated => "delegated",
Containment::None => "none",
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum ContainMode {
Strongest,
TreeWalk,
Session,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Nesting {
#[default]
Mark,
Opaque,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ContainRequest {
pub mode: Option<ContainMode>,
pub nesting: Nesting,
}
impl Default for ContainRequest {
fn default() -> ContainRequest {
ContainRequest {
mode: None,
nesting: Nesting::Mark,
}
}
}
pub(crate) const NESTED_ENV: &str = "__COSCA_GROUP_ROOT";
#[cfg(unix)]
#[path = "containment/unix.rs"]
pub(crate) mod unix;
#[path = "containment/cgroup.rs"]
pub(crate) mod cgroup;
#[cfg(windows)]
#[path = "containment/windows.rs"]
pub(crate) mod windows;
#[path = "containment/enumerate.rs"]
pub(crate) mod enumerate;
#[cfg(target_os = "macos")]
#[path = "containment/fdmarker.rs"]
pub(crate) mod fdmarker;
#[path = "containment/treewalk.rs"]
pub(crate) mod treewalk;
#[path = "containment/dispatch.rs"]
pub(crate) mod dispatch;
#[allow(unused_imports)]
pub(crate) use dispatch::{attach, prepare, Attached, Prepared};
#[cfg(target_os = "macos")]
#[path = "containment/marker_eof.rs"]
pub(crate) mod marker_eof;
#[cfg(test)]
#[path = "containment_tests.rs"]
mod containment_tests;