core: don't drop timer expired but not yet processed when system date is changed 27/192227/2
authorHyotaek Shim <hyotaek.shim@samsung.com>
Thu, 1 Nov 2018 04:38:43 +0000 (13:38 +0900)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Thu, 1 Nov 2018 04:45:29 +0000 (13:45 +0900)
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
----------------------------------------------------------------------

Origin source: https://github.com/systemd/systemd/commit/fee04d7f3ab810e99b97535ca5fda2f9517acda9

Change-Id: Ibaaa89bec9c55f43336388ee85f07b89c1a50ea4
Signed-off-by: Hyotaek Shim <hyotaek.shim@samsung.com>
src/core/timer.c

index 3206296..8056f8d 100644 (file)
@@ -269,7 +269,7 @@ static void timer_set_state(Timer *t, TimerState state) {
         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
 }
 
-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);
@@ -281,7 +281,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);
 
@@ -351,7 +351,7 @@ static void add_random(Timer *t, usec_t *v) {
         log_unit_info(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;
         usec_t ts_realtime, ts_monotonic;
         usec_t base = 0;
@@ -458,7 +458,8 @@ static void timer_enter_waiting(Timer *t, bool initial) {
 
                         v->next_elapse = base + v->value;
 
-                        if (!initial && v->next_elapse < ts_monotonic && IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
+                        if (!initial && !time_change &&
+                            v->next_elapse < ts_monotonic && IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
                                 /* This is a one time trigger, disable it now */
                                 v->disabled = true;
                                 continue;
@@ -636,7 +637,7 @@ static int timer_start(Unit *u) {
         }
 
         t->result = TIMER_SUCCESS;
-        timer_enter_waiting(t, true);
+        timer_enter_waiting(t, true, false);
         return 1;
 }
 
@@ -759,14 +760,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;
 
@@ -799,7 +800,7 @@ static void timer_time_change(Unit *u) {
                 return;
 
         log_unit_debug(u, "Time change, recalculating next elapse.");
-        timer_enter_waiting(t, false);
+        timer_enter_waiting(t, false, true);
 }
 
 static const char* const timer_base_table[_TIMER_BASE_MAX] = {