diff options
author | Christian Poessinger <christian@poessinger.com> | 2020-03-29 15:46:12 +0200 |
---|---|---|
committer | Christian Poessinger <christian@poessinger.com> | 2020-03-29 15:46:12 +0200 |
commit | 35e0254622f8f099db3c7829cd61b85f6976c416 (patch) | |
tree | 77014e7562e26edf4f0561a79c72babcdf52df14 | |
parent | 552106b0fb27003e5e5be5494f2213f54a699b9e (diff) | |
download | vyos-1x-35e0254622f8f099db3c7829cd61b85f6976c416.tar.gz vyos-1x-35e0254622f8f099db3c7829cd61b85f6976c416.zip |
vyos.util: increase usage of process_running() and remove duplicated code
OpenVPN, WIFI, SSTP all had the same boiler plate copied about checking if a
process associated with a pidfile is running or not. This has been migrated to
the common library function vyos.util.process_running().
-rw-r--r-- | python/vyos/util.py | 2 | ||||
-rwxr-xr-x | src/conf_mode/interfaces-openvpn.py | 9 | ||||
-rwxr-xr-x | src/conf_mode/interfaces-wireless.py | 23 | ||||
-rwxr-xr-x | src/conf_mode/vpn_sstp.py | 12 | ||||
-rwxr-xr-x | src/op_mode/reset_openvpn.py | 21 |
5 files changed, 21 insertions, 46 deletions
diff --git a/python/vyos/util.py b/python/vyos/util.py index 635b11ee5..67aa87a3a 100644 --- a/python/vyos/util.py +++ b/python/vyos/util.py @@ -84,6 +84,8 @@ def colon_separated_to_dict(data_string, uniquekeys=False): def process_running(pid_file): """ Checks if a process with PID in pid_file is running """ + if not os.path.isfile(pid_file): + return False with open(pid_file, 'r') as f: pid = f.read().strip() return psutil.pid_exists(int(pid)) diff --git a/src/conf_mode/interfaces-openvpn.py b/src/conf_mode/interfaces-openvpn.py index 17aa4697f..fb2d6e6d9 100755 --- a/src/conf_mode/interfaces-openvpn.py +++ b/src/conf_mode/interfaces-openvpn.py @@ -24,7 +24,6 @@ from stat import S_IRUSR,S_IRWXU,S_IRGRP,S_IXGRP,S_IROTH,S_IXOTH from grp import getgrnam from ipaddress import ip_address,ip_network,IPv4Interface from netifaces import interfaces -from psutil import pid_exists from pwd import getpwnam from subprocess import Popen, PIPE from time import sleep @@ -33,6 +32,7 @@ from shutil import rmtree from vyos import ConfigError from vyos.config import Config from vyos.ifconfig import VTunIf +from vyos.util import process_running from vyos.validate import is_addr_assigned user = 'openvpn' @@ -977,17 +977,12 @@ def generate(openvpn): return None def apply(openvpn): - pid = 0 pidfile = '/var/run/openvpn/{}.pid'.format(openvpn['intf']) - if os.path.isfile(pidfile): - pid = 0 - with open(pidfile, 'r') as f: - pid = int(f.read()) # Always stop OpenVPN service. We can not send a SIGUSR1 for restart of the # service as the configuration is not re-read. Stop daemon only if it's # running - it could have died or killed by someone evil - if pid_exists(pid): + if process_running(pidfile): cmd = 'start-stop-daemon' cmd += ' --stop ' cmd += ' --quiet' diff --git a/src/conf_mode/interfaces-wireless.py b/src/conf_mode/interfaces-wireless.py index 454ad96dc..82a80d247 100755 --- a/src/conf_mode/interfaces-wireless.py +++ b/src/conf_mode/interfaces-wireless.py @@ -25,15 +25,15 @@ from grp import getgrnam from re import findall from subprocess import Popen, PIPE -from psutil import pid_exists from netifaces import interfaces from netaddr import * -from vyos.ifconfig import WiFiIf -from vyos.ifconfig_vlan import apply_vlan_config, verify_vlan_config +from vyos import ConfigError from vyos.configdict import list_diff, vlan_to_dict from vyos.config import Config -from vyos import ConfigError +from vyos.ifconfig import WiFiIf +from vyos.ifconfig_vlan import apply_vlan_config, verify_vlan_config +from vyos.util import process_running user = 'root' group = 'vyattacfg' @@ -1364,15 +1364,9 @@ def verify(wifi): return None def generate(wifi): - pid = 0 # always stop hostapd service first before reconfiguring it pidfile = get_pid('hostapd', wifi['intf']) - if os.path.isfile(pidfile): - pid = 0 - with open(pidfile, 'r') as f: - pid = int(f.read()) - - if pid_exists(pid): + if process_running(pidfile): cmd = 'start-stop-daemon' cmd += ' --stop ' cmd += ' --quiet' @@ -1382,12 +1376,7 @@ def generate(wifi): # always stop wpa_supplicant service first before reconfiguring it pidfile = get_pid('wpa_supplicant', wifi['intf']) - if os.path.isfile(pidfile): - pid = 0 - with open(pidfile, 'r') as f: - pid = int(f.read()) - - if pid_exists(pid): + if process_running(pidfile): cmd = 'start-stop-daemon' cmd += ' --stop ' cmd += ' --quiet' diff --git a/src/conf_mode/vpn_sstp.py b/src/conf_mode/vpn_sstp.py index 8e5c7587c..070437443 100755 --- a/src/conf_mode/vpn_sstp.py +++ b/src/conf_mode/vpn_sstp.py @@ -23,9 +23,9 @@ from subprocess import Popen, PIPE, check_output from socket import socket, AF_INET, SOCK_STREAM from copy import deepcopy from stat import S_IRUSR, S_IWUSR, S_IRGRP -from psutil import pid_exists from vyos.config import Config +from vyos.util import process_running from vyos import ConfigError pidfile = r'/var/run/accel_sstp.pid' @@ -489,14 +489,8 @@ def generate(sstp): return sstp def apply(sstp): - pid = 0 - if os.path.isfile(pidfile): - pid = 0 - with open(pidfile, 'r') as f: - pid = int(f.read()) - if sstp is None: - if pid_exists(pid): + if process_running(pidfile): cmd = 'start-stop-daemon' cmd += ' --stop ' cmd += ' --quiet' @@ -509,7 +503,7 @@ def apply(sstp): return None - if not pid_exists(pid): + if not process_running(pidfile): if os.path.exists(pidfile): os.remove(pidfile) diff --git a/src/op_mode/reset_openvpn.py b/src/op_mode/reset_openvpn.py index 176cd51cf..4c29fbbba 100755 --- a/src/op_mode/reset_openvpn.py +++ b/src/op_mode/reset_openvpn.py @@ -17,10 +17,10 @@ import sys import os -from psutil import pid_exists from subprocess import Popen, PIPE from time import sleep from netifaces import interfaces +from vyos.util import process_running def get_config_name(intf): cfg_file = r'/opt/vyatta/etc/openvpn/openvpn-{}.conf'.format(intf) @@ -42,18 +42,13 @@ if __name__ == '__main__': interface = sys.argv[1] if os.path.isfile(get_config_name(interface)): pidfile = '/var/run/openvpn/{}.pid'.format(interface) - if os.path.isfile(pidfile): - pid = 0 - with open(pidfile, 'r') as f: - pid = int(f.read()) - - if pid_exists(pid): - cmd = 'start-stop-daemon' - cmd += ' --stop' - cmd += ' --oknodo' - cmd += ' --quiet' - cmd += ' --pidfile ' + pidfile - subprocess_cmd(cmd) + if process_running(pidfile): + cmd = 'start-stop-daemon' + cmd += ' --stop' + cmd += ' --oknodo' + cmd += ' --quiet' + cmd += ' --pidfile ' + pidfile + subprocess_cmd(cmd) # When stopping OpenVPN we need to wait for the 'old' interface to # vanish from the Kernel, if it is not gone, OpenVPN will report: |