ryantimwilson / rpms / systemd

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