Commit 0adea630 authored by Ad Schellevis's avatar Ad Schellevis

refactor configd calls, part of this discussion :...

refactor configd calls, part of this discussion : https://github.com/opnsense/core/commit/71ff705e01b0a8b95b4a41973b579a468d0a8c7c#commitcomment-10597568
parent 52f09b34
......@@ -120,8 +120,8 @@ function filter_configure()
* The reason is that rc.bootup calls filter_configure_sync directly which does this too.
*/
if (!file_exists("/var/run/booting")) {
send_event('filter reload');
send_event('filter sync restart');
configd_run('filter reload');
configd_run('filter sync restart');
}
}
......
......@@ -1262,7 +1262,7 @@ function interface_bring_down($interface = "wan", $destroy = false, $ifacecfg =
foreach ($ppps as $pppid => $ppp) {
if ($realif == $ppp['if']) {
if (isset($ppp['ondemand']) && !$destroy){
send_event("interface reconfigure {$interface}");
configd_run("interface reconfigure {$interface}");
break;
}
if (file_exists("{$g['varrun_path']}/{$ppp['type']}_{$interface}.pid")) {
......@@ -3169,7 +3169,7 @@ function interface_configure($interface = "wan", $reloadall = false, $linkupeven
services_unbound_configure();
/* update dyndns */
send_event("service reload dyndns {$interface}");
configd_run("service reload dyndns {$interface}");
/* XXX: which CPZONE? Needed? */
/* reload captive portal */
......
......@@ -843,7 +843,7 @@ function openvpn_restart($mode, $settings) {
mwexec_bg("/usr/local/sbin/openvpn --config " . escapeshellarg($fpath));
if (!file_exists("/var/run/booting"))
send_event("filter reload");
configd_run("filter reload");
}
function openvpn_delete($mode, & $settings) {
......
......@@ -607,7 +607,7 @@ function reload_interfaces_sync() {
* none
******/
function reload_all() {
send_event("service reload all");
configd_run("service reload all");
}
/****f* legacy/reload_interfaces
......@@ -619,7 +619,7 @@ function reload_all() {
* none
******/
function reload_interfaces() {
send_event("interface all reload");
configd_run("interface all reload");
}
/****f* legacy/reload_all_sync
......
......@@ -379,7 +379,7 @@ if (!function_exists('service_control_start')) {
vpn_ipsec_force_reload();
break;
case 'sshd':
send_event("service restart sshd");
configd_run("service restart sshd");
break;
case 'openvpn':
$vpnmode = isset($extras['vpnmode']) ? htmlspecialchars($extras['vpnmode']) : htmlspecialchars($extras['mode']);
......@@ -523,7 +523,7 @@ if (!function_exists('service_control_restart')) {
vpn_ipsec_force_reload();
break;
case 'sshd':
send_event("service restart sshd");
configd_run("service restart sshd");
break;
case 'openvpn':
$vpnmode = htmlspecialchars($extras['vpnmode']);
......
......@@ -142,18 +142,11 @@ function unlock($cfglckkey = 0)
fclose($cfglckkey);
}
function send_event($cmd)
function configd_run($cmd, $detach = false)
{
require_once("script/load_phalcon.php");
$backend = new OPNsense\Core\Backend();
return $backend->sendEvent("{$cmd}");
}
function send_background_event($cmd)
{
require_once("script/load_phalcon.php");
$backend = new OPNsense\Core\Backend();
return $backend->sendBackgroundEvent("{$cmd}");
return $backend->configdRun("{$cmd}", $detach);
}
function is_module_loaded($module_name) {
......@@ -1462,7 +1455,7 @@ function is_interface_mismatch()
/* sync carp entries to other firewalls */
function carp_sync_client()
{
send_event('filter sync load');
configd_run('filter sync load');
}
/****f* util/isAjax
......
......@@ -194,7 +194,7 @@ interfaces_loopback_configure();
system_syslogd_start();
echo "Starting Secure Shell Services...";
send_event("service reload sshd");
configd_run("service reload sshd");
echo "done.\n";
/* setup polling */
......@@ -286,7 +286,7 @@ services_dhcrelay_configure();
services_dhcrelay6_configure();
/* dyndns service updates */
send_event("service reload dyndnsall");
configd_run("service reload dyndnsall");
/* Run a filter configure now that most all services have started */
filter_configure_sync();
......
......@@ -60,7 +60,7 @@ function handle_argument_group($iface, $argument2) {
interfaces_bring_up($iface);
/* NOTE: Do not generate event for OpenVPN since the daemon does that for us. */
if (($argument2 == "start" || $argument2 == "up") && substr($iface, 0, 4) != "ovpn")
send_event("interface newip {$iface}");
configd_run("interface newip {$iface}");
} else {
switch ($argument2) {
case "stop":
......
......@@ -52,7 +52,7 @@ function restart_packages() {
system_ntp_configure(false);
mwexec_bg("/usr/local/sbin/ntpdate_sync_once.sh", false, true);
log_error("{$g['product_name']} package system has detected an ip change $oldip -> $curwanip ... Restarting packages.");
send_event("service reload packages");
configd_run("service reload packages");
}
/* Interface IP address has changed */
......@@ -94,7 +94,7 @@ if ($curwanip == "0.0.0.0" || !is_ipaddr($curwanip)) {
if (substr($interface_real, 0, 4) != "ovpn") {
if (!empty($config['interfaces'][$interface]['ipaddr'])) {
log_error("rc.newwanip: Failed to update {$interface} IP, restarting...");
send_event("interface reconfigure {$interface}");
configd_run("interface reconfigure {$interface}");
return;
}
}
......
......@@ -48,7 +48,7 @@ function restart_packages() {
system_ntp_configure(false);
mwexec_bg("/usr/local/sbin/ntpdate_sync_once.sh", false, true);
log_error("{$g['product_name']} package system has detected an ip change $oldipv6 -> $curwanipv6 ... Restarting packages.");
send_event("service reload packages");
configd_run("service reload packages");
}
/* Interface IP address has changed */
......@@ -84,7 +84,7 @@ if (file_exists("/var/run/booting") && $config['interfaces'][$interface]['ipaddr
*/
if ((empty($curwanipv6) || !is_ipaddrv6($curwanipv6)) && substr($interface_real, 0, 4) != "ovpn") {
log_error("rc.newwanipv6: Failed to update {$interface_descr}[{$interface}] IPv6, restarting...");
// send_event("interface reconfigure {$interface}");
// configd_run("interface reconfigure {$interface}");
return;
}
......
......@@ -7,6 +7,11 @@ require_once('shaper.inc');
require_once('captiveportal.inc');
require_once('rrd.inc');
if (count($argv) > 1 && $argv[1] == 'delayed') {
// starting delayed.
sleep(2);
}
echo 'Restarting webConfigurator...';
killbyname('lighttpd');
......
......@@ -32,5 +32,5 @@ require_once("functions.inc");
if (update_alias_url_data()) {
write_config();
send_event("filter reload");
configd_run("filter reload");
}
......@@ -54,5 +54,5 @@ if (count($todo) > 0) {
}
if ($filter_reload)
send_event("filter reload");
configd_run("filter reload");
}
......@@ -45,7 +45,7 @@ class ServiceController extends ApiControllerBase
public function startAction()
{
$backend = new Backend();
$response = $backend->sendEvent("service start proxy");
$response = $backend->configdRun("service start proxy");
return array("response" => $response);
}
......@@ -56,7 +56,7 @@ class ServiceController extends ApiControllerBase
public function stopAction()
{
$backend = new Backend();
$response = $backend->sendEvent("service stop proxy");
$response = $backend->configdRun("service stop proxy");
return array("response" => $response);
}
......@@ -67,7 +67,7 @@ class ServiceController extends ApiControllerBase
public function restartAction()
{
$backend = new Backend();
$response = $backend->sendEvent("service restart proxy");
$response = $backend->configdRun("service restart proxy");
return array("response" => $response);
}
......@@ -79,7 +79,7 @@ class ServiceController extends ApiControllerBase
public function statusAction()
{
$backend = new Backend();
$response = $backend->sendEvent("service status proxy");
$response = $backend->configdRun("service status proxy");
if (strpos($response, "not running") > 0) {
$status = "stopped";
......@@ -112,12 +112,12 @@ class ServiceController extends ApiControllerBase
}
// generate template
$backend->sendEvent("template reload OPNsense.Proxy");
$backend->configdRun("template reload OPNsense.Proxy");
// (res)start daemon
if ($mdlProxy->general->enabled->__toString() == 1) {
if ($runStatus['status'] == "running") {
$backend->sendEvent("service reconfigure proxy");
$backend->configdRun("service reconfigure proxy");
} else {
$this->startAction();
}
......
......@@ -50,11 +50,12 @@ class Backend
/**
* send event to backend
* @param string $event event string
* @param bool $detach detach process
* @param int $timeout timeout in seconds
* @return string
* @throws \Exception
*/
public function sendEvent($event, $timeout = 120)
public function configdRun($event, $detach = false, $timeout = 120)
{
$endOfStream = chr(0).chr(0).chr(0);
$poll_timeout = 2 ; // poll timeout interval
......@@ -67,7 +68,11 @@ class Backend
stream_set_timeout($stream, $poll_timeout);
// send command
fwrite($stream, $event);
if ($detach) {
fwrite($stream, "&".$event);
} else {
fwrite($stream, $event);
}
// read response data
$starttime = time() ;
......@@ -89,15 +94,5 @@ class Backend
return $resp;
}
/**
* send event to backend, but prefix & to force it to run in background
* @param string $event event string
* @param int $timeout timeout in seconds
* @return string (message uuid from configd)
* @throws \Exception
*/
public function sendBackgroundEvent($event, $timeout = 120)
{
return $this->sendEvent('&'.$event, $timeout);
}
}
......@@ -4,18 +4,6 @@ parameters:
type:script
message:Reloading filter
[reconfigure]
command:/usr/local/etc/rc.filter_configure_sync
parameters:
type:script
message:Reloading filter
[restart]
command:/usr/local/etc/rc.filter_configure_sync
parameters:
type:script
message:Reloading filter
[sync]
command:/usr/local/etc/rc.filter_synchronize
parameters:%s
......
[reload|restart]
command:/usr/local/etc/rc.restart_webgui
parameters:%s
type:script
message:webConfigurator restart in progress
......@@ -52,8 +52,3 @@ parameters:
type:script
message:starting sshd
[reload|restart.webgui]
command:sleep 2;/usr/local/etc/rc.restart_webgui
parameters:
type:script
message:webConfigurator restart in progress
......@@ -361,7 +361,7 @@ class Action(object):
if self.message.count('%s') > 0 and parameters is not None and len(parameters) > 0:
log_message = log_message + self.message % tuple(parameters[0:self.message.count('%s')])
else:
log_message = log_message + self.message
log_message = log_message + self.message.replace("%s", "")
syslog.syslog(syslog.LOG_NOTICE, log_message)
# validate input
......@@ -377,9 +377,9 @@ class Action(object):
# build script command to execute, shared for both types
script_command = self.command
if self.parameters is not None and type(self.parameters) == str:
if self.parameters is not None and type(self.parameters) == str and len(parameters) > 0:
script_command = '%s %s' % (script_command, self.parameters)
if script_command.find('%s') > -1 and len(parameters) > 0:
if script_command.find('%s') > -1:
# use command execution parameters in action parameter template
# use quotes on parameters to prevent code injection
script_command = script_command % tuple(map(lambda x: '"'+x.replace('"', '\\"')+'"',
......
......@@ -160,7 +160,7 @@ if ($_POST['resetlogs'] == gettext("Reset Log Files")) {
ob_flush();
flush();
log_error(gettext("webConfigurator configuration has changed. Restarting webConfigurator."));
send_background_event("service restart webgui");
configd_run("webgui restart delayed", true);
$savemsg .= "<br />" . gettext("WebGUI process is restarting.");
}
......
......@@ -41,13 +41,13 @@ if($_GET['getstatus']) {
exit;
}
if($_POST['reloadfilter']) {
send_event("filter reload");
send_event("filter sync reload");
configd_run("filter reload");
configd_run("filter sync reload");
header("Location: status_filter_reload.php");
exit;
}
if($_POST['syncfilter']) {
send_event("filter sync");
configd_run("filter sync");
header("Location: status_filter_reload.php");
exit;
}
......
......@@ -257,7 +257,7 @@ if ($_POST) {
$retval |= system_ntp_configure();
if ($olddnsallowoverride != $config['system']['dnsallowoverride']) {
send_event("service reload dns");
configd_run("service reload dns");
}
// Reload the filter - plugins might need to be run.
......
......@@ -608,14 +608,14 @@ if ($restart_sshd) {
if ($config['system']['ssh']['enabled']) {
log_error(gettext("secure shell configuration has changed. Restarting sshd."));
send_event("service restart sshd");
configd_run("service restart sshd");
}
}
if ($restart_webgui) {
ob_flush();
flush();
log_error(gettext("webConfigurator configuration has changed. Restarting webConfigurator."));
send_background_event("service restart webgui");
configd_run("webgui restart delayed", true);
}
?>
......@@ -59,9 +59,9 @@ if ($_POST) {
$retval = system_routing_configure();
send_event('service reload dyndnsall');
send_event('service reload ipsecdns');
send_event('filter reload');
configd_run('service reload dyndnsall');
configd_run('service reload ipsecdns');
configd_run('filter reload');
/* reconfigure our gateway monitor */
setup_gateways_monitor();
......
......@@ -489,7 +489,7 @@ if ($_POST) {
echo $_POST['name'];
exit;
} else if (!empty($reloadif))
send_event("interface reconfigure {$reloadif}");
configd_run("interface reconfigure {$reloadif}");
header("Location: system_gateways.php");
exit;
......
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