Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Copyright (c) 2005 XenSource Ltd.
- # Copyright (c) 2007 Red Hat
- #
- # This library is free software; you can redistribute it and/or
- # modify it under the terms of version 2.1 of the GNU Lesser General Public
- # License as published by the Free Software Foundation.
- #
- # This library is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- # Lesser General Public License for more details.
- #
- # You should have received a copy of the GNU Lesser General Public
- # License along with this library; if not, write to the Free Software
- # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- #
- LOCK_BASEDIR=/var/run/xen-hotplug
- OLD_IFS=$IFS
- # Initialise the array of locks
- if [ -z ${_lockdict_size} ]; then
- _lockdict_size=0
- fi
- # As this has to work with the default FreeBSD shell (which is sh) we
- # need to simulate arrays, because sh does not provide them.
- # So instead of declaring an array, we use multiple variables named _lockdict$i
- # where $i would be the $i-th element in the array.
- # Dereferencing them is a bit trick, we need eval to convert \$_lockdict$i into
- # $_lockdict1
- # A lock would behave like a reentrant mutex, a process may hold a lock
- # many times. It would need to release the lock the same amount of times to free
- # the lock. If the process exits, the lock would be automatically released by the
- # operating system.
- # The array will start from 0 and $_lockdict_size would be the amount of locks
- # acquired. Note that if a lock is acquired two times, it will appear two times
- # in the array.
- # Informal description
- # Precondition is: true
- # The Invariant would be: the number of appereances of the argument $1 (the name
- # of the lock file) would be the number of times the current process has acquired that lock
- # Postcondition: we reserve
- _setlockfile(){
- local i
- IFS=''
- i=0
- while [ $i -lt ${_lockdict_size} ]; do
- [ -z "\$_lockdict$i" -o "\$_lockdict$i" = "$1" ] && break
- i=`expr $i + 1`
- done
- eval _lockdict$i="$1"
- _lockdict_size=`expr ${_lockdict_size} + 1`
- _lockfile="$LOCK_BASEDIR/$1"
- IFS=$OLDIFS
- }
- # First we keep track of the new lock by calling _setlockfile.
- # Then we'll loop waiting 10 seconds for a lock (would be good for debug purpouses)
- # Once we get the lock the echo commands would be executed. If lockf is unable to get
- # the lock that command wouldn't be executed and an error code would be returned
- # instead. We use that to check whether the lock was acquired or not.
- # If the lock could be acquired we exit the loop, if not we try to get the lock again.
- claim_lock(){
- mkdir -p "$LOCK_BASEDIR"
- _setlockfile $1
- while true; do
- lockf -k -s -t 10 $LOCK_BASEDIR/$1 echo 'Acquiring lock $LOCK_BASEDIR/$1' > /tmp/hotplug.log
- if [ "$?" -eq "0" ]; then
- break
- fi
- done
- }
- # We check for all the ocurrences of the lock, we just release one of them.
- # We should never have $_lockdict_size < 0, because that variable would only be
- # decremented if a lock previously reserved is found.
- release_lock(){
- local i
- local j
- local temp
- local found
- local more
- more=0
- found=0
- i=0
- j=${_lockdict_size}
- while [ "$i" -lt "$j" ]; do
- temp=$(eval echo \$_lockdict$i)
- echo $temp
- if [ $temp = "$1" ]; then
- # if found is 1, that would mean we previously found the
- # lock in the array, and that would mean it was acquired
- # more than one time, and so we can't remove the lock
- # file.
- if [ "$found" = "1" ]; then
- more=1
- fi
- eval _lockdict_size=`expr $_lockdict_size - 1`
- found=1
- fi
- i=`expr $i + 1`
- done
- # we can't only release this lock if was found and if it is the last acquisition
- # of the lock
- echo more: $more
- echo found: $found
- if [ "$more" = "0" -a "$found" = "1" ]; then
- if [ -e $LOCK_BASEDIR/$1 ]; then
- rm $LOCK_BASEDIR/$1
- fi
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement