From 35e0254622f8f099db3c7829cd61b85f6976c416 Mon Sep 17 00:00:00 2001 From: Christian Poessinger Date: Sun, 29 Mar 2020 15:46:12 +0200 Subject: 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(). --- src/op_mode/reset_openvpn.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) (limited to 'src/op_mode') 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: -- cgit v1.2.3