use std::os::fd::AsFd as _;
use rustix::process::{Pid, PidfdFlags};
use crate::error::Error;
use crate::identity::{
Existence, FailedCall, Kernel, Liveness, PidfdTarget, ProcView, ProcessId, Proof, ViewUnreadable,
};
use crate::os_process::poll::{look_readable, PollFailure};
use crate::os_process::OPEN_CONSEQUENCE;
const OP_NAME: &str = "process open";
#[derive(Debug)]
pub(crate) enum OpenFailure {
Call(FailedCall),
Finished(Error),
}
impl OpenFailure {
pub(crate) fn into_open_error(self) -> Error {
match self {
OpenFailure::Call(call) => open_error(call),
OpenFailure::Finished(error) => error,
}
}
}
pub(crate) fn open_verified_error_with(
kernel: &dyn Kernel,
id: ProcessId,
) -> Result<Option<rustix::fd::OwnedFd>, Error> {
open_verified_with(kernel, id).map_err(OpenFailure::into_open_error)
}
pub(crate) fn open_verified_with(
kernel: &dyn Kernel,
id: ProcessId,
) -> Result<Option<rustix::fd::OwnedFd>, OpenFailure> {
debug_assert!(
id.pid() <= i32::MAX as u32,
"pid {} exceeds i32::MAX; pidfd cast would truncate",
id.pid()
);
if let Some(ino) = id.pidfs_ino() {
return open_proved(kernel, id, ino);
}
let raw = Pid::from_raw(id.pid() as i32).expect("a resolvable ProcessId is never pid 0");
match kernel.pidfd_open(raw, PidfdFlags::empty()) {
Ok(pidfd) => verify_pidfd_target(kernel, id, pidfd).map_err(OpenFailure::Finished),
Err(rustix::io::Errno::SRCH) => Ok(None),
Err(e @ (rustix::io::Errno::INVAL | rustix::io::Errno::NOENT)) => {
verify_without_pidfd(kernel, id, e).map_err(OpenFailure::Finished)
}
Err(e) => Err(OpenFailure::Call(FailedCall::pidfd_open(e))),
}
}
fn open_proved(kernel: &dyn Kernel, id: ProcessId, ino: u64) -> Result<Option<rustix::fd::OwnedFd>, OpenFailure> {
match crate::identity::prove(kernel, id.pid(), ino) {
Proof::Holds { fd, thread: None } => Ok(Some(fd)),
Proof::Holds {
fd,
thread: Some(errno),
} => match look_readable(fd.as_fd()) {
Ok(true) => Ok(None),
Ok(false) => Err(OpenFailure::Finished(Error::NotThreadGroupLeader {
pid: id.pid(),
detail: OPEN_CONSEQUENCE.into(),
source: std::io::Error::from(errno),
})),
Err(PollFailure::Call(errno)) => Err(OpenFailure::Call(FailedCall::poll(errno))),
Err(PollFailure::Revents(e)) => Err(OpenFailure::Finished(Error::Io(crate::error::io_context("poll", e)))),
},
Proof::Lost => Ok(None),
Proof::Unknown(call) => Err(OpenFailure::Call(call)),
}
}
pub(crate) fn open_error(call: FailedCall) -> Error {
io_or_unsupported(OP_NAME, call)
}
fn io_or_unsupported(op: &str, call: FailedCall) -> Error {
match call.refusal() {
Some(name) => pidfd_open_unsupported(op, name),
None => Error::Io(crate::error::io_context(
call.name(),
std::io::Error::from(call.errno()),
)),
}
}
pub(crate) fn pidfd_open_unsupported(op: &str, errno_name: &str) -> Error {
Error::Unsupported {
op: op.into(),
platform: "linux",
detail: format!(
"cosca requires pidfd_open (Linux \u{2265} 5.3), refused here: pidfd_open answered {errno_name}"
),
}
}
pub(crate) fn spawn_open_error(errno: rustix::io::Errno) -> Error {
io_or_unsupported("spawn", FailedCall::pidfd_open(errno))
}
pub(crate) fn probe_pidfd_support_with(kernel: &dyn Kernel) -> Result<(), Error> {
kernel
.pidfd_open(rustix::process::getpid(), PidfdFlags::empty())
.map(drop)
.map_err(spawn_open_error)
}
fn verify_without_pidfd(
kernel: &dyn Kernel,
id: ProcessId,
errno: rustix::io::Errno,
) -> Result<Option<rustix::fd::OwnedFd>, Error> {
if id.signal_says_no_such_process(kernel) {
return Ok(None);
}
match crate::identity::proc_view_with(kernel) {
ProcView::Same(proc_dir) => match id.exists_in(kernel, &proc_dir) {
Existence::Gone => Ok(None),
Existence::Present => match id.is_alive_in(kernel, &proc_dir) {
Liveness::Dead => Ok(None),
Liveness::Alive => Err(Error::NotThreadGroupLeader {
pid: id.pid(),
detail: OPEN_CONSEQUENCE.into(),
source: std::io::Error::from(errno),
}),
Liveness::Unknown => Err(unassessable(id, "the OS refused the liveness query", None, Some(errno))),
},
Existence::Unknown => Err(unassessable(
id,
"the OS refused the existence query",
None,
Some(errno),
)),
},
ProcView::Diverged => Err(unassessable(
id,
"this process's /proc is an outer pid namespace's",
None,
Some(errno),
)),
ProcView::Unassessable(why) => Err(view_unreadable(id, why, Some(errno))),
}
}
fn verify_pidfd_target(
kernel: &dyn Kernel,
id: ProcessId,
pidfd: rustix::fd::OwnedFd,
) -> Result<Option<rustix::fd::OwnedFd>, Error> {
let proc_dir = kernel.open_proc().map_err(|why| view_unreadable(id, why, None))?;
match crate::identity::pidfd_pid_in(kernel, &proc_dir, pidfd.as_fd()) {
Ok(PidfdTarget::Pid(pid)) if pid == id.pid() => {}
Ok(PidfdTarget::Reaped) => return Ok(None),
Ok(PidfdTarget::Pid(pid)) => {
return Err(unassessable(
id,
&format!(
"the mounted /proc numbers the target {pid} (0 = invisible), so it is an outer pid namespace's"
),
None,
None,
));
}
Err(why) => return Err(unassessable(id, &why.reason, why.source, None)),
}
match id.exists_in(kernel, &proc_dir) {
Existence::Present => Ok(Some(pidfd)),
Existence::Gone => Ok(None),
Existence::Unknown => Err(unassessable(id, "the OS refused the existence query", None, None)),
}
}
fn view_unreadable(id: ProcessId, why: ViewUnreadable, pidfd_errno: Option<rustix::io::Errno>) -> Error {
match why.unsupported(OP_NAME) {
Some(unsupported) => {
log::warn!("wait: pid {} {OP_NAME}: {unsupported}", id.pid());
unsupported
}
None => unassessable(id, &why.reason, why.source, pidfd_errno),
}
}
fn unassessable(
id: ProcessId,
why: &str,
source: Option<std::io::Error>,
pidfd_errno: Option<rustix::io::Errno>,
) -> Error {
let mut detail = format!("pid {} identity could not be confirmed: {why}", id.pid());
if let Some(source) = &source {
detail.push_str(&format!(": {source}"));
}
if let Some(errno) = pidfd_errno {
detail.push_str(&format!(" (pidfd_open: {errno})"));
}
detail.push_str(&format!("; {OPEN_CONSEQUENCE}"));
log::warn!("wait: {detail}");
Error::Unassessable {
detail,
source: source.or_else(|| pidfd_errno.map(std::io::Error::from)),
}
}
#[cfg(test)]
#[path = "open_tests.rs"]
mod open_tests;
#[cfg(test)]
#[path = "open_namespace_tests.rs"]
mod open_namespace_tests;
#[cfg(test)]
#[path = "open_pidfs_tests.rs"]
mod open_pidfs_tests;