Skip to content

Commit 2148a52

Browse files
committed
process: Add function for getting root directory of a process
1 parent 138dc81 commit 2148a52

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::{
@@ -373,6 +374,11 @@ impl ProcessInformation {
373374
self.get_uid_or_gid_field("Gid", 1)
374375
}
375376

377+
// Root directory of the process (which can be changed by chroot)
378+
pub fn root(&mut self) -> Result<PathBuf, io::Error> {
379+
read_link(format!("/proc/{}/root", self.pid))
380+
}
381+
376382
/// Fetch run state from [ProcessInformation::cached_stat]
377383
///
378384
/// - [The /proc Filesystem: Table 1-4](https://docs.kernel.org/filesystems/proc.html#id10)
@@ -608,4 +614,11 @@ mod tests {
608614
assert_eq!(pid_entry.gid().unwrap(), uucore::process::getgid());
609615
assert_eq!(pid_entry.egid().unwrap(), uucore::process::getegid());
610616
}
617+
618+
#[test]
619+
#[cfg(target_os = "linux")]
620+
fn test_root() {
621+
let mut pid_entry = ProcessInformation::current_process_info().unwrap();
622+
assert_eq!(pid_entry.root().unwrap(), PathBuf::from("/"));
623+
}
611624
}

0 commit comments

Comments
 (0)