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