ia64/xen-unstable

changeset 8194:a57cccdf2d6d

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@xensource.com>
author emellor@leeni.uk.xensource.com
date Fri Dec 02 15:38:22 2005 +0000 (2005-12-02)
parents d581e11e247c
children df011cae33e9
files tools/examples/xen-hotplug-common.sh
line diff
     1.1 --- a/tools/examples/xen-hotplug-common.sh	Fri Dec 02 15:36:39 2005 +0000
     1.2 +++ b/tools/examples/xen-hotplug-common.sh	Fri Dec 02 15:38:22 2005 +0000
     1.3 @@ -19,6 +19,8 @@
     1.4  dir=$(dirname "$0")
     1.5  . "$dir/xen-script-common.sh"
     1.6  
     1.7 +exec 2>>/var/log/xen-hotplug.log
     1.8 +
     1.9  export PATH="/sbin:/bin:/usr/bin:/usr/sbin:$PATH"
    1.10  export LANG="POSIX"
    1.11  unset $(set | grep ^LC_ | cut -d= -f1)
    1.12 @@ -45,7 +47,7 @@ do_or_die() {
    1.13  }
    1.14  
    1.15  sigerr() {
    1.16 -  fatal "$0" "$@" "failed; error detected."
    1.17 +  fatal "$0 failed; error detected."
    1.18  }
    1.19  
    1.20  trap sigerr ERR
    1.21 @@ -86,4 +88,88 @@ xenstore_write() {
    1.22    xenstore-write "$@" || fatal "Writing $@ to xenstore failed."
    1.23  }
    1.24  
    1.25 +
    1.26 +#
    1.27 +# Serialisation
    1.28 +#
    1.29 +
    1.30 +LOCK_SLEEPTIME=1
    1.31 +LOCK_SPINNING_RETRIES=5
    1.32 +LOCK_RETRIES=10
    1.33 +LOCK_BASEDIR=/var/run/xen-hotplug
    1.34 +
    1.35 +
    1.36 +claim_lock()
    1.37 +{
    1.38 +  local lockdir="$LOCK_BASEDIR/$1"
    1.39 +  mkdir -p "$LOCK_BASEDIR"
    1.40 +  _claim_lock "$lockdir"
    1.41 +}
    1.42 +
    1.43 +
    1.44 +release_lock()
    1.45 +{
    1.46 +  _release_lock "$LOCK_BASEDIR/$1"
    1.47 +}
    1.48 +
    1.49 +
    1.50 +_claim_lock()
    1.51 +{
    1.52 +  local lockdir="$1"
    1.53 +  local owner=$(_lock_owner "$lockdir")
    1.54 +  local retries=0
    1.55 +
    1.56 +  while expr $retries '<' $LOCK_RETRIES
    1.57 +  do
    1.58 +    mkdir "$lockdir" && trap "release_lock $1; sigerr" ERR &&
    1.59 +      _update_lock_info "$lockdir" && return
    1.60 +
    1.61 +    local new_owner=$(_lock_owner "$lockdir")
    1.62 +    if [ "$new_owner" != "$owner" ]
    1.63 +    then
    1.64 +      owner="$new_owner"
    1.65 +      retries=0
    1.66 +    fi
    1.67 +
    1.68 +    if expr $retries '>' $LOCK_SPINNING_RETRIES
    1.69 +    then
    1.70 +      sleep $LOCK_SLEEPTIME
    1.71 +    else
    1.72 +      sleep 0
    1.73 +    fi
    1.74 +    retries=$(($retries + 1))
    1.75 +  done
    1.76 +  _steal_lock "$lockdir"
    1.77 +}
    1.78 +
    1.79 +
    1.80 +_release_lock()
    1.81 +{
    1.82 +  trap sigerr ERR
    1.83 +  rm -rf "$1" || true
    1.84 +}
    1.85 +
    1.86 +
    1.87 +_steal_lock()
    1.88 +{
    1.89 +  local lockdir="$1"
    1.90 +  local owner=$(cat "$lockdir/owner" 2>/dev/null || echo "unknown")
    1.91 +  log err "Forced to steal lock on $lockdir from $owner!"
    1.92 +  _release_lock "$lockdir"
    1.93 +  _claim_lock "$lockdir"
    1.94 +}
    1.95 +
    1.96 +
    1.97 +_lock_owner()
    1.98 +{
    1.99 +  cat "$1/owner" 2>/dev/null || echo "unknown"
   1.100 +}
   1.101 +
   1.102 +
   1.103 +_update_lock_info()
   1.104 +{
   1.105 +  echo "$$: $0" >"$1/owner"
   1.106 +}
   1.107 +
   1.108 +
   1.109  log debug "$@" "XENBUS_PATH=$XENBUS_PATH"