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().
This commit is contained in:
Tuomas Tynkkynen
2025-10-25 14:15:56 +03:00
parent 8edbe400b6
commit 29b8eb24e1
+6 -13
View File
@@ -6,7 +6,7 @@
use clap::ArgMatches;
#[cfg(target_family = "unix")]
use nix::errno::Errno;
use std::{cell::RefCell, path::PathBuf, rc::Rc, str::FromStr};
use std::{cell::RefCell, rc::Rc};
use uu_pgrep::process::{ProcessInformation, Teletype};
// TODO: Temporary add to this file, this function will add to uucore.
@@ -29,25 +29,18 @@ fn getsid(_pid: i32) -> Option<i32> {
Some(0)
}
// Guessing it matches the current terminal
// Default behavior: select processes with same terminal and same effective user ID
pub(crate) fn basic_collector(
proc_snapshot: &[Rc<RefCell<ProcessInformation>>],
) -> Vec<Rc<RefCell<ProcessInformation>>> {
let mut result = Vec::new();
let current_tty = {
// SAFETY: The `libc::getpid` always return i32
let proc_path =
PathBuf::from_str(&format!("/proc/{}/", unsafe { libc::getpid() })).unwrap();
let current_proc_info = ProcessInformation::try_new(proc_path).unwrap();
current_proc_info.tty()
};
let mut cur = ProcessInformation::current_process_info().unwrap();
let tty = cur.tty();
let euid = cur.euid().unwrap();
for proc_info in proc_snapshot {
let proc_ttys = proc_info.borrow().tty();
if proc_ttys == current_tty {
if proc_info.borrow().tty() == tty && proc_info.borrow_mut().euid().unwrap() == euid {
result.push(proc_info.clone());
}
}