]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
conf: replace config_parse_many_nulstr() with config_parse_config_file()
authorFranck Bui <fbui@suse.com>
Fri, 27 Jan 2023 10:32:27 +0000 (11:32 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 13 Mar 2023 18:31:21 +0000 (03:31 +0900)
All daemons use a similar scheme to read their main config files and theirs
drop-ins. The main config files are always stored in /etc/systemd directory and
it's easy enough to construct the name of the drop-in directories based on the
name of the main config file.

Hence the new helper does that internally, which allows to reduce and simplify
the args passed previously to config_parse_many_nulstr().

Besides the overall code simplification it results:

  16 files changed, 87 insertions(+), 159 deletions(-)

it allows to identify clearly the locations in the code where configuration
files are parsed.

16 files changed:
src/basic/constants.h
src/core/main.c
src/coredump/coredump.c
src/home/homed-conf.c
src/journal-remote/journal-remote-main.c
src/journal-remote/journal-upload.c
src/journal/journald-server.c
src/login/logind-core.c
src/network/networkd-conf.c
src/oom/oomd.c
src/pstore/pstore.c
src/resolve/resolved-conf.c
src/shared/conf-parser.c
src/shared/conf-parser.h
src/shared/sleep-config.c
src/timesync/timesyncd-conf.c

index 3e935f1bcaad8d57e5e8ee7c65742a6e21d87c37..3f96786da967e2874670094b62f876a5a0fc2fcb 100644 (file)
 #  define _CONF_PATHS_SPLIT_USR(n)
 #endif
 
-/* Return a nulstr for a standard cascade of configuration paths,
- * suitable to pass to conf_files_list_nulstr() or config_parse_many_nulstr()
- * to implement drop-in directories for extending configuration
- * files. */
+/* Return a nulstr for a standard cascade of configuration paths, suitable to pass to
+ * conf_files_list_nulstr() to implement drop-in directories for extending configuration files. */
 #define CONF_PATHS_NULSTR(n)                    \
         "/etc/" n "\0"                          \
         "/run/" n "\0"                          \
index 3f63150f31351303a84eef0dd4cd8d7b58379352..5f8f251e9a2004aa49446b1359d38370f68f906b 100644 (file)
@@ -704,33 +704,32 @@ static int parse_config_file(void) {
                 {}
         };
 
-        _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
-        const char *suffix;
-        int r;
-
         if (arg_runtime_scope == RUNTIME_SCOPE_SYSTEM)
-                suffix = "system.conf.d";
+                (void) config_parse_config_file("system.conf",
+                                                "Manager\0",
+                                                config_item_table_lookup, items,
+                                                CONFIG_PARSE_WARN,
+                                                NULL);
         else {
+                _cleanup_strv_free_ char **files = NULL, **dirs = NULL;
+                int r;
+
                 assert(arg_runtime_scope == RUNTIME_SCOPE_USER);
 
                 r = manager_find_user_config_paths(&files, &dirs);
                 if (r < 0)
                         return log_error_errno(r, "Failed to determine config file paths: %m");
 
-                suffix = "user.conf.d";
+                (void) config_parse_many(
+                                (const char* const*) files,
+                                (const char* const*) dirs,
+                                "user.conf.d",
+                                "Manager\0",
+                                config_item_table_lookup, items,
+                                CONFIG_PARSE_WARN,
+                                NULL, NULL, NULL);
         }
 
-        (void) config_parse_many(
-                        (const char* const*) (files ?: STRV_MAKE(PKGSYSCONFDIR "/system.conf")),
-                        (const char* const*) (dirs ?: CONF_PATHS_STRV("systemd")),
-                        suffix,
-                        "Manager\0",
-                        config_item_table_lookup, items,
-                        CONFIG_PARSE_WARN,
-                        NULL,
-                        NULL,
-                        NULL);
-
         /* Traditionally "0" was used to turn off the default unit timeouts. Fix this up so that we use
          * USEC_INFINITY like everywhere else. */
         if (arg_default_timeout_start_usec <= 0)
index d5d1f49d08aa7ebed29a6b57fef50b7b55b72566..e715fd232b777908302be5b9ca50640d2b374b62 100644 (file)
@@ -171,14 +171,9 @@ static int parse_config(void) {
                 {}
         };
 
-        return config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/coredump.conf",
-                        CONF_PATHS_NULSTR("systemd/coredump.conf.d"),
-                        "Coredump\0",
-                        config_item_table_lookup, items,
-                        CONFIG_PARSE_WARN,
-                        NULL,
-                        NULL);
+        return config_parse_config_file("coredump.conf", "Coredump\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 }
 
 static uint64_t storage_size_max(void) {
index 429a6e3c555f0ff32de2f27786d2a3c459891de1..ffa4bb3bd796b90ce6104d81f0d79f19b4dc1d84 100644 (file)
@@ -6,23 +6,12 @@
 #include "homed-conf.h"
 
 int manager_parse_config_file(Manager *m) {
-        int r;
 
         assert(m);
 
-        r = config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/homed.conf",
-                        CONF_PATHS_NULSTR("systemd/homed.conf.d"),
-                        "Home\0",
-                        config_item_perf_lookup, homed_gperf_lookup,
-                        CONFIG_PARSE_WARN,
-                        m,
-                        NULL);
-        if (r < 0)
-                return r;
-
-        return 0;
-
+        return config_parse_config_file("homed.conf", "Home\0",
+                                        config_item_perf_lookup, homed_gperf_lookup,
+                                        CONFIG_PARSE_WARN, m);
 }
 
 DEFINE_CONFIG_PARSE_ENUM(config_parse_default_storage, user_storage, UserStorage, "Failed to parse default storage setting");
index e620b60a31a66faedba4ef40e66530e29d62067b..956c96c5e34e73e6199d254b68cde0a7cc8781d0 100644 (file)
@@ -769,14 +769,9 @@ 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,
-                        CONFIG_PARSE_WARN,
-                        NULL,
-                        NULL);
+        return config_parse_config_file("journal-remote.conf", "Remote\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 }
 
 static int help(void) {
index fc55f13bab956337095619280759c2a4405b9e12..e42516c7990edc08c0b6fb96596ae5fa5fcf3f94 100644 (file)
@@ -568,14 +568,9 @@ 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,
-                        CONFIG_PARSE_WARN,
-                        NULL,
-                        NULL);
+        return config_parse_config_file("journal-upload.conf", "Upload\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 }
 
 static int help(void) {
index 3e3b6d44d67e6fa933d26d027c1c304e8710f928..a8958fcb0cfd0d332afa8d1d9972388fad6bc253 100644 (file)
@@ -1853,36 +1853,16 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
 }
 
 static int server_parse_config_file(Server *s) {
-        int r;
+        const char *conf_file = "journald.conf";
 
         assert(s);
 
-        if (s->namespace) {
-                const char *namespaced, *dropin_dirname;
-
-                /* If we are running in namespace mode, load the namespace specific configuration file, and nothing else */
-                namespaced = strjoina(PKGSYSCONFDIR "/journald@", s->namespace, ".conf");
-                dropin_dirname = strjoina("journald@", s->namespace, ".conf.d");
-
-                r = config_parse_many(
-                                STRV_MAKE_CONST(namespaced),
-                                (const char* const*) CONF_PATHS_STRV("systemd"),
-                                dropin_dirname,
-                                "Journal\0",
-                                config_item_perf_lookup, journald_gperf_lookup,
-                                CONFIG_PARSE_WARN, s, NULL, NULL);
-                if (r < 0)
-                        return r;
-
-                return 0;
-        }
+        if (s->namespace)
+                conf_file = strjoina("journald@", s->namespace, ".conf");
 
-        return config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/journald.conf",
-                        CONF_PATHS_NULSTR("systemd/journald.conf.d"),
-                        "Journal\0",
-                        config_item_perf_lookup, journald_gperf_lookup,
-                        CONFIG_PARSE_WARN, s, NULL);
+        return config_parse_config_file(conf_file, "Journal\0",
+                                        config_item_perf_lookup, journald_gperf_lookup,
+                                        CONFIG_PARSE_WARN, s);
 }
 
 static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
index 7d4ff6c98be19d8fdb054a74e421d7d8bce51a5f..618850febaf77eeb5c48e73f7f853a85884f9bc0 100644 (file)
@@ -78,13 +78,9 @@ void manager_reset_config(Manager *m) {
 int manager_parse_config_file(Manager *m) {
         assert(m);
 
-        return config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/logind.conf",
-                        CONF_PATHS_NULSTR("systemd/logind.conf.d"),
-                        "Login\0",
-                        config_item_perf_lookup, logind_gperf_lookup,
-                        CONFIG_PARSE_WARN, m,
-                        NULL);
+        return config_parse_config_file("logind.conf", "Login\0",
+                                        config_item_perf_lookup, logind_gperf_lookup,
+                                        CONFIG_PARSE_WARN, m);
 }
 
 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **ret_device) {
index f73b2d43c708179ae9d772722b58b4e3bfefbd38..063732a3b4135bfe137afcf2e9613ffa35f3b728 100644 (file)
@@ -14,17 +14,14 @@ int manager_parse_config_file(Manager *m) {
 
         assert(m);
 
-        r = config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/networkd.conf",
-                        CONF_PATHS_NULSTR("systemd/networkd.conf.d"),
-                        "Network\0"
-                        "DHCPv4\0"
-                        "DHCPv6\0"
-                        "DHCP\0",
-                        config_item_perf_lookup, networkd_gperf_lookup,
-                        CONFIG_PARSE_WARN,
-                        m,
-                        NULL);
+        r = config_parse_config_file("networkd.conf",
+                                     "Network\0"
+                                     "DHCPv4\0"
+                                     "DHCPv6\0"
+                                     "DHCP\0",
+                                     config_item_perf_lookup, networkd_gperf_lookup,
+                                     CONFIG_PARSE_WARN,
+                                     m);
         if (r < 0)
                 return r;
 
index 2d6819daeed23615be802043b8061ec5be575b52..28405e2f6463ce14b4e13c14a9d867f005d5e7c4 100644 (file)
@@ -31,14 +31,9 @@ static int parse_config(void) {
                 {}
         };
 
-        return config_parse_many_nulstr(PKGSYSCONFDIR "/oomd.conf",
-                                        CONF_PATHS_NULSTR("systemd/oomd.conf.d"),
-                                        "OOM\0",
-                                        config_item_table_lookup,
-                                        items,
-                                        CONFIG_PARSE_WARN,
-                                        NULL,
-                                        NULL);
+        return config_parse_config_file("oomd.conf", "OOM\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 }
 
 static int help(void) {
index e8c21450600695758ec6bc8d7125b197c92645e3..fa6d6ec0e9c3d8adf99c444107a7bba344ca3b24 100644 (file)
@@ -77,14 +77,9 @@ static int parse_config(void) {
                 {}
         };
 
-        return config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/pstore.conf",
-                        CONF_PATHS_NULSTR("systemd/pstore.conf.d"),
-                        "PStore\0",
-                        config_item_table_lookup, items,
-                        CONFIG_PARSE_WARN,
-                        NULL,
-                        NULL);
+        return config_parse_config_file("pstore.conf", "PStore\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 }
 
 /* File list handling - PStoreEntry is the struct and
index d6929984e9153d687e06c67f8ed8a69bac8e354a..70a6f994503107debfb28e3408615a3fb9760ac4 100644 (file)
@@ -563,14 +563,9 @@ int manager_parse_config_file(Manager *m) {
 
         assert(m);
 
-        r = config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/resolved.conf",
-                        CONF_PATHS_NULSTR("systemd/resolved.conf.d"),
-                        "Resolve\0",
-                        config_item_perf_lookup, resolved_gperf_lookup,
-                        CONFIG_PARSE_WARN,
-                        m,
-                        NULL);
+        r = config_parse_config_file("resolved.conf", "Resolve\0",
+                                     config_item_perf_lookup, resolved_gperf_lookup,
+                                     CONFIG_PARSE_WARN, m);
         if (r < 0)
                 return r;
 
index 29051ca0e34b11af116945c3f35e05998c7f3a6d..138a3a8cc9eda32c087caa11c70649fa856871c7 100644 (file)
@@ -533,27 +533,52 @@ static int config_parse_many_files(
         return 0;
 }
 
-/* Parse each config file in the directories specified as nulstr. */
-int config_parse_many_nulstr(
+/* Parse one main config file located in /etc/systemd and its drop-ins, which is what all systemd daemons
+ * do. */
+int config_parse_config_file(
                 const char *conf_file,
-                const char *conf_file_dirs,
                 const char *sections,
                 ConfigItemLookup lookup,
                 const void *table,
                 ConfigParseFlags flags,
-                void *userdata,
-                Hashmap **ret_stats_by_path) {
+                void *userdata) {
 
-        _cleanup_strv_free_ char **files = NULL;
+        _cleanup_strv_free_ char **dropins = NULL, **dropin_dirs = NULL;
+        char **conf_paths = CONF_PATHS_STRV("");
         int r;
 
-        r = conf_files_list_nulstr(&files, ".conf", NULL, 0, conf_file_dirs);
+        assert(conf_file);
+
+        /* build the dropin dir list */
+        dropin_dirs = new0(char*, strv_length(conf_paths) + 1);
+        if (!dropin_dirs) {
+                if (flags & CONFIG_PARSE_WARN)
+                        return log_oom();
+                return -ENOMEM;
+        }
+
+        size_t i = 0;
+        STRV_FOREACH(p, conf_paths) {
+                char *d;
+
+                d = strjoin(*p, "systemd/", conf_file, ".d");
+                if (!d) {
+                        if (flags & CONFIG_PARSE_WARN)
+                                return log_oom();
+                        return -ENOMEM;
+                }
+
+                dropin_dirs[i++] = d;
+        }
+
+        r = conf_files_list_strv(&dropins, ".conf", NULL, 0, (const char**) dropin_dirs);
         if (r < 0)
                 return r;
 
-        return config_parse_many_files(STRV_MAKE_CONST(conf_file),
-                                       files, sections, lookup, table, flags, userdata,
-                                       ret_stats_by_path);
+        const char *sysconf_file = strjoina(PKGSYSCONFDIR, "/", conf_file);
+
+        return config_parse_many_files(STRV_MAKE_CONST(sysconf_file), dropins,
+                                       sections, lookup, table, flags, userdata, NULL);
 }
 
 static int config_get_dropin_files(
index e1765f5874426c959cdedd594212477bccd7b74c..e5aeeac6878e9a6bc161a162b0d752c1a57060db 100644 (file)
@@ -93,15 +93,13 @@ int config_parse(
                 void *userdata,
                 struct stat *ret_stat);     /* possibly NULL */
 
-int config_parse_many_nulstr(
-                const char *conf_file,      /* possibly NULL */
-                const char *conf_file_dirs, /* nulstr */
+int config_parse_config_file(
+                const char *conf_file,
                 const char *sections,       /* nulstr */
                 ConfigItemLookup lookup,
                 const void *table,
                 ConfigParseFlags flags,
-                void *userdata,
-                Hashmap **ret_stats_by_path);   /* possibly NULL */
+                void *userdata);
 
 int config_parse_many(
                 const char* const* conf_files,  /* possibly empty */
index 1eb00717ca7549c1e4b71f8504852832fe916525..2e3b47ddcac09a7cb58222def9cc57de269b5356 100644 (file)
@@ -93,14 +93,9 @@ int parse_sleep_config(SleepConfig **ret_sleep_config) {
                 {}
         };
 
-        (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,
-                        NULL);
+        (void) config_parse_config_file("sleep.conf", "Sleep\0",
+                                        config_item_table_lookup, items,
+                                        CONFIG_PARSE_WARN, NULL);
 
         /* use default values unless set */
         sc->allow[SLEEP_SUSPEND] = allow_suspend != 0;
index 3e6cb435976f27a20af46cbd5231adf25640e284..9c0b6f7ce1f9b77b3d7bd60b4594d7c6b509f30d 100644 (file)
@@ -102,14 +102,9 @@ int manager_parse_config_file(Manager *m) {
 
         assert(m);
 
-        r = config_parse_many_nulstr(
-                        PKGSYSCONFDIR "/timesyncd.conf",
-                        CONF_PATHS_NULSTR("systemd/timesyncd.conf.d"),
-                        "Time\0",
-                        config_item_perf_lookup, timesyncd_gperf_lookup,
-                        CONFIG_PARSE_WARN,
-                        m,
-                        NULL);
+        r = config_parse_config_file("timesyncd.conf", "Time\0",
+                                     config_item_perf_lookup, timesyncd_gperf_lookup,
+                                     CONFIG_PARSE_WARN, m);
         if (r < 0)
                 return r;