resolved: maintain order when writing resolv.conf entries

http://lists.freedesktop.org/archives/systemd-devel/2015-March/029850.html
This commit is contained in:
Lennart Poettering 2015-04-08 17:23:27 +02:00
parent 4a280a7e78
commit 822db23cfa

View file

@ -30,6 +30,7 @@
#include "af-list.h" #include "af-list.h"
#include "utf8.h" #include "utf8.h"
#include "fileio-label.h" #include "fileio-label.h"
#include "ordered-set.h"
#include "resolved-dns-domain.h" #include "resolved-dns-domain.h"
#include "resolved-conf.h" #include "resolved-conf.h"
@ -702,8 +703,11 @@ static void write_resolv_conf_server(DnsServer *s, FILE *f, unsigned *count) {
(*count) ++; (*count) ++;
} }
static void write_resolv_conf_search(const char *domain, FILE *f, static void write_resolv_conf_search(
unsigned *count, unsigned *length) { const char *domain, FILE *f,
unsigned *count,
unsigned *length) {
assert(domain); assert(domain);
assert(f); assert(f);
assert(length); assert(length);
@ -724,7 +728,7 @@ static void write_resolv_conf_search(const char *domain, FILE *f,
(*count) ++; (*count) ++;
} }
static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) { static int write_resolv_conf_contents(FILE *f, OrderedSet *dns, OrderedSet *domains) {
Iterator i; Iterator i;
fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n" fputs("# This file is managed by systemd-resolved(8). Do not edit.\n#\n"
@ -733,22 +737,22 @@ static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
"# resolv.conf(5) in a different way, replace the symlink by a\n" "# resolv.conf(5) in a different way, replace the symlink by a\n"
"# static file or a different symlink.\n\n", f); "# static file or a different symlink.\n\n", f);
if (set_isempty(dns)) if (ordered_set_isempty(dns))
fputs("# No DNS servers known.\n", f); fputs("# No DNS servers known.\n", f);
else { else {
DnsServer *s; DnsServer *s;
unsigned count = 0; unsigned count = 0;
SET_FOREACH(s, dns, i) ORDERED_SET_FOREACH(s, dns, i)
write_resolv_conf_server(s, f, &count); write_resolv_conf_server(s, f, &count);
} }
if (!set_isempty(domains)) { if (!ordered_set_isempty(domains)) {
unsigned length = 0, count = 0; unsigned length = 0, count = 0;
char *domain; char *domain;
fputs("search", f); fputs("search", f);
SET_FOREACH(domain, domains, i) ORDERED_SET_FOREACH(domain, domains, i)
write_resolv_conf_search(domain, f, &count, &length); write_resolv_conf_search(domain, f, &count, &length);
fputs("\n", f); fputs("\n", f);
} }
@ -756,12 +760,11 @@ static int write_resolv_conf_contents(FILE *f, Set *dns, Set *domains) {
return fflush_and_check(f); return fflush_and_check(f);
} }
int manager_write_resolv_conf(Manager *m) { int manager_write_resolv_conf(Manager *m) {
static const char path[] = "/run/systemd/resolve/resolv.conf"; static const char path[] = "/run/systemd/resolve/resolv.conf";
_cleanup_free_ char *temp_path = NULL; _cleanup_free_ char *temp_path = NULL;
_cleanup_fclose_ FILE *f = NULL; _cleanup_fclose_ FILE *f = NULL;
_cleanup_set_free_ Set *dns = NULL, *domains = NULL; _cleanup_ordered_set_free_ OrderedSet *dns = NULL, *domains = NULL;
DnsServer *s; DnsServer *s;
Iterator i; Iterator i;
Link *l; Link *l;
@ -773,17 +776,17 @@ int manager_write_resolv_conf(Manager *m) {
manager_read_resolv_conf(m); manager_read_resolv_conf(m);
/* Add the full list to a set, to filter out duplicates */ /* Add the full list to a set, to filter out duplicates */
dns = set_new(&dns_server_hash_ops); dns = ordered_set_new(&dns_server_hash_ops);
if (!dns) if (!dns)
return -ENOMEM; return -ENOMEM;
domains = set_new(&dns_name_hash_ops); domains = ordered_set_new(&dns_name_hash_ops);
if (!domains) if (!domains)
return -ENOMEM; return -ENOMEM;
/* First add the system-wide servers */ /* First add the system-wide servers */
LIST_FOREACH(servers, s, m->dns_servers) { LIST_FOREACH(servers, s, m->dns_servers) {
r = set_put(dns, s); r = ordered_set_put(dns, s);
if (r == -EEXIST) if (r == -EEXIST)
continue; continue;
if (r < 0) if (r < 0)
@ -795,7 +798,7 @@ int manager_write_resolv_conf(Manager *m) {
char **domain; char **domain;
LIST_FOREACH(servers, s, l->dns_servers) { LIST_FOREACH(servers, s, l->dns_servers) {
r = set_put(dns, s); r = ordered_set_put(dns, s);
if (r == -EEXIST) if (r == -EEXIST)
continue; continue;
if (r < 0) if (r < 0)
@ -806,7 +809,7 @@ int manager_write_resolv_conf(Manager *m) {
continue; continue;
STRV_FOREACH(domain, l->unicast_scope->domains) { STRV_FOREACH(domain, l->unicast_scope->domains) {
r = set_put(domains, *domain); r = ordered_set_put(domains, *domain);
if (r == -EEXIST) if (r == -EEXIST)
continue; continue;
if (r < 0) if (r < 0)
@ -815,9 +818,9 @@ int manager_write_resolv_conf(Manager *m) {
} }
/* If we found nothing, add the fallback servers */ /* If we found nothing, add the fallback servers */
if (set_isempty(dns)) { if (ordered_set_isempty(dns)) {
LIST_FOREACH(servers, s, m->fallback_dns_servers) { LIST_FOREACH(servers, s, m->fallback_dns_servers) {
r = set_put(dns, s); r = ordered_set_put(dns, s);
if (r == -EEXIST) if (r == -EEXIST)
continue; continue;
if (r < 0) if (r < 0)
@ -843,8 +846,8 @@ int manager_write_resolv_conf(Manager *m) {
return 0; return 0;
fail: fail:
unlink(path); (void) unlink(path);
unlink(temp_path); (void) unlink(temp_path);
return r; return r;
} }