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_blacklistipset=
g_disconnect=
g_havemutex=
VERBOSE=
VERBOSITY=1

View File

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

View File

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