summaryrefslogtreecommitdiffstats
path: root/debian/patches/core-fix-mtime-calculation-of-dropin-files.patch
blob: 4c6b1e83ef597339362bc100c807b3b4e169c81d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
From: =?utf-8?q?Zbigniew_J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
Date: Thu, 4 Mar 2021 00:36:24 +0100
Subject: core: fix mtime calculation of dropin files

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.

(cherry picked from commit da46a1bc3cd28ac36114002c216196dae004b05c)
---
 src/core/load-dropin.c   |  1 +
 src/shared/conf-parser.c | 15 +++++++--------
 src/shared/conf-parser.h |  2 +-
 3 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/core/load-dropin.c b/src/core/load-dropin.c
index d1c85e2..3bb4856 100644
--- a/src/core/load-dropin.c
+++ b/src/core/load-dropin.c
@@ -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,
diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c
index 35d301d..099c47a 100644
--- a/src/shared/conf-parser.c
+++ b/src/shared/conf-parser.c
@@ -259,7 +259,7 @@ int config_parse(const char *unit,
                  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;
@@ -271,6 +271,9 @@ int config_parse(const char *unit,
         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) {
@@ -413,8 +416,8 @@ int config_parse(const char *unit,
                 }
         }
 
-        if (ret_mtime)
-                *ret_mtime = mtime;
+        if (latest_mtime)
+                *latest_mtime = MAX(*latest_mtime, mtime);
 
         return 0;
 }
@@ -440,13 +443,9 @@ static int config_parse_many_files(
         }
 
         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;
-                if (t > mtime) /* Find the newest */
-                        mtime = t;
         }
 
         if (ret_mtime)
diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h
index f115cb2..84c9bf6 100644
--- a/src/shared/conf-parser.h
+++ b/src/shared/conf-parser.h
@@ -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 */