From: Zbigniew Jędrzejewski-Szmek Date: Mon, 27 Nov 2017 10:22:51 +0000 (+0000) Subject: networkd: move rule loading to a separate function X-Git-Tag: v236~102^2~20 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=458d8ae3029647d85002eff6b3ef8d6bd80f2eef;p=thirdparty%2Fsystemd.git networkd: move rule loading to a separate function No functional change. --- diff --git a/src/network/networkd-manager.c b/src/network/networkd-manager.c index 9ffbe399582..852d690ef58 100644 --- a/src/network/networkd-manager.c +++ b/src/network/networkd-manager.c @@ -1004,7 +1004,6 @@ static void print_string_set(FILE *f, const char *field, OrderedSet *s) { static int manager_save(Manager *m) { _cleanup_ordered_set_free_free_ OrderedSet *dns = NULL, *ntp = NULL, *search_domains = NULL, *route_domains = NULL; - RoutingPolicyRule *rule = NULL; Link *link; Iterator i; _cleanup_free_ char *temp_path = NULL; @@ -1129,51 +1128,9 @@ static int manager_save(Manager *m) { print_string_set(f, "DOMAINS=", search_domains); print_string_set(f, "ROUTE_DOMAINS=", route_domains); - SET_FOREACH(rule, m->rules, i) { - _cleanup_free_ char *from_str = NULL, *to_str = NULL; - bool space = false; - - fputs("RULE=", f); - - if (!in_addr_is_null(rule->family, &rule->from)) { - r = in_addr_to_string(rule->family, &rule->from, &from_str); - if (r < 0) - goto fail; - - fprintf(f, "from=%s/%hhu", - from_str, rule->from_prefixlen); - space = true; - } - - if (!in_addr_is_null(rule->family, &rule->to)) { - r = in_addr_to_string(rule->family, &rule->to, &to_str); - if (r < 0) - goto fail; - - fprintf(f, "%sto=%s/%hhu", - space ? " " : "", - to_str, rule->to_prefixlen); - space = true; - } - - if (rule->tos != 0) { - fprintf(f, "%stos=%hhu", - space ? " " : "", - rule->tos); - space = true; - } - - if (rule->fwmark != 0) { - fprintf(f, "%sfwmark=%"PRIu32"/%"PRIu32, - space ? " " : "", - rule->fwmark, rule->fwmask); - space = true; - } - - fprintf(f, "%stable=%"PRIu32 "\n", - space ? " " : "", - rule->table); - } + r = routing_policy_serialize_rules(m->rules, f); + if (r < 0) + goto fail; r = fflush_and_check(f); if (r < 0) @@ -1260,7 +1217,7 @@ int manager_new(Manager **ret, sd_event *event) { m->duid.type = DUID_TYPE_EN; - (void) routing_policy_rule_load(m); + (void) routing_policy_load_rules(m->state_file, &m->rules_saved); *ret = m; m = NULL; diff --git a/src/network/networkd-routing-policy-rule.c b/src/network/networkd-routing-policy-rule.c index 775b85e3384..c8eba2298da 100644 --- a/src/network/networkd-routing-policy-rule.c +++ b/src/network/networkd-routing-policy-rule.c @@ -836,7 +836,7 @@ int config_parse_routing_policy_rule_device( return 0; } -static int routing_policy_rule_read_full_file(char *state_file, char **ret) { +static int routing_policy_rule_read_full_file(const char *state_file, char **ret) { _cleanup_free_ char *s = NULL; size_t size; int r; @@ -857,16 +857,73 @@ static int routing_policy_rule_read_full_file(char *state_file, char **ret) { return size; } -int routing_policy_rule_load(Manager *m) { +int routing_policy_serialize_rules(Set *rules, FILE *f) { + RoutingPolicyRule *rule = NULL; + Iterator i; + int r; + + assert(f); + + SET_FOREACH(rule, rules, i) { + _cleanup_free_ char *from_str = NULL, *to_str = NULL; + bool space = false; + + fputs("RULE=", f); + + if (!in_addr_is_null(rule->family, &rule->from)) { + r = in_addr_to_string(rule->family, &rule->from, &from_str); + if (r < 0) + return r; + + fprintf(f, "from=%s/%hhu", + from_str, rule->from_prefixlen); + space = true; + } + + if (!in_addr_is_null(rule->family, &rule->to)) { + r = in_addr_to_string(rule->family, &rule->to, &to_str); + if (r < 0) + return r; + + fprintf(f, "%sto=%s/%hhu", + space ? " " : "", + to_str, rule->to_prefixlen); + space = true; + } + + if (rule->tos != 0) { + fprintf(f, "%stos=%hhu", + space ? " " : "", + rule->tos); + space = true; + } + + if (rule->fwmark != 0) { + fprintf(f, "%sfwmark=%"PRIu32"/%"PRIu32, + space ? " " : "", + rule->fwmark, rule->fwmask); + space = true; + } + + fprintf(f, "%stable=%"PRIu32 "\n", + space ? " " : "", + rule->table); + } + + return 0; +} + +int routing_policy_load_rules(const char *state_file, Set **rules) { _cleanup_strv_free_ char **l = NULL; _cleanup_free_ char *data = NULL; const char *p; char **i; int r; - assert(m); + assert(state_file); + assert(rules); - r = routing_policy_rule_read_full_file(m->state_file, &data); + r = routing_policy_rule_read_full_file(state_file, &data); if (r <= 0) return r; @@ -874,7 +931,7 @@ int routing_policy_rule_load(Manager *m) { if (!l) return -ENOMEM; - r = set_ensure_allocated(&m->rules_saved, &routing_policy_rule_hash_ops); + r = set_ensure_allocated(rules, &routing_policy_rule_hash_ops); if (r < 0) return r; @@ -957,7 +1014,7 @@ int routing_policy_rule_load(Manager *m) { } } - r = set_put(m->rules_saved, rule); + r = set_put(*rules, rule); if (r < 0) { log_warning_errno(r, "Failed to add RPDB rule to saved DB, ignoring: %s", p); continue; diff --git a/src/network/networkd-routing-policy-rule.h b/src/network/networkd-routing-policy-rule.h index 01a71073661..70a861723bb 100644 --- a/src/network/networkd-routing-policy-rule.h +++ b/src/network/networkd-routing-policy-rule.h @@ -77,7 +77,8 @@ int routing_policy_rule_add_foreign(Manager *m, int family, const union in_addr_ int routing_policy_rule_get(Manager *m, int family, const union in_addr_union *from, uint8_t from_prefixlen, const union in_addr_union *to, uint8_t to_prefixlen, uint8_t tos, uint32_t fwmark, uint32_t table, char *iif, char *oif, RoutingPolicyRule **ret); int routing_policy_rule_make_local(Manager *m, RoutingPolicyRule *rule); -int routing_policy_rule_load(Manager *m); +int routing_policy_serialize_rules(Set *rules, FILE *f); +int routing_policy_load_rules(const char *state_file, Set **rules); void routing_policy_rule_purge(Manager *m, Link *link); int config_parse_routing_policy_rule_tos(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data,void *userdata);