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

[Xen-changelog] Add support for unix-domain sockets to consoles and the



ChangeSet 1.1327.2.11, 2005/04/27 17:06:43+01:00, mjw@xxxxxxxxxxxxxxxxxxx

        Add support for unix-domain sockets to consoles and the
        event server.
        
        Signed-off-by: Mike Wray <mike.wray@xxxxxx>



 util/console_client.py   |   28 ++++++++++++++++-----
 web/unix.py              |   19 +++++++++-----
 xend/server/SrvDaemon.py |    7 -----
 xend/server/console.py   |   62 +++++++++++++++++++++++++++--------------------
 xend/server/event.py     |   24 +++++++++---------
 xm/create.py             |    3 +-
 xm/main.py               |    3 +-
 7 files changed, 88 insertions(+), 58 deletions(-)


diff -Nru a/tools/python/xen/util/console_client.py 
b/tools/python/xen/util/console_client.py
--- a/tools/python/xen/util/console_client.py   2005-05-13 16:04:34 -04:00
+++ b/tools/python/xen/util/console_client.py   2005-05-13 16:04:34 -04:00
@@ -57,9 +57,18 @@
                     raise
     sys.exit(0)
 
-def connect(host,port):
-    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
-    sock.connect((host,port))
+def connect(host, port, path=None):
+    # Try inet first. If 'path' is given and the error
+    # was connection refused, try unix-domain on 'path'.
+    try:
+        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+        sock.connect((host, port))
+    except socket.error, err:
+        if (path is None) or (err[0] != errno.ECONNREFUSED):
+            raise
+        # Try unix-domain.
+        sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+        sock.connect(path)
 
     oattrs = tcgetattr(0)
     nattrs = tcgetattr(0)
@@ -86,7 +95,14 @@
         __send_to_sock(sock)
 
 if __name__ == '__main__':
-    if len(sys.argv) != 3:
-        print sys.argv[0] + " <host> <port>"
+    argc = len(sys.argv)
+    if argc < 3 or argc > 4:
+        print >>sys.stderr, sys.argv[0], "<host> <port> [<path>]"
         sys.exit(1)
-    connect(str(sys.argv[1]),int(sys.argv[2]))
+    host = sys.argv[1]
+    port = int(sys.argv[2])
+    if argc > 3:
+        path = sys.argv[3]
+    else:
+        path = None
+    connect(host, port, path=path)
diff -Nru a/tools/python/xen/web/unix.py b/tools/python/xen/web/unix.py
--- a/tools/python/xen/web/unix.py      2005-05-13 16:04:34 -04:00
+++ b/tools/python/xen/web/unix.py      2005-05-13 16:04:34 -04:00
@@ -1,6 +1,7 @@
 import sys
 import socket
 import os
+import os.path
 
 from connection import *
 from protocol import *
@@ -15,18 +16,22 @@
         self.path = path
         
     def createSocket(self):
-        try:
-            os.unlink(self.path)
-        except SystemExit:
-            raise
-        except Exception, ex:
-            pass
+        pathdir = os.path.dirname(self.path)
+        if not os.path.exists(pathdir):
+            os.makedirs(pathdir)
+        else:
+            try:
+                os.unlink(self.path)
+            except SystemExit:
+                raise
+            except Exception, ex:
+                pass
         sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
         sock.bind(self.path)
         return sock
 
     def acceptConnection(self, sock, protocol, addr):
-        return UnixServerConnection(sock, protocol, addr, self)
+        return UnixServerConnection(sock, protocol, self.path, self)
 
 class UnixClientConnection(SocketClientConnection):
 
diff -Nru a/tools/python/xen/xend/server/SrvDaemon.py 
b/tools/python/xen/xend/server/SrvDaemon.py
--- a/tools/python/xen/xend/server/SrvDaemon.py 2005-05-13 16:04:34 -04:00
+++ b/tools/python/xen/xend/server/SrvDaemon.py 2005-05-13 16:04:34 -04:00
@@ -324,7 +324,7 @@
             xroot = XendRoot.instance()
             log.info("Xend Daemon started")
             self.createFactories()
-            self.listenEvent(xroot)
+            event.listenEvent(self)
             self.listenChannels()
             servers = SrvServer.create()
             self.daemonize()
@@ -339,11 +339,6 @@
             
     def createFactories(self):
         self.channelF = channel.channelFactory()
-
-    def listenEvent(self, xroot):
-        port = xroot.get_xend_event_port()
-        interface = xroot.get_xend_address()
-        return event.listenEvent(self, port, interface)
 
     def listenChannels(self):
         def virqReceived(virq):
diff -Nru a/tools/python/xen/xend/server/console.py 
b/tools/python/xen/xend/server/console.py
--- a/tools/python/xen/xend/server/console.py   2005-05-13 16:04:34 -04:00
+++ b/tools/python/xen/xend/server/console.py   2005-05-13 16:04:34 -04:00
@@ -19,7 +19,7 @@
 from params import *
 
 class ConsoleProtocol(protocol.Protocol):
-    """Asynchronous handler for a console TCP socket.
+    """Asynchronous handler for a console socket.
     """
 
     def __init__(self, console, id):
@@ -36,10 +36,16 @@
             self.loseConnection()
             return
         else:
+            if len(self.addr) == 2:
+                host = str(self.addr[0])
+                port = str(self.addr[1])
+            else:
+                host = 'localhost'
+                port = str(addr)
             log.info("Console connected %s %s %s",
-                     self.id, str(self.addr[0]), str(self.addr[1]))
+                     self.id, host, port)
             eserver.inject('xend.console.connect',
-                           [self.id, self.addr[0], self.addr[1]])
+                           [self.id, host, port])
 
     def dataReceived(self, data):
         if self.console.receiveInput(self, data):
@@ -50,7 +56,6 @@
         return len(data)
 
     def connectionLost(self, reason=None):
-        print 'ConsoleProtocol>connectionLost>', reason
         log.info("Console disconnected %s %s %s",
                  str(self.id), str(self.addr[0]), str(self.addr[1]))
         eserver.inject('xend.console.disconnect',
@@ -60,22 +65,7 @@
     def loseConnection(self):
         self.transport.loseConnection()
 
-class ConsoleFactory(protocol.ServerFactory):
-    """Asynchronous handler for a console server socket.
-    """
-    protocol = ConsoleProtocol
-    
-    def __init__(self, console, id):
-        #protocol.ServerFactory.__init__(self)
-        self.console = console
-        self.id = id
-
-    def buildProtocol(self, addr):
-        proto = self.protocol(self.console, self.id)
-        proto.factory = self
-        return proto
-
-class ConsoleDev(Dev):
+class ConsoleDev(Dev, protocol.ServerFactory):
     """Console device for a domain.
     Does not poll for i/o itself, but relies on the domain to post console
     output and the connected TCP sockets to post console input.
@@ -96,7 +86,9 @@
         self.obuf = xu.buffer()
         self.ibuf = xu.buffer()
         self.channel = None
-        self.listener = None
+        self.listening = False
+        self.unix_listener = None
+        self.tcp_listener = None
         
         console_port = sxp.child_value(self.config, "console_port")
         if console_port is None:
@@ -188,9 +180,15 @@
         try:
             self.lock.acquire()
             self.status = self.STATUS_CLOSED
+            self.listening = False
             if self.conn:
                 self.conn.loseConnection()
-            self.listener.stopListening()
+            if self.tcp_listener:
+                self.tcp_listener.stopListening()
+                self.tcp_listener = None
+            if self.unix_listener:
+                self.unix_listener.stopListening()
+                self.unix_listener = None
         finally:
             self.lock.release()
 
@@ -201,15 +199,27 @@
             self.lock.acquire()
             if self.closed():
                 return
-            if self.listener:
+            if self.listening:
                 pass
             else:
+                self.listening = True
                 self.status = self.STATUS_LISTENING
-                cf = ConsoleFactory(self, self.id)
-                interface = xroot.get_console_address()
-                self.listener = reactor.listenTCP(self.console_port, cf, 
interface=interface)
+                if xroot.get_xend_unix_server():
+                    path = '/var/lib/xend/console-%s' % self.console_port
+                    self.unix_listener = reactor.listenUNIX(path, self)
+                if xroot.get_xend_http_server():
+                    interface = xroot.get_console_address()
+                    self.tcp_listener = reactor.listenTCP(self.console_port, 
self, interface=interface)
         finally:
             self.lock.release()
+
+    def buildProtocol(self, addr):
+        """Factory function called to create the protocol when a connection is 
accepted
+        by listenTCP.
+        """
+        proto = ConsoleProtocol(self, self.id)
+        proto.factory = self
+        return proto
 
     def connect(self, addr, conn):
         """Connect a TCP connection to the console.
diff -Nru a/tools/python/xen/xend/server/event.py 
b/tools/python/xen/xend/server/event.py
--- a/tools/python/xen/xend/server/event.py     2005-05-13 16:04:34 -04:00
+++ b/tools/python/xen/xend/server/event.py     2005-05-13 16:04:34 -04:00
@@ -11,7 +11,7 @@
 eserver = EventServer.instance()
 from xen.xend.XendError import XendError
 
-from xen.xend import XendRoot
+from xen.xend import XendRoot; xroot = XendRoot.instance()
 
 DEBUG = 1
 
@@ -165,7 +165,7 @@
 
     def op_log_stderr(self, name, v):
         mode = v[1]
-        logging = XendRoot.instance().get_logging()
+        logging = xroot.get_logging()
         if mode == 'on':

_______________________________________________
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®.