mirror of
https://github.com/pi-hole/dnsmasq.git
synced 2025-12-19 10:18:25 +00:00
Use inotify instead of polling on Linux.
This should solve problems people are seeing when a file changes twice within a second and thus is missed for polling.
This commit is contained in:
2
Makefile
2
Makefile
@@ -69,7 +69,7 @@ objs = cache.o rfc1035.o util.o option.o forward.o network.o \
|
|||||||
dnsmasq.o dhcp.o lease.o rfc2131.o netlink.o dbus.o bpf.o \
|
dnsmasq.o dhcp.o lease.o rfc2131.o netlink.o dbus.o bpf.o \
|
||||||
helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
|
helper.o tftp.o log.o conntrack.o dhcp6.o rfc3315.o \
|
||||||
dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
|
dhcp-common.o outpacket.o radv.o slaac.o auth.o ipset.o \
|
||||||
domain.o dnssec.o blockdata.o tables.o loop.o
|
domain.o dnssec.o blockdata.o tables.o loop.o inotify.o
|
||||||
|
|
||||||
hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
|
hdrs = dnsmasq.h config.h dhcp-protocol.h dhcp6-protocol.h \
|
||||||
dns-protocol.h radv-protocol.h ip6addr.h
|
dns-protocol.h radv-protocol.h ip6addr.h
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ LOCAL_SRC_FILES := bpf.c cache.c dbus.c dhcp.c dnsmasq.c \
|
|||||||
dhcp6.c rfc3315.c dhcp-common.c outpacket.c \
|
dhcp6.c rfc3315.c dhcp-common.c outpacket.c \
|
||||||
radv.c slaac.c auth.c ipset.c domain.c \
|
radv.c slaac.c auth.c ipset.c domain.c \
|
||||||
dnssec.c dnssec-openssl.c blockdata.c tables.c \
|
dnssec.c dnssec-openssl.c blockdata.c tables.c \
|
||||||
loop.c
|
loop.c inotify.c
|
||||||
|
|
||||||
LOCAL_MODULE := dnsmasq
|
LOCAL_MODULE := dnsmasq
|
||||||
|
|
||||||
|
|||||||
@@ -315,9 +315,15 @@ int main (int argc, char **argv)
|
|||||||
if (daemon->port != 0)
|
if (daemon->port != 0)
|
||||||
{
|
{
|
||||||
cache_init();
|
cache_init();
|
||||||
|
|
||||||
#ifdef HAVE_DNSSEC
|
#ifdef HAVE_DNSSEC
|
||||||
blockdata_init();
|
blockdata_init();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
if (!option_bool(OPT_NO_POLL))
|
||||||
|
inotify_dnsmasq_init();
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
if (option_bool(OPT_DBUS))
|
if (option_bool(OPT_DBUS))
|
||||||
@@ -793,6 +799,11 @@ int main (int argc, char **argv)
|
|||||||
|
|
||||||
pid = getpid();
|
pid = getpid();
|
||||||
|
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
/* Using inotify, have to select a resolv file at startup */
|
||||||
|
poll_resolv(1, 0, now);
|
||||||
|
#endif
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
int maxfd = -1;
|
int maxfd = -1;
|
||||||
@@ -862,11 +873,16 @@ int main (int argc, char **argv)
|
|||||||
#if defined(HAVE_LINUX_NETWORK)
|
#if defined(HAVE_LINUX_NETWORK)
|
||||||
FD_SET(daemon->netlinkfd, &rset);
|
FD_SET(daemon->netlinkfd, &rset);
|
||||||
bump_maxfd(daemon->netlinkfd, &maxfd);
|
bump_maxfd(daemon->netlinkfd, &maxfd);
|
||||||
|
if (daemon->port != 0 && !option_bool(OPT_NO_POLL))
|
||||||
|
{
|
||||||
|
FD_SET(daemon->inotifyfd, &rset);
|
||||||
|
bump_maxfd(daemon->inotifyfd, &maxfd);
|
||||||
|
}
|
||||||
#elif defined(HAVE_BSD_NETWORK)
|
#elif defined(HAVE_BSD_NETWORK)
|
||||||
FD_SET(daemon->routefd, &rset);
|
FD_SET(daemon->routefd, &rset);
|
||||||
bump_maxfd(daemon->routefd, &maxfd);
|
bump_maxfd(daemon->routefd, &maxfd);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
FD_SET(piperead, &rset);
|
FD_SET(piperead, &rset);
|
||||||
bump_maxfd(piperead, &maxfd);
|
bump_maxfd(piperead, &maxfd);
|
||||||
|
|
||||||
@@ -929,6 +945,10 @@ int main (int argc, char **argv)
|
|||||||
route_sock();
|
route_sock();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
if (daemon->port != 0 && !option_bool(OPT_NO_POLL) && FD_ISSET(daemon->inotifyfd, &rset) && inotify_check())
|
||||||
|
poll_resolv(1, 1, now);
|
||||||
|
#else
|
||||||
/* Check for changes to resolv files once per second max. */
|
/* Check for changes to resolv files once per second max. */
|
||||||
/* Don't go silent for long periods if the clock goes backwards. */
|
/* Don't go silent for long periods if the clock goes backwards. */
|
||||||
if (daemon->last_resolv == 0 ||
|
if (daemon->last_resolv == 0 ||
|
||||||
@@ -941,7 +961,8 @@ int main (int argc, char **argv)
|
|||||||
poll_resolv(0, daemon->last_resolv != 0, now);
|
poll_resolv(0, daemon->last_resolv != 0, now);
|
||||||
daemon->last_resolv = now;
|
daemon->last_resolv = now;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (FD_ISSET(piperead, &rset))
|
if (FD_ISSET(piperead, &rset))
|
||||||
async_event(piperead, now);
|
async_event(piperead, now);
|
||||||
|
|
||||||
|
|||||||
@@ -541,6 +541,10 @@ struct resolvc {
|
|||||||
int is_default, logged;
|
int is_default, logged;
|
||||||
time_t mtime;
|
time_t mtime;
|
||||||
char *name;
|
char *name;
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
int wd; /* inotify watch descriptor */
|
||||||
|
char *file; /* pointer to file part if path */
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
/* adn-hosts parms from command-line (also dhcp-hostsfile and dhcp-optsfile */
|
/* adn-hosts parms from command-line (also dhcp-hostsfile and dhcp-optsfile */
|
||||||
@@ -998,7 +1002,7 @@ extern struct daemon {
|
|||||||
/* DHCP state */
|
/* DHCP state */
|
||||||
int dhcpfd, helperfd, pxefd;
|
int dhcpfd, helperfd, pxefd;
|
||||||
#if defined(HAVE_LINUX_NETWORK)
|
#if defined(HAVE_LINUX_NETWORK)
|
||||||
int netlinkfd;
|
int netlinkfd, inotifyfd;
|
||||||
#elif defined(HAVE_BSD_NETWORK)
|
#elif defined(HAVE_BSD_NETWORK)
|
||||||
int dhcp_raw_fd, dhcp_icmp_fd, routefd;
|
int dhcp_raw_fd, dhcp_icmp_fd, routefd;
|
||||||
#endif
|
#endif
|
||||||
@@ -1469,3 +1473,8 @@ void loop_send_probes();
|
|||||||
int detect_loop(char *query, int type);
|
int detect_loop(char *query, int type);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* inotify.c */
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
void inotify_dnsmasq_init();
|
||||||
|
int inotify_check(void);
|
||||||
|
#endif
|
||||||
|
|||||||
102
src/inotify.c
Normal file
102
src/inotify.c
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
/* dnsmasq is Copyright (c) 2000-2014 Simon Kelley
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation; version 2 dated June, 1991, or
|
||||||
|
(at your option) version 3 dated 29 June, 2007.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "dnsmasq.h"
|
||||||
|
#include <sys/inotify.h>
|
||||||
|
|
||||||
|
#ifdef HAVE_LINUX_NETWORK
|
||||||
|
|
||||||
|
/* the strategy is to set a inotify on the directories containing
|
||||||
|
resolv files, for any files in the directory which are close-write
|
||||||
|
or moved into the directory.
|
||||||
|
|
||||||
|
When either of those happen, we look to see if the file involved
|
||||||
|
is actually a resolv-file, and if so, call poll-resolv with
|
||||||
|
the "force" argument, to ensure it's read.
|
||||||
|
|
||||||
|
This adds one new error condition: the directories containing
|
||||||
|
all specified resolv-files must exist at start-up, even if the actual
|
||||||
|
files don't.
|
||||||
|
*/
|
||||||
|
|
||||||
|
static char *inotify_buffer;
|
||||||
|
#define INOTIFY_SZ (sizeof(struct inotify_event) + NAME_MAX + 1)
|
||||||
|
|
||||||
|
void inotify_dnsmasq_init()
|
||||||
|
{
|
||||||
|
struct resolvc *res;
|
||||||
|
|
||||||
|
inotify_buffer = safe_malloc(INOTIFY_SZ);
|
||||||
|
|
||||||
|
daemon->inotifyfd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
|
||||||
|
|
||||||
|
if (daemon->inotifyfd == -1)
|
||||||
|
die(_("failed to create inotify: %s"), NULL, EC_MISC);
|
||||||
|
|
||||||
|
for (res = daemon->resolv_files; res; res = res->next)
|
||||||
|
{
|
||||||
|
char *d = strrchr(res->name, '/');
|
||||||
|
|
||||||
|
if (!d)
|
||||||
|
die(_("resolv-file %s not an absolute path"), res->name, EC_MISC);
|
||||||
|
|
||||||
|
*d = 0; /* make ->name just directory */
|
||||||
|
res->wd = inotify_add_watch(daemon->inotifyfd, res->name, IN_CLOSE_WRITE | IN_MOVED_TO);
|
||||||
|
res->file = d+1; /* pointer to filename */
|
||||||
|
|
||||||
|
if (res->wd == -1 && errno == ENOENT)
|
||||||
|
die(_("directory %s for resolv-file is missing, cannot poll"), res->name, EC_MISC);
|
||||||
|
|
||||||
|
*d = '/'; /* restore name */
|
||||||
|
|
||||||
|
if (res->wd == -1)
|
||||||
|
die(_("failed to create inotify for %s: %s"), res->name, EC_MISC);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int inotify_check(void)
|
||||||
|
{
|
||||||
|
int hit = 0;
|
||||||
|
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
int rc;
|
||||||
|
char *p;
|
||||||
|
struct resolvc *res;
|
||||||
|
struct inotify_event *in;
|
||||||
|
|
||||||
|
while ((rc = read(daemon->inotifyfd, inotify_buffer, INOTIFY_SZ)) == -1 && errno == EINTR);
|
||||||
|
|
||||||
|
if (rc <= 0)
|
||||||
|
break;
|
||||||
|
|
||||||
|
for (p = inotify_buffer; rc - (p - inotify_buffer) >= (int)sizeof(struct inotify_event); p += sizeof(struct inotify_event) + in->len)
|
||||||
|
{
|
||||||
|
in = (struct inotify_event*)p;
|
||||||
|
|
||||||
|
for (res = daemon->resolv_files; res; res = res->next)
|
||||||
|
if (res->wd == in->wd && in->len != 0 && strcmp(res->file, in->name) == 0)
|
||||||
|
hit = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hit;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user