Blob Blame History Raw
From 73bf41a783edbff1b367e645956ed602de1889e2 Mon Sep 17 00:00:00 2001
From: Insun <iplayinsun@gmail.com>
Date: Sun, 28 Oct 2018 21:26:13 +0900
Subject: [PATCH] core: don't drop timer expired but not yet processed when
 system date is changed

There is difference between time set by the user and real elapsed time because of accuracy feature.
If you change the system date(or time) between these times, the timer drops.

You can easily reproduce it with the following command.
-----------------------------------------------------------
$ systemd-run --on-active=3s ls; sleep 3; date -s "`date`"
-----------------------------------------------------------

In the following command, the problem is rarely reproduced. But it exists.
---------------------------------------------------------------------------------------------
$ systemd-run --on-active=3s --timer-property=AccuracySec=1us ls ; sleep 1; date -s "`date`"
---------------------------------------------------------------------------------------------

Note : Global AccuracySec value.
----------------------------------------------------------------------
$ cat /etc/systemd/system.conf
DefaultTimerAccuracySec=1min
----------------------------------------------------------------------

(cherry picked from commit fee04d7f3ab810e99b97535ca5fda2f9517acda9)

Related: #1899402
---
 src/core/timer.c | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)

diff --git a/src/core/timer.c b/src/core/timer.c
index 281ac7f97f..ef240a6f19 100644
--- a/src/core/timer.c
+++ b/src/core/timer.c
@@ -262,7 +262,7 @@ static void timer_set_state(Timer *t, TimerState state) {
         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
 }
 
-static void timer_enter_waiting(Timer *t, bool initial);
+static void timer_enter_waiting(Timer *t, bool initial, bool time_change);
 
 static int timer_coldplug(Unit *u) {
         Timer *t = TIMER(u);
@@ -274,7 +274,7 @@ static int timer_coldplug(Unit *u) {
                 return 0;
 
         if (t->deserialized_state == TIMER_WAITING)
-                timer_enter_waiting(t, false);
+                timer_enter_waiting(t, false, false);
         else
                 timer_set_state(t, t->deserialized_state);
 
@@ -334,7 +334,7 @@ static void add_random(Timer *t, usec_t *v) {
         log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
 }
 
-static void timer_enter_waiting(Timer *t, bool initial) {
+static void timer_enter_waiting(Timer *t, bool initial, bool time_change) {
         bool found_monotonic = false, found_realtime = false;
         bool leave_around = false;
         triple_timestamp ts;
@@ -444,7 +444,7 @@ static void timer_enter_waiting(Timer *t, bool initial) {
 
                         v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
 
-                        if (!initial &&
+                        if (!initial && !time_change &&
                             v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
                             IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
                                 /* This is a one time trigger, disable it now */
@@ -642,7 +642,7 @@ static int timer_start(Unit *u) {
         }
 
         t->result = TIMER_SUCCESS;
-        timer_enter_waiting(t, true);
+        timer_enter_waiting(t, true, false);
         return 1;
 }
 
@@ -764,14 +764,14 @@ static void timer_trigger_notify(Unit *u, Unit *other) {
         case TIMER_ELAPSED:
 
                 /* Recalculate sleep time */
-                timer_enter_waiting(t, false);
+                timer_enter_waiting(t, false, false);
                 break;
 
         case TIMER_RUNNING:
 
                 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
                         log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
-                        timer_enter_waiting(t, false);
+                        timer_enter_waiting(t, false, false);
                 }
                 break;
 
@@ -813,7 +813,7 @@ static void timer_time_change(Unit *u) {
                 t->last_trigger.realtime = ts;
 
         log_unit_debug(u, "Time change, recalculating next elapse.");
-        timer_enter_waiting(t, false);
+        timer_enter_waiting(t, false, true);
 }
 
 static void timer_timezone_change(Unit *u) {
@@ -825,7 +825,7 @@ static void timer_timezone_change(Unit *u) {
                 return;
 
         log_unit_debug(u, "Timezone change, recalculating next elapse.");
-        timer_enter_waiting(t, false);
+        timer_enter_waiting(t, false, false);
 }
 
 static const char* const timer_base_table[_TIMER_BASE_MAX] = {