Skip to content

Commit 29b8eb2

Browse files
committed
ps: Add euid check for default output format
For instance, leaving a `sudo sleep 9999 &` running in the same terminal shouldn't be displayed. Clean up code a bit by utilizing current_process_info().
1 parent 8edbe40 commit 29b8eb2

File tree

1 file changed

+6
-13
lines changed

1 file changed

+6
-13
lines changed

src/uu/ps/src/collector.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use clap::ArgMatches;
77
#[cfg(target_family = "unix")]
88
use nix::errno::Errno;
9-
use std::{cell::RefCell, path::PathBuf, rc::Rc, str::FromStr};
9+
use std::{cell::RefCell, rc::Rc};
1010
use uu_pgrep::process::{ProcessInformation, Teletype};
1111

1212
// TODO: Temporary add to this file, this function will add to uucore.
@@ -29,25 +29,18 @@ fn getsid(_pid: i32) -> Option<i32> {
2929
Some(0)
3030
}
3131

32-
// Guessing it matches the current terminal
32+
// Default behavior: select processes with same terminal and same effective user ID
3333
pub(crate) fn basic_collector(
3434
proc_snapshot: &[Rc<RefCell<ProcessInformation>>],
3535
) -> Vec<Rc<RefCell<ProcessInformation>>> {
3636
let mut result = Vec::new();
3737

38-
let current_tty = {
39-
// SAFETY: The `libc::getpid` always return i32
40-
let proc_path =
41-
PathBuf::from_str(&format!("/proc/{}/", unsafe { libc::getpid() })).unwrap();
42-
let current_proc_info = ProcessInformation::try_new(proc_path).unwrap();
43-
44-
current_proc_info.tty()
45-
};
38+
let mut cur = ProcessInformation::current_process_info().unwrap();
39+
let tty = cur.tty();
40+
let euid = cur.euid().unwrap();
4641

4742
for proc_info in proc_snapshot {
48-
let proc_ttys = proc_info.borrow().tty();
49-
50-
if proc_ttys == current_tty {
43+
if proc_info.borrow().tty() == tty && proc_info.borrow_mut().euid().unwrap() == euid {
5144
result.push(proc_info.clone());
5245
}
5346
}

0 commit comments

Comments
 (0)