Merge pull request #555 from dezgeg/ps_fixes

ps: Fixes
This commit is contained in:
Daniel Hofstetter
2025-10-30 09:21:21 +01:00
committed by GitHub
2 changed files with 20 additions and 18 deletions
+13 -15
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());
}
}
@@ -85,9 +78,14 @@ pub(crate) fn session_collector(
let tty = |proc: &Rc<RefCell<ProcessInformation>>| proc.borrow_mut().tty();
// flag `-d`
// TODO: Implementation this collection, guessing it pid=sid
if matches.get_flag("d") {
proc_snapshot.iter().for_each(|_| {});
for proc_info in proc_snapshot {
let pid = proc_info.borrow().pid;
if getsid(pid as i32) != Some(pid as i32) {
result.push(proc_info.clone());
}
}
}
// flag `-a`
+7 -3
View File
@@ -38,10 +38,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
.collect::<Vec<_>>();
let mut proc_infos = Vec::new();
proc_infos.extend(collector::basic_collector(&snapshot));
proc_infos.extend(collector::process_collector(&matches, &snapshot));
proc_infos.extend(collector::session_collector(&matches, &snapshot));
if !matches.get_flag("A") && !matches.get_flag("a") && !matches.get_flag("d") {
proc_infos.extend(collector::basic_collector(&snapshot));
} else {
proc_infos.extend(collector::process_collector(&matches, &snapshot));
proc_infos.extend(collector::session_collector(&matches, &snapshot));
}
proc_infos.sort_by(|a, b| a.borrow().pid.cmp(&b.borrow().pid));
proc_infos.dedup_by(|a, b| a.borrow().pid == b.borrow().pid);
sorting::sort(&mut proc_infos, &matches);