use crate::error::Error;
use crate::identity::RawPid;
#[cfg(unix)]
use crate::signal::RootState;
use crate::signal::{Sent, Sig};
pub(crate) trait Root {
fn pid(&self) -> u32;
fn signal(&self, sig: Sig) -> Result<Sent, Error>;
#[cfg(unix)]
fn state(&self) -> RootState;
#[cfg(unix)]
fn look_state(&self) -> RootState {
self.state()
}
#[cfg(target_os = "linux")]
fn pidfd(&self) -> Option<std::os::fd::BorrowedFd<'_>>;
}
#[cfg(unix)]
pub(crate) struct Seen<'a> {
root: &'a dyn Root,
look: RootState,
}
#[cfg(unix)]
impl<'a> Seen<'a> {
pub(crate) fn new(root: &'a dyn Root, look: RootState) -> Seen<'a> {
Seen { root, look }
}
}
#[cfg(unix)]
impl Root for Seen<'_> {
fn pid(&self) -> u32 {
self.root.pid()
}
fn signal(&self, sig: Sig) -> Result<Sent, Error> {
self.root.signal(sig)
}
fn state(&self) -> RootState {
self.root.state()
}
fn look_state(&self) -> RootState {
self.look.clone()
}
#[cfg(target_os = "linux")]
fn pidfd(&self) -> Option<std::os::fd::BorrowedFd<'_>> {
self.root.pidfd()
}
}
#[cfg(target_os = "linux")]
pub(crate) fn group_has_member(root: Option<&dyn Root>) -> Option<bool> {
root.and_then(Root::pidfd)
.and_then(crate::containment::unix::pidfd_group_has_member)
}
#[cfg(unix)]
pub(crate) fn check_number(root: Option<&dyn Root>) -> Result<(), NotItsGroup> {
#[cfg(test)]
crate::send_log::record_group(crate::send_log::GroupStep::Check);
#[cfg(target_os = "linux")]
match group_has_member(root) {
Some(true) => return Ok(()),
Some(false) => return Err(NotItsGroup::Empty),
None => {}
}
match root {
Some(root) => number_holds(root.pid(), root.state()).map_err(NotItsGroup::Unproven),
None => {
debug_assert!(false, "a send by the root's number was given no root to check");
Err(NotItsGroup::NoRoot)
}
}
}
#[derive(Debug)]
#[cfg_attr(not(unix), allow(dead_code, reason = "only Unix sends by the root's pid"))]
pub(crate) enum Refusal {
ReapedPidGone(RawPid),
ReapedPidTaken(RawPid),
ReapedPidUnread(RawPid),
ReapedElsewhere(RawPid),
Unsure(RawPid, std::sync::Arc<std::io::Error>),
Unpinned(RawPid),
}
impl std::fmt::Display for Refusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Refusal::ReapedPidGone(pid) => {
write!(f, "this handle reaped the root, and its pid ({pid}) names no process")
}
Refusal::ReapedPidTaken(pid) => write!(
f,
"this handle reaped the root, and another process holds its pid ({pid})"
),
Refusal::ReapedPidUnread(pid) => {
write!(f, "this handle reaped the root, and its pid ({pid}) could not be read")
}
Refusal::ReapedElsewhere(pid) => write!(f, "the root (pid {pid}) was reaped elsewhere"),
Refusal::Unsure(pid, _) => write!(
f,
"the root's own handle could not say whether it (pid {pid}) was reaped"
),
Refusal::Unpinned(pid) => write!(f, "the root (pid {pid}) is not pinned by this process"),
}
}
}
impl std::error::Error for Refusal {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Refusal::Unsure(_, e) => Some(&**e),
_ => None,
}
}
}
#[cfg(unix)]
impl Refusal {
pub(crate) fn cause(&self) -> Option<std::sync::Arc<crate::error::Error>> {
match self {
Refusal::Unsure(_, e) => Some(crate::error::shared_io_cause(e)),
_ => None,
}
}
}
#[cfg(unix)]
pub(crate) fn number_holds(pid: RawPid, state: RootState) -> Result<(), Refusal> {
use crate::identity::Resolved;
match state {
RootState::Unreaped => Ok(()),
RootState::Reaped { own: true } => Err(match crate::child::root_identity_now(pid) {
Resolved::Gone => Refusal::ReapedPidGone(pid),
Resolved::Found(_) => Refusal::ReapedPidTaken(pid),
Resolved::Unknown => Refusal::ReapedPidUnread(pid),
}),
RootState::Reaped { own: false } => Err(Refusal::ReapedElsewhere(pid)),
RootState::Unknown(e) => Err(Refusal::Unsure(pid, e)),
RootState::Unpinned => Err(Refusal::Unpinned(pid)),
}
}
#[cfg(unix)]
pub(crate) enum NumberLook {
Trusted,
#[cfg_attr(not(target_os = "macos"), allow(dead_code, reason = "only macOS reports it"))]
Untrusted(RootState),
Empty,
MayNameAnother { refusal: Refusal, exact_root: bool },
#[cfg_attr(not(target_os = "linux"), allow(dead_code, reason = "only Linux has a pidfd"))]
GroupGone,
}
#[cfg(unix)]
pub(crate) fn look(root: &dyn Root, has_group: bool) -> NumberLook {
let state = root.look_state();
#[cfg(target_os = "macos")]
if matches!(state, RootState::Unpinned | RootState::Unknown(_)) {
return NumberLook::Untrusted(state);
}
let handle_unsure = matches!(state, RootState::Unknown(_));
match number_holds(root.pid(), state) {
Ok(()) => NumberLook::Trusted,
Err(Refusal::ReapedPidTaken(_)) => NumberLook::Empty,
Err(Refusal::ReapedPidGone(_)) if !has_group => NumberLook::Empty,
Err(refusal) => {
debug_assert!(
cfg!(target_os = "macos") || !matches!(refusal, Refusal::Unpinned(_)),
"only macOS reports an unpinned root, and it is answered above"
);
NumberLook::MayNameAnother {
refusal,
exact_root: handle_unsure && cfg!(target_os = "linux"),
}
}
}
}
#[cfg(target_os = "linux")]
pub(crate) fn through_the_pidfd(look: NumberLook, root: &dyn Root) -> NumberLook {
let NumberLook::MayNameAnother { refusal, exact_root } = &look else {
return look;
};
match group_has_member(Some(root)) {
Some(true) => {
log::debug!(
"{refusal}, but its pidfd names its process group exactly, which still has a member: it is signalled",
refusal = crate::error::Report(refusal)
);
NumberLook::Trusted
}
Some(false) if !*exact_root => NumberLook::GroupGone,
_ => look,
}
}
#[cfg(unix)]
#[derive(Debug)]
pub(crate) enum NotItsGroup {
#[cfg_attr(not(target_os = "linux"), allow(dead_code, reason = "only Linux has a pidfd"))]
Empty,
Unproven(Refusal),
NoRoot,
}
#[cfg(unix)]
impl std::fmt::Display for NotItsGroup {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NotItsGroup::Empty => f.write_str("the root's process group has no member left"),
NotItsGroup::Unproven(why) => std::fmt::Display::fmt(why, f),
NotItsGroup::NoRoot => f.write_str("no root was given to check"),
}
}
}
#[cfg(unix)]
impl std::error::Error for NotItsGroup {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
NotItsGroup::Unproven(why) => std::error::Error::source(why),
NotItsGroup::Empty | NotItsGroup::NoRoot => None,
}
}
}
#[cfg(unix)]
impl NotItsGroup {
pub(crate) fn cause(&self) -> Option<std::sync::Arc<crate::error::Error>> {
match self {
NotItsGroup::Unproven(why) => why.cause(),
NotItsGroup::Empty | NotItsGroup::NoRoot => None,
}
}
}
#[cfg(all(test, unix))]
#[path = "root_tests.rs"]
mod root_tests;