chiark / gitweb /
networkd: logging - align messages
[elogind.git] / src / network / networkd-manager.c
index 71c54254e342af7fb68c846698e181678e32fc9a..cc55f9a17a0307d74ef9c901c2bc3e0c2cf6bf53 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <resolv.h>
 #include <linux/if.h>
+#include <libkmod.h>
 
 #include "path-util.h"
 #include "networkd.h"
@@ -91,7 +92,8 @@ int manager_new(Manager **ret) {
 
         sd_event_set_watchdog(m->event, true);
 
-        r = sd_rtnl_open(&m->rtnl, RTMGRP_LINK | RTMGRP_IPV4_IFADDR);
+        r = sd_rtnl_open(&m->rtnl, 3, RTNLGRP_LINK, RTNLGRP_IPV4_IFADDR,
+                         RTNLGRP_IPV6_IFADDR);
         if (r < 0)
                 return r;
 
@@ -116,6 +118,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;
@@ -142,6 +148,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);
@@ -219,6 +226,8 @@ static int manager_udev_process_link(Manager *m, struct udev_device *device) {
 static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, void *userdata) {
         Manager *m = userdata;
         Link *link = NULL;
+        NetDev *netdev = NULL;
+        uint16_t type;
         char *name;
         int r, ifindex;
 
@@ -226,41 +235,61 @@ static int manager_rtnl_process_link(sd_rtnl *rtnl, sd_rtnl_message *message, vo
         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_link_get_ifindex(message, &ifindex);
         if (r < 0 || ifindex <= 0) {
                 log_warning("rtnl: received link message without valid ifindex");
                 return 0;
-        }
-
-        link_get(m, ifindex, &link);
-        if (!link) {
-                /* link is new, so add it */
-                r = link_add(m, message, &link);
-                if (r < 0) {
-                        log_debug("could not add new link");
-                        return 0;
-                }
-        }
+        } else
+                link_get(m, ifindex, &link);
 
         r = sd_rtnl_message_read_string(message, IFLA_IFNAME, &name);
-        if (r < 0)
+        if (r < 0 || !name) {
                 log_warning("rtnl: received link message without valid ifname");
-        else {
-                NetDev *netdev;
+                return 0;
+        } else
+                netdev_get(m, name, &netdev);
+
+        switch (type) {
+        case RTM_NEWLINK:
+                if (!link) {
+                        /* link is new, so add it */
+                        r = link_add(m, message, &link);
+                        if (r < 0) {
+                                log_debug("could not add new link");
+                                return 0;
+                        }
+                }
 
-                r = netdev_get(m, name, &netdev);
-                if (r >= 0) {
+                if (netdev) {
+                        /* netdev exists, so make sure the ifindex matches */
                         r = netdev_set_ifindex(netdev, message);
                         if (r < 0) {
                                 log_debug("could not set ifindex on netdev");
                                 return 0;
                         }
                 }
-        }
 
-        r = link_update(link, message);
-        if (r < 0)
-                return 0;
+                r = link_update(link, message);
+                if (r < 0)
+                        return 0;
+
+                break;
+
+        case RTM_DELLINK:
+                link_drop(link);
+                netdev_drop(netdev);
+
+                break;
+
+        default:
+                assert_not_reached("Received invalid RTNL message type.");
+        }
 
         return 1;
 }
@@ -358,6 +387,18 @@ int manager_rtnl_listen(Manager *m) {
         if (r < 0)
                 return r;
 
+        r = sd_rtnl_add_match(m->rtnl, RTM_DELLINK, &manager_rtnl_process_link, m);
+        if (r < 0)
+                return r;
+
+        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, &link_rtnl_process_address, m);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
@@ -471,7 +512,7 @@ int manager_save(Manager *m) {
         _cleanup_free_ char *temp_path = NULL;
         _cleanup_fclose_ FILE *f = NULL;
         const char *oper_state = "unknown";
-        bool dormant, carrier;
+        bool dormant = false, carrier = false;
         int r;
 
         assert(m);