Browse Source

[433] Merge branch 'trac433'

Kean Johnston 11 years ago
parent
commit
c18a49b043
3 changed files with 39 additions and 2 deletions
  1. 19 2
      src/bin/msgq/msgq.py.in
  2. 4 0
      src/bin/msgq/msgq_messages.mes
  3. 16 0
      src/bin/msgq/tests/msgq_run_test.py

+ 19 - 2
src/bin/msgq/msgq.py.in

@@ -74,6 +74,8 @@ SPECFILE_LOCATION = SPECFILE_PATH + "/msgq.spec"
 
 
 class MsgQReceiveError(Exception): pass
 class MsgQReceiveError(Exception): pass
 
 
+class MsgQRunningError(Exception): pass
+
 class MsgQCloseOnReceive(Exception):
 class MsgQCloseOnReceive(Exception):
     """Exception raised when reading data from a socket results in 'shutdown'.
     """Exception raised when reading data from a socket results in 'shutdown'.
 
 
@@ -268,11 +270,26 @@ class MsgQ:
         """Set up the listener socket.  Internal function."""
         """Set up the listener socket.  Internal function."""
         logger.debug(TRACE_BASIC, MSGQ_LISTENER_SETUP, self.socket_file)
         logger.debug(TRACE_BASIC, MSGQ_LISTENER_SETUP, self.socket_file)
 
 
-        self.listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
-
         if os.path.exists(self.socket_file):
         if os.path.exists(self.socket_file):
+            # Rather than just blindly removing the socket file, attempt to
+            # connect to the existing socket to see if there is an existing
+            # msgq running. Only if that fails do we remove the file and
+            # attempt to create a new socket.
+            existing_msgq = None
+            try:
+                existing_msgq = isc.cc.Session(self.socket_file)
+            except isc.cc.session.SessionError:
+                existing_msgq = None
+
+            if existing_msgq:
+                existing_msgq.close()
+                logger.fatal(MSGQ_ALREADY_RUNNING)
+                raise MsgQRunningError("b10-msgq already running")
+
             os.remove(self.socket_file)
             os.remove(self.socket_file)
+
         try:
         try:
+            self.listen_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
             self.listen_socket.bind(self.socket_file)
             self.listen_socket.bind(self.socket_file)
             self.listen_socket.listen(1024)
             self.listen_socket.listen(1024)
         except Exception as e:
         except Exception as e:

+ 4 - 0
src/bin/msgq/msgq_messages.mes

@@ -19,6 +19,10 @@
 # <topsrcdir>/tools/reorder_message_file.py to make sure the
 # <topsrcdir>/tools/reorder_message_file.py to make sure the
 # messages are in the correct order.
 # messages are in the correct order.
 
 
+% MSGQ_ALREADY_RUNNING Another copy of b10-msgq is already running.
+Only a single instance of b10-msgq should ever be run at one time.
+This instance will now terminate.
+
 % MSGQ_CFGMGR_SUBSCRIBED The config manager subscribed to message queue
 % MSGQ_CFGMGR_SUBSCRIBED The config manager subscribed to message queue
 This is a debug message. The message queue has little bit of special handling
 This is a debug message. The message queue has little bit of special handling
 for the configuration manager. This special handling is happening now.
 for the configuration manager. This special handling is happening now.

+ 16 - 0
src/bin/msgq/tests/msgq_run_test.py

@@ -328,6 +328,22 @@ class MsgqRunTest(unittest.TestCase):
             'group': 'notifications/cc_members'
             'group': 'notifications/cc_members'
         }]}, msg)
         }]}, msg)
 
 
+    def test_multiple_invocations(self):
+        """
+        Check to make sure that an attempt to start a second copy of the MsgQ
+        daemon fails.
+        """
+
+        self.assertTrue (os.path.exists(SOCKET_PATH))
+        self.__retcode = subprocess.call([MSGQ_PATH, '-s', SOCKET_PATH])
+        self.assertNotEqual(self.__retcode, 0)
+
+        # Verify that the socket still exists and works. We re-call
+        # test_send_direct as a means of testing that the existing
+        # daemon is still behaving correctly.
+        self.assertTrue (os.path.exists(SOCKET_PATH))
+        self.test_send_direct()
+
 if __name__ == '__main__':
 if __name__ == '__main__':
     isc.log.init("msgq-tests")
     isc.log.init("msgq-tests")
     isc.log.resetUnitTestRootLogger()
     isc.log.resetUnitTestRootLogger()