daandemeyer / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
698723
From 4f47e35ee4026f24ee99a0bfa7ba5b2f24a92a02 Mon Sep 17 00:00:00 2001
698723
From: Yu Watanabe <watanabe.yu+github@gmail.com>
698723
Date: Mon, 16 Dec 2019 23:44:42 +0900
698723
Subject: [PATCH] udev: introduce AlternativeNamesPolicy= setting
698723
698723
(cherry picked from commit ef1d2c07f9567dfea8a4e012d8779a4ded2d9ae6)
698723
698723
Resolves: #1850986
698723
---
698723
 man/systemd.link.xml                 | 11 +++++
698723
 src/udev/net/link-config-gperf.gperf |  1 +
698723
 src/udev/net/link-config.c           | 62 ++++++++++++++++++++++++++--
698723
 src/udev/net/link-config.h           |  5 +++
698723
 4 files changed, 76 insertions(+), 3 deletions(-)
698723
698723
diff --git a/man/systemd.link.xml b/man/systemd.link.xml
698723
index c8ebb751ee..13dcce0879 100644
698723
--- a/man/systemd.link.xml
698723
+++ b/man/systemd.link.xml
698723
@@ -343,6 +343,17 @@
698723
           </para>
698723
         </listitem>
698723
       </varlistentry>
698723
+      <varlistentry>
698723
+        <term><varname>AlternativeNamesPolicy=</varname></term>
698723
+        <listitem>
698723
+          <para>A space-separated list of policies by which the interface's alternative names
698723
+          should be set. Each of the policies may fail, and all successful policies are used. The
698723
+          available policies are <literal>database</literal>, <literal>onboard</literal>,
698723
+          <literal>slot</literal>, <literal>path</literal>, and <literal>mac</literal>. If the
698723
+          kernel does not support the alternative names, then this setting will be ignored.
698723
+          </para>
698723
+        </listitem>
698723
+      </varlistentry>
698723
       <varlistentry>
698723
         <term><varname>AlternativeName=</varname></term>
698723
         <listitem>
698723
diff --git a/src/udev/net/link-config-gperf.gperf b/src/udev/net/link-config-gperf.gperf
698723
index 913c754145..df8404e7b8 100644
698723
--- a/src/udev/net/link-config-gperf.gperf
698723
+++ b/src/udev/net/link-config-gperf.gperf
698723
@@ -35,6 +35,7 @@ Link.MACAddress,                 config_parse_hwaddr,        0,
698723
 Link.NamePolicy,                 config_parse_name_policy,   0,                             offsetof(link_config, name_policy)
698723
 Link.Name,                       config_parse_ifname,        0,                             offsetof(link_config, name)
698723
 Link.AlternativeName,            config_parse_ifnames,       1,                             offsetof(link_config, alternative_names)
698723
+Link.AlternativeNamesPolicy,     config_parse_alternative_names_policy, 0,                  offsetof(link_config, alternative_names_policy)
698723
 Link.Alias,                      config_parse_ifalias,       0,                             offsetof(link_config, alias)
698723
 Link.MTUBytes,                   config_parse_mtu,           AF_UNSPEC,                     offsetof(link_config, mtu)
698723
 Link.BitsPerSecond,              config_parse_si_size,       0,                             offsetof(link_config, speed)
698723
diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c
698723
index 8e88c8e5c4..6ceb4c698e 100644
698723
--- a/src/udev/net/link-config.c
698723
+++ b/src/udev/net/link-config.c
698723
@@ -69,6 +69,7 @@ static void link_config_free(link_config *link) {
698723
         free(link->name_policy);
698723
         free(link->name);
698723
         strv_free(link->alternative_names);
698723
+        free(link->alternative_names_policy);
698723
         free(link->alias);
698723
 
698723
         free(link);
698723
@@ -349,6 +350,7 @@ static int get_mac(struct udev_device *device, bool want_random,
698723
 
698723
 int link_config_apply(link_config_ctx *ctx, link_config *config,
698723
                       struct udev_device *device, const char **name) {
698723
+        _cleanup_strv_free_ char **altnames = NULL;
698723
         bool respect_predictable = false;
698723
         struct ether_addr generated_mac;
698723
         struct ether_addr *mac = NULL;
698723
@@ -473,11 +475,52 @@ int link_config_apply(link_config_ctx *ctx, link_config *config,
698723
         if (r < 0)
698723
                 return log_warning_errno(r, "Could not set Alias=, MACAddress= or MTU= on %s: %m", old_name);
698723
 
698723
-        r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, config->alternative_names);
698723
+        if (config->alternative_names) {
698723
+                altnames = strv_copy(config->alternative_names);
698723
+                if (!altnames)
698723
+                        return log_oom();
698723
+        }
698723
+
698723
+        if (config->alternative_names_policy)
698723
+                for (NamePolicy *p = config->alternative_names_policy; *p != _NAMEPOLICY_INVALID; p++) {
698723
+                        const char *n;
698723
+
698723
+                        switch (*p) {
698723
+                        case NAMEPOLICY_DATABASE:
698723
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_FROM_DATABASE");
698723
+                                break;
698723
+                        case NAMEPOLICY_ONBOARD:
698723
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_ONBOARD");
698723
+                                break;
698723
+                        case NAMEPOLICY_SLOT:
698723
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_SLOT");
698723
+                                break;
698723
+                        case NAMEPOLICY_PATH:
698723
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_PATH");
698723
+                                break;
698723
+                        case NAMEPOLICY_MAC:
698723
+                                n = udev_device_get_property_value(device, "ID_NET_NAME_MAC");
698723
+                                break;
698723
+                        default:
698723
+                                assert_not_reached("invalid policy");
698723
+                        }
698723
+                        if (!isempty(n)) {
698723
+                                r = strv_extend(&altnames, n);
698723
+                                if (r < 0)
698723
+                                        return log_oom();
698723
+                        }
698723
+                }
698723
+
698723
+        if (new_name)
698723
+                strv_remove(altnames, new_name);
698723
+        strv_remove(altnames, old_name);
698723
+        strv_uniq(altnames);
698723
+
698723
+        r = rtnl_set_link_alternative_names(&ctx->rtnl, ifindex, altnames);
698723
         if (r == -EOPNOTSUPP)
698723
-                log_debug_errno(r, "Could not set AlternativeName= on %s, ignoring: %m", old_name);
698723
+                log_debug_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s, ignoring: %m", old_name);
698723
         else if (r < 0)
698723
-                return log_warning_errno(r, "Could not set AlternativeName= on %s: %m", old_name);
698723
+                return log_warning_errno(r, "Could not set AlternativeName= or apply AlternativeNamesPolicy= on %s: %m", old_name);
698723
 
698723
         *name = new_name;
698723
 
698723
@@ -524,3 +567,16 @@ DEFINE_STRING_TABLE_LOOKUP(name_policy, NamePolicy);
698723
 DEFINE_CONFIG_PARSE_ENUMV(config_parse_name_policy, name_policy, NamePolicy,
698723
                           _NAMEPOLICY_INVALID,
698723
                           "Failed to parse interface name policy");
698723
+
698723
+static const char* const alternative_names_policy_table[_NAMEPOLICY_MAX] = {
698723
+        [NAMEPOLICY_DATABASE] = "database",
698723
+        [NAMEPOLICY_ONBOARD] = "onboard",
698723
+        [NAMEPOLICY_SLOT] = "slot",
698723
+        [NAMEPOLICY_PATH] = "path",
698723
+        [NAMEPOLICY_MAC] = "mac",
698723
+};
698723
+
698723
+DEFINE_STRING_TABLE_LOOKUP(alternative_names_policy, NamePolicy);
698723
+DEFINE_CONFIG_PARSE_ENUMV(config_parse_alternative_names_policy, alternative_names_policy, NamePolicy,
698723
+                          _NAMEPOLICY_INVALID,
698723
+                          "Failed to parse alternative names policy");
698723
diff --git a/src/udev/net/link-config.h b/src/udev/net/link-config.h
698723
index 93d5fdce59..634bd2ec54 100644
698723
--- a/src/udev/net/link-config.h
698723
+++ b/src/udev/net/link-config.h
698723
@@ -49,6 +49,7 @@ struct link_config {
698723
         struct ether_addr *mac;
698723
         MACPolicy mac_policy;
698723
         NamePolicy *name_policy;
698723
+        NamePolicy *alternative_names_policy;
698723
         char *name;
698723
         char **alternative_names;
698723
         char *alias;
698723
@@ -78,6 +79,9 @@ int link_get_driver(link_config_ctx *ctx, struct udev_device *device, char **ret
698723
 const char *name_policy_to_string(NamePolicy p) _const_;
698723
 NamePolicy name_policy_from_string(const char *p) _pure_;
698723
 
698723
+const char *alternative_names_policy_to_string(NamePolicy p) _const_;
698723
+NamePolicy alternative_names_policy_from_string(const char *p) _pure_;
698723
+
698723
 const char *mac_policy_to_string(MACPolicy p) _const_;
698723
 MACPolicy mac_policy_from_string(const char *p) _pure_;
698723
 
698723
@@ -86,3 +90,4 @@ const struct ConfigPerfItem* link_config_gperf_lookup(const char *key, GPERF_LEN
698723
 
698723
 int config_parse_mac_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
698723
 int config_parse_name_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
698723
+int config_parse_alternative_names_policy(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);