chiark / gitweb /
sd-event: implicitly set signal event source's descriptions to the signal name
[elogind.git] / src / core / timer.c
index 6c853044a17fb5c733fc71b2394cc794520c4aed..a3713e2140d68fba370bf478b6af6899198544e3 100644 (file)
@@ -46,8 +46,8 @@ static void timer_init(Unit *u) {
         assert(u);
         assert(u->load_state == UNIT_STUB);
 
-        t->next_elapse_monotonic_or_boottime = (usec_t) -1;
-        t->next_elapse_realtime = (usec_t) -1;
+        t->next_elapse_monotonic_or_boottime = USEC_INFINITY;
+        t->next_elapse_realtime = USEC_INFINITY;
         t->accuracy_usec = u->manager->default_timer_accuracy_usec;
 }
 
@@ -95,6 +95,7 @@ static int timer_verify(Timer *t) {
 
 static int timer_add_default_dependencies(Timer *t) {
         int r;
+        TimerValue *v;
 
         assert(t);
 
@@ -106,6 +107,15 @@ static int timer_add_default_dependencies(Timer *t) {
                 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
                 if (r < 0)
                         return r;
+
+                LIST_FOREACH(value, v, t->values) {
+                        if (v->base == TIMER_CALENDAR) {
+                                r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, SPECIAL_TIME_SYNC_TARGET, NULL, true);
+                                if (r < 0)
+                                        return r;
+                                break;
+                        }
+                }
         }
 
         return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
@@ -131,7 +141,7 @@ static int timer_setup_persistent(Timer *t) {
 
                 e = getenv("XDG_DATA_HOME");
                 if (e)
-                        t->stamp_path = strjoin(e, "/systemd/timers/", UNIT(t)->id, NULL);
+                        t->stamp_path = strjoin(e, "/systemd/timers/stamp-", UNIT(t)->id, NULL);
                 else {
 
                         _cleanup_free_ char *h = NULL;
@@ -232,7 +242,7 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) {
                                 "%s%s: %s\n",
                                 prefix,
                                 timer_base_to_string(v->base),
-                                strna(format_timespan(timespan1, sizeof(timespan1), v->value, 0)));
+                                format_timespan(timespan1, sizeof(timespan1), v->value, 0));
                 }
         }
 }
@@ -496,22 +506,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)
+                touch_file(t->stamp_path, true, t->last_trigger.realtime, (uid_t) -1, (gid_t) -1, 0);
 
         timer_set_state(t, TIMER_RUNNING);
         return;
@@ -539,6 +535,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.
+                         */
+                        touch_file(t->stamp_path, true, (usec_t) -1, (uid_t) -1, (gid_t) -1, 0);
         }
 
         t->result = TIMER_SUCCESS;