mirror of
https://github.com/systemd/systemd.git
synced 2025-01-09 01:18:19 +03:00
conf-parser: introduce config section parser wrapper
It will be used later.
This commit is contained in:
parent
5a11437e2e
commit
152b8a4e71
@ -1639,13 +1639,19 @@ static DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(
|
||||
AddressFamily,
|
||||
ADDRESS_FAMILY_NO);
|
||||
|
||||
typedef struct RoutingPolicyRuleConfParser {
|
||||
ConfigParserCallback parser;
|
||||
int ltype;
|
||||
size_t offset;
|
||||
} RoutingPolicyRuleConfParser;
|
||||
int config_parse_routing_policy_rule(
|
||||
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) {
|
||||
|
||||
static RoutingPolicyRuleConfParser routing_policy_rule_conf_parser_table[_ROUTING_POLICY_RULE_CONF_PARSER_MAX] = {
|
||||
static const ConfigSectionParser table[_ROUTING_POLICY_RULE_CONF_PARSER_MAX] = {
|
||||
[ROUTING_POLICY_RULE_IIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, iif), },
|
||||
[ROUTING_POLICY_RULE_OIF] = { .parser = config_parse_ifname, .ltype = 0, .offset = offsetof(RoutingPolicyRule, oif), },
|
||||
[ROUTING_POLICY_RULE_FAMILY] = { .parser = config_parse_routing_policy_rule_family, .ltype = 0, .offset = offsetof(RoutingPolicyRule, address_family), },
|
||||
@ -1666,36 +1672,18 @@ static RoutingPolicyRuleConfParser routing_policy_rule_conf_parser_table[_ROUTIN
|
||||
[ROUTING_POLICY_RULE_UID_RANGE] = { .parser = config_parse_routing_policy_rule_uid_range, .ltype = 0, .offset = offsetof(RoutingPolicyRule, uid_range), },
|
||||
};
|
||||
|
||||
int config_parse_routing_policy_rule(
|
||||
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) {
|
||||
|
||||
_cleanup_(routing_policy_rule_unref_or_set_invalidp) RoutingPolicyRule *rule = NULL;
|
||||
Network *network = ASSERT_PTR(userdata);
|
||||
int r;
|
||||
|
||||
assert(filename);
|
||||
assert(ltype >= 0);
|
||||
assert(ltype < _ROUTING_POLICY_RULE_CONF_PARSER_MAX);
|
||||
|
||||
r = routing_policy_rule_new_static(network, filename, section_line, &rule);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
|
||||
RoutingPolicyRuleConfParser *e = routing_policy_rule_conf_parser_table + ltype;
|
||||
assert(e->parser);
|
||||
assert(e->offset < sizeof(RoutingPolicyRule));
|
||||
|
||||
r = e->parser(unit, filename, line, section, section_line, lvalue, e->ltype, rvalue,
|
||||
(uint8_t*) rule + e->offset, rule);
|
||||
r = config_section_parse(table, ELEMENTSOF(table),
|
||||
unit, filename, line, section, section_line, lvalue, ltype, rvalue, rule);
|
||||
if (r <= 0) /* 0 means non-critical error, but the section will be ignored. */
|
||||
return r;
|
||||
|
||||
|
@ -793,6 +793,37 @@ bool stats_by_path_equal(Hashmap *a, Hashmap *b) {
|
||||
return true;
|
||||
}
|
||||
|
||||
int config_section_parse(
|
||||
const ConfigSectionParser *parsers,
|
||||
size_t n_parsers,
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *lvalue,
|
||||
int ltype,
|
||||
const char *rvalue,
|
||||
void *userdata) {
|
||||
|
||||
assert(parsers);
|
||||
assert(n_parsers > 0);
|
||||
assert(ltype >= 0);
|
||||
assert((size_t) ltype < n_parsers);
|
||||
assert(userdata);
|
||||
|
||||
const ConfigSectionParser *e = parsers + ltype;
|
||||
assert(e->parser);
|
||||
|
||||
/* This is used when a object is dynamically allocated per [SECTION] in a config parser, e.g.
|
||||
* [Address] for systemd.network. Takes the allocated object as 'userdata', then it is passed to
|
||||
* config parsers in the table. The 'data' field points to an element of the passed object, where
|
||||
* its offset is given by the table. */
|
||||
|
||||
return e->parser(unit, filename, line, section, section_line, lvalue, e->ltype, rvalue,
|
||||
(uint8_t*) userdata + e->offset, userdata);
|
||||
}
|
||||
|
||||
void config_section_hash_func(const ConfigSection *c, struct siphash *state) {
|
||||
siphash24_compress_string(c->filename, state);
|
||||
siphash24_compress_typesafe(c->line, state);
|
||||
|
@ -147,6 +147,25 @@ int config_get_stats_by_path(
|
||||
int hashmap_put_stats_by_path(Hashmap **stats_by_path, const char *path, const struct stat *st);
|
||||
bool stats_by_path_equal(Hashmap *a, Hashmap *b);
|
||||
|
||||
typedef struct ConfigSectionParser {
|
||||
ConfigParserCallback parser;
|
||||
int ltype;
|
||||
size_t offset;
|
||||
} ConfigSectionParser;
|
||||
|
||||
int config_section_parse(
|
||||
const ConfigSectionParser *parsers,
|
||||
size_t n_parsers,
|
||||
const char *unit,
|
||||
const char *filename,
|
||||
unsigned line,
|
||||
const char *section,
|
||||
unsigned section_line,
|
||||
const char *lvalue,
|
||||
int ltype,
|
||||
const char *rvalue,
|
||||
void *userdata);
|
||||
|
||||
typedef struct ConfigSection {
|
||||
unsigned line;
|
||||
bool invalid;
|
||||
|
Loading…
Reference in New Issue
Block a user