dnsmasq: watch the hosts directory with --hostsdir

dnsmasq reads the hosts files in /tmp/hosts (its own per-instance files
and odhcpd's) via --addn-hosts, which only rereads them on SIGHUP. So
odhcpd's leasetrigger, odhcpd-update, sends SIGHUP to every dnsmasq
instance whenever its leases change, which also clears the whole DNS
cache and rereads all other hosts files. That spams the log (#6454),
has been suspected of leaking memory (#21729) and is slow with large
hosts files such as block lists since dnsmasq 2.93 (#25826); see also
#8271.

Use --hostsdir instead: dnsmasq then watches the directory with inotify
and only rereads the file that changed. odhcpd replaces its hosts files
by renaming a dotfile, which dnsmasq ignores, over them.

This was done before in commit a03035dad1 ("dnsmasq: use "hostsdir"
instead of "addn-hosts"") and reverted in commit bcf20e0583, because
the directory is shared by all instances and odhcpd still sent SIGHUP.

All instances have read the whole directory since commit 942904f7b9
("dnsmasq: Specify directory /tmp/hosts as argument for --addn-hosts"),
so that they get odhcpd's names, and instances can opt out with
ignore_hosts_dir since commit e8a5670122 ("dnsmasq: add ignore hosts
dir to dnsmasq init script"). This is unchanged here, including that
names leak between instances which don't opt out (#6960, #9145). What
made the sharing a problem for --hostsdir is that the temporary hosts
file of an instance is in the same directory: the other instances read
it while it is written and keep its names after it is renamed (dnsmasq
does not handle IN_MOVED_FROM) until they get a SIGHUP. Make it a
dotfile as well.

The SIGHUP was needed then, as dnsmasq only removes the names of
changed or deleted files in a hosts directory since version 2.88. It is
now redundant and odhcpd-update can be dropped in a follow-up.

Addnhosts entries that are directories would lose their names when a
file in the hosts directory changes, due to a dnsmasq bug fixed by the
previous commit.

Signed-off-by: David Härdeman <david@hardeman.nu>
Link: https://github.com/openwrt/openwrt/pull/26658
Signed-off-by: Robert Marko <robimarko@gmail.com>
This commit is contained in:
David Härdeman authored and Robert Marko committed 2026-11-29 11:35:20 +01:00
1 parent d61504b463
commit 7e7770d865
2 files changed
+4 -3

No files matched your search

+1 -1
View File
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=dnsmasq
PKG_UPSTREAM_VERSION:=2.93
PKG_VERSION:=$(subst test,~~test,$(subst rc,~rc,$(PKG_UPSTREAM_VERSION)))
PKG_RELEASE:=15
PKG_RELEASE:=16
PKG_SOURCE:=$(PKG_NAME)-$(PKG_UPSTREAM_VERSION).tar.xz
PKG_SOURCE_URL:=https://thekelleys.org.uk/dnsmasq/
@@ -931,8 +931,9 @@ dnsmasq_start()
CONFIGFILE="${BASECONFIGFILE}.${cfg}"
CONFIGFILE_TMP="${CONFIGFILE}.$$"
HOSTFILE="${BASEHOSTFILE}.${cfg}"
HOSTFILE_TMP="${HOSTFILE}.$$"
HOSTFILE_DIR="$(dirname "$HOSTFILE")"
# a dotfile, which is ignored by the dnsmasq instances watching HOSTFILE_DIR
HOSTFILE_TMP="${HOSTFILE_DIR}/.$(basename "$HOSTFILE").$$"
BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
# before we can call xappend
@@ -1073,7 +1074,7 @@ dnsmasq_start()
xappend "--addn-hosts=$HOSTFILE"
append EXTRA_MOUNT "$HOSTFILE"
else
xappend "--addn-hosts=$HOSTFILE_DIR"
xappend "--hostsdir=$HOSTFILE_DIR"
append EXTRA_MOUNT "$HOSTFILE_DIR"
fi
config_list_foreach "$cfg" "addnhosts" append_addnhosts