chiark / gitweb /
e95b4d66e10e3b1262cf4da292b817ef882ee163
[elogind.git] / src / timer.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23
24 #include "unit.h"
25 #include "unit-name.h"
26 #include "timer.h"
27 #include "dbus-timer.h"
28
29 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
30         [TIMER_DEAD] = UNIT_INACTIVE,
31         [TIMER_WAITING] = UNIT_ACTIVE,
32         [TIMER_RUNNING] = UNIT_ACTIVE,
33         [TIMER_ELAPSED] = UNIT_ACTIVE,
34         [TIMER_MAINTAINANCE] = UNIT_INACTIVE
35 };
36
37 static void timer_init(Unit *u) {
38         Timer *t = TIMER(u);
39
40         assert(u);
41         assert(u->meta.load_state == UNIT_STUB);
42
43         t->next_elapse = (usec_t) -1;
44         t->timer_watch.type = WATCH_INVALID;
45 }
46
47 static void timer_done(Unit *u) {
48         Timer *t = TIMER(u);
49         TimerValue *v;
50
51         assert(t);
52
53         while ((v = t->values)) {
54                 LIST_REMOVE(TimerValue, value, t->values, v);
55                 free(v);
56         }
57
58         unit_unwatch_timer(u, &t->timer_watch);
59 }
60
61 static int timer_verify(Timer *t) {
62         assert(t);
63
64         if (UNIT(t)->meta.load_state != UNIT_LOADED)
65                 return 0;
66
67         if (!t->values) {
68                 log_error("%s lacks value setting. Refusing.", t->meta.id);
69                 return -EINVAL;
70         }
71
72         return 0;
73 }
74
75 static int timer_load(Unit *u) {
76         Timer *t = TIMER(u);
77         int r;
78
79         assert(u);
80         assert(u->meta.load_state == UNIT_STUB);
81
82         if ((r = unit_load_fragment_and_dropin(u)) < 0)
83                 return r;
84
85         if (u->meta.load_state == UNIT_LOADED) {
86
87                 if (!t->unit)
88                         if ((r = unit_load_related_unit(u, ".service", &t->unit)))
89                                 return r;
90
91                 if ((r = unit_add_dependency(u, UNIT_BEFORE, t->unit, true)) < 0)
92                         return r;
93         }
94
95         return timer_verify(t);
96 }
97
98 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
99         Timer *t = TIMER(u);
100         const char *prefix2;
101         char *p2;
102         TimerValue *v;
103         char
104                 timespan1[FORMAT_TIMESPAN_MAX];
105
106         p2 = strappend(prefix, "\t");
107         prefix2 = p2 ? p2 : prefix;
108
109         fprintf(f,
110                 "%sTimer State: %s\n"
111                 "%sUnit: %s\n",
112                 prefix, timer_state_to_string(t->state),
113                 prefix, t->unit->meta.id);
114
115         LIST_FOREACH(value, v, t->values)
116                 fprintf(f,
117                         "%s%s: %s\n",
118                         prefix,
119                         timer_base_to_string(v->base),
120                         strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
121
122         free(p2);
123 }
124
125 static void timer_set_state(Timer *t, TimerState state) {
126         TimerState old_state;
127         assert(t);
128
129         old_state = t->state;
130         t->state = state;
131
132         if (state != TIMER_WAITING)
133                 unit_unwatch_timer(UNIT(t), &t->timer_watch);
134
135         if (state != old_state)
136                 log_debug("%s changed %s -> %s",
137                           t->meta.id,
138                           timer_state_to_string(old_state),
139                           timer_state_to_string(state));
140
141         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
142 }
143
144 static void timer_enter_waiting(Timer *t, bool initial);
145
146 static int timer_coldplug(Unit *u) {
147         Timer *t = TIMER(u);
148
149         assert(t);
150         assert(t->state == TIMER_DEAD);
151
152         if (t->deserialized_state != t->state) {
153
154                 if (t->deserialized_state == TIMER_WAITING ||
155                     t->deserialized_state == TIMER_RUNNING ||
156                     t->deserialized_state == TIMER_ELAPSED)
157                         timer_enter_waiting(t, false);
158                 else
159                         timer_set_state(t, t->deserialized_state);
160         }
161
162         return 0;
163 }
164
165 static void timer_enter_dead(Timer *t, bool success) {
166         assert(t);
167
168         if (!success)
169                 t->failure = true;
170
171         timer_set_state(t, t->failure ? TIMER_MAINTAINANCE : TIMER_DEAD);
172 }
173
174 static void timer_enter_waiting(Timer *t, bool initial) {
175         TimerValue *v;
176         usec_t base = 0, delay, n;
177         bool found = false;
178         int r;
179
180         n = now(CLOCK_MONOTONIC);
181
182         LIST_FOREACH(value, v, t->values) {
183
184                 if (v->disabled)
185                         continue;
186
187                 switch (v->base) {
188
189                 case TIMER_ACTIVE:
190                         if (state_translation_table[t->state] == UNIT_ACTIVE) {
191                                 base = t->meta.inactive_exit_timestamp.monotonic;
192                         } else
193                                 base = n;
194                         break;
195
196                 case TIMER_BOOT:
197                         /* CLOCK_MONOTONIC equals the uptime on Linux */
198                         base = 0;
199                         break;
200
201                 case TIMER_STARTUP:
202                         base = t->meta.manager->startup_timestamp.monotonic;
203                         break;
204
205                 case TIMER_UNIT_ACTIVE:
206
207                         if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
208                                 continue;
209
210                         base = t->unit->meta.inactive_exit_timestamp.monotonic;
211                         break;
212
213                 case TIMER_UNIT_INACTIVE:
214
215                         if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
216                                 continue;
217
218                         base = t->unit->meta.inactive_enter_timestamp.monotonic;
219                         break;
220
221                 default:
222                         assert_not_reached("Unknown timer base");
223                 }
224
225                 v->next_elapse = base + v->value;
226
227                 if (!initial && v->next_elapse < n) {
228                         v->disabled = true;
229                         continue;
230                 }
231
232                 if (!found)
233                         t->next_elapse = v->next_elapse;
234                 else
235                         t->next_elapse = MIN(t->next_elapse, v->next_elapse);
236
237                 found = true;
238         }
239
240         if (!found) {
241                 timer_set_state(t, TIMER_ELAPSED);
242                 return;
243         }
244
245         delay = n < t->next_elapse ? t->next_elapse - n : 0;
246
247         if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
248                 goto fail;
249
250         timer_set_state(t, TIMER_WAITING);
251         return;
252
253 fail:
254         log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
255         timer_enter_dead(t, false);
256 }
257
258 static void timer_enter_running(Timer *t) {
259         int r;
260         assert(t);
261
262         if ((r = manager_add_job(UNIT(t)->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
263                 goto fail;
264
265         timer_set_state(t, TIMER_RUNNING);
266         return;
267
268 fail:
269         log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
270         timer_enter_dead(t, false);
271 }
272
273 static int timer_start(Unit *u) {
274         Timer *t = TIMER(u);
275
276         assert(t);
277         assert(t->state == TIMER_DEAD);
278
279         timer_enter_waiting(t, true);
280         return 0;
281 }
282
283 static int timer_stop(Unit *u) {
284         Timer *t = TIMER(u);
285
286         assert(t);
287         assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
288
289         timer_enter_dead(t, true);
290         return 0;
291 }
292
293 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
294         Timer *t = TIMER(u);
295
296         assert(u);
297         assert(f);
298         assert(fds);
299
300         unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
301
302         return 0;
303 }
304
305 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
306         Timer *t = TIMER(u);
307
308         assert(u);
309         assert(key);
310         assert(value);
311         assert(fds);
312
313         if (streq(key, "state")) {
314                 TimerState state;
315
316                 if ((state = timer_state_from_string(value)) < 0)
317                         log_debug("Failed to parse state value %s", value);
318                 else
319                         t->deserialized_state = state;
320         } else
321                 log_debug("Unknown serialization key '%s'", key);
322
323         return 0;
324 }
325
326 static UnitActiveState timer_active_state(Unit *u) {
327         assert(u);
328
329         return state_translation_table[TIMER(u)->state];
330 }
331
332 static const char *timer_sub_state_to_string(Unit *u) {
333         assert(u);
334
335         return timer_state_to_string(TIMER(u)->state);
336 }
337
338 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
339         Timer *t = TIMER(u);
340
341         assert(t);
342         assert(elapsed == 1);
343
344         if (t->state != TIMER_WAITING)
345                 return;
346
347         log_debug("Timer elapsed on %s", u->meta.id);
348         timer_enter_running(t);
349 }
350
351 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
352         char *n;
353         int r;
354         Iterator i;
355
356         if (u->meta.type == UNIT_TIMER)
357                 return;
358
359         SET_FOREACH(n, u->meta.names, i) {
360                 char *k;
361                 Unit *p;
362                 Timer *t;
363                 TimerValue *v;
364
365                 if (!(k = unit_name_change_suffix(n, ".timer"))) {
366                         r = -ENOMEM;
367                         goto fail;
368                 }
369
370                 p = manager_get_unit(u->meta.manager, k);
371                 free(k);
372
373                 if (!p)
374                         continue;
375
376                 t = TIMER(p);
377
378                 if (t->meta.load_state != UNIT_LOADED)
379                         continue;
380
381                 /* Reenable all timers that depend on unit state */
382                 LIST_FOREACH(value, v, t->values)
383                         if (v->base == TIMER_UNIT_ACTIVE ||
384                             v->base == TIMER_UNIT_INACTIVE)
385                                 v->disabled = false;
386
387                 switch (t->state) {
388
389                 case TIMER_WAITING:
390                 case TIMER_ELAPSED:
391
392                         /* Recalculate sleep time */
393                         timer_enter_waiting(t, false);
394                         break;
395
396                 case TIMER_RUNNING:
397
398                         if (new_state == UNIT_INACTIVE) {
399                                 log_debug("%s got notified about unit deactivation.", t->meta.id);
400                                 timer_enter_waiting(t, false);
401                         }
402
403                         break;
404
405                 case TIMER_DEAD:
406                 case TIMER_MAINTAINANCE:
407                         ;
408
409                 default:
410                         assert_not_reached("Unknown timer state");
411                 }
412         }
413
414         return;
415
416 fail:
417         log_error("Failed find timer unit: %s", strerror(-r));
418 }
419
420 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
421         [TIMER_DEAD] = "dead",
422         [TIMER_WAITING] = "waiting",
423         [TIMER_RUNNING] = "running",
424         [TIMER_ELAPSED] = "elapsed",
425         [TIMER_MAINTAINANCE] = "maintainance"
426 };
427
428 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
429
430 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
431         [TIMER_ACTIVE] = "OnActive",
432         [TIMER_BOOT] = "OnBoot",
433         [TIMER_STARTUP] = "OnStartup",
434         [TIMER_UNIT_ACTIVE] = "OnUnitActive",
435         [TIMER_UNIT_INACTIVE] = "OnUnitInactive"
436 };
437
438 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
439
440 const UnitVTable timer_vtable = {
441         .suffix = ".timer",
442
443         .init = timer_init,
444         .done = timer_done,
445         .load = timer_load,
446
447         .coldplug = timer_coldplug,
448
449         .dump = timer_dump,
450
451         .start = timer_start,
452         .stop = timer_stop,
453
454         .serialize = timer_serialize,
455         .deserialize_item = timer_deserialize_item,
456
457         .active_state = timer_active_state,
458         .sub_state_to_string = timer_sub_state_to_string,
459
460         .timer_event = timer_timer_event,
461
462         .bus_message_handler = bus_timer_message_handler
463 };