Skip to content

Commit 26ba276

Browse files
eeyunsvartalf
authored andcommitted
Adds configurable path to /sys and /proc via naive global state
Signed-off-by: Ian Henry <[email protected]>
1 parent 970dccc commit 26ba276

File tree

29 files changed

+243
-66
lines changed

29 files changed

+243
-66
lines changed

heim-cpu/src/sys/linux/count/logical.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn sysconf() -> Result<u64> {
1616

1717
async fn cpuinfo() -> Result<u64> {
1818
rt::spawn_blocking(|| {
19-
let f = fs::File::open("/proc/cpuinfo")?;
19+
let f = fs::File::open(rt::linux::procfs_root().join("cpuinfo"))?;
2020
let reader = io::BufReader::new(f);
2121
let mut count = 0;
2222
for line in reader.lines() {
@@ -32,7 +32,7 @@ async fn cpuinfo() -> Result<u64> {
3232

3333
async fn stat() -> Result<u64> {
3434
rt::spawn_blocking(|| {
35-
let f = fs::File::open("/proc/stat")?;
35+
let f = fs::File::open(rt::linux::procfs_root().join("stat"))?;
3636
let reader = io::BufReader::new(f);
3737
let mut count = 0;
3838

heim-cpu/src/sys/linux/count/physical.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ use heim_runtime as rt;
88

99
async fn topology() -> Result<u64> {
1010
rt::spawn_blocking(|| {
11-
let entries = glob::glob("/sys/devices/system/cpu/cpu[0-9]/topology/core_id")
12-
.expect("Invalid glob pattern");
11+
let path = rt::linux::sysfs_root().join("devices/system/cpu/cpu[0-9]/topology/core_id");
12+
let entries =
13+
glob::glob(path.display().to_string().as_str()).expect("Invalid glob pattern");
1314
let mut acc = HashSet::<u64>::new();
1415

1516
for entry in entries {
@@ -48,7 +49,7 @@ fn parse_line(line: &str) -> Result<u64> {
4849
async fn cpu_info() -> Result<Option<u64>> {
4950
rt::spawn_blocking(|| {
5051
let mut acc = Collector::default();
51-
let f = fs::File::open("/proc/cpuinfo")?;
52+
let f = fs::File::open(rt::linux::procfs_root().join("cpuinfo"))?;
5253
let reader = io::BufReader::new(f);
5354

5455
let lines = reader.lines();
@@ -60,7 +61,10 @@ async fn cpu_info() -> Result<Option<u64>> {
6061
acc.physical_id = Some(core_id)
6162
} else {
6263
// TODO: In general it seems better to return an error
63-
panic!("Missed the core id value in the /proc/cpuinfo, implementation bug");
64+
panic!(
65+
"Missed the core id value in the {:?}/cpuinfo, implementation bug",
66+
rt::linux::procfs_root()
67+
);
6468
}
6569
}
6670
l if l.starts_with("core id") => {
@@ -71,7 +75,10 @@ async fn cpu_info() -> Result<Option<u64>> {
7175
let _ = acc.group.insert((physical_id, core_id));
7276
} else {
7377
// TODO: In general it seems better to return an error
74-
panic!("Missed the physical id value in the /proc/cpuinfo!");
78+
panic!(
79+
"Missed the physical id value in the {:?}/cpuinfo!",
80+
rt::linux::procfs_root()
81+
);
7582
}
7683
}
7784
_ => continue,

heim-cpu/src/sys/linux/freq.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ fn _frequencies() -> impl Iterator<Item = Result<CpuFrequency>> {
5858
// later with the thoughts and patches
5959

6060
// TODO: https://github.com/giampaolo/psutil/issues/1269
61-
let entries =
62-
glob::glob("/sys/devices/system/cpu/cpu[0-9]/cpufreq/").expect("Incorrect glob pattern");
61+
let path = rt::linux::sysfs_root().join("devices/system/cpu/cpu[0-9]/cpufreq/");
62+
63+
let entries = glob::glob(path.display().to_string().as_str()).expect("Incorrect glob pattern");
6364

6465
entries.map(|try_path| {
6566
let path = try_path.map_err(|e| e.into_error())?;

heim-cpu/src/sys/linux/stats.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ impl FromStr for CpuStats {
4444
matched_lines += 1;
4545
*field = value;
4646
}
47-
None => return Err(Error::missing_key(name, "/proc/stat")),
47+
None => {
48+
return Err(Error::missing_key(
49+
name,
50+
format!("{:?}/stat", rt::linux::procfs_root()),
51+
))
52+
}
4853
}
4954

5055
if matched_lines == 3 {
@@ -57,5 +62,5 @@ impl FromStr for CpuStats {
5762
}
5863

5964
pub async fn stats() -> Result<CpuStats> {
60-
rt::fs::read_into("/proc/stat").await
65+
rt::fs::read_into(rt::linux::procfs_root().join("stat")).await
6166
}

heim-cpu/src/sys/linux/times.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,20 @@ impl FromStr for CpuTime {
9090

9191
pub async fn time() -> Result<CpuTime> {
9292
// cumulative time is always the first line
93-
let lines = rt::fs::read_lines_into::<_, CpuTime, _>("/proc/stat").await?;
93+
let lines =
94+
rt::fs::read_lines_into::<_, CpuTime, _>(rt::linux::procfs_root().join("stat")).await?;
9495
rt::pin!(lines);
9596
match lines.next().await {
9697
Some(line) => line,
97-
None => Err(Error::missing_key("cumulative time line", "/proc/stat")),
98+
None => Err(Error::missing_key(
99+
"cumulative time line",
100+
format!("{:?}/stat", rt::linux::procfs_root()),
101+
)),
98102
}
99103
}
100104

101105
pub async fn times() -> Result<impl Stream<Item = Result<CpuTime>>> {
102-
let lines = rt::fs::read_lines("/proc/stat").await?;
106+
let lines = rt::fs::read_lines(rt::linux::procfs_root().join("stat")).await?;
103107

104108
let stream = lines.skip(1).filter_map(|try_line| async move {
105109
match try_line {

heim-disk/src/sys/linux/counters.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,11 @@ impl IoCounters {
6060
// Based on the sysstat code:
6161
// https://github.com/sysstat/sysstat/blob/1c711c1fd03ac638cfc1b25cdf700625c173fd2c/common.c#L200
6262
async fn is_storage_device(&self) -> Result<bool> {
63-
let path = CString::new(format!("/sys/block/{}", self.name.replace("/", "!")))?;
63+
let path = CString::new(format!(
64+
"{}/block/{}",
65+
rt::linux::sysfs_root().display(),
66+
self.name.replace("/", "!")
67+
))?;
6468

6569
let result =
6670
rt::spawn_blocking(move || unsafe { libc::access(path.as_ptr(), libc::F_OK) }).await;
@@ -111,7 +115,8 @@ impl FromStr for IoCounters {
111115
}
112116

113117
pub async fn io_counters() -> Result<impl Stream<Item = Result<IoCounters>>> {
114-
let stream = rt::fs::read_lines_into::<_, _, Error>("/proc/diskstats").await?;
118+
let stream =
119+
rt::fs::read_lines_into::<_, _, Error>(rt::linux::procfs_root().join("diskstats")).await?;
115120

116121
Ok(stream)
117122
}

heim-disk/src/sys/linux/partitions.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ use heim_runtime as rt;
1010

1111
use crate::FileSystem;
1212

13-
static PROC_MOUNTS: &str = "/proc/mounts";
14-
1513
#[derive(Debug)]
1614
pub struct Partition {
1715
device: Option<String>,
@@ -46,22 +44,43 @@ impl FromStr for Partition {
4644
fn from_str(line: &str) -> Result<Partition> {
4745
// Example: `/dev/sda3 /home ext4 rw,relatime,data=ordered 0 0`
4846
let mut parts = line.splitn(5, ' ');
47+
let mount_root = rt::linux::procfs_root().join("mounts");
4948
let device = match parts.next() {
5049
Some(device) if device == "none" => None,
5150
Some(device) => Some(device.to_string()),
52-
None => return Err(Error::missing_key("device", PROC_MOUNTS)),
51+
None => {
52+
return Err(Error::missing_key(
53+
"device",
54+
format!("{}", mount_root.display()),
55+
))
56+
}
5357
};
5458
let mount_point = match parts.next() {
5559
Some(point) => PathBuf::from(point),
56-
None => return Err(Error::missing_key("mount point", PROC_MOUNTS)),
60+
None => {
61+
return Err(Error::missing_key(
62+
"mount point",
63+
format!("{}", mount_root.display()),
64+
))
65+
}
5766
};
5867
let fs_type = match parts.next() {
5968
Some(fs) => FileSystem::from_str(fs)?,
60-
_ => return Err(Error::missing_key("file-system type", PROC_MOUNTS)),
69+
_ => {
70+
return Err(Error::missing_key(
71+
"file-system type",
72+
format!("{}", mount_root.display()),
73+
))
74+
}
6175
};
6276
let options = match parts.next() {
6377
Some(opts) => opts.to_string(),
64-
None => return Err(Error::missing_key("options", PROC_MOUNTS)),
78+
None => {
79+
return Err(Error::missing_key(
80+
"options",
81+
format!("{}", mount_root.display()),
82+
))
83+
}
6584
};
6685

6786
Ok(Partition {
@@ -76,7 +95,7 @@ impl FromStr for Partition {
7695
// Returns stream with known physical (only!) partitions
7796
async fn known_filesystems() -> Result<HashSet<FileSystem>> {
7897
rt::spawn_blocking(|| {
79-
let file = fs::File::open("/proc/filesystems")?;
98+
let file = fs::File::open(rt::linux::procfs_root().join("filesystems"))?;
8099
let reader = io::BufReader::new(file);
81100
let mut acc = HashSet::with_capacity(4);
82101

@@ -105,7 +124,7 @@ async fn known_filesystems() -> Result<HashSet<FileSystem>> {
105124
}
106125

107126
pub async fn partitions() -> Result<impl Stream<Item = Result<Partition>>> {
108-
let lines = rt::fs::read_lines(PROC_MOUNTS).await?;
127+
let lines = rt::fs::read_lines(rt::linux::procfs_root().join("mounts")).await?;
109128
let stream = lines
110129
.map_err(Error::from)
111130
.try_filter_map(|line| async move {

heim-host/src/sys/linux/boot_time.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ use heim_common::{
44
};
55
use heim_runtime as rt;
66

7-
const PROC_STAT: &str = "/proc/stat";
8-
97
pub async fn boot_time() -> Result<Time> {
10-
let contents = rt::fs::read_to_string(PROC_STAT).await?;
8+
let contents = rt::fs::read_to_string(rt::linux::procfs_root().join("stat")).await?;
119

1210
for line in contents.lines() {
1311
if line.starts_with("btime ") {
@@ -19,10 +17,16 @@ pub async fn boot_time() -> Result<Time> {
1917
.parse::<f64>()
2018
.map(Time::new::<time::second>)
2119
.map_err(Into::into),
22-
None => Err(Error::missing_key("btime", PROC_STAT)),
20+
None => Err(Error::missing_key(
21+
"btime",
22+
format!("{}/stat", rt::linux::procfs_root().display()),
23+
)),
2324
};
2425
}
2526
}
2627

27-
Err(Error::missing_key("btime", PROC_STAT))
28+
Err(Error::missing_key(
29+
"btime",
30+
format!("{}/stat", rt::linux::procfs_root().display()),
31+
))
2832
}

heim-host/src/sys/linux/uptime.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ use heim_common::{
44
};
55
use heim_runtime as rt;
66

7-
const PROC_UPTIME: &str = "/proc/uptime";
8-
97
pub async fn uptime() -> Result<Time> {
10-
let contents = rt::fs::read_to_string(PROC_UPTIME).await?;
8+
let contents = rt::fs::read_to_string(rt::linux::procfs_root().join("uptime")).await?;
119

1210
match contents.splitn(2, ' ').next() {
1311
Some(raw_value) => {
1412
let seconds = raw_value.parse::<f64>()?;
1513

1614
Ok(Time::new::<time::second>(seconds))
1715
}
18-
None => Err(Error::missing_key("uptime", "/proc/uptime")),
16+
None => Err(Error::missing_key(
17+
"uptime",
18+
format!("{}/uptime", rt::linux::procfs_root().display()),
19+
)),
1920
}
2021
}

heim-memory/src/sys/linux/memory.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ impl FromStr for Memory {
101101
// but at this point we are not tracking which exact field are we missing.
102102
// TODO: Rewrite parser and use `Error::missing_key` instead
103103
let inner = io::Error::from(io::ErrorKind::InvalidData);
104-
Err(Error::from(inner).with_file("/proc/meminfo"))
104+
Err(Error::from(inner).with_file(rt::linux::procfs_root().join("meminfo")))
105105
}
106106
}
107107

108108
pub async fn memory() -> Result<Memory> {
109-
rt::fs::read_into("/proc/meminfo").await
109+
rt::fs::read_into(rt::linux::procfs_root().join("meminfo")).await
110110
}

0 commit comments

Comments
 (0)