summaryrefslogtreecommitdiff
path: root/src/leap/eip/openvpnconnection.py
diff options
context:
space:
mode:
authorkali <kali@leap.se>2012-10-18 02:19:03 +0900
committerkali <kali@leap.se>2012-10-18 02:19:03 +0900
commit17896b9f9cbfbca7bc0a0344050dddea8ba61880 (patch)
tree47a708e0a77a176c414cee69ce3001f5af5c60dc /src/leap/eip/openvpnconnection.py
parent28dcbfbc6e3a61d47c2a1218bce5d2693c77d04d (diff)
parentf6e900f024074435349eb778a2d89baed55e1e6c (diff)
Merge branch 'develop' into feature/generic-wizard
Diffstat (limited to 'src/leap/eip/openvpnconnection.py')
-rw-r--r--src/leap/eip/openvpnconnection.py63
1 files changed, 52 insertions, 11 deletions
diff --git a/src/leap/eip/openvpnconnection.py b/src/leap/eip/openvpnconnection.py
index 14839f6b..d93bc40f 100644
--- a/src/leap/eip/openvpnconnection.py
+++ b/src/leap/eip/openvpnconnection.py
@@ -168,24 +168,58 @@ 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.')
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")
+
+ 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
@@ -221,9 +255,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):
"""