From 078b9f3137ad17feef21f0ac9fa593a07bb2ab83 Mon Sep 17 00:00:00 2001 From: Sheng Yang Date: Thu, 25 Aug 2011 17:54:22 -0700 Subject: [PATCH] bug 11266: Add lockfile for scripts in system vm Otherwise it's easy to trigger the racy issue. This one just contained fix for reconfigLB.sh --- patches/systemvm/debian/config/root/func.sh | 44 +++++++++++++++++++ .../systemvm/debian/config/root/reconfigLB.sh | 19 ++++++-- 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 patches/systemvm/debian/config/root/func.sh diff --git a/patches/systemvm/debian/config/root/func.sh b/patches/systemvm/debian/config/root/func.sh new file mode 100644 index 00000000000..c1a921a86c9 --- /dev/null +++ b/patches/systemvm/debian/config/root/func.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# getLockFile() parameters +# $1 lock filename +# $2 timeout seconds +getLockFile() { + __locked=0 + __LOCKFILE="/tmp/$1.lock" + if [ $2 ] + then + __TIMEOUT=$2 + else + __TIMEOUT=10 + fi + + for i in `seq 1 $__TIMEOUT` + do + if [ ! -e $__LOCKFILE ] + then + touch $__LOCKFILE + __locked=1 + break + fi + sleep 1 + logger -t cloud "sleep 1 second wait for the lock file " $__LOCKFILE + done + if [ $__locked -ne 1 ] + then + logger -t cloud "fail to acquire the lock file $__LOCKFILE after $__TIMEOUT seconds time out!" + fi + echo $__locked +} + +# releaseLockFile() parameters +# $1 lock filename +# $2 locked(1) or not(0) +releaseLockFile() { + __LOCKFILE="/tmp/$1.lock" + __locked=$2 + if [ "$__locked" == "1" ] + then + rm $__LOCKFILE + fi +} diff --git a/patches/systemvm/debian/config/root/reconfigLB.sh b/patches/systemvm/debian/config/root/reconfigLB.sh index 38b07142f99..ef4ac7be011 100755 --- a/patches/systemvm/debian/config/root/reconfigLB.sh +++ b/patches/systemvm/debian/config/root/reconfigLB.sh @@ -18,9 +18,18 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . # - +name="reconfigLB" +source func.sh +locked=$(getLockFile $name) +if [ "$locked" != "1" ] +then + logger -t cloud "Fail to get the lock for " $name + exit 1 +fi + +ret=0 # save previous state mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.old mv /var/run/haproxy.pid /var/run/haproxy.pid.old @@ -32,7 +41,7 @@ echo "New haproxy instance successfully loaded, stopping previous one." kill -KILL $(cat /var/run/haproxy.pid.old) rm -f /var/run/haproxy.pid.old - exit 0 + ret=0 else echo "New instance failed to start, resuming previous one." kill -TTIN $(cat /var/run/haproxy.pid.old) @@ -40,5 +49,9 @@ mv /var/run/haproxy.pid.old /var/run/haproxy.pid mv /etc/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg.new mv /etc/haproxy/haproxy.cfg.old /etc/haproxy/haproxy.cfg - exit 1 + ret=1 fi + +releaseLockFile $name $locked + +exit $ret