From 7e7770d865450d7d4124e4599eefae9231ca62e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20H=C3=A4rdeman?= Date: Thu, 12 Nov 2026 04:15:31 +0000 Subject: [PATCH] dnsmasq: watch the hosts directory with --hostsdir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 a03035dad198 ("dnsmasq: use "hostsdir" instead of "addn-hosts"") and reverted in commit bcf20e05832a, because the directory is shared by all instances and odhcpd still sent SIGHUP. All instances have read the whole directory since commit 942904f7b936 ("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 e8a5670122e0 ("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 Link: https://github.com/openwrt/openwrt/pull/26658 Signed-off-by: Robert Marko --- package/network/services/dnsmasq/Makefile | 2 +- package/network/services/dnsmasq/files/dnsmasq.init | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package/network/services/dnsmasq/Makefile b/package/network/services/dnsmasq/Makefile index edad9c99076..3bb9dea6e8a 100644 --- a/package/network/services/dnsmasq/Makefile +++ b/package/network/services/dnsmasq/Makefile @@ -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/ diff --git a/package/network/services/dnsmasq/files/dnsmasq.init b/package/network/services/dnsmasq/files/dnsmasq.init index ae3d3487a1c..93b590c0e9b 100755 --- a/package/network/services/dnsmasq/files/dnsmasq.init +++ b/package/network/services/dnsmasq/files/dnsmasq.init @@ -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