Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix process iterator when used with a custom root #204

Merged
merged 2 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,19 @@ mod tests {
println!("{:?}", load);
}

#[test]
fn test_loadavg_from_reader() -> ProcResult<()> {
let load_average = LoadAverage::from_reader("2.63 1.00 1.42 3/4280 2496732".as_bytes())?;

assert_eq!(load_average.one, 2.63);
assert_eq!(load_average.five, 1.00);
assert_eq!(load_average.fifteen, 1.42);
assert_eq!(load_average.max, 4280);
assert_eq!(load_average.cur, 3);
assert_eq!(load_average.latest_pid, 2496732);
Ok(())
}

#[test]
fn test_from_str() -> ProcResult<()> {
assert_eq!(from_str!(u8, "12"), 12);
Expand Down
10 changes: 7 additions & 3 deletions src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,7 +862,7 @@ impl Process {
}

/// Is this process still alive?
///
///
/// Processes in the Zombie or Dead state are not considered alive.
pub fn is_alive(&self) -> bool {
if let Ok(stat) = self.stat() {
Expand Down Expand Up @@ -1516,7 +1516,10 @@ pub fn all_processes_with_root(root: impl AsRef<Path>) -> ProcResult<ProcessesIt
)
)?;
let dir = wrap_io_error!(root, rustix::fs::Dir::read_from(dir))?;
Ok(ProcessesIter { inner: dir })
Ok(ProcessesIter {
root: PathBuf::from(root),
inner: dir,
})
}

/// An iterator over all processes in the system.
Expand All @@ -1528,6 +1531,7 @@ pub fn all_processes_with_root(root: impl AsRef<Path>) -> ProcResult<ProcessesIt
/// for more information.
#[derive(Debug)]
pub struct ProcessesIter {
root: PathBuf,
inner: rustix::fs::Dir,
}

Expand All @@ -1538,7 +1542,7 @@ impl std::iter::Iterator for ProcessesIter {
match self.inner.next() {
Some(Ok(entry)) => {
if let Ok(pid) = i32::from_str(&entry.file_name().to_string_lossy()) {
if let Ok(proc) = Process::new(pid) {
if let Ok(proc) = Process::new_with_root(self.root.join(pid.to_string())) {
break Some(Ok(proc));
}
}
Expand Down