1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-22 13:33:56 +03:00

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.

(cherry picked from commit 4d600667f8)
(cherry picked from commit 42d8817bd6)
(cherry picked from commit a536073a62)
This commit is contained in:
Nick Rosbrook 2022-11-02 05:36:14 -04:00 committed by Luca Boccassi
parent be8b55dfcf
commit 00cdc7b300

View File

@ -12,6 +12,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);
@ -31,21 +32,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(