Skip to content

Commit

Permalink
services: refactor lease handling and dns along with it
Browse files Browse the repository at this point in the history
  • Loading branch information
fichtner committed Jul 24, 2016
1 parent 6bbfe4e commit 835ad1b
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 78 deletions.
1 change: 1 addition & 0 deletions src/etc/inc/interfaces.inc
Expand Up @@ -2952,6 +2952,7 @@ function interface_configure($interface = 'wan', $reloadall = false, $linkupeven
if ($interface == 'lan') {
/* make new hosts file */
system_hosts_generate();
services_dhcpleases_configure();
}

if ($reloadall == true) {
Expand Down
36 changes: 30 additions & 6 deletions src/etc/inc/services.inc
Expand Up @@ -386,6 +386,31 @@ function services_dhcpdv6_leasesfile()
return "{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases";
}

function services_dhcpleases_configure()
{
global $config, $g;

killbypid('/var/run/dhcpleases.pid', 'TERM', true);

if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcp'])) {
mwexec("/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db");
touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
if (isvalidpid('/var/run/dhcpleases.pid')) {
killbypid('/var/run/dhcpleases.pid', 'HUP');
} else {
mwexecf(
'/usr/local/sbin/dhcpleases -l %s -d %s -p %s -h %s',
array(
"{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases",
$config['system']['domain'],
'/var/run/dnsmasq.pid',
'/etc/hosts'
)
);
}
}
}

function services_dhcpd_configure($family = 'all', $blacklist = array())
{
global $g;
Expand Down Expand Up @@ -1081,8 +1106,8 @@ function services_dhcpdv6_configure($blacklist = array())
global $config, $g;

/* kill any running dhcpd */
killbypid("{$g['dhcpd_chroot_path']}/var/run/dhcpdv6.pid");
killbypid('/var/run/dhcpleases6.pid');
killbypid("{$g['dhcpd_chroot_path']}/var/run/dhcpdv6.pid", 'TERM', true);
killbypid('/var/run/dhcpleases6.pid', 'TERM', true);

/* DHCP enabled on any interfaces? */
if (!is_dhcpv6_server_enabled()) {
Expand Down Expand Up @@ -1966,7 +1991,7 @@ function services_dnsmasq_configure($dhcp_reload = true)
/* run dnsmasq */
$cmd = "/usr/local/sbin/dnsmasq --all-servers {$dns_rebind} {$args}";
mwexec_bg($cmd);
system_dhcpleases_configure();
services_dhcpleases_configure();
unset($args);

if (file_exists("/var/run/booting")) {
Expand All @@ -1978,7 +2003,7 @@ function services_dnsmasq_configure($dhcp_reload = true)
* XXX this is overly convoluted, potentiall
* restarting all of dhcp up to three times ;)
*/
if (!file_exists("/var/run/booting") && $dhcp_reload) {
if ($dhcp_reload) {
services_dhcpd_configure();
}
}
Expand All @@ -1995,7 +2020,6 @@ function services_unbound_configure($dhcp_reload = true)
}

sync_unbound_service();
system_dhcpleases_configure();

if (file_exists("/var/run/booting")) {
echo gettext("done.") . "\n";
Expand All @@ -2006,7 +2030,7 @@ function services_unbound_configure($dhcp_reload = true)
* XXX this is overly convoluted, potentially
* restarting all of dhcp up to three times ;)
*/
if (!file_exists("/var/run/booting") && $dhcp_reload) {
if ($dhcp_reload) {
services_dhcpd_configure();
}
}
Expand Down
41 changes: 1 addition & 40 deletions src/etc/inc/system.inc
Expand Up @@ -412,7 +412,7 @@ function system_hosts_generate()
* Do not remove this because dhcpleases monitors with kqueue
* it needs to be * killed before writing to hosts files.
*/
killbypid('/var/run/dhcpleases.pid');
killbypid('/var/run/dhcpleases.pid', 'TERM', true);

$fd = fopen('/etc/hosts', 'w');
if (!$fd) {
Expand All @@ -426,48 +426,9 @@ function system_hosts_generate()
unbound_hosts_generate();
}

system_dhcpleases_configure();

return 0;
}

function system_dhcpleases_configure()
{
global $config, $g;

/* Start the monitoring process for dynamic dhcpclients. */
if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcp'])) {
/* Make sure we do not error out */
mwexec("/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db");
if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases")) {
@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
}
if (isvalidpid('/var/run/dhcpleases.pid')) {
killbypid('/var/run/dhcpleases.pid', 'HUP');
} else {
/* To ensure we do not start multiple instances of dhcpleases, perform some clean-up first. */
killbyname('dhcpleases');
@unlink('/var/run/dhcpleases.pid');
if (isset($config['unbound']['enable'])) {
$dns_pid = 'unbound.pid';
} else {
$dns_pid = 'dnsmasq.pid';
}
mwexecf(
'/usr/local/sbin/dhcpleases -l %s -d %s -p %s -h %s',
array(
"{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases",
$config['system']['domain'],
"/var/run/{$dns_pid}",
'/etc/hosts'
)
);
}
} else {
killbypid('/var/run/dhcpleases.pid');
}
}

function system_hostname_configure()
{
global $config;
Expand Down
18 changes: 6 additions & 12 deletions src/etc/inc/xmlrpc/legacy.inc
Expand Up @@ -134,20 +134,14 @@ function filter_configure_xmlrpc()
filter_configure();
system_routing_configure();
setup_gateways_monitor();
relayd_configure();
openvpn_resync_all();
if (isset($config['dnsmasq']['enable'])) {
services_dnsmasq_configure();
} elseif (isset($config['unbound']['enable'])) {
services_unbound_configure();
} else {
# Both calls above run services_dhcpd_configure(), then we just
# need to call it when them are not called to avoid restart dhcpd
# twice, as described on ticket #3797
services_dhcpd_configure();
}
system_hosts_generate();
services_dhcpleases_configure();
local_sync_accounts();
services_dnsmasq_configure(false);
services_unbound_configure(false);
services_dhcpd_configure();
relayd_configure();
openvpn_resync_all();

return true;
}
Expand Down
7 changes: 3 additions & 4 deletions src/etc/rc.bootup
Expand Up @@ -125,7 +125,6 @@ require_once("interfaces.inc");
echo ".";
require_once("services.inc");
echo ".";
echo ".";
require_once("system.inc");
echo ".";
require_once("unbound.inc");
Expand Down Expand Up @@ -265,10 +264,10 @@ system_routing_configure();
system_routing_enable();

/* start dnsmasq service */
services_dnsmasq_configure();
services_dnsmasq_configure(false);

/* start unbound service */
services_unbound_configure();
services_unbound_configure(false);

/* Do an initial time sync */
echo "Starting NTP time client...";
Expand All @@ -282,7 +281,7 @@ relayd_configure();
services_dhcpd_configure();

/* start dhcpleases dhpcp hosts leases program */
system_dhcpleases_configure();
services_dhcpleases_configure();

/* start DHCP relay */
services_dhcrelay_configure();
Expand Down
1 change: 1 addition & 0 deletions src/etc/rc.newwanip
Expand Up @@ -141,6 +141,7 @@ if (!empty($bridgetmp)) {

/* make new hosts file */
system_hosts_generate();
services_dhcpleases_configure();

/* check tunneled IPv6 interface tracking */
if (isset($config['interfaces'][$interface]['ipaddrv6'])) {
Expand Down
1 change: 1 addition & 0 deletions src/etc/rc.reload_all
Expand Up @@ -49,6 +49,7 @@ system_timezone_configure();
system_firmware_configure();
system_hostname_configure();
system_hosts_generate();
services_dhcpleases_configure();
system_resolvconf_generate();
system_routing_enable();
interfaces_configure();
Expand Down
13 changes: 6 additions & 7 deletions src/www/services_dhcp.php
Expand Up @@ -110,17 +110,16 @@ function reconfigure_dhcpd()
killbyname("dhcpd");
dhcp_clean_leases();
system_hosts_generate();
/* dnsmasq_configure calls dhcpd_configure */
/* no need to restart dhcpd twice */
services_dhcpleases_configure();
if (isset($config['dnsmasq']['enable']) && isset($config['dnsmasq']['regdhcpstatic'])) {
services_dnsmasq_configure();
services_dnsmasq_configure(false);
clear_subsystem_dirty('hosts');
} elseif (isset($config['unbound']['enable']) && isset($config['unbound']['regdhcpstatic'])) {
services_unbound_configure();
}
if (isset($config['unbound']['enable']) && isset($config['unbound']['regdhcpstatic'])) {
services_unbound_configure(false);
clear_subsystem_dirty('unbound');
} else {
services_dhcpd_configure();
}
services_dhcpd_configure();

clear_subsystem_dirty('staticmaps');
}
Expand Down
1 change: 1 addition & 0 deletions src/www/services_dnsmasq.php
Expand Up @@ -118,6 +118,7 @@
/* Update resolv.conf in case the interface bindings exclude localhost. */
system_resolvconf_generate();
system_hosts_generate();
services_dhcpleases_configure();
services_dnsmasq_configure();
clear_subsystem_dirty('hosts');
header("Location: services_dnsmasq.php");
Expand Down
11 changes: 4 additions & 7 deletions src/www/system_advanced_admin.php
Expand Up @@ -232,13 +232,10 @@

system_console_configure();
system_hosts_generate();

// Restart DNS in case dns rebinding toggled
if (isset($config['dnsmasq']['enable'])) {
services_dnsmasq_configure();
} elseif (isset($config['unbound']['enable'])) {
services_unbound_configure();
}
services_dhcpleases_configure();
services_dnsmasq_configure(false);
services_unbound_configure(false);
services_dhcpd_configure();

if ($restart_sshd) {
configd_run('sshd restart', true);
Expand Down
6 changes: 4 additions & 2 deletions src/www/system_general.php
Expand Up @@ -245,9 +245,11 @@ function get_locale_list()
prefer_ipv4_or_ipv6();
system_hostname_configure();
system_hosts_generate();
services_dhcpleases_configure();
system_resolvconf_generate();
services_dnsmasq_configure();
services_unbound_configure();
services_dnsmasq_configure(false);
services_unbound_configure(false);
services_dhcpd_configure();
system_timezone_configure();

if ($olddnsallowoverride != $config['system']['dnsallowoverride']) {
Expand Down

0 comments on commit 835ad1b

Please sign in to comment.