resolved: use stat_inode_unmodified() to detect /etc/hosts changes

This commit is contained in:
Lennart Poettering 2020-11-06 14:56:16 +01:00 committed by Yu Watanabe
parent fbbc72189f
commit 36d892b7e6
3 changed files with 7 additions and 15 deletions

View File

@ -10,6 +10,7 @@
#include "resolved-dns-synthesize.h" #include "resolved-dns-synthesize.h"
#include "resolved-etc-hosts.h" #include "resolved-etc-hosts.h"
#include "socket-netlink.h" #include "socket-netlink.h"
#include "stat-util.h"
#include "string-util.h" #include "string-util.h"
#include "strv.h" #include "strv.h"
#include "time-util.h" #include "time-util.h"
@ -36,9 +37,7 @@ void etc_hosts_free(EtcHosts *hosts) {
void manager_etc_hosts_flush(Manager *m) { void manager_etc_hosts_flush(Manager *m) {
etc_hosts_free(&m->etc_hosts); etc_hosts_free(&m->etc_hosts);
m->etc_hosts_mtime = USEC_INFINITY; m->etc_hosts_stat = (struct stat) {};
m->etc_hosts_ino = 0;
m->etc_hosts_dev = 0;
} }
static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) { static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
@ -214,7 +213,7 @@ static int manager_etc_hosts_read(Manager *m) {
m->etc_hosts_last = ts; m->etc_hosts_last = ts;
if (m->etc_hosts_mtime != USEC_INFINITY) { if (m->etc_hosts_stat.st_mode != 0) {
if (stat("/etc/hosts", &st) < 0) { if (stat("/etc/hosts", &st) < 0) {
if (errno != ENOENT) if (errno != ENOENT)
return log_error_errno(errno, "Failed to stat /etc/hosts: %m"); return log_error_errno(errno, "Failed to stat /etc/hosts: %m");
@ -224,8 +223,7 @@ static int manager_etc_hosts_read(Manager *m) {
} }
/* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */ /* Did the mtime or ino/dev change? If not, there's no point in re-reading the file. */
if (timespec_load(&st.st_mtim) == m->etc_hosts_mtime && if (stat_inode_unmodified(&m->etc_hosts_stat, &st))
st.st_ino == m->etc_hosts_ino && st.st_dev == m->etc_hosts_dev)
return 0; return 0;
} }
@ -248,9 +246,7 @@ static int manager_etc_hosts_read(Manager *m) {
if (r < 0) if (r < 0)
return r; return r;
m->etc_hosts_mtime = timespec_load(&st.st_mtim); m->etc_hosts_stat = st;
m->etc_hosts_ino = st.st_ino;
m->etc_hosts_dev = st.st_dev;
m->etc_hosts_last = ts; m->etc_hosts_last = ts;
return 1; return 1;

View File

@ -641,9 +641,6 @@ int manager_new(Manager **ret) {
.read_resolv_conf = true, .read_resolv_conf = true,
.need_builtin_fallbacks = true, .need_builtin_fallbacks = true,
.etc_hosts_last = USEC_INFINITY, .etc_hosts_last = USEC_INFINITY,
.etc_hosts_mtime = USEC_INFINITY,
.etc_hosts_ino = 0,
.etc_hosts_dev = 0,
.read_etc_hosts = true, .read_etc_hosts = true,
}; };

View File

@ -129,9 +129,8 @@ struct Manager {
/* Data from /etc/hosts */ /* Data from /etc/hosts */
EtcHosts etc_hosts; EtcHosts etc_hosts;
usec_t etc_hosts_last, etc_hosts_mtime; usec_t etc_hosts_last;
ino_t etc_hosts_ino; struct stat etc_hosts_stat;
dev_t etc_hosts_dev;
bool read_etc_hosts; bool read_etc_hosts;
OrderedSet *dns_extra_stub_listeners; OrderedSet *dns_extra_stub_listeners;