Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/uu/ps/src/process_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ pub struct ProcessSelectionSettings {

/// - `-C` Select by command name
pub command_names: Option<HashSet<String>>,
/// - `-q, --quick-pid` Quick process selection by PID
pub quick_pids: Option<HashSet<usize>>,
/// - `-p, --pid` Select specific process IDs
pub pids: Option<HashSet<usize>>,
/// - `--ppid` Select specific parent process IDs
Expand Down Expand Up @@ -81,6 +83,9 @@ impl ProcessSelectionSettings {
command_names: matches
.get_many::<Vec<String>>("command")
.map(|xs| xs.flatten().cloned().collect()),
quick_pids: matches
.get_many::<Vec<usize>>("quick-pid")
.map(|xs| xs.flatten().copied().collect()),
pids: matches
.get_many::<Vec<usize>>("pid")
.map(|xs| xs.flatten().copied().collect()),
Expand Down Expand Up @@ -108,6 +113,18 @@ impl ProcessSelectionSettings {
}

pub fn select_processes(self) -> UResult<Vec<ProcessInformation>> {
if let Some(ref quick_pids) = self.quick_pids {
let mut selected = Vec::new();
for &pid in quick_pids {
if let Ok(process) =
ProcessInformation::try_new(std::path::PathBuf::from(format!("/proc/{}", pid)))
{
selected.push(process);
}
}
return Ok(selected);
}

let mut current_process = ProcessInformation::current_process_info().unwrap();
let current_tty = current_process.tty();
let current_euid = current_process.euid().unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/uu/ps/src/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ pub fn uu_app() -> Command {
.value_parser(parse_uid_list)
.help("select by effective user ID (EUID) or name"),
)
// .args([
// Arg::new("pPID").long("ppid").help("parent process id"),
// Arg::new("qPID")
// .short('q')
// .long("quick-pid")
// .help("process id"),
// Arg::new("t").short('t').long("tty").help("terminal"),
// ])
.arg(
Arg::new("quick-pid")
.short('q')
.long("quick-pid")
.action(ArgAction::Append)
.value_parser(parse_numeric_list)
.help("quick process selection by PID"),
)
}
28 changes: 14 additions & 14 deletions tests/by-util/test_ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn test_command_name_selection() {

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

for flag in ["-p", "--pid"] {
for flag in ["-p", "--pid", "-q", "--quick-pid"] {
test(&[flag, &format!("1 {our_pid}")]);
test(&[flag, &format!("1,{our_pid}")]);
test(&[flag, "1", flag, &our_pid.to_string()]);
}

// Test nonexistent PID
new_ucmd!()
.args(&["-p", "0", "--no-headers"])
.fails()
.code_is(1)
.no_output();
// Test nonexistent PID
new_ucmd!()
.args(&[flag, "0", "--no-headers"])
.fails()
.code_is(1)
.no_output();

// Test invalid PID
new_ucmd!()
.args(&["-p", "invalid"])
.fails()
.stderr_contains("invalid number");
// Test invalid PID
new_ucmd!()
.args(&[flag, "invalid"])
.fails()
.stderr_contains("invalid number");
}
}

#[test]
Expand Down
Loading