Skip to content

Commit

Permalink
slam_tomb: simplify and rename to _kill_processes
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Narrat authored and jaromil committed Sep 1, 2024
1 parent 284fb4a commit 653609a
Showing 1 changed file with 55 additions and 63 deletions.
118 changes: 55 additions & 63 deletions tomb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ typeset -i DECLOAKIFY=1
typeset -i RESIZER=1
typeset -i RECOLL=1
typeset -i QRENCODE=1
typeset -i LSOF=1

# Default mount options
typeset MOUNTOPTS="rw,noatime,nodev"
Expand Down Expand Up @@ -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"
}

# check if there are bind mounted dirs and close them first
# Can be due to bind-hooks or outside --bind mounts
Expand Down Expand Up @@ -3006,12 +3017,12 @@ list_processes() {
# runs lsof on the mounted_tombs
local mounted_tombs i indicator lsofres
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 [[ -z $1 ]]; then
_success "Looking for processes running inside all open tombs..."
_message "Looking for processes running inside all open tombs..."
else
_success "Looking for processes running inside tomb '::1 tombname::'..." "$1"
_message "Looking for processes running inside tomb ::1 tombname::..." "$1"
fi

for i in ${mounted_tombs}; do
Expand All @@ -3024,61 +3035,45 @@ list_processes() {
return $indicator
}

# Kill all processes using the tomb
slam_tomb() {
# $1 = (optional) name of tomb to slam, or "all" if more mounted

if [ "$1" = "all" ]; then
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." }
# Kill all processes using the specified tomb
_kill_processes() {
# $1 = name of tomb to slam
# $2 = mount location of tomb
[[ -z "$2" ]] && _failure "Missing arguments for slamming."

local pnum result lsofres
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

_verbose "::1 tombname:: Determining if there are open processes that needs killing" "$1"

list_processes "$1"
[[ $? -eq 0 ]] && {
_verbose "Found open processes in ::1 tombname::" "$1"
# iterate through 3 different signals, break on success
for s in TERM HUP KILL; do
lsofres=(`_sudo lsof -t +D "$2"`)
[[ -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::" \
${1} ${s} ${pnum}
_sudo kill -$s $pnum
done
} || { break }
# give some time to the process for a clean quit
sleep .5
done
}
# 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 ]] && {
for pnum in $lsofres; do
_warning "Couldn't kill ::1 pnum::" $pnum
done
result=1
}
# if it failed killing a process, report it
[[ $result = 0 ]] && umount_tomb "${tombname:1:-1}"
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
}

Expand Down Expand Up @@ -3293,18 +3288,15 @@ main() {

# Slam a tomb killing all processes running inside
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
# `slam` is used to force closing.
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]
;;

Expand Down

0 comments on commit 653609a

Please sign in to comment.