size_t n_addresses, n_allocated;
} EtcHostsItemByName;
-void manager_etc_hosts_flush(Manager *m) {
- EtcHostsItem *item;
- EtcHostsItemByName *bn;
-
- while ((item = hashmap_steal_first(m->etc_hosts_by_address))) {
- strv_free(item->names);
- free(item);
- }
+static inline void etc_hosts_item_free(EtcHostsItem *item) {
+ strv_free(item->names);
+ free(item);
+}
- while ((bn = hashmap_steal_first(m->etc_hosts_by_name))) {
- free(bn->name);
- free(bn->addresses);
- free(bn);
- }
+static inline void etc_hosts_item_by_name_free(EtcHostsItemByName *item) {
+ free(item->name);
+ free(item->addresses);
+ free(item);
+}
- m->etc_hosts_by_address = hashmap_free(m->etc_hosts_by_address);
- m->etc_hosts_by_name = hashmap_free(m->etc_hosts_by_name);
+static void etc_hosts_free(EtcHosts *hosts) {
+ hosts->by_address = hashmap_free_with_destructor(hosts->by_address, etc_hosts_item_free);
+ hosts->by_name = hashmap_free_with_destructor(hosts->by_name, etc_hosts_item_by_name_free);
+}
+void manager_etc_hosts_flush(Manager *m) {
+ etc_hosts_free(&m->etc_hosts);
m->etc_hosts_mtime = USEC_INFINITY;
}
-static int parse_line(Manager *m, unsigned nr, const char *line) {
+static int parse_line(EtcHosts *hosts, unsigned nr, const char *line) {
_cleanup_free_ char *address_str = NULL;
struct in_addr_data address = {};
bool suppressed = false;
EtcHostsItem *item;
int r;
- assert(m);
+ assert(hosts);
assert(line);
r = extract_first_word(&line, &address_str, NULL, EXTRACT_RELAX);
else {
/* If this is a normal address, then, simply add entry mapping it to the specified names */
- item = hashmap_get(m->etc_hosts_by_address, &address);
+ item = hashmap_get(hosts->by_address, &address);
if (!item) {
- r = hashmap_ensure_allocated(&m->etc_hosts_by_address, &in_addr_data_hash_ops);
+ r = hashmap_ensure_allocated(&hosts->by_address, &in_addr_data_hash_ops);
if (r < 0)
return log_oom();
item->address = address;
- r = hashmap_put(m->etc_hosts_by_address, &item->address, item);
+ r = hashmap_put(hosts->by_address, &item->address, item);
if (r < 0) {
free(item);
return log_oom();
return log_oom();
}
- bn = hashmap_get(m->etc_hosts_by_name, name);
+ bn = hashmap_get(hosts->by_name, name);
if (!bn) {
- r = hashmap_ensure_allocated(&m->etc_hosts_by_name, &dns_name_hash_ops);
+ r = hashmap_ensure_allocated(&hosts->by_name, &dns_name_hash_ops);
if (r < 0)
return log_oom();
if (!bn)
return log_oom();
- r = hashmap_put(m->etc_hosts_by_name, name, bn);
+ r = hashmap_put(hosts->by_name, name, bn);
if (r < 0) {
free(bn);
return log_oom();
return 0;
}
+static int etc_hosts_parse(EtcHosts *hosts, FILE *f) {
+ _cleanup_(etc_hosts_free) EtcHosts t = {};
+ char line[LINE_MAX];
+ unsigned nr = 0;
+ int r;
+
+ FOREACH_LINE(line, f, return log_error_errno(errno, "Failed to read /etc/hosts: %m")) {
+ char *l;
+
+ nr++;
+
+ l = strstrip(line);
+ if (isempty(l))
+ continue;
+ if (l[0] == '#')
+ continue;
+
+ r = parse_line(&t, nr, l);
+ if (r < 0)
+ return r;
+ }
+
+ *hosts = t;
+ t = (EtcHosts) {}; /* prevent cleanup */
+ return 0;
+}
+
static int manager_etc_hosts_read(Manager *m) {
_cleanup_fclose_ FILE *f = NULL;
- char line[LINE_MAX];
struct stat st;
usec_t ts;
- unsigned nr = 0;
int r;
assert_se(sd_event_now(m->event, clock_boottime_or_monotonic(), &ts) >= 0);
if (m->etc_hosts_mtime != USEC_INFINITY) {
if (stat("/etc/hosts", &st) < 0) {
- if (errno == ENOENT) {
- r = 0;
- goto clear;
- }
+ 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");
+ manager_etc_hosts_flush(m);
+ return 0;
}
/* Did the mtime change? If not, there's no point in re-reading the file. */
f = fopen("/etc/hosts", "re");
if (!f) {
- if (errno == ENOENT) {
- r = 0;
- goto clear;
- }
+ if (errno != ENOENT)
+ return log_error_errno(errno, "Failed to open /etc/hosts: %m");
- return log_error_errno(errno, "Failed to open /etc/hosts: %m");
+ manager_etc_hosts_flush(m);
+ return 0;
}
/* Take the timestamp at the beginning of processing, so that any changes made later are read on the next
manager_etc_hosts_flush(m);
- FOREACH_LINE(line, f, return log_error_errno(errno, "Failed to read /etc/hosts: %m")) {
- char *l;
-
- nr++;
-
- l = strstrip(line);
- if (isempty(l))
- continue;
- if (l[0] == '#')
- continue;
-
- r = parse_line(m, nr, l);
- if (r == -ENOMEM) /* On OOM we abandon the half-built-up structure. All other errors we ignore and proceed */
- goto clear;
- }
+ r = etc_hosts_parse(&m->etc_hosts, f);
+ if (r == -ENOMEM)
+ /* On OOM we bail. All other errors we ignore and proceed. */
+ return r;
m->etc_hosts_mtime = timespec_load(&st.st_mtim);
m->etc_hosts_last = ts;
return 1;
-
-clear:
- manager_etc_hosts_flush(m);
- return r;
}
int manager_etc_hosts_lookup(Manager *m, DnsQuestion* q, DnsAnswer **answer) {
EtcHostsItem *item;
DnsResourceKey *found_ptr = NULL;
- item = hashmap_get(m->etc_hosts_by_address, &k);
+ item = hashmap_get(m->etc_hosts.by_address, &k);
if (!item)
return 0;
return 1;
}
- bn = hashmap_get(m->etc_hosts_by_name, name);
+ bn = hashmap_get(m->etc_hosts.by_name, name);
if (!bn)
return 0;