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::{OpenFailure, OPEN_CONSEQUENCE};
const OP_NAME: &str = "process open";
#[cfg(test)]
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: Error::io_cause(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 {
call.unsupported(op).unwrap_or_else(|| {
Error::Io(crate::error::io_context(
call.name(),
std::io::Error::from(call.errno()),
))
})
}
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: Error::io_cause(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.into(), 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(view_unreadable(id, why, 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) => 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 causes = source
.into_iter()
.chain(pidfd_errno.map(|errno| crate::error::io_context("pidfd_open", std::io::Error::from(errno))));
Error::Unassessable {
detail: format!(
"pid {} identity could not be confirmed: {why}; {OPEN_CONSEQUENCE}",
id.pid()
),
source: crate::error::Failures::collect(causes.map(Error::io_cause)).map(crate::error::Failures::into_cause),
}
}
#[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;