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

76 lines, 44 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 case "$do_trap" in
45 # Demo for blog post: trap ' ' is different than trap '' !
46 SPACE)
47 trap ' ' USR2
48 ;;
49 # trap '# comment' is also different than trap ''
50 COMMENT)
51 trap '# comment' USR2
52 ;;
53 *)
54 # trap '' sets SIG_IGN
55 trap '' USR2
56 #echo ' Ignoring USR2'
57 ;;
58 esac
59
60 # Print trap state
61 trap
62 echo
63 fi
64
65
66 if false; then
67 # raw
68 grep '^SigIgn:' "/proc/$pid/status"
69 else
70 grep '^Sig...:' "/proc/$pid/status" | while read state hex_mask; do
71 printf "%s%s\n" "$state" "$(sigparse "$hex_mask")"
72 done
73 fi
74}
75
76"$@"