use std::io;
use super::proc_view::ProcDir;
#[derive(Debug)]
pub(crate) enum PidStat {
Read(Vec<u8>),
Skipped(io::Error),
}
#[derive(Debug)]
pub(crate) struct Unreadable {
pid: u32,
kind: Kind,
source: io::Error,
}
#[derive(Debug)]
enum Kind {
BeyondMount,
DirectoryRefuses(io::Error),
Other,
}
impl Unreadable {
pub(crate) fn into_source(self) -> io::Error {
self.source
}
}
impl std::fmt::Display for Unreadable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Unreadable { pid, kind, source } = self;
match kind {
Kind::BeyondMount => write!(f, "{pid}/stat lies beyond a mount in /proc and was not read: {source}"),
Kind::DirectoryRefuses(refusal) => write!(
f,
"{pid}/stat answered {source} and the checked /proc then refused self/stat: {refusal}"
),
Kind::Other => write!(f, "{pid}/stat could not be read: {source}"),
}
}
}
pub(crate) fn read(dir: &ProcDir, pid: u32) -> Result<PidStat, Unreadable> {
let source = match read_raw(dir, pid) {
Ok(stat) => return Ok(PidStat::Read(stat)),
Err(e) => e,
};
let kind = match source.raw_os_error() {
Some(libc::ENOENT | libc::ESRCH | libc::EACCES) => return Ok(PidStat::Skipped(source)),
Some(libc::EPERM) => match check_dir_answers(dir) {
Ok(()) => return Ok(PidStat::Skipped(source)),
Err(refusal) => Kind::DirectoryRefuses(refusal),
},
Some(libc::EXDEV | libc::ELOOP) => Kind::BeyondMount,
_ => Kind::Other,
};
Err(Unreadable { pid, kind, source })
}
fn read_raw(dir: &ProcDir, pid: u32) -> io::Result<Vec<u8>> {
#[cfg(test)]
if let Some(errno) = fault::forced_stat_read_errno() {
return Err(io::Error::from_raw_os_error(errno));
}
#[cfg(test)]
if let Some(bytes) = fault::forced_stat_bytes() {
return Ok(bytes);
}
dir.read(&format!("{pid}/stat"))
}
fn check_dir_answers(dir: &ProcDir) -> io::Result<()> {
#[cfg(test)]
if let Some(errno) = fault::forced_recheck_errno() {
return Err(io::Error::from_raw_os_error(errno));
}
dir.read("self/stat").map(drop)
}
#[cfg(test)]
pub(crate) mod fault {
thread_local! {
static STAT_READ: std::cell::Cell<Option<(i32, Option<i32>)>> = const { std::cell::Cell::new(None) };
static STAT_BYTES: std::cell::RefCell<Option<Vec<u8>>> = const { std::cell::RefCell::new(None) };
}
#[must_use = "dropping this immediately disarms the forced read"]
pub(crate) struct ForcedStatRead(());
impl Drop for ForcedStatRead {
fn drop(&mut self) {
STAT_READ.with(|f| f.set(None));
STAT_BYTES.with(|f| f.borrow_mut().take());
}
}
pub(crate) fn force_stat_bytes(bytes: &[u8]) -> ForcedStatRead {
STAT_BYTES.with(|f| *f.borrow_mut() = Some(bytes.to_vec()));
ForcedStatRead(())
}
pub(super) fn forced_stat_bytes() -> Option<Vec<u8>> {
STAT_BYTES.with(|f| f.borrow().clone())
}
pub(crate) fn force_stat_read(errno: i32, recheck: Option<i32>) -> ForcedStatRead {
STAT_READ.with(|f| f.set(Some((errno, recheck))));
ForcedStatRead(())
}
pub(super) fn forced_recheck_errno() -> Option<i32> {
STAT_READ.with(|f| f.get()).and_then(|(_, recheck)| recheck)
}
pub(super) fn forced_stat_read_errno() -> Option<i32> {
STAT_READ.with(|f| f.get()).map(|(errno, _)| errno)
}
}