Skip to content

Commit cb6a977

Browse files
committed
process: Add methods for pgid and sid
Also extract a helper in test code.
1 parent 428cb2b commit cb6a977

File tree

1 file changed

+37
-13
lines changed

1 file changed

+37
-13
lines changed

src/uu/pgrep/src/process.rs

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ impl ProcessInformation {
305305
self.get_numeric_stat_field(3)
306306
}
307307

308+
pub fn pgid(&mut self) -> Result<u64, io::Error> {
309+
// the process group ID is the fifth field in /proc/<PID>/stat
310+
// (https://www.kernel.org/doc/html/latest/filesystems/proc.html#id10)
311+
self.get_numeric_stat_field(4)
312+
}
313+
314+
pub fn sid(&mut self) -> Result<u64, io::Error> {
315+
// the session ID is the sixth field in /proc/<PID>/stat
316+
// (https://www.kernel.org/doc/html/latest/filesystems/proc.html#id10)
317+
self.get_numeric_stat_field(5)
318+
}
319+
308320
fn get_uid_or_gid_field(&mut self, field: &str, index: usize) -> Result<u32, io::Error> {
309321
self.status()
310322
.get(field)
@@ -474,6 +486,12 @@ mod tests {
474486
.unwrap()
475487
}
476488

489+
#[cfg(target_os = "linux")]
490+
fn current_process_info() -> ProcessInformation {
491+
ProcessInformation::try_new(PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap())
492+
.unwrap()
493+
}
494+
477495
#[test]
478496
#[cfg(target_os = "linux")]
479497
fn test_walk_pid() {
@@ -489,11 +507,7 @@ mod tests {
489507
fn test_pid_entry() {
490508
let current_pid = current_pid();
491509

492-
let pid_entry = ProcessInformation::try_new(
493-
PathBuf::from_str(&format!("/proc/{}", current_pid)).unwrap(),
494-
)
495-
.unwrap();
496-
510+
let pid_entry = current_process_info();
497511
let mut result = WalkDir::new(format!("/proc/{}/fd", current_pid))
498512
.into_iter()
499513
.flatten()
@@ -514,10 +528,7 @@ mod tests {
514528
fn test_thread_ids() {
515529
let main_tid = unsafe { uucore::libc::gettid() };
516530
std::thread::spawn(move || {
517-
let mut pid_entry = ProcessInformation::try_new(
518-
PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap(),
519-
)
520-
.unwrap();
531+
let mut pid_entry = current_process_info();
521532
let thread_ids = pid_entry.thread_ids();
522533

523534
assert!(thread_ids.contains(&(main_tid as usize)));
@@ -544,13 +555,26 @@ mod tests {
544555
assert!(stat_split(case)[1] == "sleep (2) .sh");
545556
}
546557

558+
#[test]
559+
#[cfg(target_os = "linux")]
560+
fn test_ids() {
561+
let mut pid_entry = current_process_info();
562+
assert_eq!(
563+
pid_entry.ppid().unwrap(),
564+
unsafe { uucore::libc::getppid() } as u64
565+
);
566+
assert_eq!(
567+
pid_entry.pgid().unwrap(),
568+
unsafe { uucore::libc::getpgid(0) } as u64
569+
);
570+
assert_eq!(pid_entry.sid().unwrap(), unsafe { uucore::libc::getsid(0) }
571+
as u64);
572+
}
573+
547574
#[test]
548575
#[cfg(target_os = "linux")]
549576
fn test_uid_gid() {
550-
let mut pid_entry = ProcessInformation::try_new(
551-
PathBuf::from_str(&format!("/proc/{}", current_pid())).unwrap(),
552-
)
553-
.unwrap();
577+
let mut pid_entry = current_process_info();
554578
assert_eq!(pid_entry.uid().unwrap(), uucore::process::getuid());
555579
assert_eq!(pid_entry.euid().unwrap(), uucore::process::geteuid());
556580
assert_eq!(pid_entry.gid().unwrap(), uucore::process::getgid());

0 commit comments

Comments
 (0)