OILS / test / signal-report.sh View on Github | oils.pub

62 lines, 34 significant
1#!/bin/sh
2#
3# Show signal state in a shell
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# ./signal-report.sh <function name>
10
11set -o nounset
12set -o errexit
13#set -o pipefail
14
15sigparse() {
16 # Parse signal format in /proc.
17 local hex_mask=$1
18
19 local i=0
20
21 # bits="$(printf "16i 2o %X p" "0x$1" | dc)" # variant for busybox
22
23 # hex to binary. Could also do this with Python.
24 bits="$(printf 'ibase=16; obase=2; %X\n' "0x$hex_mask" | bc)"
25 while test -n "$bits"; do
26 i=$((i + 1))
27 case "$bits" in
28 *1)
29 local sig_name=$(kill -l "$i")
30 printf ' %s(%s)' "$sig_name" "$i"
31 ;;
32 esac
33 bits="${bits%?}"
34 done
35}
36
37report() {
38 local do_trap=${1:-}
39
40 local pid=$$
41 echo "PID $pid"
42
43 if test -n "$do_trap"; then
44 # trap '' sets SIG_IGN
45 trap '' USR2
46 #echo ' Ignoring USR2'
47 trap
48 echo
49 fi
50
51
52 if false; then
53 # raw
54 grep '^SigIgn:' "/proc/$pid/status"
55 else
56 grep '^Sig...:' "/proc/$pid/status" | while read state hex_mask; do
57 printf "%s%s\n" "$state" "$(sigparse "$hex_mask")"
58 done
59 fi
60}
61
62"$@"