forked from legionus/libshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-signal
77 lines (70 loc) · 1.83 KB
/
shell-signal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh -efu
### This file is covered by the GNU General Public License,
### which should be included with libshell as the file LICENSE.
### All copyright information are listed in the COPYING.
if [ -z "${__included_shell_signal-}" ]; then
__included_shell_signal=1
. shell-error
. shell-quote
__shell_signal_handlers=
### Set handler code whan any of the specified signals are received.
### Return code of handler function will be ignored. Special handlers is
### SIG_IGN and SIG_DFL (See signal(2)).
###
### Usage example:
### signal_handler 'echo $arg' TERM EXIT HUP
### signal_handler SIG_IGN TERM EXIT HUP
### signal_handler SIG_DFL TERM EXIT HUP
signal_handler()
{
local action real_action sign
action="$1"; shift
for sign; do
sign="${sign#SIG}"
case "$action" in
SIG_IGN)
eval "unset __signal_handlers_$sign"
real_action=:
trap : "$sign"
;;
SIG_DFL)
eval "unset __signal_handlers_$sign"
real_action=-
trap - "$sign"
;;
*)
eval "handler=\"\${__signal_handlers_$sign-} \$action;\""
trap "$handler" "$sign"
eval "__signal_handlers_$sign=\"\$handler\""
;;
esac
done
}
### Set exit handler. Return code of handler function will be ignored.
###
### Usage example:
### exit_function() { echo "Exit with return code '$1'"; }
### set_cleanup_handler exit_function
__cleanup_handler_name=
set_cleanup_handler()
{
__cleanup_handler_name="${1-}"
__cleanup_handler()
{
trap - EXIT
[ -z "${__cleanup_handler_name-}" ] ||
"$__cleanup_handler_name" "$1" ||:
exit "$1"
}
signal_handler '__cleanup_handler $?' EXIT
signal_handler '__cleanup_handler 1' HUP PIPE INT QUIT TERM
}
### Remove exit handler.
###
### Usage example: unset_cleanup_handler
unset_cleanup_handler()
{
signal_handler SIG_DFL EXIT HUP PIPE INT QUIT TERM
__cleanup_handler_name=
}
fi #__included_shell_signal