summaryrefslogtreecommitdiff
path: root/src/leap/eip/openvpnconnection.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/leap/eip/openvpnconnection.py')
-rw-r--r--src/leap/eip/openvpnconnection.py80
1 files changed, 46 insertions, 34 deletions
diff --git a/src/leap/eip/openvpnconnection.py b/src/leap/eip/openvpnconnection.py
index c2dc71a6..05979ff7 100644
--- a/src/leap/eip/openvpnconnection.py
+++ b/src/leap/eip/openvpnconnection.py
@@ -2,17 +2,21 @@
OpenVPN Connection
"""
from __future__ import (print_function)
+from functools import partial
import logging
import os
import psutil
import shutil
+import select
import socket
-from functools import partial
+from time import sleep
logger = logging.getLogger(name=__name__)
from leap.base.connection import Connection
+from leap.base.constants import OPENVPN_BIN
from leap.util.coroutines import spawn_and_watch_process
+from leap.util.misc import get_openvpn_pids
from leap.eip.udstelnet import UDSTelnet
from leap.eip import config as eip_config
@@ -83,7 +87,7 @@ class OpenVPNManagement(object):
try:
self._connect_to_management()
except eip_exceptions.MissingSocketError:
- logger.warning('missing management socket')
+ #logger.warning('missing management socket')
return []
try:
if hasattr(self, 'tn'):
@@ -92,14 +96,19 @@ class OpenVPNManagement(object):
logger.error('socket error')
self._close_management_socket(announce=False)
return []
- buf = self.tn.read_until(b"END", 2)
- self._seek_to_eof()
- blist = buf.split('\r\n')
- if blist[-1].startswith('END'):
- del blist[-1]
- return blist
- else:
- return []
+ try:
+ buf = self.tn.read_until(b"END", 2)
+ self._seek_to_eof()
+ blist = buf.split('\r\n')
+ if blist[-1].startswith('END'):
+ del blist[-1]
+ return blist
+ else:
+ return []
+ except socket.error as exc:
+ logger.debug('socket error: %s' % exc.message)
+ except select.error as exc:
+ logger.debug('select error: %s' % exc.message)
def _send_short_command(self, cmd):
"""
@@ -271,22 +280,20 @@ to be triggered for each one of them.
# checks
+
def _check_if_running_instance(self):
"""
check if openvpn is already running
"""
- try:
- for process in psutil.get_process_list():
- if process.name == "openvpn":
- logger.debug('an openvpn instance is already running.')
- logger.debug('attempting to stop openvpn instance.')
- if not self._stop_openvpn():
- raise eip_exceptions.OpenVPNAlreadyRunning
-
- except psutil.error.NoSuchProcess:
- logger.debug('detected a process which died. passing.')
-
- logger.debug('no openvpn instance found.')
+ openvpn_pids = get_openvpn_pids()
+ if openvpn_pids:
+ logger.debug('an openvpn instance is already running.')
+ logger.debug('attempting to stop openvpn instance.')
+ if not self._stop_openvpn():
+ raise eip_exceptions.OpenVPNAlreadyRunning
+ return
+ else:
+ logger.debug('no openvpn instance found.')
def _set_ovpn_command(self):
try:
@@ -327,12 +334,13 @@ to be triggered for each one of them.
#deprecate watcher_cb,
#use _only_ signal_maps instead
- logger.debug('_launch_openvpn called')
+ #logger.debug('_launch_openvpn called')
if self.watcher_cb is not None:
linewrite_callback = self.watcher_cb
else:
#XXX get logger instead
- linewrite_callback = lambda line: print('watcher: %s' % line)
+ linewrite_callback = lambda line: logger.debug(
+ 'watcher: %s' % line)
# the partial is not
# being applied now because we're not observing the process
@@ -340,7 +348,8 @@ to be triggered for each one of them.
# here since it will be handy for observing patterns in the
# thru-the-manager updates (with regex)
observers = (linewrite_callback,
- partial(lambda con_status, line: None, self.status))
+ partial(lambda con_status,
+ line: linewrite_callback, self.status))
subp, watcher = spawn_and_watch_process(
self.command,
self.args,
@@ -355,23 +364,24 @@ to be triggered for each one of them.
interface
"""
# XXX method a bit too long, split
- logger.debug("terminating openvpn process...")
+ logger.debug("atempting to terminate openvpn process...")
if self.connected():
try:
self._send_command("signal SIGTERM\n")
+ sleep(1)
+ if not self.subp: # XXX ???
+ return True
except socket.error:
logger.warning('management socket died')
return
- if self.subp:
- # ???
- return True
#shutting openvpn failured
#try patching in old openvpn host and trying again
+ # XXX could be more than one!
process = self._get_openvpn_process()
if process:
- logger.debug('process :%s' % process)
+ logger.debug('process: %s' % process.name)
cmdline = process.cmdline
manag_flag = "--management"
@@ -392,9 +402,11 @@ to be triggered for each one of them.
return True
def _get_openvpn_process(self):
- # plist = [p for p in psutil.get_process_list() if p.name == "openvpn"]
- # return plist[0] if plist else None
- for process in psutil.get_process_list():
- if process.name == "openvpn":
+ for process in psutil.process_iter():
+ if OPENVPN_BIN in process.name:
return process
return None
+
+ def get_log(self, lines=1):
+ log = self._send_command("log %s" % lines)
+ return log