1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-23 17:34:00 +03:00

shared/conf-parser: allow more than one location of the main config file

No functional change as long as only one path is passed.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2021-02-20 17:04:05 +01:00
parent 38ba3da0a1
commit 8b8024f1c2
7 changed files with 26 additions and 20 deletions

View File

@ -376,6 +376,7 @@ static inline int __coverity_check_and_return__(int condition) {
#define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
#define STRV_MAKE_EMPTY ((char*[1]) { NULL })
#define STRV_MAKE_CONST(...) ((const char* const*) ((const char*[]) { __VA_ARGS__, NULL }))
/* Pointers range from NULL to POINTER_MAX */
#define POINTER_MAX ((void*) UINTPTR_MAX)

View File

@ -686,7 +686,7 @@ int netdev_load_one(Manager *manager, const char *filename) {
dropin_dirname = strjoina(basename(filename), ".d");
r = config_parse_many(
filename, NETWORK_DIRS, dropin_dirname,
STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname,
NETDEV_COMMON_SECTIONS NETDEV_OTHER_SECTIONS,
config_item_perf_lookup, network_netdev_gperf_lookup,
CONFIG_PARSE_WARN,
@ -729,7 +729,7 @@ int netdev_load_one(Manager *manager, const char *filename) {
NETDEV_VTABLE(netdev)->init(netdev);
r = config_parse_many(
filename, NETWORK_DIRS, dropin_dirname,
STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname,
NETDEV_VTABLE(netdev)->sections,
config_item_perf_lookup, network_netdev_gperf_lookup,
CONFIG_PARSE_WARN,

View File

@ -446,7 +446,7 @@ int network_load_one(Manager *manager, OrderedHashmap **networks, const char *fi
};
r = config_parse_many(
filename, NETWORK_DIRS, dropin_dirname,
STRV_MAKE_CONST(filename), NETWORK_DIRS, dropin_dirname,
"Match\0"
"Link\0"
"SR-IOV\0"

View File

@ -88,7 +88,7 @@ static int dnssd_service_load(Manager *manager, const char *filename) {
dropin_dirname = strjoina(service->name, ".dnssd.d");
r = config_parse_many(
filename, DNSSD_SERVICE_DIRS, dropin_dirname,
STRV_MAKE_CONST(filename), DNSSD_SERVICE_DIRS, dropin_dirname,
"Service\0",
config_item_perf_lookup, resolved_dnssd_gperf_lookup,
CONFIG_PARSE_WARN,

View File

@ -419,11 +419,11 @@ int config_parse(
if (ret_mtime)
*ret_mtime = mtime;
return 0;
return 1;
}
static int config_parse_many_files(
const char *conf_file,
const char* const* conf_files,
char **files,
const char *sections,
ConfigItemLookup lookup,
@ -436,20 +436,23 @@ static int config_parse_many_files(
char **fn;
int r;
if (conf_file) {
r = config_parse(NULL, conf_file, NULL, sections, lookup, table, flags, userdata, &mtime);
/* First read the first found main config file. */
STRV_FOREACH(fn, (char**) conf_files) {
r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata, &mtime);
if (r < 0)
return r;
if (r > 0)
break;
}
/* Then read all the drop-ins. */
STRV_FOREACH(fn, files) {
usec_t t;
r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata, &t);
if (r < 0)
return r;
if (t > mtime) /* Find the newest */
mtime = t;
mtime = MAX(mtime, t); /* Find the newest */
}
if (ret_mtime)
@ -476,12 +479,14 @@ int config_parse_many_nulstr(
if (r < 0)
return r;
return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata, ret_mtime);
return config_parse_many_files(STRV_MAKE_CONST(conf_file),
files, sections, lookup, table, flags, userdata,
ret_mtime);
}
/* Parse each config file in the directories specified as strv. */
int config_parse_many(
const char *conf_file,
const char* const* conf_files,
const char* const* conf_file_dirs,
const char *dropin_dirname,
const char *sections,
@ -505,7 +510,7 @@ int config_parse_many(
if (r < 0)
return r;
return config_parse_many_files(conf_file, files, sections, lookup, table, flags, userdata, ret_mtime);
return config_parse_many_files(conf_files, files, sections, lookup, table, flags, userdata, ret_mtime);
}
#define DEFINE_PARSER(type, vartype, conv_func) \

View File

@ -102,7 +102,7 @@ int config_parse_many_nulstr(
usec_t *ret_mtime); /* possibly NULL */
int config_parse_many(
const char *conf_file, /* possibly NULL */
const char* const* conf_files, /* possibly empty */
const char* const* conf_file_dirs,
const char *dropin_dirname,
const char *sections, /* nulstr */

View File

@ -349,27 +349,27 @@ static void test_config_parse(unsigned i, const char *s) {
switch (i) {
case 0 ... 4:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, "1"));
break;
case 5 ... 10:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, "1 2 3"));
break;
case 11:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, "1\\\\ \\\\2"));
break;
case 12:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, x1000("ABCD")));
break;
case 13 ... 14:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, x1000("ABCD") " foobar"));
break;
@ -379,7 +379,7 @@ static void test_config_parse(unsigned i, const char *s) {
break;
case 17:
assert_se(r == 0);
assert_se(r == 1);
assert_se(streq(setting1, "2"));
break;
}