Commit 2ddb95f7 authored by Franco Fichtner's avatar Franco Fichtner

inc: align service configure

o Add $waitforit flag to killbypid() and use it to simplify code.

o When requesting a configure via id, do not exclude the service
  because it is disabled.  The pidfile needs to be killed still.

o Unwind the convoluted code paths in the VPN plugin during early
  configure: kill service, probe for active, then do work.
parent 6a6f7f3b
...@@ -489,7 +489,7 @@ function ipsec_configure() ...@@ -489,7 +489,7 @@ function ipsec_configure()
@mkdir('/usr/local/etc/ipsec.d/reqs'); @mkdir('/usr/local/etc/ipsec.d/reqs');
if (file_exists("/var/run/booting")) { if (file_exists("/var/run/booting")) {
echo gettext("Configuring IPsec VPN... "); echo gettext("Configuring IPsec VPN...");
} }
/* fastforwarding is not compatible with ipsec tunnels */ /* fastforwarding is not compatible with ipsec tunnels */
......
...@@ -815,9 +815,6 @@ function openvpn_restart_by_id($mode, $id) ...@@ -815,9 +815,6 @@ function openvpn_restart_by_id($mode, $id)
if (isset($config['openvpn']["openvpn-$mode"])) { if (isset($config['openvpn']["openvpn-$mode"])) {
foreach ($config['openvpn']["openvpn-$mode"] as $settings) { foreach ($config['openvpn']["openvpn-$mode"] as $settings) {
if (isset($settings['disable'])) {
continue;
}
if ($id != 0 && $id == $settings['vpnid']) { if ($id != 0 && $id == $settings['vpnid']) {
$found = $settings; $found = $settings;
break; break;
...@@ -839,12 +836,7 @@ function openvpn_restart($mode, $settings) ...@@ -839,12 +836,7 @@ function openvpn_restart($mode, $settings)
$vpnid = $settings['vpnid']; $vpnid = $settings['vpnid'];
$mode_id = $mode.$vpnid; $mode_id = $mode.$vpnid;
/* kill the process if running */ killbypid("/var/run/openvpn_{$mode_id}.pid", 'TERM', true);
$pfile = "/var/run/openvpn_{$mode_id}.pid";
killbypid($pfile);
while (isvalidpid($pfile)) {
usleep(250000);
}
if (isset($settings['disable'])) { if (isset($settings['disable'])) {
return; return;
......
...@@ -110,29 +110,21 @@ function vpn_pptpd_configure() ...@@ -110,29 +110,21 @@ function vpn_pptpd_configure()
$syscfg = $config['system']; $syscfg = $config['system'];
$pptpdcfg = $config['pptpd']; $pptpdcfg = $config['pptpd'];
if (file_exists("/var/run/booting")) { killbypid('/var/run/pptp-vpn.pid', 'TERM', true);
if (!$pptpdcfg['mode'] || ($pptpdcfg['mode'] == "off"))
return 0;
echo gettext("Configuring PPTP VPN service... ");
} else {
/* kill mpd */
killbypid('/var/run/pptp-vpn.pid');
/* wait for process to die */
sleep(3);
if (is_process_running("mpd -b")) { if (!isset($pptpdcfg['mode']) || $pptpdcfg['mode'] == 'off') {
killbypid('/var/run/pptp-vpn.pid'); return 0;
log_error(gettext("Could not kill mpd within 3 seconds. Trying again.")); }
}
/* remove mpd.conf, if it exists */ if (file_exists('/var/run/booting')) {
@unlink('/var/etc/pptp-vpn/mpd.conf'); echo gettext("Configuring PPTP VPN service...");
@unlink('/var/etc/pptp-vpn/mpd.links');
@unlink('/var/etc/pptp-vpn/mpd.secret');
} }
/* remove mpd.conf, if it exists */
@unlink('/var/etc/pptp-vpn/mpd.conf');
@unlink('/var/etc/pptp-vpn/mpd.links');
@unlink('/var/etc/pptp-vpn/mpd.secret');
if (empty($pptpdcfg['n_pptp_units'])) { if (empty($pptpdcfg['n_pptp_units'])) {
log_error("Something wrong in the PPTPd configuration. Preventing starting the daemon because issues would arise."); log_error("Something wrong in the PPTPd configuration. Preventing starting the daemon because issues would arise.");
return; return;
...@@ -320,8 +312,9 @@ EOD; ...@@ -320,8 +312,9 @@ EOD;
break; break;
} }
if (file_exists("/var/run/booting")) if (file_exists('/var/run/booting')) {
echo "done\n"; echo gettext("done") . "\n";
}
return 0; return 0;
} }
...@@ -345,9 +338,6 @@ function vpn_pppoe_configure_by_id($id) ...@@ -345,9 +338,6 @@ function vpn_pppoe_configure_by_id($id)
if (isset($config['pppoes']['pppoe'])) { if (isset($config['pppoes']['pppoe'])) {
foreach ($config['pppoes']['pppoe'] as $pppoe) { foreach ($config['pppoes']['pppoe'] as $pppoe) {
if (!isset($pppoe['mode']) || $pppoe['mode'] == 'off') {
continue;
}
if ($id != 0 && $id == $pppoe['pppoeid']) { if ($id != 0 && $id == $pppoe['pppoeid']) {
$found = $pppoe; $found = $pppoe;
break; break;
...@@ -368,27 +358,23 @@ function vpn_pppoe_configure(&$pppoecfg) ...@@ -368,27 +358,23 @@ function vpn_pppoe_configure(&$pppoecfg)
$syscfg = $config['system']; $syscfg = $config['system'];
/* create directory if it does not exist */ killbypid("/var/run/pppoe{$pppoecfg['pppoeid']}-vpn.pid", 'TERM', true);
@mkdir("/var/etc/pppoe{$pppoecfg['pppoeid']}-vpn");
if (file_exists("/var/run/booting")) { if (!isset($pppoecfg['mode']) || $pppoecfg['mode'] == 'off') {
if (!$pppoecfg['mode'] || ($pppoecfg['mode'] == "off"))
return 0; return 0;
}
echo gettext("Configuring PPPoE VPN service... "); if (file_exists('/var/run/booting')) {
} else { echo gettext("Configuring PPPoE VPN service...");
/* kill mpd */
killbypid("/var/run/pppoe{$pppoecfg['pppoeid']}-vpn.pid");
/* wait for process to die */
sleep(2);
} }
switch ($pppoecfg['mode']) { switch ($pppoecfg['mode']) {
case 'server' : case 'server' :
/* create directory if it does not exist */
@mkdir("/var/etc/pppoe{$pppoecfg['pppoeid']}-vpn");
$pppoe_interface = get_real_interface($pppoecfg['interface']); $pppoe_interface = get_real_interface($pppoecfg['interface']);
if ($pppoecfg['paporchap'] == "chap") if ($pppoecfg['paporchap'] == "chap")
...@@ -555,8 +541,6 @@ EOD; ...@@ -555,8 +541,6 @@ EOD;
chmod("/var/etc/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", 0600); chmod("/var/etc/pppoe{$pppoecfg['pppoeid']}-vpn/mpd.secret", 0600);
} }
killbypid("/var/run/pppoe{$pppoecfg['pppoeid']}-vpn.pid");
legacy_netgraph_attach($pppoe_interface); legacy_netgraph_attach($pppoe_interface);
mwexec("/usr/local/sbin/mpd4 -b -d /var/etc/pppoe{$pppoecfg['pppoeid']}-vpn -p /var/run/pppoe{$pppoecfg['pppoeid']}-vpn.pid -s poes poes"); mwexec("/usr/local/sbin/mpd4 -b -d /var/etc/pppoe{$pppoecfg['pppoeid']}-vpn -p /var/run/pppoe{$pppoecfg['pppoeid']}-vpn.pid -s poes poes");
...@@ -564,8 +548,9 @@ EOD; ...@@ -564,8 +548,9 @@ EOD;
break; break;
} }
if (file_exists("/var/run/booting")) if (file_exists('/var/run/booting')) {
echo gettext("done") . "\n"; echo gettext("done") . "\n";
}
return 0; return 0;
} }
...@@ -574,6 +559,8 @@ function vpn_l2tp_configure() ...@@ -574,6 +559,8 @@ function vpn_l2tp_configure()
{ {
global $config; global $config;
killbypid('/var/run/l2tp-vpn.pid', 'TERM', true);
$syscfg = $config['system']; $syscfg = $config['system'];
if (isset($config['l2tp'])) { if (isset($config['l2tp'])) {
$l2tpcfg = $config['l2tp']; $l2tpcfg = $config['l2tp'];
...@@ -581,16 +568,12 @@ function vpn_l2tp_configure() ...@@ -581,16 +568,12 @@ function vpn_l2tp_configure()
return 0; return 0;
} }
if (file_exists("/var/run/booting")) { if (!isset($l2tpcfg['mode']) || $l2tpcfg['mode'] == 'off') {
if (!isset($l2tpcfg['mode']) || $l2tpcfg['mode'] == "off") return 0;
return 0; }
echo gettext("Configuring l2tp VPN service... "); if (file_exists('/var/run/booting')) {
} else { echo gettext('Configuring L2TP VPN service...');
while (isvalidpid('/var/run/l2tp-vpn.pid')) {
killbypid('/var/run/l2tp-vpn.pid');
usleep(250 * 1000);
}
} }
@mkdir('/var/etc/l2tp-vpn'); @mkdir('/var/etc/l2tp-vpn');
...@@ -757,8 +740,9 @@ EOD; ...@@ -757,8 +740,9 @@ EOD;
break; break;
} }
if (file_exists("/var/run/booting")) if (file_exists('/var/run/booting')) {
echo "done\n"; echo gettext("done") . "\n";
}
return 0; return 0;
} }
...@@ -1474,10 +1474,8 @@ function system_ntp_configure($start_ntpd = true) ...@@ -1474,10 +1474,8 @@ function system_ntp_configure($start_ntpd = true)
{ {
global $config; global $config;
/* if ntpd is running, kill it when restart is not delayed */ if ($start_ntpd) {
while ($start_ntpd && isvalidpid('/var/run/ntpd.pid')) { killbypid('/var/run/ntpd.pid', 'TERM', true);
killbypid('/var/run/ntpd.pid');
usleep(200 * 1000);
} }
if (!isset($config['system']['timeservers'])) { if (!isset($config['system']['timeservers'])) {
......
...@@ -37,13 +37,21 @@ function killbyname($procname, $sig = 'TERM') ...@@ -37,13 +37,21 @@ function killbyname($procname, $sig = 'TERM')
mwexecf('/bin/pkill -%s %s', array($sig, $procname)); mwexecf('/bin/pkill -%s %s', array($sig, $procname));
} }
function killbypid($pidfile, $sig = 'TERM') function killbypid($pidfile, $sig = 'TERM', $waitforit = false)
{ {
if (!isvalidpid($pidfile)) { if (!isvalidpid($pidfile)) {
return; return;
} }
mwexecf('/bin/pkill -%s -F %s', array($sig, $pidfile)); mwexecf('/bin/pkill -%s -F %s', array($sig, $pidfile));
if (!$waitforit) {
return;
}
while (isvalidpid($pidfile)) {
usleep(200 * 1000);
}
} }
function isvalidpid($pidfile) function isvalidpid($pidfile)
......
...@@ -129,7 +129,7 @@ function service_control_stop($name, $extras) ...@@ -129,7 +129,7 @@ function service_control_stop($name, $extras)
mwexec($cmd); mwexec($cmd);
} }
} elseif (isset($service['pidfile'])) { } elseif (isset($service['pidfile'])) {
killbypid($service['pidfile']); killbypid($service['pidfile'], 'TERM', true);
} else { } else {
/* last resort, but not very elegant */ /* last resort, but not very elegant */
killbyname($service['name']); killbyname($service['name']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment