Skip to content

Commit 804275b

Browse files
authored
Merge pull request #580 from dezgeg/quick_pids
ps: Add --quick-pid option
2 parents e6ff158 + 9add1c1 commit 804275b

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/uu/ps/src/process_selection.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ pub struct ProcessSelectionSettings {
4949

5050
/// - `-C` Select by command name
5151
pub command_names: Option<HashSet<String>>,
52+
/// - `-q, --quick-pid` Quick process selection by PID
53+
pub quick_pids: Option<HashSet<usize>>,
5254
/// - `-p, --pid` Select specific process IDs
5355
pub pids: Option<HashSet<usize>>,
5456
/// - `--ppid` Select specific parent process IDs
@@ -81,6 +83,9 @@ impl ProcessSelectionSettings {
8183
command_names: matches
8284
.get_many::<Vec<String>>("command")
8385
.map(|xs| xs.flatten().cloned().collect()),
86+
quick_pids: matches
87+
.get_many::<Vec<usize>>("quick-pid")
88+
.map(|xs| xs.flatten().copied().collect()),
8489
pids: matches
8590
.get_many::<Vec<usize>>("pid")
8691
.map(|xs| xs.flatten().copied().collect()),
@@ -108,6 +113,18 @@ impl ProcessSelectionSettings {
108113
}
109114

110115
pub fn select_processes(self) -> UResult<Vec<ProcessInformation>> {
116+
if let Some(ref quick_pids) = self.quick_pids {
117+
let mut selected = Vec::new();
118+
for &pid in quick_pids {
119+
if let Ok(process) =
120+
ProcessInformation::try_new(std::path::PathBuf::from(format!("/proc/{}", pid)))
121+
{
122+
selected.push(process);
123+
}
124+
}
125+
return Ok(selected);
126+
}
127+
111128
let mut current_process = ProcessInformation::current_process_info().unwrap();
112129
let current_tty = current_process.tty();
113130
let current_euid = current_process.euid().unwrap();

src/uu/ps/src/ps.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ pub fn uu_app() -> Command {
379379
.value_parser(parse_uid_list)
380380
.help("select by effective user ID (EUID) or name"),
381381
)
382-
// .args([
383-
// Arg::new("pPID").long("ppid").help("parent process id"),
384-
// Arg::new("qPID")
385-
// .short('q')
386-
// .long("quick-pid")
387-
// .help("process id"),
388-
// Arg::new("t").short('t').long("tty").help("terminal"),
389-
// ])
382+
.arg(
383+
Arg::new("quick-pid")
384+
.short('q')
385+
.long("quick-pid")
386+
.action(ArgAction::Append)
387+
.value_parser(parse_numeric_list)
388+
.help("quick process selection by PID"),
389+
)
390390
}

tests/by-util/test_ps.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ fn test_command_name_selection() {
230230

231231
#[test]
232232
#[cfg(target_os = "linux")]
233-
fn test_pid_selection() {
233+
fn test_pid_and_quick_pid_selection() {
234234
let our_pid = std::process::id();
235235
// Test that only pid 1 and pid of the test runner is present
236236
let test = |pid_args: &[&str]| {
@@ -243,24 +243,24 @@ fn test_pid_selection() {
243243
.stdout_matches(&match_regex);
244244
};
245245

246-
for flag in ["-p", "--pid"] {
246+
for flag in ["-p", "--pid", "-q", "--quick-pid"] {
247247
test(&[flag, &format!("1 {our_pid}")]);
248248
test(&[flag, &format!("1,{our_pid}")]);
249249
test(&[flag, "1", flag, &our_pid.to_string()]);
250-
}
251250

252-
// Test nonexistent PID
253-
new_ucmd!()
254-
.args(&["-p", "0", "--no-headers"])
255-
.fails()
256-
.code_is(1)
257-
.no_output();
251+
// Test nonexistent PID
252+
new_ucmd!()
253+
.args(&[flag, "0", "--no-headers"])
254+
.fails()
255+
.code_is(1)
256+
.no_output();
258257

259-
// Test invalid PID
260-
new_ucmd!()
261-
.args(&["-p", "invalid"])
262-
.fails()
263-
.stderr_contains("invalid number");
258+
// Test invalid PID
259+
new_ucmd!()
260+
.args(&[flag, "invalid"])
261+
.fails()
262+
.stderr_contains("invalid number");
263+
}
264264
}
265265

266266
#[test]

0 commit comments

Comments
 (0)