]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
core: fix mtime calculation of dropin files
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 3 Mar 2021 23:36:24 +0000 (00:36 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 4 Mar 2021 07:07:03 +0000 (16:07 +0900)
Nominally, the bug was in unit_load_dropin(), which just took the last mtime
instead of calculating the maximum. But instead of adding code to wrap the
loop, this patch goes in the other direction.

All (correct) callers of config_parse() followed a very similar pattern to
calculate the maximum mtime. So let's simplify things by making config_parse()
assume that mtime is initialized and update it to the maximum. This makes all
the callers that care about mtime simpler and also fixes the issue in
unit_load_dropin().

config_parse_many_nulstr() and config_parse_many() are different, because it
makes sense to call them just once, and current ret_mtime behaviour make sense.

Fixes #17730, https://bugzilla.redhat.com/show_bug.cgi?id=1933137.

src/core/load-dropin.c
src/shared/conf-parser.c
src/shared/conf-parser.h

index d1c85e23bfaf1a0cc4bdd33be5bd842886dd4f7a..3bb48564ccdfd642cf0dcc6aac6c28cb8abcd349 100644 (file)
@@ -112,6 +112,7 @@ int unit_load_dropin(Unit *u) {
                         return log_oom();
         }
 
+        u->dropin_mtime = 0;
         STRV_FOREACH(f, u->dropin_paths)
                 (void) config_parse(
                                 u->id, *f, NULL,
index 64ed2571e9b5f183907e5a414264ac2e3d79cf6d..9dfa1907511d16d3c0f4941328be424397c63cd8 100644 (file)
@@ -263,7 +263,7 @@ int config_parse(
                 const void *table,
                 ConfigParseFlags flags,
                 void *userdata,
-                usec_t *ret_mtime) {
+                usec_t *latest_mtime) {
 
         _cleanup_free_ char *section = NULL, *continuation = NULL;
         _cleanup_fclose_ FILE *ours = NULL;
@@ -275,6 +275,9 @@ int config_parse(
         assert(filename);
         assert(lookup);
 
+        /* latest_mtime is an input-output parameter: it will be updated if the mtime of the file we're
+         * looking at is later than the current *latest_mtime value. */
+
         if (!f) {
                 f = ours = fopen(filename, "re");
                 if (!f) {
@@ -417,8 +420,8 @@ int config_parse(
                 }
         }
 
-        if (ret_mtime)
-                *ret_mtime = mtime;
+        if (latest_mtime)
+                *latest_mtime = MAX(*latest_mtime, mtime);
 
         return 1;
 }
@@ -448,12 +451,9 @@ static int config_parse_many_files(
 
         /* 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);
+                r = config_parse(NULL, *fn, NULL, sections, lookup, table, flags, userdata, &mtime);
                 if (r < 0)
                         return r;
-                mtime = MAX(mtime, t); /* Find the newest */
         }
 
         if (ret_mtime)
index cd09491112428c9695c41731795046af4ccdf962..c4b9891428d27cb576cdf026fcfcb846703d82a7 100644 (file)
@@ -89,7 +89,7 @@ int config_parse(
                 const void *table,
                 ConfigParseFlags flags,
                 void *userdata,
-                usec_t *ret_mtime);         /* possibly NULL */
+                usec_t *latest_mtime);      /* input/output, possibly NULL */
 
 int config_parse_many_nulstr(
                 const char *conf_file,      /* possibly NULL */