Ticket #151: ticket151.diff

File ticket151.diff, 5.4 KB (added by zhanglikun, 10 years ago)

the patch for avoid more than one xfrout process starting at same time.

  • tests/xfrout_test.py

     
    278278        self.unix.decrease_transfers_counter()
    279279        self.assertEqual(count - 1, self.unix._transfers_counter)
    280280
     281    def _remove_file(self, sock_file):
     282        try:
     283            os.remove(sock_file)
     284        except OSError:
     285            pass
     286 
     287    def test_sock_file_in_use_file_exist(self):
     288        sock_file = 'temp.sock.file'
     289        self._remove_file(sock_file)
     290        self.assertFalse(self.unix._sock_file_in_use(sock_file))
     291        self.assertFalse(os.path.exists(sock_file))
    281292
     293    def test_sock_file_in_use_file_not_exist(self):
     294        self.assertFalse(self.unix._sock_file_in_use('temp.sock.file'))
     295
     296    def _start_unix_sock_server(self, sock_file):
     297        serv = ThreadingUnixStreamServer(sock_file, BaseRequestHandler)
     298        serv_thread = threading.Thread(target=serv.serve_forever)
     299        serv_thread.setDaemon(True)
     300        serv_thread.start()
     301
     302    def test_sock_file_in_use(self):
     303        sock_file = 'temp.sock.file'
     304        self._remove_file(sock_file)
     305        self.assertFalse(self.unix._sock_file_in_use(sock_file))
     306        self._start_unix_sock_server(sock_file)
     307
     308        old_stdout = sys.stdout
     309        sys.stdout = open(os.devnull, 'w')
     310        self.assertTrue(self.unix._sock_file_in_use(sock_file))
     311        sys.stdout = old_stdout
     312
     313    def test_remove_unused_sock_file_in_use(self):
     314        sock_file = 'temp.sock.file'
     315        self._remove_file(sock_file)
     316        self.assertFalse(self.unix._sock_file_in_use(sock_file))
     317        self._start_unix_sock_server(sock_file)
     318        old_stdout = sys.stdout
     319        sys.stdout = open(os.devnull, 'w')
     320        try:
     321            self.unix._remove_unused_sock_file(sock_file)
     322        except SystemExit:
     323            pass
     324        else:
     325            # This should never happen
     326            self.assertTrue(False)
     327
     328        sys.stdout = old_stdout
     329
     330    def test_remove_unused_sock_file_dir(self):
     331        import tempfile
     332        dir_name = tempfile.mkdtemp()
     333        old_stdout = sys.stdout
     334        sys.stdout = open(os.devnull, 'w')
     335        try:
     336            self.unix._remove_unused_sock_file(dir_name)
     337        except SystemExit:
     338            pass
     339        else:
     340            # This should never happen
     341            self.assertTrue(False)
     342
     343        sys.stdout = old_stdout
     344        os.rmdir(dir_name)
     345
    282346if __name__== "__main__":
    283347    unittest.main()
  • xfrout.py.in

     
    2828from isc.config.ccsession import *
    2929from isc.cc import SessionError
    3030import socket
     31import errno
    3132from optparse import OptionParser, OptionValueError
    3233try:
    3334    from bind10_xfr import *
     
    5758    def handle(self):
    5859        fd = recv_fd(self.request.fileno())
    5960        if fd < 0:
    60             raise XfroutException("failed to receive the FD for XFR connection")
     61            # This may happen when one xfrout process try to connect to
     62            # xfrout unix socket server, to check whether there is another
     63            # xfrout running.
     64            print("[b10-xfrout] Failed to receive the FD for XFR connection, "
     65                  "maybe because another frout process was started.")
     66            return
     67
    6168        data_len = self.request.recv(2)
    6269        msg_len = struct.unpack('!H', data_len)[0]
    6370        msgdata = self.request.recv(msg_len)
     
    277284    '''The unix domain socket server which accept xfr query sent from auth server.'''
    278285
    279286    def __init__(self, sock_file, handle_class, shutdown_event, config_data):
    280         try:
    281             os.unlink(sock_file)
    282         except:
    283             pass
    284  
     287        self._remove_unused_sock_file(sock_file)
    285288        self._sock_file = sock_file
    286289        ThreadingUnixStreamServer.__init__(self, sock_file, handle_class)
    287290        self._lock = threading.Lock()
    288291        self._transfers_counter = 0
    289292        self._shutdown_event = shutdown_event
    290293        self.update_config_data(config_data)
    291    
    292294
     295    def _remove_unused_sock_file(self, sock_file):
     296        '''Try to remove the socket file. If the file is being used
     297        by one running xfrout process, exit from python.
     298        If it's not a socket file or nobody is listening
     299        , it will be removed. If it can't be removed, exit from python. '''
     300        if self._sock_file_in_use(sock_file):
     301            print("[b10-xfrout] Fail to start xfrout process, unix socket"
     302                  " file '%s' is being used by another xfrout process" % sock_file)
     303            sys.exit(0)
     304        else:
     305            try:
     306                os.unlink(sock_file)
     307            except OSError as err:
     308                print('[b10-xfrout] Fail to remove file ' + sock_file, err)
     309                sys.exit(0)
     310   
     311    def _sock_file_in_use(self, sock_file):
     312        '''Check whether the socket file 'sock_file' exists and
     313        is being used by one running xfrout process. If it is,
     314        return True, or else return False. '''
     315        try:
     316            sock = socket.socket(socket.AF_UNIX)
     317            sock.connect(sock_file)
     318        except socket.error as err:
     319            return False
     320        else:
     321            return True
     322
    293323    def shutdown(self):
    294324        ThreadingUnixStreamServer.shutdown(self)
    295325        try: