ia64/xen-unstable

changeset 8311:53cff3f88e45

Merged.
author emellor@leeni.uk.xensource.com
date Fri Dec 09 11:05:06 2005 +0000 (2005-12-09)
parents 9558361d8040 a8be51efaf46
children 1712c7882f7e
files
line diff
     1.1 --- a/tools/examples/network-bridge	Fri Dec 09 11:04:55 2005 +0000
     1.2 +++ b/tools/examples/network-bridge	Fri Dec 09 11:05:06 2005 +0000
     1.3 @@ -89,6 +89,31 @@ legacy_mask_to_prefix() {
     1.4      exit 1
     1.5  }
     1.6  
     1.7 +parse_kernel_ip() {
     1.8 +    if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then
     1.9 +	kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
    1.10 +	kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
    1.11 +	kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
    1.12 +    fi
    1.13 +}
    1.14 +
    1.15 +do_ifup() {
    1.16 +    if ! ifup $1 ; then
    1.17 +        if [ ${kip} ] ; then
    1.18 +            # use the addresses we grocked from /proc/cmdline
    1.19 +            if [ -z "${kmask}" ]; then 
    1.20 +                PREFIX=32
    1.21 +            else 
    1.22 +                legacy_mask_to_prefix ${kmask}
    1.23 +            fi
    1.24 +            ip addr flush $1
    1.25 +            ip addr add ${kip}/${PREFIX} dev $1
    1.26 +            ip link set dev $1 up
    1.27 +            [ ${kgate} ] && ip route add default via ${kgate}
    1.28 +        fi
    1.29 +    fi
    1.30 +}
    1.31 +
    1.32  # Usage: transfer_addrs src dst
    1.33  # Copy all IP addresses (including aliases) from device $src to device $dst.
    1.34  transfer_addrs () {
    1.35 @@ -234,14 +259,11 @@ using loopback.nloopbacks=<N> on the dom
    1.36  	preiftransfer ${netdev}
    1.37  	transfer_addrs ${netdev} ${vdev}
    1.38  	if ! ifdown ${netdev}; then
    1.39 -	    # if ifup didn't work, see if we have an ip= on cmd line
    1.40 -	    if egrep 'ip=[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+:' /proc/cmdline; then
    1.41 -		kip=`sed -e 's!.*ip=\([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\):.*!\1!' /proc/cmdline`
    1.42 -		kmask=`sed -e 's!.*ip=[^:]*:[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline` 
    1.43 -		kgate=`sed -e 's!.*ip=[^:]*:[^:]*:\([^:]*\):.*!\1!' /proc/cmdline`
    1.44 -		ip link set ${netdev} down
    1.45 -		ip addr flush ${netdev}
    1.46 -	    fi
    1.47 +	    # If ifdown fails, take the IP details from the kernel command
    1.48 +	    # line.
    1.49 +	    parse_kernel_ip
    1.50 +	    ip link set ${netdev} down
    1.51 +	    ip addr flush ${netdev}
    1.52  	fi
    1.53  	ip link set ${netdev} name ${pdev}
    1.54  	ip link set ${vdev} name ${netdev}
    1.55 @@ -253,20 +275,8 @@ using loopback.nloopbacks=<N> on the dom
    1.56  	add_to_bridge ${bridge} ${pdev}
    1.57  	ip link set ${bridge} up
    1.58  	ip link set ${vif0} up
    1.59 -	ip link set ${pdev} up 
    1.60 -	if ! ifup ${netdev} ; then
    1.61 -	    if [ ${kip} ] ; then
    1.62 -		# use the addresses we grocked from /proc/cmdline
    1.63 -		if [ -z "${kmask}" ]; then 
    1.64 -		    PREFIX=32; 
    1.65 -		else 
    1.66 -		    legacy_mask_to_prefix ${kmask};
    1.67 -		fi
    1.68 -		ip addr add ${kip}/${PREFIX} dev ${netdev}
    1.69 -		ip link set dev ${netdev} up
    1.70 -		[ ${kgate} ] && ip route add default via ${kgate}
    1.71 -	    fi
    1.72 -	fi
    1.73 +	ip link set ${pdev} up
    1.74 +	do_ifup ${netdev}
    1.75      else
    1.76  	# old style without ${vdev}
    1.77  	transfer_addrs  ${netdev} ${bridge}
    1.78 @@ -290,7 +300,9 @@ op_stop () {
    1.79  	ip link set dev ${vif0} down
    1.80  	mac=`ip link show ${netdev} | grep 'link\/ether' | sed -e 's/.*ether \(..:..:..:..:..:..\).*/\1/'`
    1.81  	transfer_addrs ${netdev} ${pdev}
    1.82 -	ifdown ${netdev}
    1.83 +	if ! ifdown ${netdev}; then
    1.84 +	    parse_kernel_ip
    1.85 +	fi
    1.86  	ip link set ${netdev} down arp off
    1.87  	ip link set ${netdev} addr fe:ff:ff:ff:ff:ff
    1.88  	ip link set ${pdev} down
    1.89 @@ -303,8 +315,7 @@ op_stop () {
    1.90  
    1.91  	ip link set ${netdev} name ${vdev}
    1.92  	ip link set ${pdev} name ${netdev}
    1.93 -	ifup ${netdev}
    1.94 -
    1.95 +	do_ifup ${netdev}
    1.96      else
    1.97  	transfer_routes ${bridge} ${netdev}
    1.98  	ip link set ${bridge} down
     2.1 --- a/tools/examples/vif-bridge	Fri Dec 09 11:04:55 2005 +0000
     2.2 +++ b/tools/examples/vif-bridge	Fri Dec 09 11:05:06 2005 +0000
     2.3 @@ -61,9 +61,8 @@ case "$command" in
     2.4          ;;
     2.5  
     2.6      offline)
     2.7 -        brctl delif "$bridge" "$vif" ||
     2.8 -          log debug "brctl delif $bridge $vif failed"
     2.9 -        ifconfig "$vif" down || log debug "ifconfig $vif down failed"
    2.10 +        do_without_error brctl delif "$bridge" "$vif"
    2.11 +        do_without_error ifconfig "$vif" down
    2.12          ;;
    2.13  esac
    2.14  
     3.1 --- a/tools/examples/vif-common.sh	Fri Dec 09 11:04:55 2005 +0000
     3.2 +++ b/tools/examples/vif-common.sh	Fri Dec 09 11:05:06 2005 +0000
     3.3 @@ -113,7 +113,7 @@ function handle_iptable()
     3.4  #
     3.5  function ip_of()
     3.6  {
     3.7 -  ip addr show "$1" | sed -n 's/^.*inet \([0-9.]*\).*$/\1/p'
     3.8 +  ip addr show "$1" | awk "/^.*inet.*$1\$/{print \$2}" | sed 's,/.*,,'
     3.9  }
    3.10  
    3.11  
     4.1 --- a/tools/examples/vif-nat	Fri Dec 09 11:04:55 2005 +0000
     4.2 +++ b/tools/examples/vif-nat	Fri Dec 09 11:05:06 2005 +0000
     4.3 @@ -155,7 +155,7 @@ case "$command" in
     4.4          ;;
     4.5      offline)
     4.6          [ "$dhcp" != 'no' ] && dhcp_down
     4.7 -        ifconfig "$vif" down || true
     4.8 +        do_without_error ifconfig "$vif" down
     4.9          ;;
    4.10  esac
    4.11  
     5.1 --- a/tools/examples/xen-hotplug-common.sh	Fri Dec 09 11:04:55 2005 +0000
     5.2 +++ b/tools/examples/xen-hotplug-common.sh	Fri Dec 09 11:05:06 2005 +0000
     5.3 @@ -46,6 +46,10 @@ do_or_die() {
     5.4    "$@" || fatal "$@ failed"
     5.5  }
     5.6  
     5.7 +do_without_error() {
     5.8 +  "$@" 2>/dev/null || log debug "$@ failed"
     5.9 +}
    5.10 +
    5.11  sigerr() {
    5.12    fatal "$0 failed; error detected."
    5.13  }
    5.14 @@ -121,7 +125,7 @@ release_lock()
    5.15  
    5.16    while [ $retries -lt $LOCK_RETRIES ]
    5.17    do
    5.18 -    mkdir "$lockdir" && trap "release_lock $1; sigerr" ERR &&
    5.19 +    mkdir "$lockdir" 2>/dev/null && trap "release_lock $1; sigerr" ERR &&
    5.20        _update_lock_info "$lockdir" && return
    5.21  
    5.22      local new_owner=$(_lock_owner "$lockdir")
     6.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Fri Dec 09 11:04:55 2005 +0000
     6.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Fri Dec 09 11:05:06 2005 +0000
     6.3 @@ -23,73 +23,37 @@ import relocate
     6.4  from params import *
     6.5  
     6.6  
     6.7 +XEND_PROCESS_NAME = 'xend'
     6.8 +
     6.9 +
    6.10  class Daemon:
    6.11      """The xend daemon.
    6.12      """
    6.13      def __init__(self):
    6.14 -        self.shutdown = 0
    6.15 -        self.traceon = 0
    6.16 +        self.traceon = False
    6.17          self.tracefile = None
    6.18          self.traceindent = 0
    6.19          self.child = 0 
    6.20 -        
    6.21 -    def read_pid(self, pidfile):
    6.22 -        """Read process id from a file.
    6.23  
    6.24 -        @param pidfile: file to read
    6.25 -        @return pid or 0
    6.26 -        """
    6.27 -        if os.path.isfile(pidfile) and os.path.getsize(pidfile):
    6.28 -            try:
    6.29 -                f = open(pidfile, 'r')
    6.30 -                try:
    6.31 -                    return int(f.read())
    6.32 -                finally:
    6.33 -                    f.close()
    6.34 -            except:
    6.35 -                return 0
    6.36 -        else:
    6.37 -            return 0
    6.38  
    6.39 -    def find_process(self, pid, name):
    6.40 -        """Search for a process.
    6.41 -
    6.42 -        @param pid: process id
    6.43 -        @param name: process name
    6.44 -        @return: pid if found, 0 otherwise
    6.45 -        """
    6.46 -        running = 0
    6.47 -        if pid:
    6.48 -            lines = os.popen('ps %d 2>/dev/null' % pid).readlines()
    6.49 -            exp = '^ *%d.+%s' % (pid, name)
    6.50 -            for line in lines:
    6.51 -                if re.search(exp, line):
    6.52 -                    running = pid
    6.53 -                    break
    6.54 -        return running
    6.55 -
    6.56 -    def cleanup_process(self, pidfile, name, kill):
    6.57 -        """Clean up the pidfile for a process.
    6.58 +    def cleanup_xend(self, kill):
    6.59 +        """Clean up the Xend pidfile.
    6.60          If a running process is found, kills it if 'kill' is true.
    6.61  
    6.62 -        @param pidfile: pid file
    6.63 -        @param name: process name
    6.64          @param kill: whether to kill the process
    6.65          @return running process id or 0
    6.66          """
    6.67          running = 0
    6.68 -        pid = self.read_pid(pidfile)
    6.69 -        if self.find_process(pid, name):
    6.70 +        pid = read_pid(XEND_PID_FILE)
    6.71 +        if find_process(pid, XEND_PROCESS_NAME):
    6.72              if kill:
    6.73 -                os.kill(pid, 1)
    6.74 +                os.kill(pid, signal.SIGTERM)
    6.75              else:
    6.76                  running = pid
    6.77 -        if running == 0 and os.path.isfile(pidfile):
    6.78 -            os.remove(pidfile)
    6.79 +        if running == 0 and os.path.isfile(XEND_PID_FILE):
    6.80 +            os.remove(XEND_PID_FILE)
    6.81          return running
    6.82  
    6.83 -    def cleanup_xend(self, kill):
    6.84 -        return self.cleanup_process(XEND_PID_FILE, "xend", kill)
    6.85  
    6.86      def status(self):
    6.87          """Returns the status of the xend daemon.
    6.88 @@ -97,15 +61,15 @@ class Daemon:
    6.89          0  Running
    6.90          3  Not running
    6.91          """
    6.92 -        if self.cleanup_process(XEND_PID_FILE, "xend", False) == 0:
    6.93 +        if self.cleanup_xend(False) == 0:
    6.94              return 3
    6.95          else:
    6.96              return 0
    6.97  
    6.98 -    def fork_pid(self, pidfile):
    6.99 -        """Fork and write the pid of the child to 'pidfile'.
   6.100  
   6.101 -        @param pidfile: pid file
   6.102 +    def fork_pid(self):
   6.103 +        """Fork and write the pid of the child to XEND_PID_FILE.
   6.104 +
   6.105          @return: pid of child in parent, 0 in child
   6.106          """
   6.107  
   6.108 @@ -113,7 +77,7 @@ class Daemon:
   6.109  
   6.110          if self.child:
   6.111              # Parent
   6.112 -            pidfile = open(pidfile, 'w')
   6.113 +            pidfile = open(XEND_PID_FILE, 'w')
   6.114              try:
   6.115                  pidfile.write(str(self.child))
   6.116              finally:
   6.117 @@ -121,11 +85,21 @@ class Daemon:
   6.118  
   6.119          return self.child
   6.120  
   6.121 +
   6.122      def daemonize(self):
   6.123          if not XEND_DAEMONIZE: return
   6.124 + 
   6.125          # Detach from TTY.
   6.126 +
   6.127 +        # Become the group leader (already a child process)
   6.128          os.setsid()
   6.129  
   6.130 +        # Fork, this allows the group leader to exit,
   6.131 +        # which means the child can never again regain control of the
   6.132 +        # terminal
   6.133 +        if os.fork():
   6.134 +            os._exit(0)
   6.135 +
   6.136          # Detach from standard file descriptors, and redirect them to
   6.137          # /dev/null or the log as appropriate.
   6.138          os.close(0)
   6.139 @@ -164,7 +138,7 @@ class Daemon:
   6.140          # we can avoid a race condition during startup
   6.141          
   6.142          r,w = os.pipe()
   6.143 -        if self.fork_pid(XEND_PID_FILE):
   6.144 +        if os.fork():
   6.145              os.close(w)
   6.146              r = os.fdopen(r, 'r')
   6.147              try:
   6.148 @@ -178,8 +152,43 @@ class Daemon:
   6.149          else:
   6.150              os.close(r)
   6.151              # Child
   6.152 +            self.daemonize()
   6.153              self.tracing(trace)
   6.154 -            self.run(os.fdopen(w, 'w'))
   6.155 +
   6.156 +            # If Xend proper segfaults, then we want to restart it.  Thus,
   6.157 +            # we fork a child for running Xend itself, and if it segfaults
   6.158 +            # (or exits any way other than cleanly) then we run it again.
   6.159 +            # The first time through we want the server to write to the (r,w)
   6.160 +            # pipe created above, so that we do not exit until the server is
   6.161 +            # ready to receive requests.  All subsequent restarts we don't
   6.162 +            # want this behaviour, or the pipe will eventually fill up, so
   6.163 +            # we just pass None into run in subsequent cases (by clearing w
   6.164 +            # in the parent of the first fork).
   6.165 +            while True:
   6.166 +                pid = self.fork_pid()
   6.167 +                if pid:
   6.168 +                    os.close(w)
   6.169 +                    w = False
   6.170 +
   6.171 +                    (_, status) = os.waitpid(pid, 0)
   6.172 +
   6.173 +                    if os.WIFEXITED(status):
   6.174 +                        code = os.WEXITSTATUS(status)
   6.175 +                        log.info('Xend exited with status %d.', code)
   6.176 +                        sys.exit(code)
   6.177 +
   6.178 +                    if os.WIFSIGNALED(status):
   6.179 +                        sig = os.WTERMSIG(status)
   6.180 +
   6.181 +                        if sig in (signal.SIGINT, signal.SIGTERM):
   6.182 +                            log.info('Xend stopped due to signal %d.', sig)
   6.183 +                            sys.exit(0)
   6.184 +                        else:
   6.185 +                            log.fatal(
   6.186 +                                'Xend died due to signal %d!  Restarting it.',
   6.187 +                                sig)
   6.188 +                else:
   6.189 +                    self.run(w and os.fdopen(w, 'w') or None)
   6.190  
   6.191          return ret
   6.192  
   6.193 @@ -274,25 +283,17 @@ class Daemon:
   6.194  
   6.195              relocate.listenRelocation()
   6.196              servers = SrvServer.create()
   6.197 -            self.daemonize()
   6.198              servers.start(status)
   6.199          except Exception, ex:
   6.200              print >>sys.stderr, 'Exception starting xend:', ex
   6.201              if XEND_DEBUG:
   6.202                  traceback.print_exc()
   6.203              log.exception("Exception starting xend (%s)" % ex)
   6.204 -            status.write('1')
   6.205 -            status.close()
   6.206 -            self.exit(1)
   6.207 +            if status:
   6.208 +                status.write('1')
   6.209 +                status.close()
   6.210 +            sys.exit(1)
   6.211              
   6.212 -    def exit(self, rc=0):
   6.213 -        # Calling sys.exit() raises a SystemExit exception, which only
   6.214 -        # kills the current thread. Calling os._exit() makes the whole
   6.215 -        # Python process exit immediately. There doesn't seem to be another
   6.216 -        # way to exit a Python with running threads.
   6.217 -        #sys.exit(rc)
   6.218 -        os._exit(rc)
   6.219 -
   6.220  def instance():
   6.221      global inst
   6.222      try:
   6.223 @@ -302,10 +303,47 @@ def instance():
   6.224      return inst
   6.225  
   6.226  
   6.227 +def read_pid(pidfile):
   6.228 +    """Read process id from a file.
   6.229 +
   6.230 +    @param pidfile: file to read
   6.231 +    @return pid or 0
   6.232 +    """
   6.233 +    if os.path.isfile(pidfile) and os.path.getsize(pidfile):
   6.234 +        try:
   6.235 +            f = open(pidfile, 'r')
   6.236 +            try:
   6.237 +                return int(f.read())
   6.238 +            finally:
   6.239 +                f.close()
   6.240 +        except:
   6.241 +            return 0
   6.242 +    else:
   6.243 +        return 0
   6.244 +
   6.245 +
   6.246 +def find_process(pid, name):
   6.247 +    """Search for a process.
   6.248 +
   6.249 +    @param pid: process id
   6.250 +    @param name: process name
   6.251 +    @return: pid if found, 0 otherwise
   6.252 +    """
   6.253 +    running = 0
   6.254 +    if pid:
   6.255 +        lines = os.popen('ps %d 2>/dev/null' % pid).readlines()
   6.256 +        exp = '^ *%d.+%s' % (pid, name)
   6.257 +        for line in lines:
   6.258 +            if re.search(exp, line):
   6.259 +                running = pid
   6.260 +                break
   6.261 +    return running
   6.262 +
   6.263 +
   6.264  def main(argv = None):
   6.265      global XEND_DAEMONIZE
   6.266      
   6.267 -    XEND_DAEMONIZE = 0
   6.268 +    XEND_DAEMONIZE = False
   6.269      if argv is None:
   6.270          argv = sys.argv
   6.271  
     7.1 --- a/tools/python/xen/xend/server/SrvServer.py	Fri Dec 09 11:04:55 2005 +0000
     7.2 +++ b/tools/python/xen/xend/server/SrvServer.py	Fri Dec 09 11:05:06 2005 +0000
     7.3 @@ -68,7 +68,8 @@ class XendServers:
     7.4          # Running the network script will spawn another process, which takes
     7.5          # the status fd with it unless we set FD_CLOEXEC.  Failing to do this
     7.6          # causes the read in SrvDaemon to hang even when we have written here.
     7.7 -        fcntl.fcntl(status, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
     7.8 +        if status:
     7.9 +            fcntl.fcntl(status, fcntl.F_SETFD, fcntl.FD_CLOEXEC)
    7.10          
    7.11          Vifctl.network('start')
    7.12          threads = []
    7.13 @@ -93,8 +94,9 @@ class XendServers:
    7.14              if threads_left:
    7.15                  time.sleep(.5)
    7.16  
    7.17 -        status.write('0')
    7.18 -        status.close()
    7.19 +        if status:
    7.20 +            status.write('0')
    7.21 +            status.close()
    7.22  
    7.23          for t in threads:
    7.24              t.join()