+
+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;
+}