From e21318d22359c7160ea7c7f4a610b28a30d48c84 Mon Sep 17 00:00:00 2001
From: systemd team <systemd-maint@redhat.com>
Date: Tue, 7 Mar 2017 08:20:10 +0000
Subject: [PATCH] udev-add-actions-while-rename-netif-failed
---
 src/udev/udev-event.c | 51 +++++++++++++++++++++++++++++++++++++------
 1 file changed, 44 insertions(+), 7 deletions(-)

diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index ed22c8b..a387517 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -1,5 +1,6 @@
 /* SPDX-License-Identifier: GPL-2.0-or-later */
 
+#include <net/if.h>
 #include "alloc-util.h"
 #include "device-internal.h"
 #include "device-private.h"
@@ -10,6 +11,7 @@
 #include "path-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "strxcpyx.h"
 #include "udev-event.h"
 #include "udev-node.h"
 #include "udev-trace.h"
@@ -107,6 +109,7 @@ static int rename_netif(UdevEvent *event) {
         const char *s;
         sd_device *dev;
         int ifindex, r;
+        char name[IFNAMSIZ];
 
         assert(event);
 
@@ -177,21 +180,55 @@ static int rename_netif(UdevEvent *event) {
                 goto revert;
         }
 
-        r = rtnl_set_link_name(&event->rtnl, ifindex, event->name, event->altnames);
+        strscpy(name, IFNAMSIZ, event->name);
+
+        r = rtnl_set_link_name(&event->rtnl, ifindex, name, event->altnames);
         if (r < 0) {
                 if (r == -EBUSY) {
                         log_device_info(event->dev_db_clone,
                                         "Network interface '%s' is already up, cannot rename to '%s'.",
                                         old_sysname, event->name);
                         r = 0;
-                } else
-                        log_device_error_errno(event->dev_db_clone, r,
-                                               "Failed to rename network interface %i from '%s' to '%s': %m",
-                                               ifindex, old_sysname, event->name);
-                goto revert;
+                        goto revert;
+                }
+                int loop;
+                if (r != -EEXIST) {
+                        log_error_errno(r, "error changing net interface name '%s' to '%s': %m", old_sysname, name);
+                        goto revert;
+                }
+
+                snprintf(name, IFNAMSIZ, "rename%d", ifindex);
+                r = rtnl_set_link_name(&event->rtnl, ifindex, name, event->altnames);
+                if (r < 0) {
+                        log_error_errno(r, "error changing net interface name '%s' to '%s': %m", old_sysname, name);
+                        goto revert;
+                }
+
+                log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, old_sysname, name);
+
+                /* wait 90 seconds for our target to become available */
+                loop = 90 * 20;
+                while (loop--) {
+                        const struct timespec duration = { 0, 1000 * 1000 * 1000 / 20 };
+
+                        r = rtnl_set_link_name(&event->rtnl, ifindex, event->name, event->altnames);
+                        if (r == 0) {
+                                log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, name, event->name);
+                                goto revert;
+                        }
+
+                        if (r != -EEXIST) {
+                                log_error_errno(r, "error changing net interface name '%s' to '%s': %m", name, event->name);
+                                goto revert;
+                        }
+                        log_debug( "wait for netif '%s' to become free, loop=%i\n",
+                                event->name, (90 * 20) - loop);
+                        nanosleep(&duration, NULL);
+                }
+
         }
 
-        log_device_debug(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, old_sysname, event->name);
+        log_device_info(dev, "Network interface %i is renamed from '%s' to '%s'", ifindex, old_sysname, event->name);
         return 1;
 
 revert:
-- 
2.33.0

