[PATCH] pgrep/pkill: fix -x to also match comm field

Paulius Zaleckas paulius.zaleckas at gmail.com
Wed Aug 27 09:03:54 UTC 2025


When running `pgrep -x example` against a process `/bin/example --arg`,
BusyBox fails to match, while GNU pgrep succeeds. The reason is that the
comparison is done only against the full argv[0] rather than comm. This
patch changes pgrep -x to also try /proc/[pid]/comm for exact matching.

Before fix:
  $ /usr/bin/sleep 1000 &
  [1] 400479
  $ pgrep -x sleep
  400479
  $ ./busybox pgrep -x sleep
  $ ./busybox pgrep sleep
  400479
After fix:
  $ ./busybox pgrep -x sleep
  400479

Signed-off-by: Paulius Zaleckas <paulius.zaleckas at gmail.com>
---
diff --git a/procps/pgrep.c b/procps/pgrep.c
index 04ae92a67..299e2dac7 100644
--- a/procps/pgrep.c
+++ b/procps/pgrep.c
@@ -215,16 +215,16 @@ int pgrep_main(int argc UNUSED_PARAM, char **argv)
 		if (!match) {
  again:
 			match = (regexec(&re_buffer, cmd, 1, re_match, 0) == 0);
+			if (match && OPT_ANCHOR) {
+				/* -x requires full string match */
+				match = (re_match[0].rm_so == 0 && cmd[re_match[0].rm_eo] == '\0');
+			}
 			if (!match && cmd != proc->comm) {
 				/* if argv[] did not match, try comm */
 				cmdlen = -1;
 				cmd = proc->comm;
 				goto again;
 			}
-			if (match && OPT_ANCHOR) {
-				/* -x requires full string match */
-				match = (re_match[0].rm_so == 0 && cmd[re_match[0].rm_eo] == '\0');
-			}
 		}
 
 		/* NB: OPT_INVERT is always 0 or 1 */


More information about the busybox mailing list