base-files: stage2: improve /proc/*/stat parser

Simply reading /proc/*/stat as a space-separated string will not work
as the process name may itself contain spaces. Hence we must match on
the '(' and ')' characters around the process name and can then handle
the remaining string as space-separated values.
This fixes shell error messages which have been popping up the console
due to spaces in process names being interpreted as field separators.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
This commit is contained in:
Daniel Golle 2021-11-13 16:29:52 +00:00
parent f81dc44fd5
commit 4d3303b1bd
No known key found for this signature in database
GPG Key ID: 5A8F39C31C3217CA
1 changed files with 5 additions and 2 deletions

View File

@ -97,12 +97,15 @@ kill_remaining() { # [ <signal> [ <loop> ] ]
[ -f "$stat" ] || continue
local pid name state ppid rest
read pid name state ppid rest < $stat
name="${name#(}"; name="${name%)}"
read pid rest < $stat
name="${rest#\(}" ; rest="${name##*\) }" ; name="${name%\)*}"
set -- $rest ; state="$1" ; ppid="$2"
# Skip PID1, our parent, ourself and our children
[ $pid -ne 1 -a $pid -ne $proc_ppid -a $pid -ne $$ -a $ppid -ne $$ ] || continue
[ -f "/proc/$pid/cmdline" ] || continue
local cmdline
read cmdline < /proc/$pid/cmdline