Skip to content

Commit f636cb9

Browse files
authored
Merge pull request #555 from dezgeg/ps_fixes
ps: Fixes
2 parents 0287bbd + 29b8eb2 commit f636cb9

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

src/uu/ps/src/collector.rs

Lines changed: 13 additions & 15 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
}
@@ -85,9 +78,14 @@ pub(crate) fn session_collector(
8578
let tty = |proc: &Rc<RefCell<ProcessInformation>>| proc.borrow_mut().tty();
8679

8780
// flag `-d`
88-
// TODO: Implementation this collection, guessing it pid=sid
8981
if matches.get_flag("d") {
90-
proc_snapshot.iter().for_each(|_| {});
82+
for proc_info in proc_snapshot {
83+
let pid = proc_info.borrow().pid;
84+
85+
if getsid(pid as i32) != Some(pid as i32) {
86+
result.push(proc_info.clone());
87+
}
88+
}
9189
}
9290

9391
// flag `-a`

src/uu/ps/src/ps.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
3838
.collect::<Vec<_>>();
3939
let mut proc_infos = Vec::new();
4040

41-
proc_infos.extend(collector::basic_collector(&snapshot));
42-
proc_infos.extend(collector::process_collector(&matches, &snapshot));
43-
proc_infos.extend(collector::session_collector(&matches, &snapshot));
41+
if !matches.get_flag("A") && !matches.get_flag("a") && !matches.get_flag("d") {
42+
proc_infos.extend(collector::basic_collector(&snapshot));
43+
} else {
44+
proc_infos.extend(collector::process_collector(&matches, &snapshot));
45+
proc_infos.extend(collector::session_collector(&matches, &snapshot));
46+
}
4447

48+
proc_infos.sort_by(|a, b| a.borrow().pid.cmp(&b.borrow().pid));
4549
proc_infos.dedup_by(|a, b| a.borrow().pid == b.borrow().pid);
4650

4751
sorting::sort(&mut proc_infos, &matches);

0 commit comments

Comments
 (0)