OILS / test / signal-state.sh View on Github | oilshell.org

102 lines, 58 significant
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
11sigparse() {
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
33report() {
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
39do-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
48compare-shells() {
49 local do_child=${1:-}
50
51 local osh_cpp=_bin/cxx-dbg/osh
52 ninja $osh_cpp
53
54 local -a shells=(bash dash mksh zsh bin/osh $osh_cpp)
55
56 # Hm non-interactive shells have consistency.
57 # SIGCHLD and SIGINT are caught in bash, dash, zsh, mksh. mksh catches
58 # several more.
59
60 for sh in ${shells[@]}; do
61 echo
62 echo "---- $sh ----"
63 echo
64
65 $sh -c 'script=$1; $script report $$' -- $0
66
67 if test -n "$do_child"; then
68 do-child $sh
69 fi
70 done
71
72 echo
73 echo
74
75 # -i messes things up
76 return
77
78 for sh in ${shells[@]}; do
79 echo
80 echo "---- $sh -i ----"
81 echo
82
83 # NOTE: If we don't set --rcfile, somehow this parent shell gets
84 # [2]+ Stopped devtools/sigparse.sh compare-shells
85 # Seems related to spec test flakiness.
86
87 local more_flags=''
88 case $sh in
89 (bash|bin/osh)
90 more_flags='--rcfile /dev/null'
91 ;;
92 esac
93
94 $sh $more_flags -i -c 'script=$1; $script report $$' -- $0
95
96 if test -n "$do_child"; then
97 do-child $sh
98 fi
99 done
100}
101
102"$@"