conf-parser: turn three bool function params into a flags fields

This makes things more readable and fixes some issues with incorrect
flag propagation between the various flavours of config_parse().
This commit is contained in:
Lennart Poettering 2017-11-09 00:26:11 +01:00
parent 92b5e60542
commit bcde742e78
21 changed files with 88 additions and 91 deletions

View File

@ -154,12 +154,11 @@ int unit_load_dropin(Unit *u) {
return log_oom();
}
STRV_FOREACH(f, u->dropin_paths) {
config_parse(u->id, *f, NULL,
UNIT_VTABLE(u)->sections,
config_item_perf_lookup, load_fragment_gperf_lookup,
false, false, false, u);
}
STRV_FOREACH(f, u->dropin_paths)
(void) config_parse(u->id, *f, NULL,
UNIT_VTABLE(u)->sections,
config_item_perf_lookup, load_fragment_gperf_lookup,
0, u);
u->dropin_mtime = now(CLOCK_REALTIME);

View File

@ -4450,7 +4450,7 @@ static int load_from_path(Unit *u, const char *path) {
r = config_parse(u->id, filename, f,
UNIT_VTABLE(u)->sections,
config_item_perf_lookup, load_fragment_gperf_lookup,
false, true, false, u);
CONFIG_PARSE_ALLOW_INCLUDE, u);
if (r < 0)
return r;
}

View File

@ -768,7 +768,7 @@ static int parse_config_file(void) {
CONF_PATHS_NULSTR("systemd/system.conf.d") :
CONF_PATHS_NULSTR("systemd/user.conf.d");
config_parse_many_nulstr(fn, conf_dirs_nulstr, "Manager\0", config_item_table_lookup, items, false, NULL);
(void) config_parse_many_nulstr(fn, conf_dirs_nulstr, "Manager\0", config_item_table_lookup, items, CONFIG_PARSE_WARN, NULL);
/* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we used USEC_INFINITY
* like everywhere else. */

View File

@ -147,7 +147,7 @@ static int parse_config(void) {
CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
"Coredump\0",
config_item_table_lookup, items,
false, NULL);
CONFIG_PARSE_WARN, NULL);
}
static inline uint64_t storage_size_max(void) {

View File

@ -1254,7 +1254,7 @@ static int parse_config(void) {
return config_parse_many_nulstr(PKGSYSCONFDIR "/journal-remote.conf",
CONF_PATHS_NULSTR("systemd/journal-remote.conf.d"),
"Remote\0", config_item_table_lookup, items,
false, NULL);
CONFIG_PARSE_WARN, NULL);
}
static void help(void) {

View File

@ -543,7 +543,7 @@ static int parse_config(void) {
return config_parse_many_nulstr(PKGSYSCONFDIR "/journal-upload.conf",
CONF_PATHS_NULSTR("systemd/journal-upload.conf.d"),
"Upload\0", config_item_table_lookup, items,
false, NULL);
CONFIG_PARSE_WARN, NULL);
}
static void help(void) {

View File

@ -1398,7 +1398,7 @@ static int server_parse_config_file(Server *s) {
CONF_PATHS_NULSTR("systemd/journald.conf.d"),
"Journal\0",
config_item_perf_lookup, journald_gperf_lookup,
false, s);
CONFIG_PARSE_WARN, s);
}
static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {

View File

@ -1072,7 +1072,7 @@ static int manager_parse_config_file(Manager *m) {
CONF_PATHS_NULSTR("systemd/logind.conf.d"),
"Login\0",
config_item_perf_lookup, logind_gperf_lookup,
false, m);
CONFIG_PARSE_WARN, m);
}
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {

View File

@ -630,7 +630,7 @@ static int netdev_load_one(Manager *manager, const char *filename) {
r = config_parse_many(filename, network_dirs, dropin_dirname,
"Match\0NetDev\0",
config_item_perf_lookup, network_netdev_gperf_lookup,
true, netdev_raw);
CONFIG_PARSE_WARN, netdev_raw);
if (r < 0)
return r;
@ -671,7 +671,7 @@ static int netdev_load_one(Manager *manager, const char *filename) {
r = config_parse(NULL, filename, file,
NETDEV_VTABLE(netdev)->sections,
config_item_perf_lookup, network_netdev_gperf_lookup,
false, false, false, netdev);
CONFIG_PARSE_WARN, netdev);
if (r < 0)
return r;

View File

@ -35,7 +35,7 @@ int manager_parse_config_file(Manager *m) {
CONF_PATHS_NULSTR("systemd/networkd.conf.d"),
"DHCP\0",
config_item_perf_lookup, networkd_gperf_lookup,
false, m);
CONFIG_PARSE_WARN, m);
}
static const char* const duid_type_table[_DUID_TYPE_MAX] = {

View File

@ -280,7 +280,7 @@ static int network_load_one(Manager *manager, const char *filename) {
"IPv6PrefixDelegation\0"
"IPv6Prefix\0",
config_item_perf_lookup, network_network_gperf_lookup,
false, network);
CONFIG_PARSE_WARN, network);
if (r < 0)
return r;

View File

@ -59,9 +59,7 @@ int settings_load(FILE *f, const char *path, Settings **ret) {
"Network\0"
"Files\0",
config_item_perf_lookup, nspawn_gperf_lookup,
false,
false,
true,
CONFIG_PARSE_WARN,
s);
if (r < 0)
return r;

View File

@ -236,7 +236,7 @@ int manager_parse_config_file(Manager *m) {
CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
"Resolve\0",
config_item_perf_lookup, resolved_gperf_lookup,
false, m);
CONFIG_PARSE_WARN, m);
if (r < 0)
return r;

View File

@ -121,17 +121,18 @@ int config_item_perf_lookup(
}
/* Run the user supplied parser for an assignment */
static int next_assignment(const char *unit,
const char *filename,
unsigned line,
ConfigItemLookup lookup,
const void *table,
const char *section,
unsigned section_line,
const char *lvalue,
const char *rvalue,
bool relaxed,
void *userdata) {
static int next_assignment(
const char *unit,
const char *filename,
unsigned line,
ConfigItemLookup lookup,
const void *table,
const char *section,
unsigned section_line,
const char *lvalue,
const char *rvalue,
ConfigParseFlags flags,
void *userdata) {
ConfigParserCallback func = NULL;
int ltype = 0;
@ -157,26 +158,26 @@ static int next_assignment(const char *unit,
}
/* Warn about unknown non-extension fields. */
if (!relaxed && !startswith(lvalue, "X-"))
if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(lvalue, "X-"))
log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown lvalue '%s' in section '%s'", lvalue, section);
return 0;
}
/* Parse a variable assignment line */
static int parse_line(const char* unit,
const char *filename,
unsigned line,
const char *sections,
ConfigItemLookup lookup,
const void *table,
bool relaxed,
bool allow_include,
char **section,
unsigned *section_line,
bool *section_ignored,
char *l,
void *userdata) {
static int parse_line(
const char* unit,
const char *filename,
unsigned line,
const char *sections,
ConfigItemLookup lookup,
const void *table,
ConfigParseFlags flags,
char **section,
unsigned *section_line,
bool *section_ignored,
char *l,
void *userdata) {
char *e;
@ -204,7 +205,7 @@ static int parse_line(const char* unit,
*
* Support for them should be eventually removed. */
if (!allow_include) {
if (!(flags & CONFIG_PARSE_ALLOW_INCLUDE)) {
log_syntax(unit, LOG_ERR, filename, line, 0, ".include not allowed here. Ignoring.");
return 0;
}
@ -213,7 +214,7 @@ static int parse_line(const char* unit,
if (!fn)
return -ENOMEM;
return config_parse(unit, fn, NULL, sections, lookup, table, relaxed, false, false, userdata);
return config_parse(unit, fn, NULL, sections, lookup, table, flags, userdata);
}
if (*l == '[') {
@ -234,7 +235,7 @@ static int parse_line(const char* unit,
if (sections && !nulstr_contains(sections, n)) {
if (!relaxed && !startswith(n, "X-"))
if (!(flags & CONFIG_PARSE_RELAXED) && !startswith(n, "X-"))
log_syntax(unit, LOG_WARNING, filename, line, 0, "Unknown section '%s'. Ignoring.", n);
free(n);
@ -253,7 +254,7 @@ static int parse_line(const char* unit,
if (sections && !*section) {
if (!relaxed && !*section_ignored)
if (!(flags & CONFIG_PARSE_RELAXED) && !*section_ignored)
log_syntax(unit, LOG_WARNING, filename, line, 0, "Assignment outside of section. Ignoring.");
return 0;
@ -277,7 +278,7 @@ static int parse_line(const char* unit,
*section_line,
strstrip(l),
strstrip(e),
relaxed,
flags,
userdata);
}
@ -288,15 +289,13 @@ int config_parse(const char *unit,
const char *sections,
ConfigItemLookup lookup,
const void *table,
bool relaxed,
bool allow_include,
bool warn,
ConfigParseFlags flags,
void *userdata) {
_cleanup_free_ char *section = NULL, *continuation = NULL;
_cleanup_fclose_ FILE *ours = NULL;
unsigned line = 0, section_line = 0;
bool section_ignored = false, allow_bom = true;
bool section_ignored = false;
int r;
assert(filename);
@ -307,7 +306,7 @@ int config_parse(const char *unit,
if (!f) {
/* Only log on request, except for ENOENT,
* since we return 0 to the caller. */
if (warn || errno == ENOENT)
if ((flags & CONFIG_PARSE_WARN) || errno == ENOENT)
log_full(errno == ENOENT ? LOG_DEBUG : LOG_ERR,
"Failed to open configuration file '%s': %m", filename);
return errno == ENOENT ? 0 : -errno;
@ -325,38 +324,38 @@ int config_parse(const char *unit,
if (r == 0)
break;
if (r == -ENOBUFS) {
if (warn)
if (flags & CONFIG_PARSE_WARN)
log_error_errno(r, "%s:%u: Line too long", filename, line);
return r;
}
if (r < 0) {
if (warn)
if (CONFIG_PARSE_WARN)
log_error_errno(r, "%s:%u: Error while reading configuration file: %m", filename, line);
return r;
}
l = buf;
if (allow_bom) {
if (!(flags & CONFIG_PARSE_REFUSE_BOM)) {
char *q;
q = startswith(buf, UTF8_BYTE_ORDER_MARK);
if (q) {
l = q;
allow_bom = false;
flags |= CONFIG_PARSE_REFUSE_BOM;
}
}
if (continuation) {
if (strlen(continuation) + strlen(l) > LONG_LINE_MAX) {
if (warn)
if (flags & CONFIG_PARSE_WARN)
log_error("%s:%u: Continuation line too long", filename, line);
return -ENOBUFS;
}
if (!strextend(&continuation, l, NULL)) {
if (warn)
if (flags & CONFIG_PARSE_WARN)
log_oom();
return -ENOMEM;
}
@ -378,7 +377,7 @@ int config_parse(const char *unit,
if (!continuation) {
continuation = strdup(l);
if (!continuation) {
if (warn)
if (flags & CONFIG_PARSE_WARN)
log_oom();
return -ENOMEM;
}
@ -393,15 +392,14 @@ int config_parse(const char *unit,
sections,
lookup,
table,
relaxed,
allow_include,
flags,
&section,
&section_line,
&section_ignored,
p,
userdata);
if (r < 0) {
if (warn)
if (flags & CONFIG_PARSE_WARN)
log_warning_errno(r, "%s:%u: Failed to parse file: %m", filename, line);
return r;
@ -419,20 +417,20 @@ static int config_parse_many_files(
const char *sections,
ConfigItemLookup lookup,
const void *table,
bool relaxed,
ConfigParseFlags flags,
void *userdata) {
char **fn;
int r;
if (conf_file) {
r = config_parse(NULL, conf_file, NULL, sections, lookup, table, relaxed, false, true, userdata);
r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata);
if (r < 0)
return r;
}
STRV_FOREACH(fn, files) {
r = config_parse(NULL, *fn, NULL, sections, lookup, table, relaxed, false, true, userdata);
r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata);
if (r < 0)
return r;
}
@ -447,7 +445,7 @@ int config_parse_many_nulstr(
const char *sections,
ConfigItemLookup lookup,
const void *table,
bool relaxed,
ConfigParseFlags flags,
void *userdata) {
_cleanup_strv_free_ char **files = NULL;
@ -457,8 +455,7 @@ int config_parse_many_nulstr(
if (r < 0)
return r;
return config_parse_many_files(conf_file, files,
sections, lookup, table, relaxed, userdata);
return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
}
/* Parse each config file in the directories specified as strv. */
@ -469,7 +466,7 @@ int config_parse_many(
const char *sections,
ConfigItemLookup lookup,
const void *table,
bool relaxed,
ConfigParseFlags flags,
void *userdata) {
_cleanup_strv_free_ char **dropin_dirs = NULL;
@ -486,8 +483,7 @@ int config_parse_many(
if (r < 0)
return r;
return config_parse_many_files(conf_file, files,
sections, lookup, table, relaxed, userdata);
return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata);
}
#define DEFINE_PARSER(type, vartype, conv_func) \

View File

@ -29,8 +29,14 @@
#include "log.h"
#include "macro.h"
/* An abstract parser for simple, line based, shallow configuration
* files consisting of variable assignments only. */
/* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
typedef enum ConfigParseFlags {
CONFIG_PARSE_RELAXED = 1U << 0,
CONFIG_PARSE_ALLOW_INCLUDE = 1U << 1,
CONFIG_PARSE_WARN = 1U << 2,
CONFIG_PARSE_REFUSE_BOM = 1U << 3,
} ConfigParseFlags;
/* Prototype for a parser for a specific configuration setting */
typedef int (*ConfigParserCallback)(const char *unit,
@ -91,9 +97,7 @@ int config_parse(
const char *sections, /* nulstr */
ConfigItemLookup lookup,
const void *table,
bool relaxed,
bool allow_include,
bool warn,
ConfigParseFlags flags,
void *userdata);
int config_parse_many_nulstr(
@ -102,7 +106,7 @@ int config_parse_many_nulstr(
const char *sections, /* nulstr */
ConfigItemLookup lookup,
const void *table,
bool relaxed,
ConfigParseFlags flags,
void *userdata);
int config_parse_many(
@ -112,7 +116,7 @@ int config_parse_many(
const char *sections, /* nulstr */
ConfigItemLookup lookup,
const void *table,
bool relaxed,
ConfigParseFlags flags,
void *userdata);
/* Generic parsers */

View File

@ -1308,7 +1308,7 @@ static int unit_file_load(
r = config_parse(info->name, path, f,
NULL,
config_item_table_lookup, items,
true, true, false, info);
CONFIG_PARSE_RELAXED|CONFIG_PARSE_ALLOW_INCLUDE, info);
if (r < 0)
return log_debug_errno(r, "Failed to parse %s: %m", info->name);

View File

@ -58,10 +58,10 @@ int parse_sleep_config(const char *verb, char ***_modes, char ***_states) {
{}
};
config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
"Sleep\0", config_item_table_lookup, items,
false, NULL);
(void) config_parse_many_nulstr(PKGSYSCONFDIR "/sleep.conf",
CONF_PATHS_NULSTR("systemd/sleep.conf.d"),
"Sleep\0", config_item_table_lookup, items,
CONFIG_PARSE_WARN, NULL);
if (streq(verb, "suspend")) {
/* empty by default */

View File

@ -311,7 +311,7 @@ static void test_config_parse(unsigned i, const char *s) {
r = config_parse(NULL, name, f,
"Section\0",
config_item_table_lookup, items,
false, false, true, NULL);
CONFIG_PARSE_WARN, NULL);
switch (i) {
case 0 ... 3:

View File

@ -114,7 +114,7 @@ int manager_parse_config_file(Manager *m) {
CONF_PATHS_NULSTR("systemd/timesyncd.conf.d"),
"Time\0",
config_item_perf_lookup, timesyncd_gperf_lookup,
false, m);
CONFIG_PARSE_WARN, m);
if (r < 0)
return r;

View File

@ -310,7 +310,7 @@ static int parse_password(const char *filename, char **wall) {
r = config_parse(NULL, filename, NULL,
NULL,
config_item_table_lookup, items,
true, false, true, NULL);
CONFIG_PARSE_RELAXED|CONFIG_PARSE_WARN, NULL);
if (r < 0)
return r;

View File

@ -175,7 +175,7 @@ static int load_link(link_config_ctx *ctx, const char *filename) {
r = config_parse(NULL, filename, file,
"Match\0Link\0Ethernet\0",
config_item_perf_lookup, link_config_gperf_lookup,
false, false, true, link);
CONFIG_PARSE_WARN, link);
if (r < 0)
return r;
else