diff options
author | Kali Kaneko <kali@leap.se> | 2017-08-22 23:15:38 -0400 |
---|---|---|
committer | Kali Kaneko <kali@leap.se> | 2017-08-30 16:18:00 -0400 |
commit | 2d7543e8c3828a8a0ab7b77b1c3a492c7ff28ed7 (patch) | |
tree | 581e98abdc1b412ec5ff2e1f7445d16cbaddcd74 /src/leap/bitmask/vpn/launchers | |
parent | a4e14e01b1f8259f72b27712c53bd50c0a8bf20f (diff) |
[refactor] move terminate_or_kill to linux launcher
Diffstat (limited to 'src/leap/bitmask/vpn/launchers')
-rw-r--r-- | src/leap/bitmask/vpn/launchers/linux.py | 41 |
1 files changed, 41 insertions, 0 deletions
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. |