networkd: use an OrderedSet instead of Set to collect link domains

For the search domain logic the order is highly relevant, hence make sure when collecting the various search domains to
add them to an ordered set, so that the order between search domains of a specific link is retained.
This commit is contained in:
Lennart Poettering 2016-01-25 20:31:11 +01:00
parent dce8364918
commit 0061695507
4 changed files with 91 additions and 19 deletions

View file

@ -840,6 +840,7 @@ libbasic_la_SOURCES = \
src/basic/siphash24.h \
src/basic/set.h \
src/basic/ordered-set.h \
src/basic/ordered-set.c \
src/basic/bitmap.c \
src/basic/bitmap.h \
src/basic/fdset.c \

64
src/basic/ordered-set.c Normal file
View file

@ -0,0 +1,64 @@
/***
This file is part of systemd.
Copyright 2016 Lennart Poettering
systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "ordered-set.h"
#include "strv.h"
int ordered_set_consume(OrderedSet *s, void *p) {
int r;
r = ordered_set_put(s, p);
if (r <= 0)
free(p);
return r;
}
int ordered_set_put_strdup(OrderedSet *s, const char *p) {
char *c;
int r;
assert(s);
assert(p);
c = strdup(p);
if (!c)
return -ENOMEM;
r = ordered_set_consume(s, c);
if (r == -EEXIST)
return 0;
return r;
}
int ordered_set_put_strdupv(OrderedSet *s, char **l) {
int n = 0, r;
char **i;
STRV_FOREACH(i, l) {
r = ordered_set_put_strdup(s, *i);
if (r < 0)
return r;
n += r;
}
return n;
}

View file

@ -62,9 +62,15 @@ static inline bool ordered_set_iterate(OrderedSet *s, Iterator *i, void **value)
return ordered_hashmap_iterate((OrderedHashmap*) s, i, value, NULL);
}
int ordered_set_consume(OrderedSet *s, void *p);
int ordered_set_put_strdup(OrderedSet *s, const char *p);
int ordered_set_put_strdupv(OrderedSet *s, char **l);
#define ORDERED_SET_FOREACH(e, s, i) \
for ((i) = ITERATOR_FIRST; ordered_set_iterate((s), &(i), (void**)&(e)); )
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedSet*, ordered_set_free_free);
#define _cleanup_ordered_set_free_ _cleanup_(ordered_set_freep)
#define _cleanup_ordered_set_free_free_ _cleanup_(ordered_set_free_freep)

View file

@ -35,6 +35,7 @@
#include "local-addresses.h"
#include "netlink-util.h"
#include "networkd.h"
#include "ordered-set.h"
#include "path-util.h"
#include "set.h"
#include "udev-util.h"
@ -776,7 +777,7 @@ static int manager_connect_rtnl(Manager *m) {
return 0;
}
static int set_put_in_addr(Set *s, const struct in_addr *address) {
static int ordered_set_put_in_addr(OrderedSet *s, const struct in_addr *address) {
char *p;
int r;
@ -786,21 +787,21 @@ static int set_put_in_addr(Set *s, const struct in_addr *address) {
if (r < 0)
return r;
r = set_consume(s, p);
r = ordered_set_consume(s, p);
if (r == -EEXIST)
return 0;
return r;
}
static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
static int ordered_set_put_in_addrv(OrderedSet *s, const struct in_addr *addresses, int n) {
int r, i, c = 0;
assert(s);
assert(n <= 0 || addresses);
for (i = 0; i < n; i++) {
r = set_put_in_addr(s, addresses+i);
r = ordered_set_put_in_addr(s, addresses+i);
if (r < 0)
return r;
@ -810,17 +811,17 @@ static int set_put_in_addrv(Set *s, const struct in_addr *addresses, int n) {
return c;
}
static void print_string_set(FILE *f, const char *field, Set *s) {
static void print_string_set(FILE *f, const char *field, OrderedSet *s) {
bool space = false;
Iterator i;
char *p;
if (set_isempty(s))
if (ordered_set_isempty(s))
return;
fputs(field, f);
SET_FOREACH(p, s, i) {
ORDERED_SET_FOREACH(p, s, i) {
if (space)
fputc(' ', f);
fputs(p, f);
@ -830,7 +831,7 @@ static void print_string_set(FILE *f, const char *field, Set *s) {
}
static int manager_save(Manager *m) {
_cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
_cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL;
Link *link;
Iterator i;
_cleanup_free_ char *temp_path = NULL;
@ -843,19 +844,19 @@ static int manager_save(Manager *m) {
assert(m->state_file);
/* We add all NTP and DNS server to a set, to filter out duplicates */
dns = set_new(&string_hash_ops);
dns = ordered_set_new(&string_hash_ops);
if (!dns)
return -ENOMEM;
ntp = set_new(&string_hash_ops);
ntp = ordered_set_new(&string_hash_ops);
if (!ntp)
return -ENOMEM;
search_domains = set_new(&string_hash_ops);
search_domains = ordered_set_new(&string_hash_ops);
if (!search_domains)
return -ENOMEM;
route_domains = set_new(&string_hash_ops);
route_domains = ordered_set_new(&string_hash_ops);
if (!route_domains)
return -ENOMEM;
@ -870,19 +871,19 @@ static int manager_save(Manager *m) {
continue;
/* First add the static configured entries */
r = set_put_strdupv(dns, link->network->dns);
r = ordered_set_put_strdupv(dns, link->network->dns);
if (r < 0)
return r;
r = set_put_strdupv(ntp, link->network->ntp);
r = ordered_set_put_strdupv(ntp, link->network->ntp);
if (r < 0)
return r;
r = set_put_strdupv(search_domains, link->network->search_domains);
r = ordered_set_put_strdupv(search_domains, link->network->search_domains);
if (r < 0)
return r;
r = set_put_strdupv(route_domains, link->network->route_domains);
r = ordered_set_put_strdupv(route_domains, link->network->route_domains);
if (r < 0)
return r;
@ -895,7 +896,7 @@ static int manager_save(Manager *m) {
r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
if (r > 0) {
r = set_put_in_addrv(dns, addresses, r);
r = ordered_set_put_in_addrv(dns, addresses, r);
if (r < 0)
return r;
} else if (r < 0 && r != -ENODATA)
@ -907,7 +908,7 @@ static int manager_save(Manager *m) {
r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
if (r > 0) {
r = set_put_in_addrv(ntp, addresses, r);
r = ordered_set_put_in_addrv(ntp, addresses, r);
if (r < 0)
return r;
} else if (r < 0 && r != -ENODATA)
@ -919,7 +920,7 @@ static int manager_save(Manager *m) {
r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
if (r >= 0) {
r = set_put_strdup(search_domains, domainname);
r = ordered_set_put_strdup(search_domains, domainname);
if (r < 0)
return r;
} else if (r != -ENODATA)