ia64/xen-unstable

view tools/hotplug/Linux/locking.sh @ 18770:4bfc67b09e9c

tools/hotplug: Separate OS-specific scripts.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Nov 04 12:13:42 2008 +0000 (2008-11-04)
parents
children
line source
1 #
2 # Copyright (c) 2005 XenSource Ltd.
3 #
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.
7 #
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.
12 #
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
16 #
18 #
19 # Serialisation
20 #
22 LOCK_SLEEPTIME=1
23 LOCK_SPINNING_RETRIES=5
24 LOCK_RETRIES=100
25 LOCK_BASEDIR=/var/run/xen-hotplug
28 claim_lock()
29 {
30 local lockdir="$LOCK_BASEDIR/$1"
31 mkdir -p "$LOCK_BASEDIR"
32 _claim_lock "$lockdir"
33 }
36 release_lock()
37 {
38 _release_lock "$LOCK_BASEDIR/$1"
39 }
42 _claim_lock()
43 {
44 local lockdir="$1"
45 local owner=$(_lock_owner "$lockdir")
46 local retries=0
48 while [ $retries -lt $LOCK_RETRIES ]
49 do
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" ]
55 then
56 owner="$new_owner"
57 retries=0
58 fi
60 if [ $retries -gt $LOCK_SPINNING_RETRIES ]
61 then
62 sleep $LOCK_SLEEPTIME
63 else
64 sleep 0
65 fi
66 retries=$(($retries + 1))
67 done
68 _steal_lock "$lockdir"
69 }
72 _release_lock()
73 {
74 trap sigerr ERR
75 rm -rf "$1" 2>/dev/null || true
76 }
79 _steal_lock()
80 {
81 local lockdir="$1"
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"
86 }
89 _lock_owner()
90 {
91 cat "$1/owner" 2>/dev/null || echo "unknown"
92 }
95 _update_lock_info()
96 {
97 echo "$$: $0" >"$1/owner"
98 }