From 2d7543e8c3828a8a0ab7b77b1c3a492c7ff28ed7 Mon Sep 17 00:00:00 2001 From: Kali Kaneko Date: Tue, 22 Aug 2017 23:15:38 -0400 Subject: [refactor] move terminate_or_kill to linux launcher --- src/leap/bitmask/vpn/launchers/linux.py | 41 +++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/leap/bitmask/vpn/launchers') diff --git a/src/leap/bitmask/vpn/launchers/linux.py b/src/leap/bitmask/vpn/launchers/linux.py index 38b9e417..052040dc 100644 --- a/src/leap/bitmask/vpn/launchers/linux.py +++ b/src/leap/bitmask/vpn/launchers/linux.py @@ -33,6 +33,11 @@ from leap.bitmask.vpn.privilege import LinuxPolicyChecker from leap.bitmask.vpn.management import ManagementProtocol from leap.bitmask.vpn.launcher import VPNLauncher + +TERMINATE_MAXTRIES = 10 +TERMINATE_WAIT = 1 # secs +RESTART_WAIT = 2 # secs + log = Logger() @@ -144,6 +149,42 @@ class LinuxVPNLauncher(VPNLauncher): return command + def terminate_or_kill(self, terminatefun, killfun, proc): + terminatefun() + + # we trigger a countdown to be unpolite + # if strictly needed. + d = defer.Deferred() + reactor.callLater( + TERMINATE_WAIT, self._wait_and_kill, killfun, proc, d) + return d + + def _wait_and_kill(self, killfun, proc, deferred, tries=0): + """ + Check if the process is still alive, and call the killfun + after waiting several times during a timeout period. + + :param tries: counter of tries, used in recursion + :type tries: int + """ + if tries < TERMINATE_MAXTRIES: + if proc.transport.pid is None: + deferred.callback(True) + return + else: + self.log.debug('Process did not die, waiting...') + + tries += 1 + reactor.callLater( + TERMINATE_WAIT, + self._wait_and_kill, killfun, proc, deferred, tries) + return + + # after running out of patience, we try a killProcess + d = killfun() + d.addCallback(lambda _: deferred.callback(True)) + return d + def kill_previous_openvpn(kls): """ Checks if VPN is already running and tries to stop it. -- cgit v1.2.3