kiss: Fix signal handling and implement SIGINT and SIGEXIT hooks.

Ctrl+C is now correctly blocked during installation and removal
and ordering of pkg_clean is maintained.

Two new hooks have been added, SIGINT and SIGEXIT. These run in
the corresponding signal handlers.

Fixes #280
This commit is contained in:
Dylan Araps 2021-10-06 09:10:56 +03:00
parent 764acdcde7
commit 228075aab1
No known key found for this signature in database
GPG Key ID: 13295DAC2CF13B5C
1 changed files with 34 additions and 21 deletions

55
kiss
View File

@ -1394,15 +1394,12 @@ pkg_remove() {
# differently and configuration files are *not* overwritten. # differently and configuration files are *not* overwritten.
[ -d "$sys_db/$1" ] || die "'$1' not installed" [ -d "$sys_db/$1" ] || die "'$1' not installed"
trap_off
# Intended behavior. # Intended behavior.
# shellcheck disable=2030,2031 # shellcheck disable=2030,2031
equ "$KISS_FORCE" 1 || pkg_removable "$1" equ "$KISS_FORCE" 1 || pkg_removable "$1"
# Block being able to abort the script with 'Ctrl+C' during removal.
# Removes all risk of the user aborting a package removal leaving an
# incomplete package installed.
trap '' INT
# arg1: pre-remove # arg1: pre-remove
# arg2: package name # arg2: package name
# arg3: path to installed database # arg3: path to installed database
@ -1415,10 +1412,7 @@ pkg_remove() {
log "$1" "Removing package" log "$1" "Removing package"
pkg_remove_files < "$sys_db/$1/manifest" 3< "$_tmp_file" pkg_remove_files < "$sys_db/$1/manifest" 3< "$_tmp_file"
# Reset 'trap' to its original value. Removal is done so trap_on
# we no longer need to block 'Ctrl+C'.
trap pkg_clean EXIT INT
log "$1" "Removed successfully" log "$1" "Removed successfully"
} }
@ -1491,6 +1485,7 @@ pkg_install() {
;; ;;
esac esac
trap_off
mkcd "$tar_dir/$_pkg" mkcd "$tar_dir/$_pkg"
# The tarball is extracted to a temporary directory where its contents are # The tarball is extracted to a temporary directory where its contents are
@ -1538,11 +1533,6 @@ pkg_install() {
tmp_file "$_pkg" manifest-reverse tmp_file "$_pkg" manifest-reverse
sort "$tar_man" > "$_tmp_file" sort "$tar_man" > "$_tmp_file"
# Block being able to abort the script with Ctrl+C during installation.
# Removes all risk of the user aborting a package installation leaving
# an incomplete package installed.
trap '' INT
if if
# Install the package's files by iterating over its manifest. # Install the package's files by iterating over its manifest.
pkg_install_files -z "$PWD" < "$_tmp_file" 3< "$_tmp_file_pre_pre" && pkg_install_files -z "$PWD" < "$_tmp_file" 3< "$_tmp_file_pre_pre" &&
@ -1557,9 +1547,7 @@ pkg_install() {
pkg_install_files -e "$PWD" < "$_tmp_file" 3< "$_tmp_file_pre_pre" pkg_install_files -e "$PWD" < "$_tmp_file" 3< "$_tmp_file_pre_pre"
then then
# Reset 'trap' to its original value. Installation is done so we no longer trap_on
# need to block 'Ctrl+C'.
trap pkg_clean EXIT INT
# arg1: post-install # arg1: post-install
# arg2: package name # arg2: package name
@ -1755,6 +1743,30 @@ pkg_help_ext() {
done >&2 done >&2
} }
trap_on() {
# Catch errors and ensure that build files and directories are cleaned
# up before we die. This occurs on 'Ctrl+C' as well as success and error.
trap trap_INT INT
trap trap_EXIT EXIT
}
trap_INT() {
run_hook SIGINT
exit 1
}
trap_EXIT() {
pkg_clean
run_hook SIGEXIT
}
trap_off() {
# Block being able to abort the script with 'Ctrl+C'. Removes all risk of
# the user aborting a package install/removal leaving an incomplete package
# installed.
trap "" INT EXIT
}
args() { args() {
# Parse script arguments manually. This is rather easy to do in our case # Parse script arguments manually. This is rather easy to do in our case
# since the first argument is always an "action" and the arguments that # since the first argument is always an "action" and the arguments that
@ -1818,6 +1830,8 @@ args() {
# shellcheck disable=2030,2031 # shellcheck disable=2030,2031
case $action in a|alternatives|i|install|r|remove) case $action in a|alternatives|i|install|r|remove)
if ok "$1" && ! am_owner "$KISS_ROOT/"; then if ok "$1" && ! am_owner "$KISS_ROOT/"; then
trap_off
as_user env \ as_user env \
LOGNAME="$user" \ LOGNAME="$user" \
HOME="$HOME" \ HOME="$HOME" \
@ -1832,6 +1846,8 @@ args() {
KISS_PID="$KISS_PID" \ KISS_PID="$KISS_PID" \
_KISS_LVL="$_KISS_LVL" \ _KISS_LVL="$_KISS_LVL" \
"$0" "$action" "$@" "$0" "$action" "$@"
trap_on
return return
fi fi
esac esac
@ -1979,10 +1995,7 @@ main() {
time=$(date +%Y-%m-%d-%H:%M) time=$(date +%Y-%m-%d-%H:%M)
create_tmp_dirs create_tmp_dirs
trap_on
# Catch errors and ensure that build files and directories are cleaned
# up before we die. This occurs on 'Ctrl+C' as well as success and error.
trap pkg_clean EXIT INT
args "$@" args "$@"
} }