summaryrefslogtreecommitdiff
path: root/src/network
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2021-01-17 15:39:41 +0900
committerGitHub <noreply@github.com>2021-01-17 15:39:41 +0900
commit16ed53658a81bcd75e8be747c7b302092b7a21cf (patch)
tree350cb117403ba1f07f4f50270e4338b6d1c13148 /src/network
parent95dd454edcfb506d9d921a259620e1e8c73ae39c (diff)
parenta307a7dd3864ed0185ccb3efe31f28efcc9703fe (diff)
downloadsystemd-16ed53658a81bcd75e8be747c7b302092b7a21cf.tar.gz
Merge pull request #18243 from ssahani/ensure-put-use
tree-wide: introduce hashmap_ensure_put() and use it
Diffstat (limited to 'src/network')
-rw-r--r--src/network/generator/network-generator.c18
-rw-r--r--src/network/netdev/netdev.c8
-rw-r--r--src/network/netdev/wireguard.c6
-rw-r--r--src/network/networkd-address-label.c6
-rw-r--r--src/network/networkd-dhcp6.c6
-rw-r--r--src/network/networkd-fdb.c6
-rw-r--r--src/network/networkd-link.c12
-rw-r--r--src/network/networkd-mdb.c6
-rw-r--r--src/network/networkd-neighbor.c6
-rw-r--r--src/network/networkd-network.c6
-rw-r--r--src/network/networkd-nexthop.c6
-rw-r--r--src/network/networkd-radv.c6
-rw-r--r--src/network/networkd-route.c6
-rw-r--r--src/network/networkd-routing-policy-rule.c6
-rw-r--r--src/network/wait-online/wait-online.c6
15 files changed, 23 insertions, 87 deletions
diff --git a/src/network/generator/network-generator.c b/src/network/generator/network-generator.c
index f9b51d8b7b..940a718d20 100644
--- a/src/network/generator/network-generator.c
+++ b/src/network/generator/network-generator.c
@@ -190,11 +190,7 @@ static int network_new(Context *context, const char *name, Network **ret) {
.dhcp_use_dns = -1,
};
- r = hashmap_ensure_allocated(&context->networks_by_name, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(context->networks_by_name, network->ifname, network);
+ r = hashmap_ensure_put(&context->networks_by_name, &string_hash_ops, network->ifname, network);
if (r < 0)
return r;
@@ -247,11 +243,7 @@ static int netdev_new(Context *context, const char *_kind, const char *_ifname,
.ifname = TAKE_PTR(ifname),
};
- r = hashmap_ensure_allocated(&context->netdevs_by_name, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(context->netdevs_by_name, netdev->ifname, netdev);
+ r = hashmap_ensure_put(&context->netdevs_by_name, &string_hash_ops, netdev->ifname, netdev);
if (r < 0)
return r;
@@ -299,11 +291,7 @@ static int link_new(Context *context, const char *name, struct ether_addr *mac,
.mac = *mac,
};
- r = hashmap_ensure_allocated(&context->links_by_name, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(context->links_by_name, link->ifname, link);
+ r = hashmap_ensure_put(&context->links_by_name, &string_hash_ops, link->ifname, link);
if (r < 0)
return r;
diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c
index 755a67fea8..81e8fd7cf1 100644
--- a/src/network/netdev/netdev.c
+++ b/src/network/netdev/netdev.c
@@ -756,11 +756,9 @@ int netdev_load_one(Manager *manager, const char *filename) {
netdev->ifname);
}
- r = hashmap_ensure_allocated(&netdev->manager->netdevs, &string_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(netdev->manager->netdevs, netdev->ifname, netdev);
+ r = hashmap_ensure_put(&netdev->manager->netdevs, &string_hash_ops, netdev->ifname, netdev);
+ if (r == -ENOMEM)
+ return log_oom();
if (r == -EEXIST) {
NetDev *n = hashmap_get(netdev->manager->netdevs, netdev->ifname);
diff --git a/src/network/netdev/wireguard.c b/src/network/netdev/wireguard.c
index 76444bdd7c..e442ecc266 100644
--- a/src/network/netdev/wireguard.c
+++ b/src/network/netdev/wireguard.c
@@ -91,11 +91,7 @@ static int wireguard_peer_new_static(Wireguard *w, const char *filename, unsigne
LIST_PREPEND(peers, w->peers, peer);
- r = hashmap_ensure_allocated(&w->peers_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(w->peers_by_section, peer->section, peer);
+ r = hashmap_ensure_put(&w->peers_by_section, &network_config_hash_ops, peer->section, peer);
if (r < 0)
return r;
diff --git a/src/network/networkd-address-label.c b/src/network/networkd-address-label.c
index 44d0b6c847..7b7b72469e 100644
--- a/src/network/networkd-address-label.c
+++ b/src/network/networkd-address-label.c
@@ -55,11 +55,7 @@ static int address_label_new_static(Network *network, const char *filename, unsi
.section = TAKE_PTR(n),
};
- r = hashmap_ensure_allocated(&network->address_labels_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->address_labels_by_section, label->section, label);
+ r = hashmap_ensure_put(&network->address_labels_by_section, &network_config_hash_ops, label->section, label);
if (r < 0)
return r;
diff --git a/src/network/networkd-dhcp6.c b/src/network/networkd-dhcp6.c
index 9571221da2..d9b45a8a14 100644
--- a/src/network/networkd-dhcp6.c
+++ b/src/network/networkd-dhcp6.c
@@ -309,11 +309,9 @@ static int dhcp6_set_pd_route(Link *link, const union in_addr_union *prefix, con
.link = link_ref(link),
};
- r = hashmap_ensure_allocated(&link->manager->dhcp6_prefixes, &in6_addr_hash_ops);
- if (r < 0)
+ r = hashmap_ensure_put(&link->manager->dhcp6_prefixes, &in6_addr_hash_ops, &pd->prefix, pd);
+ if (r == -ENOMEM)
return log_oom();
-
- r = hashmap_put(link->manager->dhcp6_prefixes, &pd->prefix, pd);
if (r < 0)
return log_link_error_errno(link, r, "Failed to store DHCPv6 prefix route at manager: %m");
diff --git a/src/network/networkd-fdb.c b/src/network/networkd-fdb.c
index 283dece04f..c18e4d81d9 100644
--- a/src/network/networkd-fdb.c
+++ b/src/network/networkd-fdb.c
@@ -79,11 +79,7 @@ static int fdb_entry_new_static(
.fdb_ntf_flags = NEIGHBOR_CACHE_ENTRY_FLAGS_SELF,
};
- r = hashmap_ensure_allocated(&network->fdb_entries_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->fdb_entries_by_section, fdb_entry->section, fdb_entry);
+ r = hashmap_ensure_put(&network->fdb_entries_by_section, &network_config_hash_ops, fdb_entry->section, fdb_entry);
if (r < 0)
return r;
diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
index aa82576c17..3fad8f12f8 100644
--- a/src/network/networkd-link.c
+++ b/src/network/networkd-link.c
@@ -449,11 +449,7 @@ static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
return -ENOMEM;
- r = hashmap_ensure_allocated(&manager->links, NULL);
- if (r < 0)
- return r;
-
- r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
+ r = hashmap_ensure_put(&manager->links, NULL, INT_TO_PTR(link->ifindex), link);
if (r < 0)
return r;
@@ -1605,11 +1601,7 @@ static int link_put_carrier(Link *link, Link *carrier, Hashmap **h) {
if (hashmap_get(*h, INT_TO_PTR(carrier->ifindex)))
return 0;
- r = hashmap_ensure_allocated(h, NULL);
- if (r < 0)
- return r;
-
- r = hashmap_put(*h, INT_TO_PTR(carrier->ifindex), carrier);
+ r = hashmap_ensure_put(h, NULL, INT_TO_PTR(carrier->ifindex), carrier);
if (r < 0)
return r;
diff --git a/src/network/networkd-mdb.c b/src/network/networkd-mdb.c
index 33ad9e969d..f5aff72248 100644
--- a/src/network/networkd-mdb.c
+++ b/src/network/networkd-mdb.c
@@ -70,11 +70,7 @@ static int mdb_entry_new_static(
.section = TAKE_PTR(n),
};
- r = hashmap_ensure_allocated(&network->mdb_entries_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->mdb_entries_by_section, mdb_entry->section, mdb_entry);
+ r = hashmap_ensure_put(&network->mdb_entries_by_section, &network_config_hash_ops, mdb_entry->section, mdb_entry);
if (r < 0)
return r;
diff --git a/src/network/networkd-neighbor.c b/src/network/networkd-neighbor.c
index 847ecd27c3..22b716b14a 100644
--- a/src/network/networkd-neighbor.c
+++ b/src/network/networkd-neighbor.c
@@ -60,11 +60,7 @@ static int neighbor_new_static(Network *network, const char *filename, unsigned
.section = TAKE_PTR(n),
};
- r = hashmap_ensure_allocated(&network->neighbors_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->neighbors_by_section, neighbor->section, neighbor);
+ r = hashmap_ensure_put(&network->neighbors_by_section, &network_config_hash_ops, neighbor->section, neighbor);
if (r < 0)
return r;
diff --git a/src/network/networkd-network.c b/src/network/networkd-network.c
index a74a1e946e..0920a9d282 100644
--- a/src/network/networkd-network.c
+++ b/src/network/networkd-network.c
@@ -142,11 +142,9 @@ static int network_resolve_stacked_netdevs(Network *network) {
if (r <= 0)
continue;
- r = hashmap_ensure_allocated(&network->stacked_netdevs, &string_hash_ops);
- if (r < 0)
+ r = hashmap_ensure_put(&network->stacked_netdevs, &string_hash_ops, netdev->ifname, netdev);
+ if (r == -ENOMEM)
return log_oom();
-
- r = hashmap_put(network->stacked_netdevs, netdev->ifname, netdev);
if (r < 0)
return log_error_errno(r, "%s: Failed to add NetDev '%s' to network: %m",
network->filename, (const char *) name);
diff --git a/src/network/networkd-nexthop.c b/src/network/networkd-nexthop.c
index 46e1e42b30..ea9c252cc7 100644
--- a/src/network/networkd-nexthop.c
+++ b/src/network/networkd-nexthop.c
@@ -79,11 +79,7 @@ static int nexthop_new_static(Network *network, const char *filename, unsigned s
nexthop->network = network;
nexthop->section = TAKE_PTR(n);
- r = hashmap_ensure_allocated(&network->nexthops_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->nexthops_by_section, nexthop->section, nexthop);
+ r = hashmap_ensure_put(&network->nexthops_by_section, &network_config_hash_ops, nexthop->section, nexthop);
if (r < 0)
return r;
diff --git a/src/network/networkd-radv.c b/src/network/networkd-radv.c
index a8e1b2b489..4de6a21122 100644
--- a/src/network/networkd-radv.c
+++ b/src/network/networkd-radv.c
@@ -75,11 +75,7 @@ static int prefix_new_static(Network *network, const char *filename, unsigned se
prefix->network = network;
prefix->section = TAKE_PTR(n);
- r = hashmap_ensure_allocated(&network->prefixes_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->prefixes_by_section, prefix->section, prefix);
+ r = hashmap_ensure_put(&network->prefixes_by_section, &network_config_hash_ops, prefix->section, prefix);
if (r < 0)
return r;
diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c
index e0d589818d..3142bd686f 100644
--- a/src/network/networkd-route.c
+++ b/src/network/networkd-route.c
@@ -227,11 +227,7 @@ static int route_new_static(Network *network, const char *filename, unsigned sec
route->network = network;
route->section = TAKE_PTR(n);
- r = hashmap_ensure_allocated(&network->routes_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->routes_by_section, route->section, route);
+ r = hashmap_ensure_put(&network->routes_by_section, &network_config_hash_ops, route->section, route);
if (r < 0)
return r;
diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c
index e179ffbb9e..6e00e5f9cb 100644
--- a/src/network/networkd-routing-policy-rule.c
+++ b/src/network/networkd-routing-policy-rule.c
@@ -100,11 +100,7 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
rule->section = TAKE_PTR(n);
rule->protocol = RTPROT_STATIC;
- r = hashmap_ensure_allocated(&network->rules_by_section, &network_config_hash_ops);
- if (r < 0)
- return r;
-
- r = hashmap_put(network->rules_by_section, rule->section, rule);
+ r = hashmap_ensure_put(&network->rules_by_section, &network_config_hash_ops, rule->section, rule);
if (r < 0)
return r;
diff --git a/src/network/wait-online/wait-online.c b/src/network/wait-online/wait-online.c
index c2bdcd4907..d6716e1874 100644
--- a/src/network/wait-online/wait-online.c
+++ b/src/network/wait-online/wait-online.c
@@ -83,11 +83,9 @@ static int parse_interface_with_operstate_range(const char *str) {
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"Invalid interface name '%s'", ifname);
- r = hashmap_ensure_allocated(&arg_interfaces, &string_hash_ops);
- if (r < 0)
+ r = hashmap_ensure_put(&arg_interfaces, &string_hash_ops, ifname, TAKE_PTR(range));
+ if (r == -ENOMEM)
return log_oom();
-
- r = hashmap_put(arg_interfaces, ifname, TAKE_PTR(range));
if (r < 0)
return log_error_errno(r, "Failed to store interface name: %m");
if (r == 0)