]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
conf-parser: turn three bool function params into a flags fields
authorLennart Poettering <lennart@poettering.net>
Wed, 8 Nov 2017 23:26:11 +0000 (00:26 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 13 Nov 2017 09:24:03 +0000 (10:24 +0100)
This makes things more readable and fixes some issues with incorrect
flag propagation between the various flavours of config_parse().

21 files changed:
src/core/load-dropin.c
src/core/load-fragment.c
src/core/main.c
src/coredump/coredump.c
src/journal-remote/journal-remote.c
src/journal-remote/journal-upload.c
src/journal/journald-server.c
src/login/logind.c
src/network/netdev/netdev.c
src/network/networkd-conf.c
src/network/networkd-network.c
src/nspawn/nspawn-settings.c
src/resolve/resolved-conf.c
src/shared/conf-parser.c
src/shared/conf-parser.h
src/shared/install.c
src/shared/sleep-config.c
src/test/test-conf-parser.c
src/timesync/timesyncd-conf.c
src/tty-ask-password-agent/tty-ask-password-agent.c
src/udev/net/link-config.c

index 948d1bc248ceddd38515786f2eb131c14ec00d23..c98ab4164f588a60c7ee2d4083f4d04e59195520 100644 (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);
 
index 84f2931b6302656d1143f7faec3cebcf0e871793..53a95caeaf33dabf878cf7273f495b136f400878 100644 (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;
         }
index 3e766f0645151b31af8323d28de3345f0e09f166..96cac1cd9ecd77cc82b07c97d4ed419439da71ed 100644 (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. */
index 300d6479039d77197a13269eed8bea215b35f581..ee258a121953587a9032aefeba6109eedf7c3a9f 100644 (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) {
index e045e9a8424d4657ca3ef3d9fa4fd0dd03fc3feb..90cd4447d75f4fd7cc81012623269392b7f5e356 100644 (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) {
index ea264989ab1b6052c98b9ad807b59bb638aed3fc..1e3e541998804d2b65eaf2babab4f22f9385eb68 100644 (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) {
index 00ad168286724b926ea39dcb8c4ecc2413613faa..e719fae007a958383a2d40067f886b2d7adfdc51 100644 (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) {
index 6046596684e512503ea52f92ca05be6675e03d61..cdd1710fd34894a01cb4c3c90293145908b3542a 100644 (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) {
index 0e1a7d1335e9902d6411dadcff13eade68c6e6de..2ec526e636ca1629f1be139d1cf6c0122323cdfb 100644 (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;
 
index 025662437b8bd0d14aeb3ad21252dc988bf9a2ca..25ec160df84e5b18396a5ae63f250acc24287146 100644 (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] = {
index 29f1586d1303a408bea4a139bd6c95d7567f8b5f..3ed82ca3f9ee07cf735b7aa57a32865902730798 100644 (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;
 
index 285e22820f099e8149ae62e8946100c7768b754c..5f9c1f5ea4d5a13ee70a6fba2d112081c6618784 100644 (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;
index 3cf4261ff0a85294587d48c42a2702205c961c09..39dc358a489932f819ffd8c5a888c9616afa1ef4 100644 (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;
 
index d1c73b6f2dd7168f3fb361ed44b54dee97862225..86cf71afac396c2320b514c734e038af57bb9786 100644 (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)                         \
index ce1113485d5915797bacdcad8f25e1e5da4d2cd2..a270488c233317c57fc9e83cf015252645867a40 100644 (file)
 #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 */
index ed0c4a5a11bbafb2800f2571ccda7aa5f659915f..d122aebc0987a203937b2f17d637f1f47e53e864 100644 (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);
 
index 8c1624ff46733d65e8d6af7ada8f86823496405c..b4c3037f0cc3c7eb12a5abb26df22980453ccbdc 100644 (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 */
index 7a7de98bec655899204c0961fdf79fe70ff870c7..2974d533c3292f667d9bc10384ad9507318e22a7 100644 (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:
index b62e20c287e233b4abb10b9c442fb3d07d837b45..333f81a94899c148bbaf71db5bae4bc4f399116a 100644 (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;
 
index 495ae464b475d9233f003e6fe289a55fed877bc2..9dd7ea181107ae47668742e39cc12fa7b9cdc70d 100644 (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;
 
index a5f3b1a1b0f163dbec86170f5c138c201d639787..fcbadf3e7959030504d8805206626fa0eeee6f45 100644 (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