From 990e977d86a1fa58f86f381eba7430957ddbc477 Mon Sep 17 00:00:00 2001 From: Likun Zhang Date: Tue, 8 Jun 2010 11:41:54 +0000 Subject: When xfrout is launched, check whether the socket file is being used by one running xfrout process, if it is, exit from python. If the file isn't a socket file or nobody is listening, it will be removed. If it can't be removed, exit from python. git-svn-id: svn://bind10.isc.org/svn/bind10/trunk@2091 e5f2f494-b856-4b98-b285-d166d9295462 --- src/bin/xfrout/xfrout.py.in | 47 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) (limited to 'src/bin/xfrout/xfrout.py.in') diff --git a/src/bin/xfrout/xfrout.py.in b/src/bin/xfrout/xfrout.py.in index af1506ec69..d0580553ab 100644 --- a/src/bin/xfrout/xfrout.py.in +++ b/src/bin/xfrout/xfrout.py.in @@ -28,6 +28,7 @@ import os from isc.config.ccsession import * from isc.cc import SessionError import socket +import errno from optparse import OptionParser, OptionValueError try: from bind10_xfr import * @@ -57,7 +58,13 @@ class XfroutSession(BaseRequestHandler): def handle(self): fd = recv_fd(self.request.fileno()) if fd < 0: - raise XfroutException("failed to receive the FD for XFR connection") + # This may happen when one xfrout process try to connect to + # xfrout unix socket server, to check whether there is another + # xfrout running. + print("[b10-xfrout] Failed to receive the FD for XFR connection, " + "maybe because another xfrout process was started.") + return + data_len = self.request.recv(2) msg_len = struct.unpack('!H', data_len)[0] msgdata = self.request.recv(msg_len) @@ -277,18 +284,44 @@ class UnixSockServer(ThreadingUnixStreamServer): '''The unix domain socket server which accept xfr query sent from auth server.''' def __init__(self, sock_file, handle_class, shutdown_event, config_data): - try: - os.unlink(sock_file) - except: - pass - + self._remove_unused_sock_file(sock_file) self._sock_file = sock_file ThreadingUnixStreamServer.__init__(self, sock_file, handle_class) self._lock = threading.Lock() self._transfers_counter = 0 self._shutdown_event = shutdown_event self.update_config_data(config_data) - + + def _remove_unused_sock_file(self, sock_file): + '''Try to remove the socket file. If the file is being used + by one running xfrout process, exit from python. + If it's not a socket file or nobody is listening + , it will be removed. If it can't be removed, exit from python. ''' + if self._sock_file_in_use(sock_file): + print("[b10-xfrout] Fail to start xfrout process, unix socket" + " file '%s' is being used by another xfrout process" % sock_file) + sys.exit(0) + else: + if not os.path.exists(sock_file): + return + + try: + os.unlink(sock_file) + except OSError as err: + print('[b10-xfrout] Fail to remove file ' + sock_file, err) + sys.exit(0) + + def _sock_file_in_use(self, sock_file): + '''Check whether the socket file 'sock_file' exists and + is being used by one running xfrout process. If it is, + return True, or else return False. ''' + try: + sock = socket.socket(socket.AF_UNIX) + sock.connect(sock_file) + except socket.error as err: + return False + else: + return True def shutdown(self): ThreadingUnixStreamServer.shutdown(self) -- cgit v1.2.3