fixed issue #20: created slam_tomb() that take cares about killing and checking pids

This commit is contained in:
Anathema 2011-08-16 02:43:15 +02:00
parent 21be9e204e
commit afd785be62

View File

@ -757,6 +757,18 @@ exec_safe_post_hooks() {
fi fi
} }
slam_tomb() {
# $1 = tomb mount point
local pidk p
pidk=`lsof -t "$1"`
if [[ ! -z "$pidk" ]]; then
for p in "$pidk"; do
func "killing PID $p..."
kill -9 $p
done
fi
}
umount_tomb() { umount_tomb() {
local tombs how_many_tombs local tombs how_many_tombs
local pathmap mapper tombname tombmount loopdev local pathmap mapper tombname tombmount loopdev
@ -798,26 +810,20 @@ umount_tomb() {
pathmap=`dirname "$1"` pathmap=`dirname "$1"`
if [ "${pathmap}" = "/dev/mapper" ]; then if [ "${pathmap}" = "/dev/mapper" ]; then
mapper="$1" # argument is the mapper (or none which autofills mapper)
mapper="$1" # argument is the mapper (or none which autofills mapper) tombname="`print $mapper | cut -d. -f2`"
tombname="`print $mapper | cut -d. -f2`" tombmount=`mount -l | \
tombmount=`mount -l | \
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '` awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '`
elif [ "$pathmap" = "." ]; then elif [ "$pathmap" = "." ]; then
tombname="$1" # argument is the name
tombname="$1" # argument is the name mapper=`mount -l | \
mapper=`mount -l | \
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 } '` awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $1 } '`
tombmount=`mount -l | \ tombmount=`mount -l | \
awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '` awk -vtomb="[$tombname]" '/^\/dev\/mapper\/tomb/ { if($7==tomb) print $3 } '`
else else
tombmount="$1" # argument should be the mount
tombmount="$1" # argument should be the mount
mapper=`mount | awk -vmnt="$tombmount" '/^\/dev\/mapper\/tomb/ { if($3==mnt) print $1 }'` mapper=`mount | awk -vmnt="$tombmount" '/^\/dev\/mapper\/tomb/ { if($3==mnt) print $1 }'`
tombname="`print $mapper | cut -d. -f2`" tombname="`print $mapper | cut -d. -f2`"
fi fi
# avoid block when the same tomb is mounted, take only the first # avoid block when the same tomb is mounted, take only the first
@ -835,45 +841,35 @@ umount_tomb() {
fi fi
if [ $SLAM ]; then if [ $SLAM ]; then
notice "Slamming tomb $tombname mounted on $tombmount" notice "Slamming tomb $tombname mounted on $tombmount"
act "Kill all processes busy inside the tomb" act "Kill all processes busy inside the tomb"
pidk=`lsof -t "$tombmount"` slam_tomb "$tombmount"
for p in "$pidk"; do
pname=`pidof $p`
func "killing PID $p of $pname..."
kill -9 $p
done
else else
notice "Closing tomb $tombname mounted on $tombmount" notice "Closing tomb $tombname mounted on $tombmount"
fi fi
# check if there are binded dirs and close them # check if there are binded dirs and close them
tombmount_esc=`sed 's:\/:\\\/:g' <<< $tombmount ` tombmount_esc=`sed 's:\/:\\\/:g' <<< $tombmount `
unbind=`mount | awk "/^$tombmount_esc.*bind/"' { print $3 }'` unbind=`mount | awk "/^$tombmount_esc.*bind/"' { print $3 }'`
for b in ${(f)unbind}; do for b in ${(f)unbind}; do
hook="`basename $b`" hook="`basename $b`"
act "closing tomb hook: $hook" act "closing tomb hook: $hook"
umount $b umount $b
if ! [ $? = 0 ]; then if ! [ $? = 0 ]; then
if [ $SLAM ]; then if [ $SLAM ]; then
notice "Slamming tomb: killing all processes using this hook" notice "Slamming tomb: killing all processes using this hook"
pidk=`lsof -t $b` slam_tomb "$b"
for p in "$pidk"; do umount $b
pname=`pidof $p` else
notice "Killing PID $p of $pname..." error "Tomb hook is busy, cannot close tomb."
kill -9 $p return 1
done fi
umount $b
else
error "Tomb hook is busy, cannot close tomb."
return 1
fi fi
fi
done done
# Execute post-hooks for eventual cleanup # Execute post-hooks for eventual cleanup
if ! [ $NOBIND ]; then if ! [ $NOBIND ]; then
exec_safe_post_hooks ${tombmount%%/} close exec_safe_post_hooks ${tombmount%%/} close
fi fi
if [ $tombmount ]; then # tomb is actively mounted if [ $tombmount ]; then # tomb is actively mounted