]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
network: fixes related to NetworkConfigSection
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 12 Nov 2018 05:57:04 +0000 (14:57 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 12 Nov 2018 07:32:11 +0000 (16:32 +0900)
- Do not allocate NetworkConfigSection when filename == NULL
- set .network element before calling hashmap_put()
- Always free NetworkConfigSection in each object.

src/network/networkd-address-label.c
src/network/networkd-address-label.h
src/network/networkd-address.c
src/network/networkd-radv.c
src/network/networkd-route.c
src/network/networkd-routing-policy-rule.c

index 98e40d3f6a1d535544618328c752e2763d7e375a..1392cba538d0950245631f55c988310dae8960a2 100644 (file)
 #include "parse-util.h"
 #include "socket-util.h"
 
-int address_label_new(AddressLabel **ret) {
-        _cleanup_(address_label_freep) AddressLabel *addrlabel = NULL;
-
-        addrlabel = new0(AddressLabel, 1);
-        if (!addrlabel)
-                return -ENOMEM;
-
-        *ret = TAKE_PTR(addrlabel);
-
-        return 0;
-}
-
 void address_label_free(AddressLabel *label) {
         if (!label)
                 return;
@@ -50,31 +38,38 @@ static int address_label_new_static(Network *network, const char *filename, unsi
         assert(ret);
         assert(!!filename == (section_line > 0));
 
-        r = network_config_section_new(filename, section_line, &n);
-        if (r < 0)
-                return r;
+        if (filename) {
+                r = network_config_section_new(filename, section_line, &n);
+                if (r < 0)
+                        return r;
 
-        label = hashmap_get(network->address_labels_by_section, n);
-        if (label) {
-                *ret = TAKE_PTR(label);
+                label = hashmap_get(network->address_labels_by_section, n);
+                if (label) {
+                        *ret = TAKE_PTR(label);
 
-                return 0;
+                        return 0;
+                }
         }
 
-        r = address_label_new(&label);
-        if (r < 0)
-                return r;
-
-        label->section = TAKE_PTR(n);
+        label = new(AddressLabel, 1);
+        if (!label)
+                return -ENOMEM;
 
-        r = hashmap_put(network->address_labels_by_section, label->section, label);
-        if (r < 0)
-                return r;
+        *label = (AddressLabel) {
+                .network = network,
+        };
 
-        label->network = network;
         LIST_APPEND(labels, network->address_labels, label);
         network->n_address_labels++;
 
+        if (filename) {
+                label->section = TAKE_PTR(n);
+
+                r = hashmap_put(network->address_labels_by_section, label->section, label);
+                if (r < 0)
+                        return r;
+        }
+
         *ret = TAKE_PTR(label);
 
         return 0;
index 2e55feabd0be739ae104255f4eab47d5ee45dd90..da506b2de48851cd9c1fd9c1ae8284d6178b1033 100644 (file)
@@ -28,7 +28,6 @@ struct AddressLabel {
         LIST_FIELDS(AddressLabel, labels);
 };
 
-int address_label_new(AddressLabel **ret);
 void address_label_free(AddressLabel *label);
 
 DEFINE_TRIVIAL_CLEANUP_FUNC(AddressLabel*, address_label_free);
index 66e00493d67a37811a7150348d4822df09f2a20e..d028f7226e79de3475b4874c5638d344bac800d2 100644 (file)
@@ -67,6 +67,10 @@ int address_new_static(Network *network, const char *filename, unsigned section_
         if (r < 0)
                 return r;
 
+        address->network = network;
+        LIST_APPEND(addresses, network->static_addresses, address);
+        network->n_static_addresses++;
+
         if (filename) {
                 address->section = TAKE_PTR(n);
 
@@ -75,10 +79,6 @@ int address_new_static(Network *network, const char *filename, unsigned section_
                         return r;
         }
 
-        address->network = network;
-        LIST_APPEND(addresses, network->static_addresses, address);
-        network->n_static_addresses++;
-
         *ret = TAKE_PTR(address);
 
         return 0;
index 600fb27d75586ae9d2737449ecdd27cccbb0b32d..d893c1e5d5396d8513d870fbacdde04553aad35d 100644 (file)
@@ -153,6 +153,10 @@ int prefix_new_static(Network *network, const char *filename,
         if (r < 0)
                 return r;
 
+        prefix->network = network;
+        LIST_APPEND(prefixes, network->static_prefixes, prefix);
+        network->n_static_prefixes++;
+
         if (filename) {
                 prefix->section = TAKE_PTR(n);
 
@@ -162,10 +166,6 @@ int prefix_new_static(Network *network, const char *filename,
                         return r;
         }
 
-        prefix->network = network;
-        LIST_APPEND(prefixes, network->static_prefixes, prefix);
-        network->n_static_prefixes++;
-
         *ret = TAKE_PTR(prefix);
 
         return 0;
index 63126b4d139290a98066a8d73877b18a4a977739..1e16e4a392129452a441cec36289536c77c4a4b2 100644 (file)
@@ -95,6 +95,9 @@ int route_new_static(Network *network, const char *filename, unsigned section_li
                 return r;
 
         route->protocol = RTPROT_STATIC;
+        route->network = network;
+        LIST_PREPEND(routes, network->static_routes, route);
+        network->n_static_routes++;
 
         if (filename) {
                 route->section = TAKE_PTR(n);
@@ -104,10 +107,6 @@ int route_new_static(Network *network, const char *filename, unsigned section_li
                         return r;
         }
 
-        route->network = network;
-        LIST_PREPEND(routes, network->static_routes, route);
-        network->n_static_routes++;
-
         *ret = TAKE_PTR(route);
 
         return 0;
index 739f9e6a5a260513cbc1358fb12eace515c922ac..4750073d28b1ef4e9f4adaab07694ad5d81bc710 100644 (file)
@@ -40,11 +40,8 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) {
                 assert(rule->network->n_rules > 0);
                 rule->network->n_rules--;
 
-                if (rule->section) {
+                if (rule->section)
                         hashmap_remove(rule->network->rules_by_section, rule->section);
-                        network_config_section_free(rule->section);
-                }
-
         }
 
         if (rule->manager) {
@@ -52,6 +49,7 @@ void routing_policy_rule_free(RoutingPolicyRule *rule) {
                 set_remove(rule->manager->rules_foreign, rule);
         }
 
+        network_config_section_free(rule->section);
         free(rule->iif);
         free(rule->oif);
         free(rule);
@@ -390,31 +388,35 @@ static int routing_policy_rule_new_static(Network *network, const char *filename
         assert(ret);
         assert(!!filename == (section_line > 0));
 
-        r = network_config_section_new(filename, section_line, &n);
-        if (r < 0)
-                return r;
+        if (filename) {
+                r = network_config_section_new(filename, section_line, &n);
+                if (r < 0)
+                        return r;
 
-        rule = hashmap_get(network->rules_by_section, n);
-        if (rule) {
-                *ret = TAKE_PTR(rule);
+                rule = hashmap_get(network->rules_by_section, n);
+                if (rule) {
+                        *ret = TAKE_PTR(rule);
 
-                return 0;
+                        return 0;
+                }
         }
 
         r = routing_policy_rule_new(&rule);
         if (r < 0)
                 return r;
 
-        rule->section = TAKE_PTR(n);
         rule->network = network;
-
-        r = hashmap_put(network->rules_by_section, rule->section, rule);
-        if (r < 0)
-                return r;
-
         LIST_APPEND(rules, network->rules, rule);
         network->n_rules++;
 
+        if (filename) {
+                rule->section = TAKE_PTR(n);
+
+                r = hashmap_put(network->rules_by_section, rule->section, rule);
+                if (r < 0)
+                        return r;
+        }
+
         *ret = TAKE_PTR(rule);
 
         return 0;