Don't allow two copies of the server at once
authorJames Bunton <jamesbunton@delx.net.au>
Wed, 29 Feb 2012 02:07:04 +0000 (13:07 +1100)
committerJames Bunton <jamesbunton@delx.net.au>
Wed, 29 Feb 2012 02:07:04 +0000 (13:07 +1100)
randombg.py

index 4727b20..9ff4642 100755 (executable)
@@ -354,10 +354,6 @@ class Listener(asyncore.dispatcher):
                asyncore.dispatcher.__init__(self)
                self.cycler = cycler
                self.create_socket(socket.AF_UNIX, socket.SOCK_STREAM)
-               try:
-                       os.unlink(socket_filename)
-               except OSError:
-                       pass
                self.bind(socket_filename)
                self.listen(2) # Backlog = 2
        
@@ -370,6 +366,19 @@ class Listener(asyncore.dispatcher):
                
 
 def do_server(options, paths):
+       try:
+               sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
+               sock.connect(options.socket_filename)
+               print >>sys.stderr, "Server is already running! Will do nothing"
+               return
+       except:
+               pass
+
+       try:
+               os.unlink(options.socket_filename)
+       except OSError:
+               pass
+
        try:
                cycler = Cycler()
                listener = Listener(options.socket_filename, cycler)
@@ -384,7 +393,7 @@ def do_server(options, paths):
                # Make sure that the socket is cleaned up
                try:
                        os.unlink(options.socket_filename)
-               except:
+               except OSError:
                        pass
 
 def do_client(options, args):