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).
This commit is contained in:
Tuomas Tynkkynen
2025-02-25 23:31:45 +02:00
parent 4a88d6960b
commit 16b5cc2464
2 changed files with 17 additions and 0 deletions
+5
View File
@@ -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())
+12
View File
@@ -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();
}