Skip to content

Commit e4bb033

Browse files
kmeawyuwata
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.
1 parent 2c885a0 commit e4bb033

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
@@ -1235,14 +1235,43 @@ static bool matches_weekday(int weekdays_bits, const struct tm *tm, bool utc) {
12351235
return (weekdays_bits & (1 << k));
12361236
}
12371237

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

12421271
static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
12431272
struct tm c;
1244-
int tm_usec;
1245-
int r;
1273+
int tm_usec, r;
1274+
bool invalidate_dst = false;
12461275

12471276
/* Returns -ENOENT if the expression is not going to elapse anymore */
12481277

@@ -1255,7 +1284,8 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
12551284
for (unsigned iteration = 0; iteration < MAX_CALENDAR_ITERATIONS; iteration++) {
12561285
/* Normalize the current date */
12571286
(void) mktime_or_timegm_usec(&c, spec->utc, /* ret= */ NULL);
1258-
c.tm_isdst = spec->dst;
1287+
if (!invalidate_dst)
1288+
c.tm_isdst = spec->dst;
12591289

12601290
c.tm_year += 1900;
12611291
r = find_matching_component(spec, spec->year, &c, &c.tm_year);
@@ -1345,6 +1375,18 @@ static int find_next(const CalendarSpec *spec, struct tm *tm, usec_t *usec) {
13451375
if (r == 0)
13461376
continue;
13471377

1378+
r = tm_compare(tm, &c);
1379+
if (r == 0) {
1380+
assert(tm_usec + 1 <= 1000000);
1381+
r = CMP(*usec, (usec_t) tm_usec + 1);
1382+
}
1383+
if (r >= 0) {
1384+
/* We're stuck - advance, let mktime determine DST transition and try again. */
1385+
invalidate_dst = true;
1386+
c.tm_hour++;
1387+
continue;
1388+
}
1389+
13481390
*tm = c;
13491391
*usec = tm_usec;
13501392
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)