Skip to content

Commit

Permalink
Merge pull request #339 from dezgeg/self-check
Browse files Browse the repository at this point in the history
process_matcher: Don't match own procps process
  • Loading branch information
cakebaker authored Feb 26, 2025
2 parents 4a88d69 + 16b5cc2 commit 33b471e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/uu/pgrep/src/process_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
let filtered: Vec<ProcessInformation> = {
let mut tmp_vec = Vec::new();

let our_pid = std::process::id() as usize;
for mut pid in walk_process().collect::<Vec<_>>() {
if pid.pid == our_pid {
continue;
}

let run_state_matched = match (&settings.runstates, pid.run_state()) {
(Some(arg_run_states), Ok(pid_state)) => {
arg_run_states.contains(&pid_state.to_string())
Expand Down
12 changes: 12 additions & 0 deletions tests/by-util/test_pgrep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,15 @@ fn test_current_user() {
.arg(uucore::process::getuid().to_string())
.succeeds();
}

#[test]
#[cfg(target_os = "linux")]
fn test_does_not_match_current_process() {
let our_pid = std::process::id();
dbg!(&our_pid);
new_ucmd!()
.arg("-f")
.arg("UNIQUE_STRING_THAT_DOES_NOT_MATCH_ANY_OTHER_PROCESS")
.fails()
.no_output();
}

0 comments on commit 33b471e

Please sign in to comment.