use std::fmt;
use std::io;
use std::path::PathBuf;
#[derive(Debug, thiserror::Error)]
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "produced and consumed only by the linux-gated cgroup leaf machinery"
)
)]
pub(crate) enum LeafError {
#[error("could not read /proc/self/cgroup: {0}")]
ReadProcSelfCgroup(#[source] io::Error),
#[error(
"/proc/self/cgroup has no cgroup v2 unified (`0::`) line — a v1-only host, or the \
unified hierarchy is not mounted; the file has {line_count} line(s), naming \
{controllers} (their paths are dropped: none of them is one cosca touched, and they \
do not tell these cases apart)"
)]
NoUnifiedLine { line_count: usize, controllers: String },
#[error("could not create the leaf cgroup {}: {source}", path.display())]
CreateLeafDir {
path: PathBuf,
#[source]
source: io::Error,
},
#[error(
"the leaf cgroup {} has no cgroup.kill — the kernel is older than 5.14, so there is \
no atomic tree kill to back CgroupV2 containment",
path.display()
)]
KillUnsupported { path: PathBuf },
#[error("could not check for {}: {source}", path.display())]
CheckKill {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("could not open {} for writing: {source}", path.display())]
OpenProcs {
path: PathBuf,
#[source]
source: io::Error,
},
#[error("could not open the placement-report channel shared with the forked child: {0}")]
OpenReportChannel(#[source] io::Error),
#[error("could not watch {} for its drain: {source}", path.display())]
WatchDrain {
path: PathBuf,
#[source]
source: io::Error,
},
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "produced and consumed only by the linux-gated cgroup leaf machinery"
)
)]
pub(crate) enum PlacementReport {
NotReported,
Placed,
WriteFailed(i32),
}
impl fmt::Display for PlacementReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PlacementReport::NotReported => f.write_str(
"the child exited before reporting its placement outcome (its pre_exec may not \
have run, or may have been interrupted)",
),
PlacementReport::Placed => f.write_str("the child's pre_exec self-placement write succeeded"),
PlacementReport::WriteFailed(errno) => write!(
f,
"the child's pre_exec write to cgroup.procs failed: {} (errno {errno})",
io::Error::from_raw_os_error(*errno)
),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "produced and consumed only by the linux-gated cgroup leaf machinery"
)
)]
pub(crate) enum NotEntered {
NotReported,
WriteFailed(i32),
}
impl fmt::Display for NotEntered {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
NotEntered::NotReported => PlacementReport::NotReported.fmt(f),
NotEntered::WriteFailed(errno) => PlacementReport::WriteFailed(errno).fmt(f),
}
}
}
#[derive(Debug)]
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "produced and consumed only by the linux-gated cgroup leaf machinery"
)
)]
pub(crate) enum NotPlaced {
Absent {
pid: u32,
path: PathBuf,
procs: String,
report: NotEntered,
child_state: Option<char>,
},
Unreadable {
pid: u32,
path: PathBuf,
source: io::Error,
report: NotEntered,
},
}
impl fmt::Display for NotPlaced {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NotPlaced::Absent {
pid,
path,
procs,
report,
child_state,
} => {
let listed = procs.trim();
let listed = if listed.is_empty() {
"empty".to_string()
} else {
format!("{listed:?}")
};
let state = match child_state {
Some('Z') => "it had already exited when cgroup.procs was read".to_string(),
Some(state) => format!("it is still running (/proc state {state})"),
None => "its /proc state could not be read".to_string(),
};
write!(
f,
"child {pid} is not in the leaf cgroup: {report}; {} is {listed}; {state}",
path.display()
)
}
NotPlaced::Unreadable {
pid,
path,
source,
report,
} => write!(
f,
"child {pid} is not in the leaf cgroup: {report}; {} could not be read: {source}",
path.display()
),
}
}
}
#[cfg_attr(
not(target_os = "linux"),
allow(
dead_code,
reason = "dispatch.rs's only call sites are behind cgroup::try_create_leaf/CgroupLeaf::take_placement, both linux-gated"
)
)]
pub(crate) fn log_degrade(reason: &dyn fmt::Display) {
log::warn!("cgroup v2 containment: degrading to a process group — {reason}");
}
#[cfg(test)]
#[path = "degrade_tests.rs"]
mod degrade_tests;