chiark / gitweb /
networkd: resolv.conf - reword comment
[elogind.git] / src / network / networkd-manager.c
index 8378e92a81e150ade9fc530ef876adff1bdd946d..8eaf101df3956e9d4ac56f24ef9732f1544ccfdd 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
  ***/
 
+#include <resolv.h>
+
 #include "path-util.h"
 #include "networkd.h"
 #include "libudev-private.h"
+#include "udev-util.h"
+#include "mkdir.h"
+
+const char* const network_dirs[] = {
+        "/etc/systemd/network",
+        "/run/systemd/network",
+        "/usr/lib/systemd/network",
+#ifdef HAVE_SPLIT_USER
+        "/lib/systemd/network",
+#endif
+        NULL};
 
 int manager_new(Manager **ret) {
         _cleanup_manager_free_ Manager *m = NULL;
@@ -35,7 +48,13 @@ int manager_new(Manager **ret) {
         if (r < 0)
                 return r;
 
-        r = sd_rtnl_open(0, &m->rtnl);
+        sd_event_set_watchdog(m->event, true);
+
+        r = sd_rtnl_open(RTMGRP_LINK | RTMGRP_IPV4_IFADDR, &m->rtnl);
+        if (r < 0)
+                return r;
+
+        r = sd_bus_default_system(&m->bus);
         if (r < 0)
                 return r;
 
@@ -51,20 +70,11 @@ int manager_new(Manager **ret) {
         if (!m->links)
                 return -ENOMEM;
 
-        LIST_HEAD_INIT(m->networks);
-
-        m->network_dirs = strv_new("/etc/systemd/network/",
-                        "/run/systemd/network/",
-                        "/usr/lib/systemd/network",
-#ifdef HAVE_SPLIT_USER
-                        "/lib/systemd/network",
-#endif
-                        NULL);
-        if (!m->network_dirs)
+        m->bridges = hashmap_new(string_hash_func, string_compare_func);
+        if (!m->bridges)
                 return -ENOMEM;
 
-        if (!path_strv_canonicalize_uniq(m->network_dirs))
-                return -ENOMEM;
+        LIST_HEAD_INIT(m->networks);
 
         *ret = m;
         m = NULL;
@@ -73,17 +83,53 @@ int manager_new(Manager **ret) {
 }
 
 void manager_free(Manager *m) {
+        Network *network;
+        Bridge *bridge;
+        Link *link;
+
         udev_monitor_unref(m->udev_monitor);
         udev_unref(m->udev);
+        sd_bus_unref(m->bus);
         sd_event_source_unref(m->udev_event_source);
         sd_event_unref(m->event);
+
+        while ((network = m->networks))
+                network_free(network);
+
+        while ((link = hashmap_first(m->links)))
+                link_free(link);
         hashmap_free(m->links);
-        strv_free(m->network_dirs);
+
+        while ((bridge = hashmap_first(m->bridges)))
+                bridge_free(bridge);
+        hashmap_free(m->bridges);
+
         sd_rtnl_unref(m->rtnl);
 
         free(m);
 }
 
+int manager_load_config(Manager *m) {
+        int r;
+
+        /* update timestamp */
+        paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, true);
+
+        r = bridge_load(m);
+        if (r < 0)
+                return r;
+
+        r = network_load(m);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+bool manager_should_reload(Manager *m) {
+        return paths_check_timestamp(network_dirs, &m->network_dirs_ts_usec, false);
+}
+
 static int manager_process_link(Manager *m, struct udev_device *device) {
         Link *link;
         int r;
@@ -91,6 +137,8 @@ static int manager_process_link(Manager *m, struct udev_device *device) {
         if (streq_ptr(udev_device_get_action(device), "remove")) {
                 uint64_t ifindex;
 
+                log_debug("%s: link removed", udev_device_get_sysname(device));
+
                 ifindex = udev_device_get_ifindex(device);
                 link = hashmap_get(m->links, &ifindex);
                 if (!link)
@@ -98,83 +146,73 @@ static int manager_process_link(Manager *m, struct udev_device *device) {
 
                 link_free(link);
         } else {
-                r = link_add(m, device);
+                r = link_add(m, device, &link);
                 if (r < 0) {
-                        log_error("Could not handle link %s: %s",
-                                        udev_device_get_sysname(device),
-                                        strerror(-r));
-                }
+                        if (r == -EEXIST)
+                                log_debug("%s: link already exists, ignoring",
+                                          link->ifname);
+                        else
+                                log_error("%s: could not handle link: %s",
+                                          udev_device_get_sysname(device),
+                                          strerror(-r));
+                } else
+                        log_debug("%s: link (with ifindex %" PRIu64") added",
+                                  link->ifname, link->ifindex);
         }
 
         return 0;
 }
 
 int manager_udev_enumerate_links(Manager *m) {
+        _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
         struct udev_list_entry *item = NULL, *first = NULL;
-        struct udev_enumerate *e;
         int r;
 
         assert(m);
 
         e = udev_enumerate_new(m->udev);
-        if (!e) {
-                r = -ENOMEM;
-                goto finish;
-        }
+        if (!e)
+                return -ENOMEM;
 
         r = udev_enumerate_add_match_subsystem(e, "net");
         if (r < 0)
-                goto finish;
+                return r;
 
-        r = udev_enumerate_add_match_tag(e, "systemd-networkd");
+        r = udev_enumerate_add_match_is_initialized(e);
         if (r < 0)
-                goto finish;
+                return r;
 
         r = udev_enumerate_scan_devices(e);
         if (r < 0)
-                goto finish;
+                return r;
 
         first = udev_enumerate_get_list_entry(e);
         udev_list_entry_foreach(item, first) {
-                struct udev_device *d;
+                _cleanup_udev_device_unref_ struct udev_device *d = NULL;
                 int k;
 
                 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
-                if (!d) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
+                if (!d)
+                        return -ENOMEM;
 
                 k = manager_process_link(m, d);
-                udev_device_unref(d);
-
                 if (k < 0)
                         r = k;
         }
 
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
         return r;
 }
 
 static int manager_dispatch_link_udev(sd_event_source *source, int fd, uint32_t revents, void *userdata) {
         Manager *m = userdata;
         struct udev_monitor *monitor = m->udev_monitor;
-        struct udev_device *device;
-        int r;
+        _cleanup_udev_device_unref_ struct udev_device *device = NULL;
 
         device = udev_monitor_receive_device(monitor);
         if (!device)
                 return -ENOMEM;
 
-        r = manager_process_link(m, device);
-        if (r < 0)
-                return r;
-
-        udev_device_unref(device);
-
+        manager_process_link(m, device);
         return 0;
 }
 
@@ -187,12 +225,6 @@ int manager_udev_listen(Manager *m) {
                 return r;
         }
 
-        r = udev_monitor_filter_add_match_tag(m->udev_monitor, "systemd-networkd");
-        if (r < 0) {
-                log_error("Could not add udev monitor filter: %s", strerror(-r));
-                return r;
-        }
-
         r = udev_monitor_enable_receiving(m->udev_monitor);
         if (r < 0) {
                 log_error("Could not enable udev monitor");
@@ -208,3 +240,134 @@ int manager_udev_listen(Manager *m) {
 
         return 0;
 }
+
+static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
+        Manager *m = userdata;
+        Link *link;
+        uint64_t ifindex_64;
+        int r, ifindex;
+
+        r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
+        if (r < 0) {
+                log_debug("received RTM_NEWLINK message without valid ifindex");
+                return 0;
+        }
+
+        ifindex_64 = ifindex;
+        link = hashmap_get(m->links, &ifindex_64);
+        if (!link) {
+                log_debug("received RTM_NEWLINK message for untracked ifindex %d", ifindex);
+                return 0;
+        }
+
+        /* only track the status of links we want to manage */
+        if (link->network) {
+                r = link_update(link, message);
+                if (r < 0)
+                        return 0;
+        } else
+                log_debug("%s: received RTM_NEWLINK message for unmanaged link", link->ifname);
+
+        return 1;
+}
+
+int manager_rtnl_listen(Manager *m) {
+        int r;
+
+        r = sd_rtnl_attach_event(m->rtnl, m->event, 0);
+        if (r < 0)
+                return r;
+
+        r = sd_rtnl_add_match(m->rtnl, RTM_NEWLINK, &manager_rtnl_process_link, m);
+        if (r < 0)
+                return r;
+
+        return 0;
+}
+
+int manager_bus_listen(Manager *m) {
+        int r;
+
+        r = sd_bus_attach_event(m->bus, m->event, 0);
+        if (r < 0)
+                return r;
+
+        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;
+        int r;
+
+        assert(m);
+
+        r = mkdir_safe_label("/run/systemd/network", 0755, 0, 0);
+        if (r < 0)
+                return r;
+
+        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) {
+                        struct in_addr *nameservers;
+                        size_t nameservers_size;
+
+                        r = sd_dhcp_client_get_dns(link->dhcp, &nameservers, &nameservers_size);
+                        if (r >= 0) {
+                                unsigned j;
+
+                                for (j = 0; j < nameservers_size; j++)
+                                        append_dns(f, &nameservers[j], AF_INET, &count);
+                        }
+                }
+        }
+
+        HASHMAP_FOREACH(link, m->links, i)
+                if (link->network && link->network->dns)
+                        append_dns(f, &link->network->dns->in_addr.in,
+                                   link->network->dns->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;
+}