ia64/xen-unstable

view tools/examples/xen-hotplug-common.sh @ 8964:8946b6dcd49e

Fix x86_64 Xen build.

event_callback_cs and failsafe_callback_cs are x86_32 only.

Signed-off-by: Ian Campbell <Ian.Campbell@XenSource.com>
author Ian.Campbell@xensource.com
date Wed Feb 22 17:26:39 2006 +0000 (2006-02-22)
parents a12bd9e8fdf8
children 911f3ecd975e
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 do_without_error() {
50 "$@" 2>/dev/null || log debug "$@ failed"
51 }
53 sigerr() {
54 fatal "$0 failed; error detected."
55 }
57 trap sigerr ERR
60 ##
61 # xenstore_read <path>+
62 #
63 # Read each of the given paths, returning each result on a separate line, or
64 # exit this script if any of the paths is missing.
65 #
66 xenstore_read() {
67 local v=$(xenstore-read "$@" || true)
68 [ "$v" != "" ] || fatal "xenstore-read $@ failed."
69 echo "$v"
70 }
73 ##
74 # xenstore_read_default <path> <default>
75 #
76 # Read the given path, returning the value there or the given default if the
77 # path is not present.
78 #
79 xenstore_read_default() {
80 xenstore-read "$1" 2>/dev/null || echo "$2"
81 }
84 ##
85 # xenstore_write (<path> <value>)+
86 #
87 # Write each of the key/value pairs to the store, and exit this script if any
88 # such writing fails.
89 #
90 xenstore_write() {
91 log debug "Writing $@ to xenstore."
92 xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
93 }
96 #
97 # Serialisation
98 #
100 LOCK_SLEEPTIME=1
101 LOCK_SPINNING_RETRIES=5
102 LOCK_RETRIES=10
103 LOCK_BASEDIR=/var/run/xen-hotplug
106 claim_lock()
107 {
108 local lockdir="$LOCK_BASEDIR/$1"
109 mkdir -p "$LOCK_BASEDIR"
110 _claim_lock "$lockdir"
111 }
114 release_lock()
115 {
116 _release_lock "$LOCK_BASEDIR/$1"
117 }
120 _claim_lock()
121 {
122 local lockdir="$1"
123 local owner=$(_lock_owner "$lockdir")
124 local retries=0
126 while [ $retries -lt $LOCK_RETRIES ]
127 do
128 mkdir "$lockdir" 2>/dev/null && trap "release_lock $1; sigerr" ERR &&
129 _update_lock_info "$lockdir" && return
131 local new_owner=$(_lock_owner "$lockdir")
132 if [ "$new_owner" != "$owner" ]
133 then
134 owner="$new_owner"
135 retries=0
136 fi
138 if [ $retries -gt $LOCK_SPINNING_RETRIES ]
139 then
140 sleep $LOCK_SLEEPTIME
141 else
142 sleep 0
143 fi
144 retries=$(($retries + 1))
145 done
146 _steal_lock "$lockdir"
147 }
150 _release_lock()
151 {
152 trap sigerr ERR
153 rm -rf "$1" 2>/dev/null || true
154 }
157 _steal_lock()
158 {
159 local lockdir="$1"
160 local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
161 log err "Forced to steal lock on $lockdir from $owner!"
162 _release_lock "$lockdir"
163 _claim_lock "$lockdir"
164 }
167 _lock_owner()
168 {
169 cat "$1/owner" 2>/dev/null || echo "unknown"
170 }
173 _update_lock_info()
174 {
175 echo "$$: $0" >"$1/owner"
176 }
179 log debug "$@" "XENBUS_PATH=$XENBUS_PATH"