698723
From 3d4280d0a487109f8f648147083baf573e4418a3 Mon Sep 17 00:00:00 2001
698723
From: =?UTF-8?q?Michal=20Koutn=C3=BD?= <mkoutny@suse.com>
698723
Date: Fri, 2 Nov 2018 20:56:08 +0100
698723
Subject: [PATCH] core: Detect initial timer state from serialized data
698723
698723
We keep a mark whether a single-shot timer was triggered in the caller's
698723
variable initial. When such a timer elapses while we are
698723
serializing/deserializing the inner state, we consider the timer
698723
incorrectly as elapsed and don't trigger it later.
698723
698723
This patch exploits last_trigger timestamp that we already serialize,
698723
hence we can eliminate the argument initial completely.
698723
698723
A reproducer for OnBootSec= timers:
698723
        cat >repro.c <
698723
        /*
698723
         * Compile:	gcc repro.c -o repro
698723
         * Run:		./repro
698723
         */
698723
        #include <errno.h>
698723
        #include <fcntl.h>
698723
        #include <stdio.h>
698723
        #include <stdlib.h>
698723
        #include <sys/stat.h>
698723
        #include <sys/types.h>
698723
        #include <time.h>
698723
        #include <unistd.h>
698723
698723
        int main(int argc, char *argv[]) {
698723
        	char command[1024];
698723
        	int pause;
698723
698723
        	struct timespec now;
698723
698723
        	while (1) {
698723
        		usleep(rand() % 200000); // prevent periodic repeats
698723
               		clock_gettime(CLOCK_MONOTONIC, &now;;
698723
        		printf("%i\n", now.tv_sec);
698723
698723
        		system("rm -f $PWD/mark");
698723
        		snprintf(command, 1024, "systemd-run --user --on-boot=%i --timer-property=AccuracySec=100ms "
698723
        					"touch $PWD/mark", now.tv_sec + 1);
698723
        		system(command);
698723
        		system("systemctl --user list-timers");
698723
        		pause = (1000000000 - now.tv_nsec)/1000 - 70000; // fiddle to hit the middle of reloading
698723
        		usleep(pause > 0 ? pause : 0);
698723
        		system("systemctl --user daemon-reload");
698723
        		sync();
698723
        		sleep(2);
698723
        		if (open("./mark", 0) < 0)
698723
        			if (errno == ENOENT) {
698723
        				printf("mark file does not exist\n");
698723
        				break;
698723
        			}
698723
        	}
698723
698723
        	return 0;
698723
        }
698723
        EOD
698723
698723
(cherry picked from commit aa1f95d2647197eca84c33a0f10adaeada08467d)
698723
698723
Resolves: #1899402
698723
---
698723
 src/core/timer.c | 19 ++++++++++---------
698723
 1 file changed, 10 insertions(+), 9 deletions(-)
698723
698723
diff --git a/src/core/timer.c b/src/core/timer.c
698723
index ef240a6f19..1718ffc5a5 100644
698723
--- a/src/core/timer.c
698723
+++ b/src/core/timer.c
698723
@@ -262,7 +262,7 @@ static void timer_set_state(Timer *t, TimerState state) {
698723
         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
698723
 }
698723
 
698723
-static void timer_enter_waiting(Timer *t, bool initial, bool time_change);
698723
+static void timer_enter_waiting(Timer *t, bool time_change);
698723
 
698723
 static int timer_coldplug(Unit *u) {
698723
         Timer *t = TIMER(u);
698723
@@ -274,7 +274,7 @@ static int timer_coldplug(Unit *u) {
698723
                 return 0;
698723
 
698723
         if (t->deserialized_state == TIMER_WAITING)
698723
-                timer_enter_waiting(t, false, false);
698723
+                timer_enter_waiting(t, false);
698723
         else
698723
                 timer_set_state(t, t->deserialized_state);
698723
 
698723
@@ -334,7 +334,7 @@ static void add_random(Timer *t, usec_t *v) {
698723
         log_unit_debug(UNIT(t), "Adding %s random time.", format_timespan(s, sizeof(s), add, 0));
698723
 }
698723
 
698723
-static void timer_enter_waiting(Timer *t, bool initial, bool time_change) {
698723
+static void timer_enter_waiting(Timer *t, bool time_change) {
698723
         bool found_monotonic = false, found_realtime = false;
698723
         bool leave_around = false;
698723
         triple_timestamp ts;
698723
@@ -444,7 +444,8 @@ static void timer_enter_waiting(Timer *t, bool initial, bool time_change) {
698723
 
698723
                         v->next_elapse = usec_add(usec_shift_clock(base, CLOCK_MONOTONIC, TIMER_MONOTONIC_CLOCK(t)), v->value);
698723
 
698723
-                        if (!initial && !time_change &&
698723
+                        if (dual_timestamp_is_set(&t->last_trigger) &&
698723
+                            !time_change &&
698723
                             v->next_elapse < triple_timestamp_by_clock(&ts, TIMER_MONOTONIC_CLOCK(t)) &&
698723
                             IN_SET(v->base, TIMER_ACTIVE, TIMER_BOOT, TIMER_STARTUP)) {
698723
                                 /* This is a one time trigger, disable it now */
698723
@@ -642,7 +643,7 @@ static int timer_start(Unit *u) {
698723
         }
698723
 
698723
         t->result = TIMER_SUCCESS;
698723
-        timer_enter_waiting(t, true, false);
698723
+        timer_enter_waiting(t, false);
698723
         return 1;
698723
 }
698723
 
698723
@@ -764,14 +765,14 @@ static void timer_trigger_notify(Unit *u, Unit *other) {
698723
         case TIMER_ELAPSED:
698723
 
698723
                 /* Recalculate sleep time */
698723
-                timer_enter_waiting(t, false, false);
698723
+                timer_enter_waiting(t, false);
698723
                 break;
698723
 
698723
         case TIMER_RUNNING:
698723
 
698723
                 if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(other))) {
698723
                         log_unit_debug(UNIT(t), "Got notified about unit deactivation.");
698723
-                        timer_enter_waiting(t, false, false);
698723
+                        timer_enter_waiting(t, false);
698723
                 }
698723
                 break;
698723
 
698723
@@ -813,7 +814,7 @@ static void timer_time_change(Unit *u) {
698723
                 t->last_trigger.realtime = ts;
698723
 
698723
         log_unit_debug(u, "Time change, recalculating next elapse.");
698723
-        timer_enter_waiting(t, false, true);
698723
+        timer_enter_waiting(t, true);
698723
 }
698723
 
698723
 static void timer_timezone_change(Unit *u) {
698723
@@ -825,7 +826,7 @@ static void timer_timezone_change(Unit *u) {
698723
                 return;
698723
 
698723
         log_unit_debug(u, "Timezone change, recalculating next elapse.");
698723
-        timer_enter_waiting(t, false, false);
698723
+        timer_enter_waiting(t, false);
698723
 }
698723
 
698723
 static const char* const timer_base_table[_TIMER_BASE_MAX] = {