Skip to content

Commit f3dc34e

Browse files
kmeawbluca
authored andcommitted
shared/calendarspec: fix normalization when DST is negative
When trying to calculate the next firing of 'hourly', we'd lose the tm_isdst value on the next iteration. On most systems in Europe/Dublin it would cause a 100% cpu hang due to timers restarting. This happens in Europe/Dublin because Ireland defines the Irish Standard Time as UTC+1, so winter time is encoded in tzdata as negative 1 hour of daylight saving. Before this patch: $ env TZ=IST-1GMT-0,M10.5.0/1,M3.5.0/1 systemd-analyze calendar --base-time='Sat 2025-03-29 22:00:00 UTC' --iterations=5 'hourly' Original form: hourly Normalized form: *-*-* *:00:00 Next elapse: Sat 2025-03-29 23:00:00 GMT (in UTC): Sat 2025-03-29 23:00:00 UTC From now: 13h ago Iteration #2: Sun 2025-03-30 00:00:00 GMT (in UTC): Sun 2025-03-30 00:00:00 UTC From now: 12h ago Iteration #3: Sun 2025-03-30 00:00:00 GMT <-- note every next iteration having the same firing time (in UTC): Sun 2025-03-30 00:00:00 UTC From now: 12h ago ... With this patch: $ env TZ=IST-1GMT-0,M10.5.0/1,M3.5.0/1 systemd-analyze calendar --base-time='Sat 2025-03-29 22:00:00 UTC' --iterations=5 'hourly' Original form: hourly Normalized form: *-*-* *:00:00 Next elapse: Sat 2025-03-29 23:00:00 GMT (in UTC): Sat 2025-03-29 23:00:00 UTC From now: 13h ago Iteration #2: Sun 2025-03-30 00:00:00 GMT (in UTC): Sun 2025-03-30 00:00:00 UTC From now: 12h ago Iteration #3: Sun 2025-03-30 02:00:00 IST <-- the expected 1 hour jump (in UTC): Sun 2025-03-30 01:00:00 UTC From now: 11h ago ... This bug isn't reproduced on Debian and Ubuntu because they mitigate it by using the rearguard version of tzdata. ArchLinux and NixOS don't, so it would cause pid1 to spin during DST transition. This is how the affected tzdata looks like: $ zdump -V -c 2024,2025 Europe/Dublin Europe/Dublin Sun Mar 31 00:59:59 2024 UT = Sun Mar 31 00:59:59 2024 GMT isdst=1 gmtoff=0 Europe/Dublin Sun Mar 31 01:00:00 2024 UT = Sun Mar 31 02:00:00 2024 IST isdst=0 gmtoff=3600 Europe/Dublin Sun Oct 27 00:59:59 2024 UT = Sun Oct 27 01:59:59 2024 IST isdst=0 gmtoff=3600 Europe/Dublin Sun Oct 27 01:00:00 2024 UT = Sun Oct 27 01:00:00 2024 GMT isdst=1 gmtoff=0 Compare it to Europe/London: $ zdump -V -c 2024,2025 Europe/London Europe/London Sun Mar 31 00:59:59 2024 UT = Sun Mar 31 00:59:59 2024 GMT isdst=0 gmtoff=0 Europe/London Sun Mar 31 01:00:00 2024 UT = Sun Mar 31 02:00:00 2024 BST isdst=1 gmtoff=3600 Europe/London Sun Oct 27 00:59:59 2024 UT = Sun Oct 27 01:59:59 2024 BST isdst=1 gmtoff=3600 Europe/London Sun Oct 27 01:00:00 2024 UT = Sun Oct 27 01:00:00 2024 GMT isdst=0 gmtoff=0 Fixes #32039. (cherry picked from commit e4bb033) (cherry picked from commit 07c01efc82d4a239ef0d14da54d36053294ad203) There were some conflicts related to the skipping of 6f5cf41, but the tests pass with and the example output above also looks good, so I think the backport is correct. (cherry picked from commit 1568dea89ebb84ed2c9cf8c45aaf90c07858cbc0)
1 parent c19df38 commit f3dc34e

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/shared/calendarspec.c

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,14 +1229,43 @@ static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
12291229
return (weekdays_bits & (1 << k));
12301230
}
12311231

1232+
static int tm_compare(const struct tm *t1, const struct tm *t2) {
1233+
int r;
1234+
1235+
assert(t1);
1236+
assert(t2);
1237+
1238+
r = CMP(t1->tm_year, t2->tm_year);
1239+
if (r != 0)
1240+
return r;
1241+
1242+
r = CMP(t1->tm_mon, t2->tm_mon);
1243+
if (r != 0)
1244+
return r;
1245+
1246+
r = CMP(t1->tm_mday, t2->tm_mday);
1247+
if (r != 0)
1248+
return r;
1249+
1250+
r = CMP(t1->tm_hour, t2->tm_hour);
1251+
if (r != 0)
1252+
return r;
1253+
1254+
r = CMP(t1->tm_min, t2->tm_min);
1255+
if (r != 0)
1256+
return r;
1257+
1258+
return CMP(t1->tm_sec, t2->tm_sec);
1259+
}
1260+
12321261
/* A safety valve: if we get stuck in the calculation, return an error.
12331262
* C.f. https://bugzilla.redhat.com/show_bug.cgi?id=1941335. */
12341263
#define MAX_CALENDAR_ITERATIONS 1000
12351264

12361265
static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
12371266
struct tm c;
1238-
int tm_usec;
1239-
int r;
1267+
int tm_usec, r;
1268+
bool invalidate_dst = false;
12401269

12411270
/* Returns -ENOENT if the expression is not going to elapse anymore */
12421271

@@ -1249,7 +1278,8 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
12491278
for (unsigned iteration = 0; iteration < MAX_CALENDAR_ITERATIONS; iteration++) {
12501279
/* Normalize the current date */
12511280
(void) mktime_or_timegm(&c, spec->utc);
1252-
c.tm_isdst = spec->dst;
1281+
if (!invalidate_dst)
1282+
c.tm_isdst = spec->dst;
12531283

12541284
c.tm_year += 1900;
12551285
r = find_matching_component(spec, spec->year, &c, &c.tm_year);
@@ -1339,6 +1369,18 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
13391369
if (r == 0)
13401370
continue;
13411371

1372+
r = tm_compare(tm, &c);
1373+
if (r == 0) {
1374+
assert(tm_usec + 1 <= 1000000);
1375+
r = CMP(*usec, (usec_t) tm_usec + 1);
1376+
}
1377+
if (r >= 0) {
1378+
/* We're stuck - advance, let mktime determine DST transition and try again. */
1379+
invalidate_dst = true;
1380+
c.tm_hour++;
1381+
continue;
1382+
}
1383+
13421384
*tm = c;
13431385
*usec = tm_usec;
13441386
return 0;

src/test/test-calendarspec.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a
4747
if (old_tz)
4848
old_tz = strdupa_safe(old_tz);
4949

50-
if (!isempty(new_tz))
50+
if (!isempty(new_tz) && !strchr(new_tz, ','))
5151
new_tz = strjoina(":", new_tz);
5252

5353
assert_se(set_unset_env("TZ", new_tz, true) == 0);
@@ -219,6 +219,8 @@ TEST(calendar_spec_next) {
219219
/* Check that we don't start looping if mktime() moves us backwards */
220220
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "", 1616412478000000, 1617494400000000);
221221
test_next("Sun *-*-* 01:00:00 Europe/Dublin", "IST", 1616412478000000, 1617494400000000);
222+
/* Europe/Dublin TZ that moves DST backwards */
223+
test_next("hourly", "IST-1GMT-0,M10.5.0/1,M3.5.0/1", 1743292800000000, 1743296400000000);
222224
}
223225

224226
TEST(calendar_spec_from_string) {

0 commit comments

Comments
 (0)