summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2014-10-07 14:21:42 (EDT)
committer P. J. McDermott <pj@pehjota.net>2014-10-07 14:21:42 (EDT)
commit316cb246f3998da0283cd6f55febada21086dc3a (patch)
tree1a891a9fbf9a964426966003f69030b8d2f30f03
parente808e8e467554708bc54058d244a014e4a5d4b71 (diff)
session: Handle signals
-rw-r--r--lib/session.sh45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/session.sh b/lib/session.sh
index a2d18e3..fec6aec 100644
--- a/lib/session.sh
+++ b/lib/session.sh
@@ -33,6 +33,7 @@ session_arch=
session_plat=
session_mountdir=
session_atexit=
+session_sigs=
session_begin()
{
@@ -54,6 +55,8 @@ session_begin()
error 2 "$(get_msg 'install_running')"
fi
+ session_set_sigs
+
if ! mutex_lock_timeout "${session_root}/prokit/sessions.lock" 5; then
error 2 "$(get_msg 'cant_lock_sessions')"
fi
@@ -87,6 +90,8 @@ session_begin()
session_end()
{
+ session_unset_sigs
+
# Unregister the session.
profile_bind_umount "${session_arch}" "${session_plat}" \
"${session_mountdir}" \
@@ -173,3 +178,43 @@ session_umount()
sed -n '1!G;h;$p')
EOF
}
+
+session_set_sigs()
+{
+ local i=
+ local sig=
+
+ # We need the signal *number* in the signal handler. The only portable
+ # and easy way to get the number of a named signal is to search for it
+ # as in the following loop hack.
+ i=0
+ session_sigs=''
+ while [ ${i} -lt 127 ]; do
+ i=$(($i + 1))
+ sig="$(kill -l ${i} 2>/dev/null)" || continue
+ case "${sig}" in
+ 'HUP' | 'INT' | 'QUIT' | 'ABRT' | 'ALRM' | 'TERM')
+ session_sigs="${session_sigs} ${i}"
+ trap "session_handle_sig ${i}" ${i}
+ ;;
+ esac
+ done
+}
+
+session_unset_sigs()
+{
+ trap - ${session_sigs}
+}
+
+session_handle_sig()
+{
+ local sig="${1}"
+
+ session_end
+
+ if [ "x${exit_status:+set}" = 'xset' ]; then
+ exit ${exit_status}
+ else
+ exit $((128 + $sig))
+ fi
+}