use std::borrow::Cow;
use std::io;
use std::sync::Arc;
use super::kernel::Kernel;
use super::proc_read::{read_own, OwnUnreadable, ProcDir, ReadFailure};
use super::refusal::Refusal;
use crate::error::io_context;
#[derive(Debug)]
pub(crate) enum ReadOutcome<T = Vec<u8>> {
Read(T),
Exited(io::Error),
Hidden(io::Error),
Refused(Refusal),
}
#[derive(Debug)]
pub(crate) struct Unreadable {
pid: u32,
file: Cow<'static, str>,
kind: Kind,
cause: io::Error,
peer: Option<io::Error>,
}
impl Unreadable {
pub(crate) fn take_peer(&mut self) -> Option<io::Error> {
self.peer.take()
}
pub(crate) fn into_cause(mut self) -> crate::error::Error {
use crate::error::Error;
let peer = self.take_peer();
let own = Error::Io(io::Error::other(self));
match peer {
None => own,
Some(peer) => Error::Several(crate::error::Failures::new(
std::sync::Arc::new(own),
[Error::io_cause(peer)],
)),
}
}
pub(crate) fn into_io(self) -> io::Error {
self.into_cause().into_io_over("two reads failed")
}
}
#[derive(Debug)]
enum Kind {
BeyondMount,
BesideRefusal,
RecheckFailed,
Other,
}
impl std::fmt::Display for Unreadable {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let Unreadable { pid, file, kind, .. } = self;
match kind {
Kind::BeyondMount => write!(f, "{pid}/{file} lies beyond a mount in /proc and was not read"),
Kind::BesideRefusal => write!(
f,
"{pid}/{file} was refused, and the checked /proc refused self/stat too"
),
Kind::RecheckFailed => write!(f, "{pid}/{file} was refused, and the re-check of self/stat failed"),
Kind::Other => write!(f, "{pid}/{file} could not be read"),
}
}
}
impl std::error::Error for Unreadable {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.cause)
}
}
pub(crate) fn read(
kernel: &dyn Kernel,
dir: &ProcDir,
pid: u32,
file: &'static str,
) -> Result<ReadOutcome, Unreadable> {
classify(
kernel,
dir,
pid,
file.into(),
Entry::File,
kernel.read(dir, &format!("{pid}/{file}")),
)
}
pub(crate) fn read_task_stat(
kernel: &dyn Kernel,
dir: &ProcDir,
pid: u32,
tid: u32,
) -> Result<ReadOutcome, Unreadable> {
classify(
kernel,
dir,
pid,
format!("task/{tid}/stat").into(),
Entry::File,
kernel.read(dir, &format!("{pid}/task/{tid}/stat")),
)
}
pub(crate) fn read_tasks(kernel: &dyn Kernel, dir: &ProcDir, pid: u32) -> Result<ReadOutcome<Vec<u32>>, Unreadable> {
classify(kernel, dir, pid, "task".into(), Entry::File, kernel.tids(dir, pid))
}
pub(crate) fn read_link(
kernel: &dyn Kernel,
dir: &ProcDir,
pid: u32,
file: &'static str,
) -> Result<ReadOutcome, Unreadable> {
classify(
kernel,
dir,
pid,
file.into(),
Entry::Link,
kernel.read_link(dir, &format!("{pid}/{file}")),
)
}
#[derive(Clone, Copy)]
enum Entry {
File,
Link,
}
fn classify<T>(
kernel: &dyn Kernel,
dir: &ProcDir,
pid: u32,
file: Cow<'static, str>,
entry: Entry,
read: Result<T, ReadFailure>,
) -> Result<ReadOutcome<T>, Unreadable> {
let source = match read {
Ok(read) => return Ok(ReadOutcome::Read(read)),
Err(ReadFailure::Open(source)) => source,
Err(ReadFailure::Read(source)) => match (entry, source.raw_os_error()) {
(_, Some(libc::ESRCH)) => return Ok(ReadOutcome::Exited(source)),
(Entry::Link, Some(libc::EACCES)) => {
return Ok(ReadOutcome::Hidden(io_context(
"the link was not read: ptrace of the task is denied, or the task has exited",
source,
)));
}
(Entry::Link, Some(libc::ENOENT)) => return Ok(ReadOutcome::Exited(source)),
_ => {
return Err(Unreadable {
pid,
file,
kind: Kind::Other,
cause: source,
peer: None,
});
}
},
};
let (kind, cause, peer) = match source.raw_os_error() {
Some(libc::ESRCH) => return Ok(ReadOutcome::Exited(source)),
Some(libc::ENOENT | libc::EACCES) => return Ok(ReadOutcome::Hidden(source)),
Some(libc::EPERM) => match read_own(kernel, dir, "self/stat") {
Ok(_) => return Ok(ReadOutcome::Hidden(source)),
Err(OwnUnreadable::Refused(refusal)) => {
let pid_failure = Unreadable {
pid,
file,
kind: Kind::BesideRefusal,
cause: source,
peer: None,
};
return Ok(ReadOutcome::Refused(refusal.beside(Arc::new(pid_failure.into_cause()))));
}
Err(OwnUnreadable::Failed(recheck)) => (Kind::RecheckFailed, recheck, Some(source)),
},
Some(libc::EXDEV | libc::ELOOP) => (Kind::BeyondMount, source, None),
_ => {
let failure = ReadFailure::Open(source);
match failure.refusal() {
Some(refusal) => return Ok(ReadOutcome::Refused(refusal)),
None => (Kind::Other, failure.into_io(), None),
}
}
};
Err(Unreadable {
pid,
file,
kind,
cause,
peer,
})
}
#[cfg(test)]
#[path = "pid_file_tests.rs"]
mod pid_file_tests;