heim_process/sys/linux/process/procfs/
io.rs

1use std::str::FromStr;
2
3use heim_common::prelude::*;
4use heim_common::utils::iter::TryIterator;
5use heim_common::Pid;
6use heim_runtime::fs;
7
8use crate::os::linux::IoCounters;
9use crate::{ProcessError, ProcessResult};
10
11impl FromStr for IoCounters {
12    type Err = Error;
13
14    fn from_str(s: &str) -> Result<Self> {
15        let mut counters = IoCounters::default();
16        for line in s.lines() {
17            let mut parts = line.split_ascii_whitespace();
18            let field = match parts.try_next()? {
19                "rchar:" => &mut counters.rchar,
20                "wchar:" => &mut counters.wchar,
21                "syscr:" => &mut counters.syscr,
22                "syscw:" => &mut counters.syscw,
23                "read_bytes:" => &mut counters.read_bytes,
24                "write_bytes:" => &mut counters.write_bytes,
25                "cancelled_write_bytes:" => &mut counters.cancelled_write_bytes,
26                other => return Err(Error::incompatible(format!("Unknown field {}", other))),
27            };
28
29            *field = parts.try_next()?.parse::<u64>()?;
30        }
31
32        Ok(counters)
33    }
34}
35
36pub fn io(pid: Pid) -> impl Future<Output = ProcessResult<IoCounters>> {
37    fs::read_into(format!("/proc/{}/io", pid)).map_err(move |e: Error| {
38        match e.raw_os_error() {
39            // TODO: It is not possible to get `::std::io::ErrorKind` from the `heim::Error`
40            Some(libc::EACCES) => ProcessError::AccessDenied(pid),
41            _ => e.into(),
42        }
43    })
44}