ia64/xen-unstable

view tools/examples/xen-hotplug-common.sh @ 8222:9b842cdbe0de

Use test ([) instead of expr to compare integers.
expr $int1 '<' $int2 seems to be broken on some shells.
Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
author cl349@firebug.cl.cam.ac.uk
date Sun Dec 04 00:49:51 2005 +0100 (2005-12-04)
parents a57cccdf2d6d
children 964d6e7eb3e5
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 #
19 dir=$(dirname "$0")
20 . "$dir/xen-script-common.sh"
22 exec 2>>/var/log/xen-hotplug.log
24 export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
25 export LANG="POSIX"
26 unset $(set | grep ^LC_ | cut -d= -f1)
28 log() {
29 local level="$1"
30 shift
31 logger -p "daemon.$level" -- "$0:" "$@" || echo "$0 $@" >&2
32 }
34 fatal() {
35 xenstore_write "$XENBUS_PATH"/hotplug-status error
36 log err "$@"
37 exit 1
38 }
40 success() {
41 # Tell DevController that backend is "connected"
42 xenstore_write "$XENBUS_PATH"/hotplug-status connected
43 }
45 do_or_die() {
46 "$@" || fatal "$@ failed"
47 }
49 sigerr() {
50 fatal "$0 failed; error detected."
51 }
53 trap sigerr ERR
56 ##
57 # xenstore_read <path>+
58 #
59 # Read each of the given paths, returning each result on a separate line, or
60 # exit this script if any of the paths is missing.
61 #
62 xenstore_read() {
63 local v=$(xenstore-read "$@" || true)
64 [ "$v" != "" ] || fatal "xenstore-read $@ failed."
65 echo "$v"
66 }
69 ##
70 # xenstore_read_default <path> <default>
71 #
72 # Read the given path, returning the value there or the given default if the
73 # path is not present.
74 #
75 xenstore_read_default() {
76 xenstore-read "$1" || echo "$2"
77 }
80 ##
81 # xenstore_write (<path> <value>)+
82 #
83 # Write each of the key/value pairs to the store, and exit this script if any
84 # such writing fails.
85 #
86 xenstore_write() {
87 log debug "Writing $@ to xenstore."
88 xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
89 }
92 #
93 # Serialisation
94 #
96 LOCK_SLEEPTIME=1
97 LOCK_SPINNING_RETRIES=5
98 LOCK_RETRIES=10
99 LOCK_BASEDIR=/var/run/xen-hotplug
102 claim_lock()
103 {
104 local lockdir="$LOCK_BASEDIR/$1"
105 mkdir -p "$LOCK_BASEDIR"
106 _claim_lock "$lockdir"
107 }
110 release_lock()
111 {
112 _release_lock "$LOCK_BASEDIR/$1"
113 }
116 _claim_lock()
117 {
118 local lockdir="$1"
119 local owner=$(_lock_owner "$lockdir")
120 local retries=0
122 while [ $retries -lt $LOCK_RETRIES ]
123 do
124 mkdir "$lockdir" && trap "release_lock $1; sigerr" ERR &&
125 _update_lock_info "$lockdir" && return
127 local new_owner=$(_lock_owner "$lockdir")
128 if [ "$new_owner" != "$owner" ]
129 then
130 owner="$new_owner"
131 retries=0
132 fi
134 if [ $retries -gt $LOCK_SPINNING_RETRIES ]
135 then
136 sleep $LOCK_SLEEPTIME
137 else
138 sleep 0
139 fi
140 retries=$(($retries + 1))
141 done
142 _steal_lock "$lockdir"
143 }
146 _release_lock()
147 {
148 trap sigerr ERR
149 rm -rf "$1" || true
150 }
153 _steal_lock()
154 {
155 local lockdir="$1"
156 local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
157 log err "Forced to steal lock on $lockdir from $owner!"
158 _release_lock "$lockdir"
159 _claim_lock "$lockdir"
160 }
163 _lock_owner()
164 {
165 cat "$1/owner" 2>/dev/null || echo "unknown"
166 }
169 _update_lock_info()
170 {
171 echo "$$: $0" >"$1/owner"
172 }
175 log debug "$@" "XENBUS_PATH=$XENBUS_PATH"