From 096fe95b2fcefde58a52876ba127ce74791feb74 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Sat, 18 Apr 2020 19:11:08 +0200 Subject: [PATCH] bkctld-rm: fix lock file management logic was badly implemented --- lib/bkctld-rm | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/bkctld-rm b/lib/bkctld-rm index 320e258..c8cbfb5 100755 --- a/lib/bkctld-rm +++ b/lib/bkctld-rm @@ -36,17 +36,21 @@ delete_inc_ext4() { if [ -f "${lock_file}" ]; then # Get Process ID from the lock file pid=$(cat "${lock_file}") - if kill -0 ${pid} 2> /dev/null; then - # Kill the children - pkill -9 --parent "${pid}" - # Kill the parent - kill -9 "${pid}" - # Remove the lock file - rm -f ${lock_file} - warning "Process ${pid} has been killed. Only one ${0} can run in parallel, the latest wins." + if [ -n "${pid}" ]; then + if kill -0 ${pid} 2> /dev/null; then + # Kill the children + pkill -9 --parent "${pid}" + # Kill the parent + kill -9 "${pid}" + warning "Process ${pid} has been killed. Only one ${0} can run in parallel, the latest wins." + else + warning "Process not found at PID \`${pid}'. Ignoring lock file \`${lock_file}'." + fi else - error "Empty lockfile '${lock_file}'. It should contain a PID." + error "Empty lockfile \`${lock_file}'. It should contain a PID." fi + # Remove the lock file + rm -f ${lock_file} fi mkdir --parents "${LOCKDIR}" && echo $$ > ${lock_file} || error "Failed to acquire lock file '${lock_file}'" @@ -60,6 +64,8 @@ delete_inc_ext4() { rsync --archive --delete "${empty}/" "${inc_path}/" rmdir "${inc_path}/" fi + # Remove the lock file + rm -f ${lock_file} end=$(current_time) notice "${jail_name}: inc '${inc_name}' has been deleted [${start}/${end}]"