summaryrefslogtreecommitdiff
path: root/core/systemd/0004-core-Make-sure-a-stamp-file-exists-for-all-Persisten.patch
blob: 741176e62ee27dfcf7a5725c05c9ed2be674f41c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
From 7cee0cf1ecc18ea8a016bef48c79f7ea4fd4195a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20B=C3=A4chler?= <thomas@archlinux.org>
Date: Wed, 2 Apr 2014 20:18:44 +0200
Subject: [PATCH 4/4] core: Make sure a stamp file exists for all
 Persistent=true timers

If a persistent timer has no stamp file yet, it behaves just like a normal
timer until it runs for the first time. If the system is always shut down
while the timer is supposed to run, a stamp file is never created and
Peristent=true has no effect.

This patch fixes this by creating a stamp file with the current time
when the timer is first started.
---
 src/core/timer.c | 40 ++++++++++++++++++++++++----------------
 1 file changed, 24 insertions(+), 16 deletions(-)

diff --git a/src/core/timer.c b/src/core/timer.c
index 6c85304..b0a9023 100644
--- a/src/core/timer.c
+++ b/src/core/timer.c
@@ -111,6 +111,23 @@ static int timer_add_default_dependencies(Timer *t) {
         return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
 }
 
+static void update_stampfile(Timer *t, usec_t timestamp) {
+        _cleanup_close_ int fd = -1;
+
+        mkdir_parents_label(t->stamp_path, 0755);
+
+        /* Update the file atime + mtime, if we can */
+        fd = open(t->stamp_path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
+        if (fd >= 0) {
+                struct timespec ts[2];
+
+                timespec_store(&ts[0], timestamp);
+                ts[1] = ts[0];
+
+                futimens(fd, ts);
+        }
+}
+
 static int timer_setup_persistent(Timer *t) {
         int r;
 
@@ -496,22 +513,8 @@ static void timer_enter_running(Timer *t) {
 
         dual_timestamp_get(&t->last_trigger);
 
-        if (t->stamp_path) {
-                _cleanup_close_ int fd = -1;
-
-                mkdir_parents_label(t->stamp_path, 0755);
-
-                /* Update the file atime + mtime, if we can */
-                fd = open(t->stamp_path, O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0644);
-                if (fd >= 0) {
-                        struct timespec ts[2];
-
-                        timespec_store(&ts[0], t->last_trigger.realtime);
-                        ts[1] = ts[0];
-
-                        futimens(fd, ts);
-                }
-        }
+        if (t->stamp_path)
+                update_stampfile(t, t->last_trigger.realtime);
 
         timer_set_state(t, TIMER_RUNNING);
         return;
@@ -539,6 +542,11 @@ static int timer_start(Unit *u) {
 
                 if (stat(t->stamp_path, &st) >= 0)
                         t->last_trigger.realtime = timespec_load(&st.st_atim);
+                else if (errno == ENOENT)
+                        /* The timer has never run before,
+                         * make sure a stamp file exists.
+                         */
+                        update_stampfile(t, now(CLOCK_REALTIME));
         }
 
         t->result = TIMER_SUCCESS;
-- 
1.9.2