]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev/net: split out udev property assignment logic
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 5 Jan 2024 11:36:52 +0000 (20:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Jan 2024 19:27:41 +0000 (04:27 +0900)
No functional change, just refactoring.

src/udev/net/link-config.c
src/udev/net/link-config.h
src/udev/udev-builtin-net_setup_link.c

index 8689630f4b5392c85e4aad4adec81a05e84f35fa..c9c4e9c9270feea9dfe016058fbac60b11f057f7 100644 (file)
@@ -15,6 +15,7 @@
 #include "creds-util.h"
 #include "device-private.h"
 #include "device-util.h"
+#include "escape.h"
 #include "ethtool-util.h"
 #include "fd-util.h"
 #include "fileio.h"
@@ -34,6 +35,7 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "strv.h"
+#include "udev-builtin.h"
 #include "utf8.h"
 
 struct LinkConfigContext {
@@ -921,7 +923,38 @@ static int link_apply_sr_iov_config(Link *link, sd_netlink **rtnl) {
         return 0;
 }
 
-int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link) {
+static int link_apply_udev_properties(Link *link, bool test) {
+        LinkConfig *config;
+        sd_device *device;
+
+        assert(link);
+
+        config = ASSERT_PTR(link->config);
+        device = ASSERT_PTR(link->device);
+
+        (void) udev_builtin_add_property(device, test, "ID_NET_LINK_FILE", config->filename);
+
+        _cleanup_free_ char *joined = NULL;
+        STRV_FOREACH(d, config->dropins) {
+                _cleanup_free_ char *escaped = NULL;
+
+                escaped = xescape(*d, ":");
+                if (!escaped)
+                        return log_oom();
+
+                if (!strextend_with_separator(&joined, ":", escaped))
+                        return log_oom();
+        }
+
+        (void) udev_builtin_add_property(device, test, "ID_NET_LINK_FILE_DROPINS", joined);
+
+        if (link->new_name)
+                (void) udev_builtin_add_property(device, test, "ID_NET_NAME", link->new_name);
+
+        return 0;
+}
+
+int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link, bool test) {
         int r;
 
         assert(ctx);
@@ -948,6 +981,10 @@ int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link) {
         if (r < 0)
                 return r;
 
+        r = link_apply_udev_properties(link, test);
+        if (r < 0)
+                return r;
+
         return 0;
 }
 
index bab9d12970a024a01ced97056fb73ee63b56dc4f..06666a23ba1535e0964e578fa7a969869f0d6c3e 100644 (file)
@@ -100,7 +100,7 @@ Link *link_free(Link *link);
 DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_free);
 
 int link_get_config(LinkConfigContext *ctx, Link *link);
-int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link);
+int link_apply_config(LinkConfigContext *ctx, sd_netlink **rtnl, Link *link, bool test);
 
 const char *mac_address_policy_to_string(MACAddressPolicy p) _const_;
 MACAddressPolicy mac_address_policy_from_string(const char *p) _pure_;
index ad73b8e1c440f3a02c2441d0c7c0f09b6baa3578..59a0043422be25cc0d7b08409d6b57044b2a4c2b 100644 (file)
@@ -3,7 +3,6 @@
 #include "alloc-util.h"
 #include "device-private.h"
 #include "device-util.h"
-#include "escape.h"
 #include "errno-util.h"
 #include "link-config.h"
 #include "log.h"
@@ -16,7 +15,6 @@ static LinkConfigContext *ctx = NULL;
 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;
-        _cleanup_free_ char *joined = NULL;
         int r;
 
         if (argc > 1)
@@ -61,31 +59,14 @@ static int builtin_net_setup_link(UdevEvent *event, int argc, char **argv, bool
                 return log_device_error_errno(dev, r, "Failed to get link config: %m");
         }
 
-        r = link_apply_config(ctx, &event->rtnl, link);
+        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->config->filename);
-        if (link->new_name)
-                udev_builtin_add_property(dev, test, "ID_NET_NAME", link->new_name);
-
         event->altnames = TAKE_PTR(link->altnames);
 
-        STRV_FOREACH(d, link->config->dropins) {
-                _cleanup_free_ char *escaped = NULL;
-
-                escaped = xescape(*d, ":");
-                if (!escaped)
-                        return log_oom();
-
-                if (!strextend_with_separator(&joined, ":", escaped))
-                        return log_oom();
-        }
-
-        udev_builtin_add_property(dev, test, "ID_NET_LINK_FILE_DROPINS", joined);
-
         return 0;
 }