From 705c8ec9fc82f3d6d9fd491278c2789b92b5be32 Mon Sep 17 00:00:00 2001 From: Matthew Grooms Date: Sun, 7 Sep 2008 06:43:30 +0000 Subject: When restarting an OpenVPN process, don't send a term signal and expect it to exit within a fixed time frame of two seconds. The old process may take longer to exit and cause the new process creation to fail. Instead, check the process status every 1/4 seconds and only continue once it terminates. --- etc/inc/openvpn.inc | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'etc') diff --git a/etc/inc/openvpn.inc b/etc/inc/openvpn.inc index abc2337..e53aa74 100644 --- a/etc/inc/openvpn.inc +++ b/etc/inc/openvpn.inc @@ -482,13 +482,22 @@ function openvpn_restart($mode, & $settings) { $vpnid = $settings['vpnid']; $mode_id = $mode.$vpnid; - $pidfile = $g['varrun_path']."/openvpn_{$mode_id}.pid"; - killbypid($pidfile); - sleep(2); + /* read the pid file */ + $pfile = $g['varrun_path']."/openvpn_{$mode_id}.pid"; + $pid = rtrim(file_get_contents($pfile)); + unlink($pfile); + + /* send the process a term signal */ + posix_kill($pid, SIGTERM); + + /* wait until the process exits */ + while(posix_kill($pid, 0)) + usleep(250000); if ($settings['disable']) return; + /* start the new process */ $fpath = $g['varetc_path']."/openvpn/{$mode_id}.conf"; mwexec_bg("nohup openvpn --config {$fpath}"); touch("{$g['tmp_path']}/filter_dirty"); @@ -506,11 +515,14 @@ function openvpn_delete($mode, & $settings) { else $devname = "ovpnc{$vpnid}"; - /* kill the process */ + /* read the pid file */ $pfile = "{$g['varrun_path']}/openvpn_{$mode_id}.pid"; - killbypid($pfile); + $pid = trim(file_get_contents($pfile)); unlink($pfile); + /* send the process a term signal */ + posix_kill($pid, SIGTERM); + /* remove the device from the openvpn group */ mwexec("/sbin/ifconfig {$devname} -group openvpn"); -- cgit v1.1