From f63b3c2020c753afeada39574c44a6c7d7865317 Mon Sep 17 00:00:00 2001 From: Sean Robinson Date: Sun, 16 Mar 2014 09:34:55 -0700 Subject: [PATCH] Move DHCP kill to a generic method Use the version of killing DHCP from disconnect as more like the pattern used in get_current_ip and its relatives. Signed-off-by: Sean Robinson --- wifiradar/connections.py | 58 ++++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 31 deletions(-) diff --git a/wifiradar/connections.py b/wifiradar/connections.py index 2d9d43e..41cf73a 100644 --- a/wifiradar/connections.py +++ b/wifiradar/connections.py @@ -154,6 +154,27 @@ class ConnectionManager(object): # call iwconfig command and wait for return if not misc.shellcmd(iwconfig_command): return + def _stop_dhcp(self, device): + """ Stop any DHCP client daemons running with our 'device'. + """ + # Kill off any existing DHCP clients running + logger.info("Kill off any existing DHCP clients running...") + if os.path.isfile(self.config.get_opt('DHCP', 'pidfile')): + logger.info("Killing existing DHCP...") + try: + if self.config.get_opt('DHCP', 'kill_args').strip() != '': + dhcp_command = [ self.config.get_opt('DHCP', 'command') ] + dhcp_command.extend( self.config.get_opt('DHCP', 'kill_args').split(' ') ) + dhcp_command.append(device) + logger.info("DHCP command: %s" % (dhcp_command, )) + # call DHCP client command and wait for return + if not misc.shellcmd(dhcp_command): return + else: + logger.info("Killing DHCP manually...") + os.kill(int(open(self.config.get_opt('DHCP', 'pidfile'), mode='r').readline()), SIGTERM) + except OSError: + logger.info("Failed to kill DHCP client") + def connect(self, profile): """ Connect to the access point specified by 'profile'. """ @@ -177,21 +198,9 @@ class ConnectionManager(object): self._prepare_nic(profile, device) # Now normal network stuff - # Kill off any existing DHCP clients running - if os.path.isfile(self.config.get_opt('DHCP', 'pidfile')): - logger.info("Killing existing DHCP...") - try: - if self.config.get_opt('DHCP', 'kill_args') != '': - # call DHCP client kill command and wait for return - misc.shellcmd([self.config.get_opt('DHCP', 'command'), self.config.get_opt('DHCP', 'kill_args')]) - else: - os.kill(int(open(self.config.get_opt('DHCP', 'pidfile'), mode='r').readline()), SIGTERM) - except OSError: - logger.info("Failed to kill DHCP client") - sys.exit() - finally: - logger.info("Stale pid file. Removing...") - os.remove(self.config.get_opt('DHCP', 'pidfile')) + + self._stop_dhcp(device) + # Kill off any existing WPA supplicants running if os.access(self.config.get_opt('WPA', 'pidfile'), os.R_OK): logger.info("Killing existing WPA supplicant...") @@ -300,22 +309,9 @@ class ConnectionManager(object): raise KeyError # Let's run the disconnection prescript self._run_script('dis_prescript', profile, device) - logger.info("Kill off any existing DHCP clients running...") - if os.path.isfile(self.config.get_opt('DHCP', 'pidfile')): - logger.info("Killing existing DHCP...") - try: - if self.config.get_opt('DHCP', 'kill_args').strip() != '': - dhcp_command = [ self.config.get_opt('DHCP', 'command') ] - dhcp_command.extend( self.config.get_opt('DHCP', 'kill_args').split(' ') ) - dhcp_command.append(device) - logger.info("DHCP command: %s" % (dhcp_command, )) - # call DHCP client command and wait for return - if not misc.shellcmd(dhcp_command): return - else: - logger.info("Killing DHCP manually...") - os.kill(int(open(self.config.get_opt('DHCP', 'pidfile'), mode='r').readline()), SIGTERM) - except OSError: - logger.info("Failed to kill DHCP client") + + self._stop_dhcp(device) + logger.info("Kill off any existing WPA supplicants running...") if os.access(self.config.get_opt('WPA', 'pidfile'), os.R_OK): logger.info("Killing existing WPA supplicant...") -- 2.11.4.GIT