Skip to content

Commit b7bcec1

Browse files
authored
Merge pull request #216 from tatref/iomem
Add /proc/iomem
2 parents 6030a50 + b6a6597 commit b7bcec1

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

examples/iomem.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Print physical location of system RAM
3+
// This requires CAP_SYS_ADMIN privilege, or root, otherwise physical memory addresses will be zero
4+
//
5+
6+
fn main() {
7+
if !rustix::process::geteuid().is_root() {
8+
println!("WARNING: Access to /proc/iomem requires root, re-run with sudo");
9+
}
10+
11+
let iomem = procfs::iomem().expect("Can't read /proc/iomem");
12+
13+
for (indent, map) in iomem.iter() {
14+
if map.name == "System RAM" {
15+
println!("Found RAM here: 0x{:x}-0x{:x}", map.address.0, map.address.1);
16+
}
17+
}
18+
19+
if !rustix::process::geteuid().is_root() {
20+
println!("\n\nWARNING: Access to /proc/iomem requires root, re-run with sudo");
21+
}
22+
}

src/iomem.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ pub mod keyring;
288288
mod uptime;
289289
pub use uptime::*;
290290

291+
mod iomem;
292+
pub use iomem::*;
293+
291294
lazy_static! {
292295
/// The number of clock ticks per second.
293296
///

support.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ This is an approximate list of all the files under the `/proc` mount, and an ind
8181
* [ ] `/proc/fs`
8282
* [ ] `/proc/ide`
8383
* [ ] `/proc/interrupts`
84-
* [ ] `/proc/iomem`
84+
* [x] `/proc/iomem`
8585
* [ ] `/proc/ioports`
8686
* [ ] `/proc/kallsyms`
8787
* [ ] `/proc/kcore`

0 commit comments

Comments
 (0)