Skip to content

Commit

Permalink
Fix shtk_abort on NetBSD 7.0 as of today
Browse files Browse the repository at this point in the history
It turns out that "ps $$" on NetBSD properly filters the output to the
given PID, but "ps -o ppid $$" does not.  Make sure to always pass the
PID via the -p flag, which is what the manpage says one must do.

I'm pretty sure this used to work on NetBSD, so I suspect ps has been
broken by any of the recent changes to it.  Anyway, better use the
documented syntax than the lone PID argument.
  • Loading branch information
jmmv committed Feb 17, 2017
1 parent 3b40751 commit 32a2a2f
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions base.subr
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ _shtk_kill_subshells() {
# happens to be the subshell we are interested in).
local script="$(mktemp "${TMPDIR:-/tmp}/shtk.XXXXXX")"
cat >"${script}" <<EOF
ps -o ppid "\${$}" | awk '${awk_get_pid}'
ps -o ppid -p "\${$}" | awk '${awk_get_pid}'
EOF
/bin/sh "${script}" >"${script}.out"
read pid <"${script}.out"
Expand All @@ -70,8 +70,9 @@ EOF

local top_pid="${$}"
local pids=
while [ "${pid}" != "${top_pid}" -a -n "${pid}" ]; do
pid="$(ps -o ppid "${pid}" | awk "${awk_get_pid}")"
local rounds=100 # Prevent infinite loops.
while [ "${pid}" != "${top_pid}" -a -n "${pid}" -a ${rounds} -gt 0 ]; do
pid="$(ps -o ppid -p "${pid}" | awk "${awk_get_pid}")"
if [ "${pid}" = 1 ]; then
# Search failed for some reason (probably due to a bug here).
# Better ignore anything we have found and leave processes around
Expand All @@ -80,9 +81,16 @@ EOF
break
fi
pids="${pid} ${pids}"
rounds="$((${rounds} - 1))"
done

[ -z "${pids}" ] || kill ${pids}
if [ ${rounds} -eq 0 ]; then
# If we encountered an infinite loop or too many processes for some
# strange reason, do nothing. This is nicer to the user and our tests
# should catch this condition.
:
else
[ -z "${pids}" ] || kill ${pids}
fi
}


Expand Down

0 comments on commit 32a2a2f

Please sign in to comment.