|
| 1 | +#[cfg(feature = "serde1")] |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | +use std::io::{BufRead, BufReader}; |
| 4 | + |
| 5 | +use super::{FileWrapper, ProcResult}; |
| 6 | +use crate::split_into_num; |
| 7 | + |
| 8 | +/// Reads and parses the `/proc/iomem`, returning an error if there are problems. |
| 9 | +/// |
| 10 | +/// Requires root, otherwise every memory address will be zero |
| 11 | +pub fn iomem() -> ProcResult<Vec<(usize, PhysicalMemoryMap)>> { |
| 12 | + let f = FileWrapper::open("/proc/iomem")?; |
| 13 | + |
| 14 | + let reader = BufReader::new(f); |
| 15 | + let mut vec = Vec::new(); |
| 16 | + |
| 17 | + for line in reader.lines() { |
| 18 | + let line = expect!(line); |
| 19 | + |
| 20 | + let (indent, map) = PhysicalMemoryMap::from_line(&line)?; |
| 21 | + |
| 22 | + vec.push((indent, map)); |
| 23 | + } |
| 24 | + |
| 25 | + Ok(vec) |
| 26 | +} |
| 27 | + |
| 28 | +/// To construct this structure, see [crate::iomem()]. |
| 29 | +#[derive(Debug, PartialEq, Eq, Clone, Hash)] |
| 30 | +#[cfg_attr(feature = "serde1", derive(Serialize, Deserialize))] |
| 31 | +pub struct PhysicalMemoryMap { |
| 32 | + /// The address space in the process that the mapping occupies. |
| 33 | + pub address: (u64, u64), |
| 34 | + pub name: String, |
| 35 | +} |
| 36 | + |
| 37 | +impl PhysicalMemoryMap { |
| 38 | + fn from_line(line: &str) -> ProcResult<(usize, PhysicalMemoryMap)> { |
| 39 | + let indent = line.chars().take_while(|c| *c == ' ').count() / 2; |
| 40 | + let line = line.trim(); |
| 41 | + let mut s = line.split(" : "); |
| 42 | + let address = expect!(s.next()); |
| 43 | + let name = expect!(s.next()); |
| 44 | + |
| 45 | + Ok(( |
| 46 | + indent, |
| 47 | + PhysicalMemoryMap { |
| 48 | + address: split_into_num(address, '-', 16)?, |
| 49 | + name: String::from(name), |
| 50 | + }, |
| 51 | + )) |
| 52 | + } |
| 53 | +} |
0 commit comments