Skip to content

Commit 16b5cc2

Browse files
committed
process_matcher: Don't match own procps process
For example, pgrep should never report itself as a match (but it can match other pgrep processes).
1 parent 4a88d69 commit 16b5cc2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
168168
let filtered: Vec<ProcessInformation> = {
169169
let mut tmp_vec = Vec::new();
170170

171+
let our_pid = std::process::id() as usize;
171172
for mut pid in walk_process().collect::<Vec<_>>() {
173+
if pid.pid == our_pid {
174+
continue;
175+
}
176+
172177
let run_state_matched = match (&settings.runstates, pid.run_state()) {
173178
(Some(arg_run_states), Ok(pid_state)) => {
174179
arg_run_states.contains(&pid_state.to_string())

tests/by-util/test_pgrep.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,15 @@ fn test_current_user() {
420420
.arg(uucore::process::getuid().to_string())
421421
.succeeds();
422422
}
423+
424+
#[test]
425+
#[cfg(target_os = "linux")]
426+
fn test_does_not_match_current_process() {
427+
let our_pid = std::process::id();
428+
dbg!(&our_pid);
429+
new_ucmd!()
430+
.arg("-f")
431+
.arg("UNIQUE_STRING_THAT_DOES_NOT_MATCH_ANY_OTHER_PROCESS")
432+
.fails()
433+
.no_output();
434+
}

0 commit comments

Comments
 (0)