]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/udev-builtin-net_setup_link.c
udev/net: introduce [Link] Property=, ImportProperty=, and UnsetProperty= settings
[thirdparty/systemd.git] / src / udev / udev-builtin-net_setup_link.c
index d40251331ce85b109ff8b78445d3cee2d277d6fd..fc614443efadcd69730ee03bdefe3374f76f6076 100644 (file)
@@ -1,51 +1,71 @@
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 
 #include "alloc-util.h"
+#include "device-private.h"
 #include "device-util.h"
 #include "errno-util.h"
 #include "link-config.h"
 #include "log.h"
 #include "string-util.h"
+#include "strv.h"
 #include "udev-builtin.h"
 
 static LinkConfigContext *ctx = NULL;
 
-static int builtin_net_setup_link(sd_device *dev, int argc, char **argv, bool test) {
-        _cleanup_free_ char *driver = NULL;
-        const char *name = NULL;
-        LinkConfig *link;
+static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv, bool test) {
+        sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
+        _cleanup_(link_freep) Link *link = NULL;
         int r;
 
         if (argc > 1)
                 return log_device_error_errno(dev, SYNTHETIC_ERRNO(EINVAL), "This program takes no arguments.");
 
-        r = link_get_driver(ctx, dev, &driver);
+        sd_device_action_t action;
+        r = sd_device_get_action(dev, &action);
         if (r < 0)
-                log_device_full_errno(dev, ERRNO_IS_NOT_SUPPORTED(r) || r == -ENODEV ? LOG_DEBUG : LOG_WARNING,
-                                      r, "Failed to query device driver: %m");
-        else
-                udev_builtin_add_property(dev, test, "ID_NET_DRIVER", driver);
+                return log_device_error_errno(dev, r, "Failed to get action: %m");
 
-        r = link_config_get(ctx, dev, &link);
+        if (!IN_SET(action, SD_DEVICE_ADD, SD_DEVICE_BIND, SD_DEVICE_MOVE)) {
+                log_device_debug(dev, "Skipping to apply .link settings on '%s' uevent.",
+                                 device_action_to_string(action));
+
+                /* Import previously assigned .link file name. */
+                (void) udev_builtin_import_property(dev, event->dev_db_clone, test, "ID_NET_LINK_FILE");
+                (void) udev_builtin_import_property(dev, event->dev_db_clone, test, "ID_NET_LINK_FILE_DROPINS");
+
+                /* Set ID_NET_NAME= with the current interface name. */
+                const char *value;
+                if (sd_device_get_sysname(dev, &value) >= 0)
+                        (void) udev_builtin_add_property(dev, test, "ID_NET_NAME", value);
+
+                return 0;
+        }
+
+        r = link_new(ctx, &event->rtnl, dev, event->dev_db_clone, &link);
+        if (r == -ENODEV) {
+                log_device_debug_errno(dev, r, "Link vanished while getting information, ignoring.");
+                return 0;
+        }
+        if (r < 0)
+                return log_device_warning_errno(dev, r, "Failed to get link information: %m");
+
+        r = link_get_config(ctx, link);
         if (r < 0) {
-                if (r == -ENOENT)
-                        return log_device_debug_errno(dev, r, "No matching link configuration found.");
-                if (r == -ENODEV)
-                        return log_device_debug_errno(dev, r, "Link vanished while searching for configuration for it.");
+                if (r == -ENOENT) {
+                        log_device_debug_errno(dev, r, "No matching link configuration found, ignoring device.");
+                        return 0;
+                }
 
                 return log_device_error_errno(dev, r, "Failed to get link config: %m");
         }
 
-        r = link_config_apply(ctx, link, dev, &name);
+        r = link_apply_config(ctx, &event->rtnl, link, test);
         if (r == -ENODEV)
                 log_device_debug_errno(dev, r, "Link vanished while applying configuration, ignoring.");
         else if (r < 0)
                 log_device_warning_errno(dev, r, "Could not apply link configuration, ignoring: %m");
 
-        udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE", link->filename);
-
-        if (name)
-                udev_builtin_add_property(dev, test, "ID_NET_NAME", name);
+        event->altnames = TAKE_PTR(link->altnames);
 
         return 0;
 }
@@ -73,12 +93,16 @@ static void builtin_net_setup_link_exit(void) {
         log_debug("Unloaded link configuration context.");
 }
 
-static bool builtin_net_setup_link_validate(void) {
-        log_debug("Check if link configuration needs reloading.");
+static bool builtin_net_setup_link_should_reload(void) {
         if (!ctx)
                 return false;
 
-        return link_config_should_reload(ctx);
+        if (link_config_should_reload(ctx)) {
+                log_debug("Link configuration context needs reloading.");
+                return true;
+        }
+
+        return false;
 }
 
 const UdevBuiltin udev_builtin_net_setup_link = {
@@ -86,7 +110,7 @@ const UdevBuiltin udev_builtin_net_setup_link = {
         .cmd = builtin_net_setup_link,
         .init = builtin_net_setup_link_init,
         .exit = builtin_net_setup_link_exit,
-        .validate = builtin_net_setup_link_validate,
+        .should_reload = builtin_net_setup_link_should_reload,
         .help = "Configure network link",
         .run_once = false,
 };