2 # Copyright (c) 2005 XenSource Ltd.
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of version 2.1 of the GNU Lesser General Public
6 # License as published by the Free Software Foundation.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 LOCK_SPINNING_RETRIES=5
25 LOCK_BASEDIR=/var/run/xen-hotplug
30 local lockdir="$LOCK_BASEDIR/$1"
31 mkdir -p "$LOCK_BASEDIR"
32 _claim_lock "$lockdir"
38 _release_lock "$LOCK_BASEDIR/$1"
45 local owner=$(_lock_owner "$lockdir")
48 while [ $retries -lt $LOCK_RETRIES ]
50 mkdir "$lockdir" 2>/dev/null && trap "release_lock $1; sigerr" ERR &&
51 _update_lock_info "$lockdir" && return
53 local new_owner=$(_lock_owner "$lockdir")
54 if [ "$new_owner" != "$owner" ]
60 if [ $retries -gt $LOCK_SPINNING_RETRIES ]
66 retries=$(($retries + 1))
68 _steal_lock "$lockdir"
75 rm -rf "$1" 2>/dev/null || true
82 local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
83 log err "Forced to steal lock on $lockdir from $owner!"
84 _release_lock "$lockdir"
85 _claim_lock "$lockdir"
91 cat "$1/owner" 2>/dev/null || echo "unknown"
97 echo "$$: $0" >"$1/owner"