chiark / gitweb /
resolved: add daemon to manage resolv.conf
[elogind.git] / src / network / networkd-manager.c
index effa93c81e918cd5f97d74708cb6dd24269007cc..2e3b4bb885e010249ca56c582a28e6b1c5415ca5 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
  ***/
 
-#include <resolv.h>
+#include <sys/socket.h>
 #include <linux/if.h>
+#include <libkmod.h>
 
+#include "conf-parser.h"
 #include "path-util.h"
 #include "networkd.h"
+#include "network-internal.h"
 #include "libudev-private.h"
 #include "udev-util.h"
 #include "rtnl-util.h"
@@ -117,6 +120,10 @@ int manager_new(Manager **ret) {
                         return -ENOMEM;
         }
 
+        m->kmod_ctx = kmod_new(NULL, NULL);
+        if (!m->kmod_ctx)
+                return -ENOMEM;
+
         m->links = hashmap_new(uint64_hash_func, uint64_compare_func);
         if (!m->links)
                 return -ENOMEM;
@@ -143,6 +150,7 @@ void manager_free(Manager *m) {
 
         free(m->state_file);
 
+        kmod_unref(m->kmod_ctx);
         udev_monitor_unref(m->udev_monitor);
         udev_unref(m->udev);
         sd_bus_unref(m->bus);
@@ -217,97 +225,6 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) {
         return 0;
 }
 
-static int manager_rtnl_process_address(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
-        Manager *m = userdata;
-        Link *link = NULL;
-        uint16_t type;
-        _cleanup_address_free_ Address *address = NULL;
-        char buf[INET6_ADDRSTRLEN];
-        int r, ifindex;
-
-        assert(rtnl);
-        assert(message);
-        assert(m);
-
-        r = sd_rtnl_message_get_type(message, &type);
-        if (r < 0) {
-                log_warning("rtnl: could not get message type");
-                return 0;
-        }
-
-        r = sd_rtnl_message_addr_get_ifindex(message, &ifindex);
-        if (r < 0 || ifindex <= 0) {
-                log_warning("rtnl: received address message without valid ifindix, ignoring");
-                return 0;
-        } else {
-                r = link_get(m, ifindex, &link);
-                if (r < 0 || !link) {
-                        log_warning("rtnl: received address for non-existing link, ignoring");
-                        return 0;
-                }
-        }
-
-        r = address_new_dynamic(&address);
-        if (r < 0)
-                return 0;
-
-        r = sd_rtnl_message_addr_get_family(message, &address->family);
-        if (r < 0 || !IN_SET(address->family, AF_INET, AF_INET6)) {
-                log_warning("rtnl: received address with invalid family, ignoring");
-                return 0;
-        }
-
-        r = sd_rtnl_message_addr_get_prefixlen(message, &address->prefixlen);
-        if (r < 0) {
-                log_warning("rtnl: recevied address with invalid prefixlen, ignoring");
-                return 0;
-        }
-
-        switch (address->family) {
-        case AF_INET:
-                r = sd_rtnl_message_read_in_addr(message, IFA_LOCAL, &address->in_addr.in);
-                if (r < 0) {
-                        log_warning("rtnl: received address without valid address, ignoring");
-                        return 0;
-                }
-
-                break;
-
-        case AF_INET6:
-                r = sd_rtnl_message_read_in6_addr(message, IFA_ADDRESS, &address->in_addr.in6);
-                if (r < 0) {
-                        log_warning("rtnl: received address without valid address, ignoring");
-                        return 0;
-                }
-
-                break;
-
-        default:
-                assert_not_reached("invalid address family");
-        }
-
-        if (!inet_ntop(address->family, &address->in_addr, buf, INET6_ADDRSTRLEN)) {
-                log_warning("could not print address");
-                return 0;
-        }
-
-        switch (type) {
-        case RTM_NEWADDR:
-                log_info("added address: %s/%u to ifindex %d", buf,
-                         address->prefixlen, ifindex);
-                break;
-
-        case RTM_DELADDR:
-                log_info("removed address: %s/%u from ifindex %d", buf,
-                         address->prefixlen, ifindex);
-                break;
-        default:
-                assert_not_reached("Received invalid RTNL message type");
-        }
-
-        return 1;
-}
-
 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
         Manager *m = userdata;
         Link *link = NULL;
@@ -476,11 +393,11 @@ int manager_rtnl_listen(Manager *m) {
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &manager_rtnl_process_address, m);
+        r = sd_rtnl_add_match(m->rtnl, RTM_NEWADDR, &link_rtnl_process_address, m);
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &manager_rtnl_process_address, m);
+        r = sd_rtnl_add_match(m->rtnl, RTM_DELADDR, &link_rtnl_process_address, m);
         if (r < 0)
                 return r;
 
@@ -502,95 +419,6 @@ int manager_bus_listen(Manager *m) {
         return 0;
 }
 
-static void append_dns(FILE *f, struct in_addr *dns, unsigned char family, unsigned *count) {
-        char buf[INET6_ADDRSTRLEN];
-        const char *address;
-
-        address = inet_ntop(family, dns, buf, INET6_ADDRSTRLEN);
-        if (!address) {
-                log_warning("Invalid DNS address. Ignoring.");
-                return;
-        }
-
-        if (*count == MAXNS)
-                fputs("# Too many DNS servers configured, the following entries "
-                      "will be ignored\n", f);
-
-        fprintf(f, "nameserver %s\n", address);
-
-        (*count) ++;
-}
-
-int manager_update_resolv_conf(Manager *m) {
-        _cleanup_free_ char *temp_path = NULL;
-        _cleanup_fclose_ FILE *f = NULL;
-        Link *link;
-        Iterator i;
-        unsigned count = 0;
-        const char *domainname = NULL;
-        int r;
-
-        assert(m);
-
-        r = fopen_temporary("/run/systemd/network/resolv.conf", &f, &temp_path);
-        if (r < 0)
-                return r;
-
-        fchmod(fileno(f), 0644);
-
-        fputs("# This file is managed by systemd-networkd(8). Do not edit.\n#\n"
-              "# Third party programs must not access this file directly, but\n"
-              "# only through the symlink at /etc/resolv.conf. To manage\n"
-              "# resolv.conf(5) in a different way, replace the symlink by a\n"
-              "# static file or a different symlink.\n\n", f);
-
-        HASHMAP_FOREACH(link, m->links, i) {
-                if (link->dhcp_lease) {
-                        struct in_addr *nameservers;
-                        size_t nameservers_size;
-
-                        if (link->network->dhcp_dns) {
-                                r = sd_dhcp_lease_get_dns(link->dhcp_lease, &nameservers, &nameservers_size);
-                                if (r >= 0) {
-                                        unsigned j;
-
-                                        for (j = 0; j < nameservers_size; j++)
-                                                append_dns(f, &nameservers[j], AF_INET, &count);
-                                }
-                        }
-
-                        if (link->network->dhcp_domainname && !domainname) {
-                                r = sd_dhcp_lease_get_domainname(link->dhcp_lease, &domainname);
-                                if (r >= 0)
-                                       fprintf(f, "domain %s\n", domainname);
-                        }
-                }
-        }
-
-        HASHMAP_FOREACH(link, m->links, i) {
-                if (link->network && link->network->dns) {
-                        Address *address;
-                        Iterator j;
-
-                        SET_FOREACH(address, link->network->dns, j) {
-                                append_dns(f, &address->in_addr.in,
-                                           address->family, &count);
-                        }
-                }
-        }
-
-        fflush(f);
-
-        if (ferror(f) || rename(temp_path, "/run/systemd/network/resolv.conf") < 0) {
-                r = -errno;
-                unlink("/run/systemd/network/resolv.conf");
-                unlink(temp_path);
-                return r;
-        }
-
-        return 0;
-}
-
 int manager_save(Manager *m) {
         Link *link;
         Iterator i;