From 0875a3d498c30187a40a788d3bd1eefa9c5924e2 Mon Sep 17 00:00:00 2001 From: antialias Date: Fri, 12 Oct 2012 15:49:28 -0400 Subject: stopping openvpn via management interface. --- src/leap/eip/eipconnection.py | 19 ++++++++++--------- src/leap/eip/openvpnconnection.py | 30 +++++++++++++++++++++--------- 2 files changed, 31 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/leap/eip/eipconnection.py b/src/leap/eip/eipconnection.py index fea830f3..f0e7861e 100644 --- a/src/leap/eip/eipconnection.py +++ b/src/leap/eip/eipconnection.py @@ -91,7 +91,8 @@ class EIPConnection(OpenVPNConnection): """ disconnects client """ - self._disconnect() + self.cleanup() + logger.debug("disconnect: clicked.") self.status.change_to(self.status.DISCONNECTED) def shutdown(self): @@ -141,14 +142,14 @@ class EIPConnection(OpenVPNConnection): # private methods # - def _disconnect(self): - """ - private method for disconnecting - """ - if self.subp is not None: - logger.debug('disconnecting...') - self.subp.terminate() - self.subp = None + #def _disconnect(self): + # """ + # private method for disconnecting + # """ + # if self.subp is not None: + # logger.debug('disconnecting...') + # self.subp.terminate() + # self.subp = None #def _is_alive(self): #""" diff --git a/src/leap/eip/openvpnconnection.py b/src/leap/eip/openvpnconnection.py index 14839f6b..96df4f1d 100644 --- a/src/leap/eip/openvpnconnection.py +++ b/src/leap/eip/openvpnconnection.py @@ -174,17 +174,22 @@ to be triggered for each one of them. def cleanup(self): """ - terminates child subprocess + terminates openvpn child subprocess """ - # XXX we should send a quit process using management - # interface. if self.subp: - try: - self.subp.terminate() - except OSError: - logger.error('cannot terminate subprocess!' + self._stop() + RETCODE = self.subp.wait() + if RETCODE: + logger.error('cannot terminate subprocess! ' '(maybe openvpn still running?)') + def _stop(self): + """ + stop openvpn process + """ + logger.debug("disconnecting...") + self._send_command("signal SIGTERM\n") + # # management methods # @@ -221,9 +226,16 @@ to be triggered for each one of them. """ Read as much as available. Position seek pointer to end of stream """ - b = self.tn.read_eager() - while b: + try: b = self.tn.read_eager() + except EOFError: + logger.debug("Could not read from socket. Assuming it died.") + return + while b: + try: + b = self.tn.read_eager() + except EOFError: + logger.debug("Could not read from socket. Assuming it died.") def connected(self): """ -- cgit v1.2.3 From a6c587edad293996e4015876d7e59432d6a4e8ea Mon Sep 17 00:00:00 2001 From: antialias Date: Mon, 15 Oct 2012 12:28:54 -0400 Subject: attempts to stop exisiting instances of openvpn when discovered at start up. --- src/leap/eip/openvpnconnection.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/leap/eip/openvpnconnection.py b/src/leap/eip/openvpnconnection.py index 96df4f1d..d93bc40f 100644 --- a/src/leap/eip/openvpnconnection.py +++ b/src/leap/eip/openvpnconnection.py @@ -168,7 +168,9 @@ to be triggered for each one of them. for process in psutil.get_process_list(): if process.name == "openvpn": logger.debug('an openvpn instance is already running.') - raise eip_exceptions.OpenVPNAlreadyRunning + logger.debug('attempting to stop openvpn instance.') + if not self._stop(): + raise eip_exceptions.OpenVPNAlreadyRunning logger.debug('no openvpn instance found.') @@ -190,7 +192,34 @@ to be triggered for each one of them. logger.debug("disconnecting...") self._send_command("signal SIGTERM\n") - # + if self.subp: + return True + + #shutting openvpn failured + #try patching in old openvpn host and trying again + process = self._get_openvpn_process() + if process: + self.host = \ + process.cmdline[process.cmdline.index("--management") + 1] + self._send_command("signal SIGTERM\n") + + #make sure the process was terminated + process = self._get_openvpn_process() + if not process: + logger.debug("Exisiting OpenVPN Process Terminated") + return True + else: + logger.error("Unable to terminate exisiting OpenVPN Process.") + return False + + return True + + def _get_openvpn_process(self): + for process in psutil.get_process_list(): + if process.name == "openvpn": + return process + return None + # management methods # # XXX REVIEW-ME -- cgit v1.2.3