Be sure that mutex is released when exiting

Signed-off-by: Tom Eastep <teastep@shorewall.net>
This commit is contained in:
Tom Eastep 2018-02-28 13:17:31 -08:00
parent 34c5441768
commit 9e002a7689
No known key found for this signature in database
GPG Key ID: 96E6B3F2423A4D10
3 changed files with 35 additions and 24 deletions

View File

@ -4435,6 +4435,7 @@ shorewall_cli() {
g_nopager= g_nopager=
g_blacklistipset= g_blacklistipset=
g_disconnect= g_disconnect=
g_havemutex=
VERBOSE= VERBOSE=
VERBOSITY=1 VERBOSITY=1

View File

@ -754,7 +754,7 @@ mutex_on()
MUTEX_TIMEOUT=${MUTEX_TIMEOUT:-60} MUTEX_TIMEOUT=${MUTEX_TIMEOUT:-60}
if [ $MUTEX_TIMEOUT -gt 0 ]; then if [ -z "$g_havemutex" -a $MUTEX_TIMEOUT -gt 0 ]; then
lockd=$(dirname $LOCKFILE) lockd=$(dirname $LOCKFILE)
@ -762,7 +762,7 @@ mutex_on()
if [ -f $lockf ]; then if [ -f $lockf ]; then
lockpid=`cat ${lockf} 2> /dev/null` lockpid=`cat ${lockf} 2> /dev/null`
if [ -z "$lockpid" -o $lockpid = 0 ]; then if [ -z "$lockpid" ] || [ $lockpid = 0 ]; then
rm -f ${lockf} rm -f ${lockf}
error_message "WARNING: Stale lockfile ${lockf} removed" error_message "WARNING: Stale lockfile ${lockf} removed"
elif [ $lockpid -eq $$ ]; then elif [ $lockpid -eq $$ ]; then
@ -775,12 +775,14 @@ mutex_on()
if qt mywhich lockfile; then if qt mywhich lockfile; then
lockfile -${MUTEX_TIMEOUT} -r1 ${lockf} lockfile -${MUTEX_TIMEOUT} -r1 ${lockf}
g_havemutex="rm -f ${lockf}"
chmod u+w ${lockf} chmod u+w ${lockf}
echo $$ > ${lockf} echo $$ > ${lockf}
chmod u-w ${lockf} chmod u-w ${lockf}
elif qt mywhich lock; then elif qt mywhich lock; then
lock ${lockf} lock ${lockf}
chmod u=r ${lockf} g_havemutex="lock -u ${lockf} && rm -f ${lockf}"
chmod u=r ${lockf}
else else
while [ -f ${lockf} -a ${try} -lt ${MUTEX_TIMEOUT} ] ; do while [ -f ${lockf} -a ${try} -lt ${MUTEX_TIMEOUT} ] ; do
sleep 1 sleep 1
@ -790,10 +792,15 @@ mutex_on()
if [ ${try} -lt ${MUTEX_TIMEOUT} ] ; then if [ ${try} -lt ${MUTEX_TIMEOUT} ] ; then
# Create the lockfile # Create the lockfile
echo $$ > ${lockf} echo $$ > ${lockf}
g_havemutex="rm -f ${lockf}"
else else
echo "Giving up on lock file ${lockf}" >&2 echo "Giving up on lock file ${lockf}" >&2
fi fi
fi fi
if [ -n "$g_havemutex" ]; then
trap mutex_off EXIT
fi
fi fi
} }
@ -802,7 +809,10 @@ mutex_on()
# #
mutex_off() mutex_off()
{ {
[ -f ${CONFDIR}/rc.common ] && lock -u ${LOCKFILE:=${VARDIR}/lock} if [ -n "$g_havemutex" ]; then
rm -f ${LOCKFILE:=${VARDIR}/lock} eval $g_havemutex
g_havemutex=
trap '' exit
fi
} }

View File

@ -642,17 +642,17 @@ start_command() {
fi fi
if [ -n "$AUTOMAKE" ]; then if [ -n "$AUTOMAKE" ]; then
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
run_it $g_firewall $g_debugging start run_it $g_firewall $g_debugging start
rc=$? rc=$?
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
else else
g_file="${VARDIR}/.start" g_file="${VARDIR}/.start"
if compiler $g_debugging $nolock compile "$g_file"; then if compiler $g_debugging $g_nolock compile "$g_file"; then
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
run_it ${VARDIR}/.start $g_debugging start run_it ${VARDIR}/.start $g_debugging start
rc=$? rc=$?
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
else else
rc=$? rc=$?
mylogger kern.err "ERROR:$g_product start failed" mylogger kern.err "ERROR:$g_product start failed"
@ -834,7 +834,7 @@ check_command() {
g_doing="Checking" g_doing="Checking"
compiler $g_debugging $nolock check compiler $g_debugging $g_nolock check
} }
# #
@ -925,7 +925,7 @@ update_command() {
g_doing="Updating" g_doing="Updating"
compiler $g_debugging $nolock check compiler $g_debugging $g_nolock check
} }
# #
@ -1027,21 +1027,21 @@ restart_command() {
g_file="${VARDIR}/.${COMMAND}" g_file="${VARDIR}/.${COMMAND}"
if [ -z "$g_fast" ]; then if [ -z "$g_fast" ]; then
if compiler $g_debugging $nolock compile "$g_file"; then if compiler $g_debugging $g_nolock compile "$g_file"; then
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
run_it ${VARDIR}/.${COMMAND} $g_debugging ${COMMAND} run_it ${VARDIR}/.${COMMAND} $g_debugging ${COMMAND}
rc=$? rc=$?
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
else else
rc=$? rc=$?
mylogger kern.err "ERROR:$g_product ${COMMAND} failed" mylogger kern.err "ERROR:$g_product ${COMMAND} failed"
fi fi
else else
[ -x $g_firewall ] || fatal_error "No $g_firewall file found" [ -x $g_firewall ] || fatal_error "No $g_firewall file found"
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
run_it $g_firewall $g_debugging $COMMAND run_it $g_firewall $g_debugging $COMMAND
rc=$? rc=$?
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
fi fi
return $rc return $rc
@ -1169,7 +1169,7 @@ safe_commands() {
;; ;;
esac esac
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
if run_it ${VARDIR}/.$command $g_debugging $command; then if run_it ${VARDIR}/.$command $g_debugging $command; then
@ -1184,7 +1184,7 @@ safe_commands() {
run_it ${VARDIR}/.$command clear run_it ${VARDIR}/.$command clear
fi fi
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
echo "New configuration has been rejected and the old one restored" echo "New configuration has been rejected and the old one restored"
exit 2 exit 2
@ -1192,7 +1192,7 @@ safe_commands() {
fi fi
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
} }
# #
@ -1282,7 +1282,7 @@ try_command() {
g_file="${VARDIR}/.$command" g_file="${VARDIR}/.$command"
if ! compiler $g_debugging $nolock compile "$g_file"; then if ! compiler $g_debugging $g_nolock compile "$g_file"; then
status=$? status=$?
exit $status exit $status
fi fi
@ -1302,7 +1302,7 @@ try_command() {
;; ;;
esac esac
[ -n "$nolock" ] || mutex_on [ -n "$g_nolock" ] || mutex_on
if run_it ${VARDIR}/.$command $g_debugging $command && [ -n "$timeout" ]; then if run_it ${VARDIR}/.$command $g_debugging $command && [ -n "$timeout" ]; then
sleep $timeout sleep $timeout
@ -1314,7 +1314,7 @@ try_command() {
fi fi
fi fi
[ -n "$nolock" ] || mutex_off [ -n "$g_nolock" ] || mutex_off
return 0 return 0
} }