From 5a8ade48c92c3938554feada59bbc4a167b96fb9 Mon Sep 17 00:00:00 2001 From: Zhang Yiming Date: Wed, 4 Nov 2026 01:15:30 -0800 Subject: [PATCH] pkill: consume the signal argument before matching pkill_main() parses a leading -SIGNAL through a local copy of toys.optargs, but leaves the argument in the global list that pgrep_main() compiles into regular expressions. As a result, "pkill -9 -f NAME" also matches unrelated command lines containing "-9". On Android, a process started with "/dev/block/dm-9" (e.g. snapuserd during an OTA's first-boot snapshot merge) is killed; every dm-user device loses its userspace daemon, and the device reboot-loops with I/O errors and SIGBUS. Consume the signal argument from toys.optargs and decrement toys.optc before invoking pgrep_main(). This restores the behavior of 148a0c3760bf ("Fix pkill -9."); any signal spelling is affected: -9, -15, -2, -TERM, -KILL, ... with or without -f/-n/-o/-x and with any of the -P/-G/-g/-s/-t/-U/-u selection options. The -l SIGNAL form was never affected and keeps working. Two behavior changes, both toward the documented interface: - "pkill -9" with neither a pattern nor a selection option now reports "No PATTERN" instead of signalling processes whose command line matches "-9"; - "pkill -9 -P PID" with no pattern now signals every child of PID instead of only those whose command line contains "-9". Validation: all 14 existing pkill tests pass. Two additional local regression cases fail before the change and pass after it; they are summarized here but not included in this patch: yes /dev/block/dm-9 >/dev/null & pid=$! sleep 1 parent=$(cut -d' ' -f4 /proc/$pid/stat) pkill -9 -f -P $parent toybox-no-such-process-$pid # expect: exit 1 (no match), $pid still alive # (-TERM case is the same with dm-TERM and -TERM) Until this lands, scripts can use the explicit form "pkill -l 9 -f PATTERN" (or -l TERM, etc.), which is unaffected. Signed-off-by: Zhang Yiming --- toys/posix/ps.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/toys/posix/ps.c b/toys/posix/ps.c index 582f76cd..36aa4c95 100644 --- a/toys/posix/ps.c +++ b/toys/posix/ps.c @@ -1995,9 +1995,10 @@ void pgrep_main(void) void pkill_main(void) { - char **args = toys.optargs; - - if (!FLAG(l) && *args && **args=='-') TT.pgrep.L = *(args++)+1; + if (!FLAG(l) && *toys.optargs && **toys.optargs=='-') { + TT.pgrep.L = *(toys.optargs++)+1; + toys.optc--; + } if (!TT.pgrep.L) TT.pgrep.signal = SIGTERM; if (FLAG(V)) TT.tty = 1; pgrep_main(); -- 2.39.5