From 4d600667f8af2985850b03a46357e068d3fb8570 Mon Sep 17 00:00:00 2001 From: Nick Rosbrook Date: Wed, 2 Nov 2022 05:36:14 -0400 Subject: sd-netlink: restore altname on error in rtnl_set_link_name If a current alternative name is to be used to rename a network interface, the alternative name must be removed first. If interface renaming fails, restore the alternative name that was deleted if necessary. --- src/libsystemd/sd-netlink/netlink-util.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/libsystemd/sd-netlink/netlink-util.c') diff --git a/src/libsystemd/sd-netlink/netlink-util.c b/src/libsystemd/sd-netlink/netlink-util.c index a53ffb2616..af601bd347 100644 --- a/src/libsystemd/sd-netlink/netlink-util.c +++ b/src/libsystemd/sd-netlink/netlink-util.c @@ -14,6 +14,7 @@ int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) { _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL; _cleanup_strv_free_ char **alternative_names = NULL; + bool altname_deleted = false; int r; assert(rtnl); @@ -33,21 +34,33 @@ int rtnl_set_link_name(sd_netlink **rtnl, int ifindex, const char *name) { if (r < 0) return log_debug_errno(r, "Failed to remove '%s' from alternative names on network interface %i: %m", name, ifindex); + + altname_deleted = true; } r = sd_rtnl_message_new_link(*rtnl, &message, RTM_SETLINK, ifindex); if (r < 0) - return r; + goto fail; r = sd_netlink_message_append_string(message, IFLA_IFNAME, name); if (r < 0) - return r; + goto fail; r = sd_netlink_call(*rtnl, message, 0, NULL); if (r < 0) - return r; + goto fail; return 0; + +fail: + if (altname_deleted) { + int q = rtnl_set_link_alternative_names(rtnl, ifindex, STRV_MAKE(name)); + if (q < 0) + log_debug_errno(q, "Failed to restore '%s' as an alternative name on network interface %i, ignoring: %m", + name, ifindex); + } + + return r; } int rtnl_set_link_properties( -- cgit v1.2.3