Skip to content

Commit

Permalink
[devtools/sigparse] Enhance it to make a report
Browse files Browse the repository at this point in the history
Hm OSH still differs from many shells
  • Loading branch information
Andy C committed Feb 3, 2022
1 parent 8796327 commit 7ff7b4e
Showing 1 changed file with 60 additions and 16 deletions.
76 changes: 60 additions & 16 deletions devtools/sigparse.sh
Expand Up @@ -6,21 +6,65 @@
# https://unix.stackexchange.com/questions/85364/how-can-i-check-what-signals-a-process-is-listening-to
#
# Usage:
# ./sigparse.sh PID

sigparse () {
i=0
# bits="$(printf "16i 2o %X p" "0x$1" | dc)" # variant for busybox
bits="$(printf "ibase=16; obase=2; %X\n" "0x$1" | bc)"
while [ -n "$bits" ] ; do
i="$(expr "$i" + 1)"
case "$bits" in
*1) printf " %s(%s)" "$(kill -l "$i")" "$i" ;;
esac
bits="${bits%?}"
done
# devtools/sigparse.sh <function name>

sigparse() {
local i=0

# bits="$(printf "16i 2o %X p" "0x$1" | dc)" # variant for busybox

# hex to binary. Could also do this with Python.
bits="$(printf "ibase=16; obase=2; %X\n" "0x$1" | bc)"
while test -n "$bits"; do
i=$((i + 1))
case "$bits" in
*1)
local sig_name=$(kill -l "$i")
printf ' %s(%s)' "$sig_name" "$i"
;;
esac
bits="${bits%?}"
done
}

report() {
grep '^Sig...:' "/proc/$1/status" | while read state hex_mask; do
printf "%s%s\n" "$state" "$(sigparse "$hex_mask")"
done
}

compare-shells() {
local -a shells=(bash dash mksh zsh osh)

# Hm non-interactive shells have consistency.
# SIGCHLD and SIGINT are caught in bash, dash, zsh, mksh. mksh catches
# several more.

for sh in ${shells[@]}; do
echo
echo "---- $sh ----"
echo

$sh -c 'script=$1; $script report $$' -- $0
done

echo
echo

for sh in ${shells[@]}; do
echo
echo "---- $sh -i ----"
echo

# Why does this cause 'stopped' ??? A stray signal. Similar to spec test
# problems.
if test $sh = 'osh'; then
echo 'SKIPPING INTERACTIVE OSH (TODO: fix)'
continue
fi

$sh -i -c 'script=$1; $script report $$' -- $0
done
}

grep "^Sig...:" "/proc/$1/status" | while read a b ; do
printf "%s%s\n" "$a" "$(sigparse "$b")"
done # | fmt -t # uncomment for pretty-printing
"$@"

0 comments on commit 7ff7b4e

Please sign in to comment.