Skip to content

Commit 43f7412

Browse files
committed
process_matcher: Fix accidentally matching against pid
Currently pgrep/pkill without -x/-f flags is matching based on the first 15 characters of /proc/<pid>/stat, which actually contains something like "1116878 (cat) R" thus matching the process id when it should just match on the name. This has probably come from misunderstanding the comment from manpage: > The process name used for matching is limited to the 15 characters > present in the output of /proc/pid/stat. ... which doesn't mean pgrep/pkill is literally matching on 15 characters of /proc/<pid>/stat but that the process name in that file is truncated to 15 characters. Fixes #307
1 parent a2d49d6 commit 43f7412

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

src/uu/pgrep/src/process_matcher.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,12 @@ fn collect_matched_pids(settings: &Settings) -> Vec<ProcessInformation> {
160160
name.into()
161161
};
162162
let pattern_matched = {
163-
let want = if settings.exact {
164-
// Equals `Name` in /proc/<pid>/status
165-
// The `unwrap` operation must succeed
166-
// because the REGEX has been verified as correct in `uumain`.
167-
&name
168-
} else if settings.full {
163+
let want = if settings.full {
169164
// Equals `cmdline` in /proc/<pid>/cmdline
170165
&pid.cmdline
171166
} else {
172-
// From manpage:
173-
// The process name used for matching is limited to the 15 characters present in the output of /proc/pid/stat.
174-
&pid.proc_stat()[..15]
167+
// Equals `Name` in /proc/<pid>/status
168+
&name
175169
};
176170

177171
settings.regex.is_match(want)

tests/by-util/test_pgrep.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,13 @@ fn test_invalid_signal() {
369369
.stderr_contains("Unknown signal 'foo'");
370370
}
371371

372+
#[test]
373+
#[cfg(target_os = "linux")]
374+
fn test_does_not_match_pid() {
375+
let our_pid = std::process::id();
376+
new_ucmd!().arg(our_pid.to_string()).fails();
377+
}
378+
372379
#[test]
373380
#[cfg(target_os = "linux")]
374381
fn test_too_long_pattern() {

0 commit comments

Comments
 (0)