ia64/xen-unstable

changeset 2867:8940f859d3fe

bitkeeper revision 1.1159.1.358 (418978baFzv03HdTaa43BJsoKTl3dA)

Better fix for popen. Also makes it easier to log stderr output
from popen'd commands, which we previously threw away. For now,
I'm also throwing it away.
author mwilli2@equilibrium.research
date Thu Nov 04 00:32:58 2004 +0000 (2004-11-04)
parents 17ab1e2fc826
children aeaa8b6b072d 1cdb1a107c48
files tools/python/xen/sv/Daemon.py tools/python/xen/util/ip.py tools/python/xen/xend/server/SrvDaemon.py tools/python/xen/xend/server/blkif.py tools/python/xen/xend/util.py
line diff
     1.1 --- a/tools/python/xen/sv/Daemon.py	Wed Nov 03 20:58:26 2004 +0000
     1.2 +++ b/tools/python/xen/sv/Daemon.py	Thu Nov 04 00:32:58 2004 +0000
     1.3 @@ -30,7 +30,7 @@ class Daemon:
     1.4          cmdex = '(?P<cmd>.*)'
     1.5          procre = re.compile('^\s*' + pidex + '\s*' + pythonex + '\s*' + cmdex + '$')
     1.6          xendre = re.compile('^/usr/sbin/xend\s*(start|restart)\s*.*$')
     1.7 -        procs = util.popen('ps -e -o pid,args 2>/dev/null')
     1.8 +        procs = util.popen('ps -e -o pid,args')
     1.9          for proc in procs:
    1.10              pm = procre.match(proc)
    1.11              if not pm: continue
    1.12 @@ -58,7 +58,7 @@ class Daemon:
    1.13              return 0
    1.14          # Read the pid of the previous invocation and search active process list.
    1.15          pid = open(PID_FILE, 'r').read()
    1.16 -        lines = util.popen('ps ' + pid + ' 2>/dev/null').readlines()
    1.17 +        lines = util.popen('ps ' + pid).readlines()
    1.18          for line in lines:
    1.19              if re.search('^ *' + pid + '.+xensv', line):
    1.20                  if not kill:
     2.1 --- a/tools/python/xen/util/ip.py	Wed Nov 03 20:58:26 2004 +0000
     2.2 +++ b/tools/python/xen/util/ip.py	Thu Nov 04 00:32:58 2004 +0000
     2.3 @@ -51,7 +51,7 @@ def get_current_ipaddr(dev='eth0'):
     2.4  
     2.5      returns interface address as a string
     2.6      """
     2.7 -    fd = util.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
     2.8 +    fd = util.popen( '/sbin/ifconfig ' + dev )
     2.9      lines = _readlines(fd)
    2.10      for line in lines:
    2.11          m = re.search( '^\s+inet addr:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
    2.12 @@ -69,7 +69,7 @@ def get_current_ipmask(dev='eth0'):
    2.13  
    2.14      returns interface netmask as a string
    2.15      """
    2.16 -    fd = util.popen( '/sbin/ifconfig ' + dev + ' 2>/dev/null' )
    2.17 +    fd = util.popen( '/sbin/ifconfig ' + dev )
    2.18      lines = _readlines(fd)
    2.19      for line in lines:
    2.20          m = re.search( '^.+Mask:([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*',
     3.1 --- a/tools/python/xen/xend/server/SrvDaemon.py	Wed Nov 03 20:58:26 2004 +0000
     3.2 +++ b/tools/python/xen/xend/server/SrvDaemon.py	Thu Nov 04 00:32:58 2004 +0000
     3.3 @@ -337,7 +337,7 @@ class Daemon:
     3.4          cmdex = '(?P<cmd>.*)'
     3.5          procre = re.compile('^\s*' + pidex + '\s*' + pythonex + '\s*' + cmdex + '$')
     3.6          xendre = re.compile('^/usr/sbin/xend\s*(start|restart)\s*.*$')
     3.7 -        procs = util.popen('ps -e -o pid,args 2>/dev/null')
     3.8 +        procs = util.popen('ps -e -o pid,args')
     3.9          for proc in procs:
    3.10              pm = procre.match(proc)
    3.11              if not pm: continue
    3.12 @@ -383,7 +383,7 @@ class Daemon:
    3.13          """
    3.14          running = 0
    3.15          if pid:
    3.16 -            lines = util.popen('ps %d 2>/dev/null' % pid).readlines()
    3.17 +            lines = util.popen('ps %d' % pid).readlines()
    3.18              exp = '^ *%d.+%s' % (pid, name)
    3.19              for line in lines:
    3.20                  if re.search(exp, line):
     4.1 --- a/tools/python/xen/xend/server/blkif.py	Wed Nov 03 20:58:26 2004 +0000
     4.2 +++ b/tools/python/xen/xend/server/blkif.py	Thu Nov 04 00:32:58 2004 +0000
     4.3 @@ -26,7 +26,7 @@ def expand_dev_name(name):
     4.4  def check_mounted(self, name):
     4.5      mode = None
     4.6      name = expand_dev_name(name)
     4.7 -    lines = util.popen('mount 2>/dev/null').readlines()
     4.8 +    lines = util.popen('mount').readlines()
     4.9      exp = re.compile('^' + name + ' .*[\(,]r(?P<mode>[ow])[,\)]')
    4.10      for line in lines:
    4.11          pm = exp.match(line)
     5.1 --- a/tools/python/xen/xend/util.py	Wed Nov 03 20:58:26 2004 +0000
     5.2 +++ b/tools/python/xen/xend/util.py	Thu Nov 04 00:32:58 2004 +0000
     5.3 @@ -3,9 +3,12 @@
     5.4  
     5.5  from twisted.internet import utils
     5.6  from twisted.internet import reactor
     5.7 +from twisted.internet import protocol
     5.8  from XendLogging import log
     5.9  from StringIO import StringIO
    5.10  
    5.11 +import os
    5.12 +
    5.13  # This is rather distasteful.  Twisted doesn't play nicely with Python's
    5.14  # standard os.popen, so here's an implementation of a synchronous popen that
    5.15  # should work reliably. - MAW
    5.16 @@ -14,22 +17,30 @@ def popen(cmd):
    5.17  
    5.18      done_flag = False
    5.19      result = ''
    5.20 -    
    5.21 -    def done(output):
    5.22 -        global done_flag, result
    5.23 -        done_flag = True
    5.24 -        result = output
    5.25  
    5.26 -    def err(output):
    5.27 -        global done_flag
    5.28 -# For normal use, suppress debug output here.  It grumbles about stderr if the
    5.29 -# program exits with $? != 0, even if stderr is redirected.  Grrr!
    5.30 -#        log.debug("util.popen(\'%s\'): %s" % (cmd, output))
    5.31 -        done_flag = True
    5.32 +    class PopenProtocol(protocol.ProcessProtocol):
    5.33 +        def connectionMade(self):
    5.34 +            self.transport.closeStdin() # we don't want stdin
    5.35 +        def outReceived(self, data):
    5.36 +            global result
    5.37 +            result = result + data
    5.38 +#        def errReceived(self, errdata):
    5.39 +#            log.debug("popen: %s" % errdata)
    5.40 +        def processEnded(self,status_obj):
    5.41 +            code = status_obj.value.exitCode
    5.42 +            if code:
    5.43 +                # todo: Should consider throwing an exception here.
    5.44 +                log.debug("popen: process exit with code %d" % code)
    5.45 +            global done_flag
    5.46 +            done_flag = True
    5.47  
    5.48 -    d = utils.getProcessOutput(cmd)
    5.49 -    d.addCallbacks(done, err)
    5.50 +    # using cmd.split is quick and dirty.  OK as long as people don't try anything
    5.51 +    # tricky with quotes, etc.
    5.52 +    args = cmd.split(' ')
    5.53 +    reactor.spawnProcess(PopenProtocol(), args[0], args, os.environ)
    5.54  
    5.55 +    # Ick!  Sit and ask the reactor to do IO, until the process finishes.
    5.56 +    # Can't just do "pass" here because then the reactor won't run at all :-(
    5.57      while not done_flag:
    5.58          reactor.iterate()
    5.59