From eece79a5da929e63e28ab84cf011270976abe243 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Old=C5=99ich=20Jedli=C4=8Dka?= Date: Tue, 29 Oct 2024 20:32:21 +0100 Subject: [PATCH] initramfs: fix killing child PIDs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ubuntu's initramfs Busybox does not support `ps -o pid,ppid`, so fallback to unpredictable `ps -l`. In any case use `awk` to find the position of PID and PPID columns and dump appropriate child PIDs. This should be hopefully bullet-proof. Also clean-up leftover PID file. Signed-off-by: Oldřich Jedlička --- .../scripts/local-bottom/clevis.in | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/initramfs-tools/scripts/local-bottom/clevis.in b/src/initramfs-tools/scripts/local-bottom/clevis.in index fff3d758..4798f203 100755 --- a/src/initramfs-tools/scripts/local-bottom/clevis.in +++ b/src/initramfs-tools/scripts/local-bottom/clevis.in @@ -34,11 +34,22 @@ esac [ -s /run/clevis.pid ] || exit 0 pid=$(cat /run/clevis.pid) -child_pids=$(ps -o pid,ppid | awk -v pid="$pid" '$2==pid { print $1 }') +child_pids="$({ ps -o pid,ppid 2>/dev/null || ps -l || + { echo 'clevis: unable to get list of processes' >&2; exit 1; }; } | + awk -v pid="$pid" ' + NR==1 { + for (i=1; i<=NF; i++) if ($i == "PID") pid_col = i; else if ($i == "PPID") ppid_col = i + if (!pid_col || !ppid_col) { print "clevis: unable to find PID and/or PPID columns in ps output" | "cat >&2"; exit 1 } + next + } + { if ($ppid_col == pid) print $pid_col }')" + for kill_pid in $pid $child_pids; do - kill "$kill_pid" + kill "$kill_pid" 2>/dev/null done +rm -f /run/clevis.pid + # Not really worried about downing extra interfaces: they will come up # during the actual boot. Might make this configurable later if needed.