]> git.ipfire.org Git - people/ms/network.git/commitdiff
links: Initialize udev device when links are created
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Jun 2023 12:04:18 +0000 (12:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 10 Jun 2023 12:04:18 +0000 (12:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/networkd/link.c

index b0d9e86501c8f95c9b6504e2e79f70eb722899c8..8ba5da59521311de667508dcf0d5146074fe9638 100644 (file)
@@ -245,6 +245,53 @@ static int nw_link_carrier_lost(nw_link* link) {
        return 0; // XXX TODO
 }
 
+static int nw_link_initialize(nw_link* link) {
+       sd_device *device = NULL;
+       int r;
+
+       // Fetch device
+       r = sd_device_new_from_ifindex(&device, link->ifindex);
+       if (r < 0) {
+               WARNING("Could not find device for link %d: %s\n", link->ifindex, strerror(-r));
+               r = 0;
+               goto ERROR;
+       }
+
+       // Check if device is initialized
+       r = sd_device_get_is_initialized(device);
+       switch (r) {
+               // Initialized - fallthrough
+               case 0:
+                       break;
+
+               case 1:
+                       DEBUG("The device has not been initialized, yet\n");
+                       r = 0;
+                       goto ERROR;
+
+               default:
+                       WARNING("Could not check whether device is initialized, ignoring: %s\n",
+                               strerror(-r));
+                       goto ERROR;
+       }
+
+       // XXX Check renaming?!
+
+       if (link->device)
+               sd_device_unref(link->device);
+
+       // Store the device
+       link->device = sd_device_ref(device);
+
+       DEBUG("Link %d has been initialized\n", link->ifindex);
+
+ERROR:
+       if (device)
+               sd_device_unref(device);
+
+       return r;
+}
+
 static int nw_link_update_ifname(nw_link* link, sd_netlink_message* message) {
        const char* ifname = NULL;
        int r;
@@ -476,6 +523,11 @@ int nw_link_process(sd_netlink* rtnl, sd_netlink_message* message, void* data) {
                                        goto ERROR;
                                }
 
+                               // Initialize the link
+                               r = nw_link_initialize(link);
+                               if (r < 0)
+                                       goto ERROR;
+
                                // Add it to the list
                                r = nw_links_add_link(links, link);
                                if (r)