ryantimwilson / rpms / systemd

Forked from rpms/systemd a month ago
Clone
be0c12
From 73ff88cdb6bd1991d75323c6c364bcc9bce7efda Mon Sep 17 00:00:00 2001
be0c12
From: Yu Watanabe <watanabe.yu+github@gmail.com>
be0c12
Date: Fri, 17 Jul 2020 21:31:24 +0900
be0c12
Subject: [PATCH] netlink: do not fail when new interface name is already used
be0c12
 as an alternative name
be0c12
be0c12
When renaming a network interface, the new name may be used as an
be0c12
alternative name. In that case, let's swap the current name and the
be0c12
alternative name. That is, first drop the new name from the list of
be0c12
alternative names, then rename the interface, finally set the old name
be0c12
as an alternative name.
be0c12
be0c12
(cherry picked from commit 434a34838034347f45fb9a47df55b1a36e5addfd)
be0c12
be0c12
Related: #2005008
be0c12
---
be0c12
 src/libsystemd/sd-netlink/netlink-util.c | 30 +++++++++++++++++++++---
be0c12
 1 file changed, 27 insertions(+), 3 deletions(-)
be0c12
be0c12
diff --git a/src/libsystemd/sd-netlink/netlink-util.c b/src/libsystemd/sd-netlink/netlink-util.c
be0c12
index 7f09261981..4e42ef9e26 100644
be0c12
--- a/src/libsystemd/sd-netlink/netlink-util.c
be0c12
+++ b/src/libsystemd/sd-netlink/netlink-util.c
be0c12
@@ -1,23 +1,40 @@
be0c12
 /* SPDX-License-Identifier: LGPL-2.1+ */
be0c12
 
be0c12
+#include <net/if.h>
be0c12
+
be0c12
 #include "sd-netlink.h"
be0c12
 
be0c12
 #include "netlink-internal.h"
be0c12
 #include "netlink-util.h"
be0c12
+#include "socket-util.h"
be0c12
+#include "string-util.h"
be0c12
 #include "strv.h"
be0c12
 
be0c12
 int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
be0c12
         _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
be0c12
+        _cleanup_strv_free_ char **alternative_names = NULL;
be0c12
+        char old_name[IF_NAMESIZE + 1] = {};
be0c12
         int r;
be0c12
 
be0c12
         assert(rtnl);
be0c12
         assert(ifindex > 0);
be0c12
         assert(name);
be0c12
 
be0c12
-        if (!*rtnl) {
be0c12
-                r = sd_netlink_open(rtnl);
be0c12
+        if (!ifname_valid(name))
be0c12
+                return -EINVAL;
be0c12
+
be0c12
+        r = rtnl_get_link_alternative_names(rtnl, ifindex, &alternative_names);
be0c12
+        if (r < 0)
be0c12
+                log_debug_errno(r, "Failed to get alternative names on network interface %i, ignoring: %m",
be0c12
+                                ifindex);
be0c12
+
be0c12
+        if (strv_contains(alternative_names, name)) {
be0c12
+                r = rtnl_delete_link_alternative_names(rtnl, ifindex, STRV_MAKE(name));
be0c12
                 if (r < 0)
be0c12
-                        return r;
be0c12
+                        return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m",
be0c12
+                                               name, ifindex);
be0c12
+
be0c12
+                if_indextoname(ifindex, old_name);
be0c12
         }
be0c12
 
be0c12
         r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex);
be0c12
@@ -32,6 +49,13 @@ int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) {
be0c12
         if (r < 0)
be0c12
                 return r;
be0c12
 
be0c12
+        if (!isempty(old_name)) {
be0c12
+                r = rtnl_set_link_alternative_names(rtnl, ifindex, STRV_MAKE(old_name));
be0c12
+                if (r < 0)
be0c12
+                        log_debug_errno(r, "Failed to set '%s' as an alternative name on network interface %i, ignoring: %m",
be0c12
+                                        old_name, ifindex);
be0c12
+        }
be0c12
+
be0c12
         return 0;
be0c12
 }
be0c12