timer: don't use persietent file timestamps from the future (#6823)

Also, use the mtime rather than the atime of the timestamp file. While
the atime is not completely wrong, the mtime appears more appropriate
as that's what we actually explicitly change, and is not effected by
mere reading.

Fixes: #6821
This commit is contained in:
Lennart Poettering 2017-09-14 18:26:10 +02:00 committed by Zbigniew Jędrzejewski-Szmek
parent 60c776fd75
commit 77542a7905
1 changed files with 17 additions and 3 deletions

View File

@ -614,9 +614,23 @@ static int timer_start(Unit *u) {
if (t->stamp_path) {
struct stat st;
if (stat(t->stamp_path, &st) >= 0)
t->last_trigger.realtime = timespec_load(&st.st_atim);
else if (errno == ENOENT)
if (stat(t->stamp_path, &st) >= 0) {
usec_t ft;
/* Load the file timestamp, but only if it is actually in the past. If it is in the future,
* something is wrong with the system clock. */
ft = timespec_load(&st.st_mtim);
if (ft < now(CLOCK_REALTIME))
t->last_trigger.realtime = ft;
else {
char z[FORMAT_TIMESTAMP_MAX];
log_unit_warning(u, "Not using persistent file timestamp %s as it is in the future.",
format_timestamp(z, sizeof(z), ft));
}
} else if (errno == ENOENT)
/* The timer has never run before,
* make sure a stamp file exists.
*/