[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] Added serialisation facilities. Udev is supposed to serialise events, but sadly



# HG changeset patch
# User emellor@xxxxxxxxxxxxxxxxxxxxxx
# Node ID a57cccdf2d6d5c70bef4f570fe2f906fd18ba406
# Parent  d581e11e247c72ac48ae5be69fd5c08a823dd655
Added serialisation facilities.  Udev is supposed to serialise events, but sadly
the older hotplug infrastructure does not, so we must be able to serialise the
block-sharing checks (etc) ourselves.

Redirect stderr to /var/log/xen-hotplug.log, for better debugging.

Signed-off-by: Ewan Mellor <ewan@xxxxxxxxxxxxx>

diff -r d581e11e247c -r a57cccdf2d6d tools/examples/xen-hotplug-common.sh
--- a/tools/examples/xen-hotplug-common.sh      Fri Dec  2 15:36:39 2005
+++ b/tools/examples/xen-hotplug-common.sh      Fri Dec  2 15:38:22 2005
@@ -18,6 +18,8 @@
 
 dir=$(dirname "$0")
 . "$dir/xen-script-common.sh"
+
+exec 2>>/var/log/xen-hotplug.log
 
 export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
 export LANG="POSIX"
@@ -45,7 +47,7 @@
 }
 
 sigerr() {
-  fatal "$0" "$@" "failed; error detected."
+  fatal "$0 failed; error detected."
 }
 
 trap sigerr ERR
@@ -86,4 +88,88 @@
   xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
 }
 
+
+#
+# Serialisation
+#
+
+LOCK_SLEEPTIME=1
+LOCK_SPINNING_RETRIES=5
+LOCK_RETRIES=10
+LOCK_BASEDIR=/var/run/xen-hotplug
+
+
+claim_lock()
+{
+  local lockdir="$LOCK_BASEDIR/$1"
+  mkdir -p "$LOCK_BASEDIR"
+  _claim_lock "$lockdir"
+}
+
+
+release_lock()
+{
+  _release_lock "$LOCK_BASEDIR/$1"
+}
+
+
+_claim_lock()
+{
+  local lockdir="$1"
+  local owner=$(_lock_owner "$lockdir")
+  local retries=0
+
+  while expr $retries '<' $LOCK_RETRIES
+  do
+    mkdir "$lockdir" && trap "release_lock $1; sigerr" ERR &&
+      _update_lock_info "$lockdir" && return
+
+    local new_owner=$(_lock_owner "$lockdir")
+    if [ "$new_owner" != "$owner" ]
+    then
+      owner="$new_owner"
+      retries=0
+    fi
+
+    if expr $retries '>' $LOCK_SPINNING_RETRIES
+    then
+      sleep $LOCK_SLEEPTIME
+    else
+      sleep 0
+    fi
+    retries=$(($retries + 1))
+  done
+  _steal_lock "$lockdir"
+}
+
+
+_release_lock()
+{
+  trap sigerr ERR
+  rm -rf "$1" || true
+}
+
+
+_steal_lock()
+{
+  local lockdir="$1"
+  local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
+  log err "Forced to steal lock on $lockdir from $owner!"
+  _release_lock "$lockdir"
+  _claim_lock "$lockdir"
+}
+
+
+_lock_owner()
+{
+  cat "$1/owner" 2>/dev/null || echo "unknown"
+}
+
+
+_update_lock_info()
+{
+  echo "$$: $0" >"$1/owner"
+}
+
+
 log debug "$@" "XENBUS_PATH=$XENBUS_PATH"

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.