[systemd-commits] 3 commits - src/network src/resolve src/shared

Lennart Poettering lennart at kemper.freedesktop.org
Tue Aug 12 04:02:35 PDT 2014


 src/network/networkd-link.c       |   21 +++---
 src/network/networkd-manager.c    |  131 +++++++++++++++++++++++++++++++++++---
 src/resolve/resolved-dns-server.c |   23 ++++++
 src/resolve/resolved-dns-server.h |    3 
 src/resolve/resolved-manager.c    |   51 +++++++++++---
 src/shared/set.c                  |   34 +++++++++
 src/shared/set.h                  |    2 
 7 files changed, 238 insertions(+), 27 deletions(-)

New commits:
commit c0c743cb3df7197b51bb89cc3c8ca7ed16d06396
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Aug 12 13:01:58 2014 +0200

    networkd: expose a global list of DNS and NTP servers in the state file

diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 8a0ed5e..28d7261 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -415,7 +415,42 @@ int manager_bus_listen(Manager *m) {
         return 0;
 }
 
+static int set_put_in_addr(Set *s, const struct in_addr *address) {
+        char *p;
+        int r;
+
+        assert(s);
+
+        r = in_addr_to_string(AF_INET, (const union in_addr_union*) address, &p);
+        if (r < 0)
+                return r;
+
+        r = 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) {
+        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);
+                if (r < 0)
+                        return r;
+
+                c += r;
+        }
+
+        return c;
+}
+
 int manager_save(Manager *m) {
+        _cleanup_set_free_free_ Set *dns = NULL, *ntp = NULL;
         Link *link;
         Iterator i;
         _cleanup_free_ char *temp_path = NULL;
@@ -427,12 +462,61 @@ int manager_save(Manager *m) {
         assert(m);
         assert(m->state_file);
 
+        /* We add all NTP and DNS server to a set, to filter out duplicates */
+        dns = set_new(string_hash_func, string_compare_func);
+        if (!dns)
+                return -ENOMEM;
+
+        ntp = set_new(string_hash_func, string_compare_func);
+        if (!ntp)
+                return -ENOMEM;
+
         HASHMAP_FOREACH(link, m->links, i) {
                 if (link->flags & IFF_LOOPBACK)
                         continue;
 
                 if (link->operstate > operstate)
                         operstate = link->operstate;
+
+                if (!link->network)
+                        continue;
+
+                /* First add the static configured entries */
+                r = set_put_strdupv(dns, link->network->dns);
+                if (r < 0)
+                        return r;
+
+                r = set_put_strdupv(ntp, link->network->ntp);
+                if (r < 0)
+                        return r;
+
+                if (!link->dhcp_lease)
+                        continue;
+
+                /* Secondly, add the entries acquired via DHCP */
+                if (link->network->dhcp_dns) {
+                        const struct in_addr *addresses;
+
+                        r = sd_dhcp_lease_get_dns(link->dhcp_lease, &addresses);
+                        if (r > 0) {
+                                r = set_put_in_addrv(dns, addresses, r);
+                                if (r < 0)
+                                        return r;
+                        } else if (r != -ENOENT)
+                                return r;
+                }
+
+                if (link->network->dhcp_ntp) {
+                        const struct in_addr *addresses;
+
+                        r = sd_dhcp_lease_get_ntp(link->dhcp_lease, &addresses);
+                        if (r > 0) {
+                                r = set_put_in_addrv(ntp, addresses, r);
+                                if (r < 0)
+                                        return r;
+                        } else if (r != -ENOENT)
+                                return r;
+                }
         }
 
         operstate_str = link_operstate_to_string(operstate);
@@ -448,6 +532,34 @@ int manager_save(Manager *m) {
                 "# This is private data. Do not parse.\n"
                 "OPER_STATE=%s\n", operstate_str);
 
+        if (!set_isempty(dns)) {
+                bool space = false;
+                char *p;
+
+                fputs("DNS=", f);
+                SET_FOREACH(p, dns, i) {
+                        if (space)
+                                fputc(' ', f);
+                        fputs(p, f);
+                        space = true;
+                }
+                fputc('\n', f);
+        }
+
+        if (!set_isempty(ntp)) {
+                bool space = false;
+                char *p;
+
+                fputs("NTP=", f);
+                SET_FOREACH(p, ntp, i) {
+                        if (space)
+                                fputc(' ', f);
+                        fputs(p, f);
+                        space = true;
+                }
+                fputc('\n', f);
+        }
+
         r = fflush_and_check(f);
         if (r < 0)
                 goto fail;
diff --git a/src/shared/set.c b/src/shared/set.c
index 5a4bf11..02ea831 100644
--- a/src/shared/set.c
+++ b/src/shared/set.c
@@ -23,6 +23,7 @@
 
 #include "set.h"
 #include "hashmap.h"
+#include "strv.h"
 
 #define MAKE_SET(h) ((Set*) (h))
 #define MAKE_HASHMAP(s) ((Hashmap*) (s))
@@ -59,6 +60,39 @@ int set_consume(Set *s, void *value) {
         return r;
 }
 
+int set_put_strdup(Set *s, const char *p) {
+        char *c;
+        int r;
+
+        assert(s);
+        assert(p);
+
+        c = strdup(p);
+        if (!c)
+                return -ENOMEM;
+
+        r = set_consume(s, c);
+        if (r == -EEXIST)
+                return 0;
+
+        return r;
+}
+
+int set_put_strdupv(Set *s, char **l) {
+        int n = 0, r;
+        char **i;
+
+        STRV_FOREACH(i, l) {
+                r = set_put_strdup(s, *i);
+                if (r < 0)
+                        return r;
+
+                n += r;
+        }
+
+        return n;
+}
+
 int set_replace(Set *s, void *value) {
         return hashmap_replace(MAKE_HASHMAP(s), value, value);
 }
diff --git a/src/shared/set.h b/src/shared/set.h
index 5612478..e0b9c9e 100644
--- a/src/shared/set.h
+++ b/src/shared/set.h
@@ -41,6 +41,8 @@ int set_ensure_allocated(Set **s, hash_func_t hash_func, compare_func_t compare_
 
 int set_put(Set *s, void *value);
 int set_consume(Set *s, void *value);
+int set_put_strdup(Set *s, const char *p);
+int set_put_strdupv(Set *s, char **l);
 int set_replace(Set *s, void *value);
 void *set_get(Set *s, void *value);
 bool set_contains(Set *s, void *value);

commit 87f5a19343acf8ba697acc5a62bdb1a2b8c9eda3
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Aug 12 12:21:10 2014 +0200

    resolved: filter out duplicate DNS servers when writing resolv.conf

diff --git a/src/resolve/resolved-dns-server.c b/src/resolve/resolved-dns-server.c
index 30d9c8b..043f6b6 100644
--- a/src/resolve/resolved-dns-server.c
+++ b/src/resolve/resolved-dns-server.c
@@ -19,6 +19,8 @@
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include "siphash24.h"
+
 #include "resolved-dns-server.h"
 
 int dns_server_new(
@@ -97,3 +99,24 @@ DnsServer* dns_server_free(DnsServer *s)  {
 
         return NULL;
 }
+
+unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) {
+        const DnsServer *s = p;
+        uint64_t u;
+
+        siphash24((uint8_t*) &u, &s->address, FAMILY_ADDRESS_SIZE(s->family), hash_key);
+        u = u * hash_key[0] + u + s->family;
+
+        return u;
+}
+
+int dns_server_compare_func(const void *a, const void *b) {
+        const DnsServer *x = a, *y = b;
+
+        if (x->family < y->family)
+                return -1;
+        if (x->family > y->family)
+                return 1;
+
+        return memcmp(&x->address, &y->address, FAMILY_ADDRESS_SIZE(x->family));
+}
diff --git a/src/resolve/resolved-dns-server.h b/src/resolve/resolved-dns-server.h
index 8a9f556..5178a6b 100644
--- a/src/resolve/resolved-dns-server.h
+++ b/src/resolve/resolved-dns-server.h
@@ -59,3 +59,6 @@ int dns_server_new(
                 const union in_addr_union *address);
 
 DnsServer* dns_server_free(DnsServer *s);
+
+unsigned long dns_server_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]);
+int dns_server_compare_func(const void *a, const void *b);
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index 988aa6e..6bb0894 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -691,7 +691,7 @@ static void write_resolve_conf_server(DnsServer *s, FILE *f, unsigned *count) {
         }
 
         if (*count == MAXNS)
-                fputs("# Too many DNS servers configured, the following entries may be ignored\n", f);
+                fputs("# Too many DNS servers configured, the following entries may be ignored.\n", f);
 
         fprintf(f, "nameserver %s\n", t);
         (*count) ++;
@@ -701,6 +701,7 @@ int manager_write_resolv_conf(Manager *m) {
         static const char path[] = "/run/systemd/resolve/resolv.conf";
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
+        _cleanup_set_free_ Set *dns = NULL;
         unsigned count = 0;
         DnsServer *s;
         Iterator i;
@@ -712,6 +713,41 @@ int manager_write_resolv_conf(Manager *m) {
         /* Read the system /etc/resolv.conf first */
         manager_read_resolv_conf(m);
 
+        /* Add the full list to a set, to filter out duplicates */
+        dns = set_new(dns_server_hash_func, dns_server_compare_func);
+        if (!dns)
+                return -ENOMEM;
+
+        /* First add the system-wide servers */
+        LIST_FOREACH(servers, s, m->dns_servers) {
+                r = set_put(dns, s);
+                if (r == -EEXIST)
+                        continue;
+                if (r < 0)
+                        return r;
+        }
+
+        /* Then, add the per-link servers */
+        HASHMAP_FOREACH(l, m->links, i)
+                LIST_FOREACH(servers, s, l->dns_servers) {
+                        r = set_put(dns, s);
+                        if (r == -EEXIST)
+                                continue;
+                        if (r < 0)
+                                return r;
+                }
+
+        /* If we found nothing, add the fallback servers */
+        if (set_isempty(dns)) {
+                LIST_FOREACH(servers, s, m->fallback_dns_servers) {
+                        r = set_put(dns, s);
+                        if (r == -EEXIST)
+                                continue;
+                        if (r < 0)
+                                return r;
+                }
+        }
+
         r = fopen_temporary(path, &f, &temp_path);
         if (r < 0)
                 return r;
@@ -724,15 +760,10 @@ int manager_write_resolv_conf(Manager *m) {
               "# resolv.conf(5) in a different way, replace the symlink by a\n"
               "# static file or a different symlink.\n\n", f);
 
-        LIST_FOREACH(servers, s, m->dns_servers)
-                write_resolve_conf_server(s, f, &count);
-
-        HASHMAP_FOREACH(l, m->links, i)
-                LIST_FOREACH(servers, s, l->dns_servers)
-                        write_resolve_conf_server(s, f, &count);
-
-        if (count == 0) {
-                LIST_FOREACH(servers, s, m->fallback_dns_servers)
+        if (set_isempty(dns))
+                fputs("# No DNS servers known.\n", f);
+        else {
+                SET_FOREACH(s, dns, i)
                         write_resolve_conf_server(s, f, &count);
         }
 

commit c2d6bd61ee77b59566116afe6e20513d19945ed4
Author: Lennart Poettering <lennart at poettering.net>
Date:   Tue Aug 12 11:55:06 2014 +0200

    networkd: update manager_save() to use fflush_and_check() to simplify things a bit

diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index a95efe3..62533c1 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -2332,7 +2332,7 @@ int link_save(Link *link) {
 
         r = fopen_temporary(link->state_file, &f, &temp_path);
         if (r < 0)
-                goto finish;
+                return r;
 
         fchmod(fileno(f), 0644);
 
@@ -2393,7 +2393,7 @@ int link_save(Link *link) {
 
                 r = dhcp_lease_save(link->dhcp_lease, link->lease_file);
                 if (r < 0)
-                        goto finish;
+                        goto fail;
 
                 fprintf(f,
                         "DHCP_LEASE=%s\n",
@@ -2401,18 +2401,21 @@ int link_save(Link *link) {
         } else
                 unlink(link->lease_file);
 
-        fflush(f);
+        r = fflush_and_check(f);
+        if (r < 0)
+                goto fail;
 
-        if (ferror(f) || rename(temp_path, link->state_file) < 0) {
+        if (rename(temp_path, link->state_file) < 0) {
                 r = -errno;
-                unlink(link->state_file);
-                unlink(temp_path);
+                goto fail;
         }
 
-finish:
-        if (r < 0)
-                log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r));
+        return 0;
 
+fail:
+        log_error_link(link, "Failed to save link data to %s: %s", link->state_file, strerror(-r));
+        unlink(link->state_file);
+        unlink(temp_path);
         return r;
 }
 
diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c
index 223cb2a..8a0ed5e 100644
--- a/src/network/networkd-manager.c
+++ b/src/network/networkd-manager.c
@@ -440,7 +440,7 @@ int manager_save(Manager *m) {
 
         r = fopen_temporary(m->state_file, &f, &temp_path);
         if (r < 0)
-                goto finish;
+                return r;
 
         fchmod(fileno(f), 0644);
 
@@ -448,18 +448,21 @@ int manager_save(Manager *m) {
                 "# This is private data. Do not parse.\n"
                 "OPER_STATE=%s\n", operstate_str);
 
-        fflush(f);
+        r = fflush_and_check(f);
+        if (r < 0)
+                goto fail;
 
-        if (ferror(f) || rename(temp_path, m->state_file) < 0) {
+        if (rename(temp_path, m->state_file) < 0) {
                 r = -errno;
-                unlink(m->state_file);
-                unlink(temp_path);
+                goto fail;
         }
 
-finish:
-        if (r < 0)
-                log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
+        return 0;
 
+fail:
+        log_error("Failed to save network state to %s: %s", m->state_file, strerror(-r));
+        unlink(m->state_file);
+        unlink(temp_path);
         return r;
 }
 



More information about the systemd-commits mailing list