]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Fix issue with system time set back (#24131)
authorJoão Loureiro <joaofl@gmail.com>
Mon, 8 Aug 2022 13:32:19 +0000 (15:32 +0200)
committerGitHub <noreply@github.com>
Mon, 8 Aug 2022 13:32:19 +0000 (15:32 +0200)
Fixes #6036

src/core/timer.c
test/units/testsuite-53.sh

index 63256d541acfe0ef7b8bce69e3012e9d472fd8ac..9de325ba66240af365e0d9ca1b00d1187ed79373 100644 (file)
@@ -394,19 +394,18 @@ static void timer_enter_waiting(Timer *t, bool time_change) {
                 if (v->base == TIMER_CALENDAR) {
                         usec_t b, rebased;
 
-                        /* If we know the last time this was
-                         * triggered, schedule the job based relative
-                         * to that. If we don't, just start from
-                         * the activation time. */
-
-                        if (t->last_trigger.realtime > 0)
+                        /* Update last_trigger to 'now' in case the system time changes, so that
+                         * next_elapse is not stuck with a future date. */
+                        if (time_change)
+                                b = ts.realtime;
+                        /* If we know the last time this was triggered, schedule the job based relative
+                         * to that. If we don't, just start from the activation time. */
+                        else if (t->last_trigger.realtime > 0)
                                 b = t->last_trigger.realtime;
-                        else {
-                                if (state_translation_table[t->state] == UNIT_ACTIVE)
-                                        b = UNIT(t)->inactive_exit_timestamp.realtime;
-                                else
-                                        b = ts.realtime;
-                        }
+                        else if (state_translation_table[t->state] == UNIT_ACTIVE)
+                                b = UNIT(t)->inactive_exit_timestamp.realtime;
+                        else
+                                b = ts.realtime;
 
                         r = calendar_spec_next_usec(v->calendar_spec, b, &v->next_elapse);
                         if (r < 0)
index 84cd66129d007e40c166bf8b772e26161a5a7b14..f12b7379fdb2720b073ded257371a7b008641a38 100755 (executable)
@@ -5,27 +5,44 @@ set -o pipefail
 
 : >/failed
 
+# Run a timer for every 15 minutes before setting the current time
+systemd-run --unit test-timer-1 --on-calendar "*:0/15:0" true
+
 # Reset host date to current time, 3 days in the past.
 date -s "-3 days"
 
-# Run a timer for every 15 minutes.
-systemd-run --unit test-timer --on-calendar "*:0/15:0" true
+# Run another timer for every 15 minutes, after setting the time
+systemd-run --unit test-timer-2 --on-calendar "*:0/15:0" true
+
+next_elapsed_t1=$(systemctl show test-timer-1.timer -p NextElapseUSecRealtime --value)
+next_elapsed_t1=$(date -d "${next_elapsed_t1}" +%s)
+now=$(date +%s)
+time_delta_t1=$((next_elapsed_t1 - now))
 
-next_elapsed=$(systemctl show test-timer.timer -p NextElapseUSecRealtime --value)
-next_elapsed=$(date -d "${next_elapsed}" +%s)
+next_elapsed_t2=$(systemctl show test-timer-2.timer -p NextElapseUSecRealtime --value)
+next_elapsed_t2=$(date -d "${next_elapsed_t2}" +%s)
 now=$(date +%s)
-time_delta=$((next_elapsed - now))
+time_delta_t2=$((next_elapsed_t2 - now))
+
+# Check that the timer will elapse in less than 20 minutes.
+((0 < time_delta_t1 && time_delta_t1 < 1200)) || {
+    echo 'Timer elapse outside of the expected 20 minute window.'
+    echo "  next_elapsed_t1=${next_elapsed_t1}"
+    echo "  now=${now}"
+    echo "  time_delta_t1=${time_delta_t1}"
+    echo ''
+} >>/failed_t1
 
 # Check that the timer will elapse in less than 20 minutes.
-((0 < time_delta && time_delta < 1200)) || {
+((0 < time_delta_t2 && time_delta_t2 < 1200)) || {
     echo 'Timer elapse outside of the expected 20 minute window.'
-    echo "  next_elapsed=${next_elapsed}"
+    echo "  next_elapsed_t2=${next_elapsed_t2}"
     echo "  now=${now}"
-    echo "  time_delta=${time_delta}"
+    echo "  time_delta_t2=${time_delta_t2}"
     echo ''
-} >>/failed
+} >>/failed_t2
 
-if test ! -s /failed ; then
-    rm -f /failed
+if test ! -s /failed_t1 && test ! -s /failed_t2; then
+    rm -f /failed_t*
     touch /testok
 fi