]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: unref sd-device object assigned to Link on remove uevent
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 23 Jul 2022 00:00:32 +0000 (09:00 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 23 Jul 2022 11:04:28 +0000 (20:04 +0900)
Otherwise, outdated device information may be used in a short timespan.

src/network/networkd-link.c

index 8b0ef8f076d4d8a8e53c0a9e1c719f3b75b0b985..838101a217d5a09fdf631155ff9d906d38715c52 100644 (file)
@@ -1495,16 +1495,24 @@ int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t
         assert(m);
         assert(device);
 
-        /* Ignore the "remove" uevent — let's remove a device only if rtnetlink says so. All other uevents
-         * are "positive" events in some form, i.e. inform us about a changed or new network interface, that
-         * still exists — and we are interested in that. */
-        if (action == SD_DEVICE_REMOVE)
-                return 0;
-
         r = sd_device_get_ifindex(device, &ifindex);
         if (r < 0)
                 return log_device_debug_errno(device, r, "Failed to get ifindex: %m");
 
+        r = link_get_by_index(m, ifindex, &link);
+        if (r < 0) {
+                /* This error is not critical, as the corresponding rtnl message may be received later. */
+                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
+                return 0;
+        }
+
+        /* Let's unref the sd-device object assigned to the corresponding Link object, but keep the Link
+         * object here. It will be removed only when rtnetlink says so. */
+        if (action == SD_DEVICE_REMOVE) {
+                link->dev = sd_device_unref(link->dev);
+                return 0;
+        }
+
         r = device_is_renaming(device);
         if (r < 0)
                 return log_device_debug_errno(device, r, "Failed to determine if the device is renaming or not: %m");
@@ -1513,13 +1521,6 @@ int manager_udev_process_link(Manager *m, sd_device *device, sd_device_action_t
                 return 0;
         }
 
-        r = link_get_by_index(m, ifindex, &link);
-        if (r < 0) {
-                /* This error is not critical, as the corresponding rtnl message may be received later. */
-                log_device_debug_errno(device, r, "Failed to get link from ifindex %i, ignoring: %m", ifindex);
-                return 0;
-        }
-
         r = link_initialized(link, device);
         if (r < 0)
                 link_enter_failed(link);