new slam and list_processes

This commit is contained in:
Jaromil 2017-12-11 13:06:01 +01:00
parent 85ac179cc8
commit 8e33be0472

108
tomb
View File

@ -2698,30 +2698,89 @@ umount_tomb() {
return 0 return 0
} }
list_processes() {
# $1 = (optional) name of tomb
# returns a list of process UIDs, one per line
local mounted_tombs i
local pnum puid pcmd powner
mounted_tombs=(`list_tomb_mounts $1`)
if [[ "${#mounted_tombs}" -gt 0 ]]; then
if [[ "$1" = "" ]]; then
_success "Listing processes running inside all open tombs..."
else
_success "Listing processes running inside tomb '::1 tombname::'..." "$1"
fi
for i in ${mounted_tombs}; do
_verbose "scanning tomb: ::1 tombmount::" $i
tombmount=${i[(ws:;:)2]}
tombname=${i[(ws:;:)5]}
for pnum in ${(f)"$(_sudo lsof -t +D $tombmount)"}; do
_verbose "process found: $pnum"
puid=`cat /proc/${pnum}/loginuid`
pcmd=`cat /proc/${pnum}/cmdline`
if [[ "$puid" = "$UID" ]]; then
powner="owned"
else powner=$puid; fi
_message "::1 tombname:: ::2 cmd:: (::3 owner::) on ::4 tombmount::" \
$tombname $pcmd $powner $tombmount
done
done
fi
}
# Kill all processes using the tomb # Kill all processes using the tomb
slam_tomb() { slam_tomb() {
# $1 = tomb mount point # $1 = (optional) name of tomb to slam, or "all" if more mounted
if [[ -z `lsof -t +D "$1" 2>/dev/null` ]]; then
return 0 if [ "$1" = "all" ]; then
mounted_tombs=(`list_tomb_mounts`)
else
mounted_tombs=(`list_tomb_mounts $1`)
fi fi
#Note: shells are NOT killed by INT or TERM, but they are killed by HUP
[[ ${#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 puid pcmd powner result
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}
# iterate through all processes running in mounted tombs
for pnum in ${(f)"$(_sudo lsof -t +D $tombmount)"}; do
puid=`cat /proc/${pnum}/loginuid`
pcmd=`cat /proc/${pnum}/cmdline`
_verbose "process found: $pnum $pcmd ($puid)"
if [[ "$puid" = "$UID" ]]; then
powner="owned"
else powner=$puid; fi
# iterate through 3 different signals to send, break on success
for s in TERM HUP KILL; do for s in TERM HUP KILL; do
_verbose "Sending ::1:: to processes inside the tomb:" $s _message "::1 tombname:: sending ::2 sig:: to ::3 cmd:: (::4 uid::)" \
if option_is_set -D; then ${tombname} ${s} ${pcmd} ${powner}
ps -fp `lsof -t +D "$1" 2>/dev/null`| kill -$s $pnum
while read line; do # stop sending other signals if kill was succesfull
_verbose $line [[ -r /proc/$pnum ]] || break
done done
fi # if process still running then signal failure
kill -$s `lsof -t +D "$1"` [[ -r /proc/$pnum ]] && {
if [[ -z `lsof -t +D "$1" 2>/dev/null` ]]; then _warning "Can't kill ::1 puid:: ::2 pcmd:: (::3 powner::)" \
return 0 $puid $pcmd $powner
fi result=1 }
if ! option_is_set -f; then
sleep 3
fi
done done
return 1 # if it failed killing a process, report it
[[ $result = 0 ]] && umount_tomb $tombname
done
return $result
} }
# }}} - Tomb close # }}} - Tomb close
@ -2774,6 +2833,7 @@ main() {
subcommands_opts[close]="" subcommands_opts[close]=""
subcommands_opts[help]="" subcommands_opts[help]=""
subcommands_opts[slam]="" subcommands_opts[slam]=""
subcommands_opts[ps]=""
subcommands_opts[list]="-get-mountpoint " subcommands_opts[list]="-get-mountpoint "
subcommands_opts[index]="" subcommands_opts[index]=""
@ -2922,9 +2982,19 @@ main() {
mount_tomb $PARAM mount_tomb $PARAM
;; ;;
# List all processes using a tomb
ps)
list_processes $PARAM
;;
# Slam a tomb killing all processes running inside
slam)
slam_tomb $PARAM
;;
# Close the tomb # Close the tomb
# `slam` is used to force closing. # `slam` is used to force closing.
umount|close|slam) umount|close)
[[ "$subcommand" == "slam" ]] && { [[ "$subcommand" == "slam" ]] && {
SLAM=1 SLAM=1
[[ $LSOF == 0 ]] && { [[ $LSOF == 0 ]] && {