| 1 | #!/usr/bin/env bash
 | 
| 2 | #
 | 
| 3 | # Test kernel state: which signals caught, ignored, etc.
 | 
| 4 | #
 | 
| 5 | # Copied from:
 | 
| 6 | # https://unix.stackexchange.com/questions/85364/how-can-i-check-what-signals-a-process-is-listening-to
 | 
| 7 | #
 | 
| 8 | # Usage:
 | 
| 9 | #   test/signal-state.sh <function name>
 | 
| 10 | 
 | 
| 11 | sigparse() {
 | 
| 12 |   # Parse signal format in /proc.
 | 
| 13 |   local hex_mask=$1
 | 
| 14 | 
 | 
| 15 |   local i=0
 | 
| 16 | 
 | 
| 17 |   # bits="$(printf "16i 2o %X p" "0x$1" | dc)" # variant for busybox
 | 
| 18 | 
 | 
| 19 |   # hex to binary.  Could also do this with Python.
 | 
| 20 |   bits="$(printf 'ibase=16; obase=2; %X\n' "0x$hex_mask" | bc)"
 | 
| 21 |   while test -n "$bits"; do
 | 
| 22 |     i=$((i + 1))
 | 
| 23 |     case "$bits" in
 | 
| 24 |       *1)
 | 
| 25 |         local sig_name=$(kill -l "$i")
 | 
| 26 |         printf ' %s(%s)' "$sig_name" "$i"
 | 
| 27 |         ;;
 | 
| 28 |     esac
 | 
| 29 |     bits="${bits%?}"
 | 
| 30 |   done
 | 
| 31 | }
 | 
| 32 | 
 | 
| 33 | report() {
 | 
| 34 |   grep '^Sig...:' "/proc/$1/status" | while read state hex_mask; do
 | 
| 35 |     printf "%s%s\n" "$state" "$(sigparse "$hex_mask")"
 | 
| 36 |   done
 | 
| 37 | }
 | 
| 38 | 
 | 
| 39 | do-child() {
 | 
| 40 |   echo
 | 
| 41 |   echo 'BACKGROUND CHILD'
 | 
| 42 |   $sh -c 'script=$1; sleep 0.5 & { sleep 0.2; $script report $!; }' -- $0
 | 
| 43 | 
 | 
| 44 |   # TODO: I think we need a foreground child too.  It can just be a C program that
 | 
| 45 |   # prints its own PID, and then waits for a byte on stdin before it exits?
 | 
| 46 | }
 | 
| 47 | 
 | 
| 48 | compare-shells() {
 | 
| 49 |   local do_child=${1:-}
 | 
| 50 | 
 | 
| 51 |   local -a shells=(bash dash mksh zsh bin/osh)
 | 
| 52 | 
 | 
| 53 |   # Hm non-interactive shells have consistency.
 | 
| 54 |   # SIGCHLD and SIGINT are caught in bash, dash, zsh, mksh.  mksh catches
 | 
| 55 |   # several more.
 | 
| 56 | 
 | 
| 57 |   for sh in ${shells[@]}; do
 | 
| 58 |     echo
 | 
| 59 |     echo "---- $sh ----"
 | 
| 60 |     echo
 | 
| 61 | 
 | 
| 62 |     $sh -c 'script=$1; $script report $$' -- $0
 | 
| 63 | 
 | 
| 64 |     if test -n "$do_child"; then
 | 
| 65 |       do-child $sh
 | 
| 66 |     fi
 | 
| 67 |   done
 | 
| 68 | 
 | 
| 69 |   echo
 | 
| 70 |   echo
 | 
| 71 | 
 | 
| 72 |   for sh in ${shells[@]}; do
 | 
| 73 |     echo
 | 
| 74 |     echo "---- $sh -i ----"
 | 
| 75 |     echo
 | 
| 76 | 
 | 
| 77 |     # NOTE: If we don't set --rcfile, somehow this parent shell gets
 | 
| 78 |     # [2]+ Stopped   devtools/sigparse.sh compare-shells
 | 
| 79 |     # Seems related to spec test flakiness.
 | 
| 80 | 
 | 
| 81 |     local more_flags=''
 | 
| 82 |     case $sh in
 | 
| 83 |       (bash|bin/osh)
 | 
| 84 |         more_flags='--rcfile /dev/null'
 | 
| 85 |         ;;
 | 
| 86 |     esac
 | 
| 87 | 
 | 
| 88 |     $sh $more_flags -i -c 'script=$1; $script report $$' -- $0
 | 
| 89 | 
 | 
| 90 |     if test -n "$do_child"; then
 | 
| 91 |       do-child $sh
 | 
| 92 |     fi
 | 
| 93 |   done
 | 
| 94 | }
 | 
| 95 | 
 | 
| 96 | "$@"
 |