slam_tomb: simplify and rename to _kill_processes

In general umount_tomb and slam_tomb shared a lot of similar code.
Main difference being, that the latter additionally searched for
processes and would still call umount_tomb if the processes could
be killed.
umount_tomb would then again search with the provided name for the
relevant tomb in list_tomb_mounts, which should be obsolete at this
point.
Therefore the decision to reduce slam_tomb in functionality. It would
only work on a supplied tombname and tombmount, look if there are
processes and is called from within umount_tomb.
(Theoretical tombname could be removed)
Calling tomb with slam or close sets a flag, which will decide if
that part in umount_tomb will be executed.
This commit is contained in:
Narrat 2024-08-08 14:22:07 +02:00 committed by Jaromil
parent 284fb4a3cd
commit 653609a4b9

120
tomb
View File

@ -64,6 +64,7 @@ typeset -i DECLOAKIFY=1
typeset -i RESIZER=1 typeset -i RESIZER=1
typeset -i RECOLL=1 typeset -i RECOLL=1
typeset -i QRENCODE=1 typeset -i QRENCODE=1
typeset -i LSOF=1
# Default mount options # Default mount options
typeset MOUNTOPTS="rw,noatime,nodev" typeset MOUNTOPTS="rw,noatime,nodev"
@ -2961,8 +2962,18 @@ umount_tomb() {
} }
} }
_message "Closing tomb ::1 tomb name:: mounted on ::2 mount point::" \ # if SLAM is set kill possible processes
[[ -v SLAM ]] && {
_message "Slamming tomb ::1 tombname:: mounted on ::2 tombmount::" \
${tombname} "${tombmount}"
_kill_processes "$tombname" "$tombmount"
[[ $? -ne 0 ]] && {
_failure "Still active processes for ::1 tombname ::, cannot close tomb." "$tombname"
}
} || {
_message "Closing tomb ::1 tomb name:: mounted on ::2 mount point::" \
$tombname "$tombmount" $tombname "$tombmount"
}
# check if there are bind mounted dirs and close them first # check if there are bind mounted dirs and close them first
# Can be due to bind-hooks or outside --bind mounts # Can be due to bind-hooks or outside --bind mounts
@ -3006,12 +3017,12 @@ list_processes() {
# runs lsof on the mounted_tombs # runs lsof on the mounted_tombs
local mounted_tombs i indicator lsofres local mounted_tombs i indicator lsofres
indicator=1 # != 0 means there were processes indicator=1 # != 0 means there were processes
mounted_tombs=(`list_tomb_mounts $1`) mounted_tombs=(`list_tomb_mounts ${1//[\[\]]/}`) # remove possible []
if [[ "${#mounted_tombs}" -gt 0 ]]; then if [[ "${#mounted_tombs}" -gt 0 ]]; then
if [[ -z $1 ]]; then if [[ -z $1 ]]; then
_success "Looking for processes running inside all open tombs..." _message "Looking for processes running inside all open tombs..."
else else
_success "Looking for processes running inside tomb '::1 tombname::'..." "$1" _message "Looking for processes running inside tomb ::1 tombname::..." "$1"
fi fi
for i in ${mounted_tombs}; do for i in ${mounted_tombs}; do
@ -3024,61 +3035,45 @@ list_processes() {
return $indicator return $indicator
} }
# Kill all processes using the tomb # Kill all processes using the specified tomb
slam_tomb() { _kill_processes() {
# $1 = (optional) name of tomb to slam, or "all" if more mounted # $1 = name of tomb to slam
# $2 = mount location of tomb
if [ "$1" = "all" ]; then [[ -z "$2" ]] && _failure "Missing arguments for slamming."
mounted_tombs=(`list_tomb_mounts`)
else
mounted_tombs=(`list_tomb_mounts $1`)
fi
[[ ${#mounted_tombs} == 0 ]] && {
_failure "There is no open tomb to be closed." }
[[ ${#mounted_tombs} -gt 1 && -z "$1" ]] && {
_warning "Too many tombs mounted, please specify one (see tomb list)"
_warning "or issue the command 'tomb close all' to close them all."
_failure "Operation aborted." }
local pnum result lsofres local pnum result lsofres
result=0 result=0
# iterate through all mounted tomb affected
for i in ${mounted_tombs}; do
tombname=${i[(ws:;:)5]}
tombmount="${i[(ws:;:)2]}"
_success "Slamming tomb ::1 tombname:: mounted on ::2 tombmount::" \
${tombname} "${tombmount}"
list_processes "${tombname:1:-1}"
[[ $? -eq 0 ]] && {
# iterate through 3 different signals, break on success
for s in TERM HUP KILL; do
lsofres=(`_sudo lsof -t +D "$tombmount"`)
[[ -n $lsofres ]] && {
# iterate through all processes before switching signals
for pnum in $lsofres; do
_message "::1 tombname:: sending ::2 sig:: to open process ::3 pnum::" \
${tombname} ${s} ${pnum}
_sudo kill -$s $pnum
done
} || { break }
# give some time to the process for a clean quit
sleep .5
done
}
# if there are still processes then signal failure _verbose "::1 tombname:: Determining if there are open processes that needs killing" "$1"
lsofres=(`_sudo lsof -t +D "$tombmount"`)
[[ -n $lsofres ]] && { list_processes "$1"
for pnum in $lsofres; do [[ $? -eq 0 ]] && {
_warning "Couldn't kill ::1 pnum::" $pnum _verbose "Found open processes in ::1 tombname::" "$1"
done # iterate through 3 different signals, break on success
result=1 for s in TERM HUP KILL; do
} lsofres=(`_sudo lsof -t +D "$2"`)
# if it failed killing a process, report it [[ -n $lsofres ]] && {
[[ $result = 0 ]] && umount_tomb "${tombname:1:-1}" # iterate through all processes before switching signals
done for pnum in $lsofres; do
_message "::1 tombname:: sending ::2 sig:: to open process ::3 pnum::" \
${1} ${s} ${pnum}
_sudo kill -$s $pnum
done
} || { break }
# give some time to the process for a clean quit
sleep .5
done
}
# if there are still processes then signal failure
lsofres=(`_sudo lsof -t +D "$tombmount"`)
[[ -n $lsofres ]] && {
_verbose "Verify that no open processes remain in ::1 tombname::" "$1"
for pnum in $lsofres; do
_warning "Couldn't kill ::1 pnum::" $pnum
done
result=1
}
return $result return $result
} }
@ -3293,18 +3288,15 @@ main() {
# Slam a tomb killing all processes running inside # Slam a tomb killing all processes running inside
slam) slam)
slam_tomb $PARAM[1] [[ $LSOF -eq 1 ]] && {
;; SLAM=1
} || {
_warning "lsof not installed: cannot slam tombs."
_warning "Trying a regular close."}
;&
# Close the tomb # Close the tomb
# `slam` is used to force closing.
umount|close) umount|close)
[[ "$subcommand" == "slam" ]] && {
SLAM=1
[[ $LSOF == 0 ]] && {
unset SLAM
_warning "lsof not installed: cannot slam tombs."
_warning "Trying a regular close." }}
umount_tomb $PARAM[1] umount_tomb $PARAM[1]
;; ;;