From: Yu Watanabe Date: Wed, 12 May 2021 12:07:14 +0000 (+0900) Subject: udev,network: make link_get_type_string() return negative errno on failure X-Git-Tag: v249-rc1~230^2~5 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1a3caa49d757ed0b90f86d0f1c3eee55a1588e61;p=thirdparty%2Fsystemd.git udev,network: make link_get_type_string() return negative errno on failure And make net_match_config() propagate the error. --- diff --git a/src/libsystemd/sd-network/network-util.c b/src/libsystemd/sd-network/network-util.c index c1d3a9f6a2d..45700983882 100644 --- a/src/libsystemd/sd-network/network-util.c +++ b/src/libsystemd/sd-network/network-util.c @@ -119,24 +119,31 @@ int parse_operational_state_range(const char *str, LinkOperationalStateRange *ou return 0; } -char *link_get_type_string(sd_device *device, unsigned short iftype) { +int link_get_type_string(sd_device *device, unsigned short iftype, char **ret) { const char *t; char *p; if (device && sd_device_get_devtype(device, &t) >= 0 && - !isempty(t)) - return strdup(t); + !isempty(t)) { + p = strdup(t); + if (!p) + return -ENOMEM; + + *ret = p; + return 0; + } t = arphrd_to_name(iftype); if (!t) - return NULL; + return -ENOENT; p = strdup(t); if (!p) - return NULL; + return -ENOMEM; - return ascii_strlower(p); + *ret = ascii_strlower(p); + return 0; } const char *net_get_name_persistent(sd_device *device) { diff --git a/src/libsystemd/sd-network/network-util.h b/src/libsystemd/sd-network/network-util.h index 3bd10c42de8..d33f42cfb05 100644 --- a/src/libsystemd/sd-network/network-util.h +++ b/src/libsystemd/sd-network/network-util.h @@ -76,6 +76,6 @@ typedef struct LinkOperationalStateRange { int parse_operational_state_range(const char *str, LinkOperationalStateRange *out); -char *link_get_type_string(sd_device *device, unsigned short iftype); +int link_get_type_string(sd_device *device, unsigned short iftype, char **ret); int net_get_unique_predictable_data(sd_device *device, bool use_sysname, uint64_t *result); const char *net_get_name_persistent(sd_device *device); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 52abcddd641..d60b75a0459 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -715,7 +715,9 @@ static int list_links(int argc, char *argv[], void *userdata) { setup_state = strdup("unmanaged"); setup_state_to_color(setup_state, &on_color_setup, NULL); - t = link_get_type_string(links[i].sd_device, links[i].iftype); + r = link_get_type_string(links[i].sd_device, links[i].iftype, &t); + if (r == -ENOMEM) + return log_oom(); r = table_add_many(table, TABLE_INT, links[i].ifindex, @@ -1436,7 +1438,9 @@ static int link_status_one( (void) sd_device_get_property_value(info->sd_device, "ID_MODEL", &model); } - t = link_get_type_string(info->sd_device, info->iftype); + r = link_get_type_string(info->sd_device, info->iftype, &t); + if (r == -ENOMEM) + return log_oom(); (void) sd_network_link_get_network_file(info->ifindex, &network); diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c index 7c24138a4a5..9d30e16b0aa 100644 --- a/src/network/networkd-link.c +++ b/src/network/networkd-link.c @@ -2228,6 +2228,7 @@ static int link_configure_continue(Link *link) { static int link_get_network(Link *link, Network **ret) { Network *network; + int r; assert(link); assert(link->manager); @@ -2236,7 +2237,7 @@ static int link_get_network(Link *link, Network **ret) { ORDERED_HASHMAP_FOREACH(network, link->manager->networks) { bool warn = false; - if (!net_match_config( + r = net_match_config( &network->match, link->sd_device, &link->hw_addr.addr.ether, @@ -2247,7 +2248,10 @@ static int link_get_network(Link *link, Network **ret) { link->alternative_names, link->wlan_iftype, link->ssid, - &link->bssid)) + &link->bssid); + if (r < 0) + return r; + if (r == 0) continue; if (network->match.ifname && link->sd_device) { diff --git a/src/shared/net-condition.c b/src/shared/net-condition.c index 2479a5672c5..4742f374474 100644 --- a/src/shared/net-condition.c +++ b/src/shared/net-condition.c @@ -134,7 +134,7 @@ static const char *const wifi_iftype_table[NL80211_IFTYPE_MAX+1] = { DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(wifi_iftype, enum nl80211_iftype); -bool net_match_config( +int net_match_config( const NetMatch *match, sd_device *device, const struct ether_addr *mac, @@ -149,10 +149,13 @@ bool net_match_config( _cleanup_free_ char *iftype_str = NULL; const char *path = NULL; + int r; assert(match); - iftype_str = link_get_type_string(device, iftype); + r = link_get_type_string(device, iftype, &iftype_str); + if (r == -ENOMEM) + return r; if (device) { const char *mac_str; diff --git a/src/shared/net-condition.h b/src/shared/net-condition.h index 8d85fc8a7f8..d61537e5e33 100644 --- a/src/shared/net-condition.h +++ b/src/shared/net-condition.h @@ -26,7 +26,7 @@ typedef struct NetMatch { void net_match_clear(NetMatch *match); bool net_match_is_empty(const NetMatch *match); -bool net_match_config( +int net_match_config( const NetMatch *match, sd_device *device, const struct ether_addr *mac, diff --git a/src/udev/net/link-config.c b/src/udev/net/link-config.c index 87afe8383ad..3685c9d2280 100644 --- a/src/udev/net/link-config.c +++ b/src/udev/net/link-config.c @@ -273,16 +273,20 @@ int link_config_get(link_config_ctx *ctx, sd_device *device, link_config **ret) (void) link_unsigned_attribute(device, "name_assign_type", &name_assign_type); LIST_FOREACH(links, link, ctx->links) { - if (net_match_config(&link->match, device, NULL, &permanent_mac, NULL, iftype, NULL, NULL, 0, NULL, NULL)) { - if (link->match.ifname && !strv_contains(link->match.ifname, "*") && name_assign_type == NET_NAME_ENUM) - log_device_warning(device, "Config file %s is applied to device based on potentially unpredictable interface name.", - link->filename); - else - log_device_debug(device, "Config file %s is applied", link->filename); - - *ret = link; - return 0; - } + r = net_match_config(&link->match, device, NULL, &permanent_mac, NULL, iftype, NULL, NULL, 0, NULL, NULL); + if (r < 0) + return r; + if (r == 0) + continue; + + if (link->match.ifname && !strv_contains(link->match.ifname, "*") && name_assign_type == NET_NAME_ENUM) + log_device_warning(device, "Config file %s is applied to device based on potentially unpredictable interface name.", + link->filename); + else + log_device_debug(device, "Config file %s is applied", link->filename); + + *ret = link; + return 0; } return -ENOENT;