Skip to content

Commit 27dd036

Browse files
committed
process: Add function for getting root directory of a process
1 parent 54c3725 commit 27dd036

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// file that was distributed with this source code.
55

66
use regex::Regex;
7+
use std::fs::read_link;
78
use std::hash::Hash;
89
use std::sync::LazyLock;
910
use std::{
@@ -377,6 +378,11 @@ impl ProcessInformation {
377378
self.get_uid_or_gid_field("Gid", 1)
378379
}
379380

381+
// Root directory of the process (which can be changed by chroot)
382+
pub fn root(&mut self) -> Result<PathBuf, io::Error> {
383+
read_link(format!("/proc/{}/root", self.pid))
384+
}
385+
380386
/// Fetch run state from [ProcessInformation::cached_stat]
381387
///
382388
/// - [The /proc Filesystem: Table 1-4](https://docs.kernel.org/filesystems/proc.html#id10)
@@ -612,4 +618,11 @@ mod tests {
612618
assert_eq!(pid_entry.gid().unwrap(), uucore::process::getgid());
613619
assert_eq!(pid_entry.egid().unwrap(), uucore::process::getegid());
614620
}
621+
622+
#[test]
623+
#[cfg(target_os = "linux")]
624+
fn test_root() {
625+
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
626+
assert_eq!(pid_entry.root().unwrap(), PathBuf::from("/"));
627+
}
615628
}

0 commit comments

Comments
 (0)