chiark / gitweb /
unit: add DefaultDependencies= setting
[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 #include "special.h"
29
30 static const UnitActiveState state_translation_table[_TIMER_STATE_MAX] = {
31         [TIMER_DEAD] = UNIT_INACTIVE,
32         [TIMER_WAITING] = UNIT_ACTIVE,
33         [TIMER_RUNNING] = UNIT_ACTIVE,
34         [TIMER_ELAPSED] = UNIT_ACTIVE,
35         [TIMER_MAINTENANCE] = UNIT_MAINTENANCE
36 };
37
38 static void timer_init(Unit *u) {
39         Timer *t = TIMER(u);
40
41         assert(u);
42         assert(u->meta.load_state == UNIT_STUB);
43
44         t->next_elapse = (usec_t) -1;
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 (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                 /* Timers shouldn't stay around on shutdown */
95                 if (t->meta.default_dependencies)
96                         if ((r = unit_add_two_dependencies_by_name(u, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
97                                 return r;
98         }
99
100         return timer_verify(t);
101 }
102
103 static void timer_dump(Unit *u, FILE *f, const char *prefix) {
104         Timer *t = TIMER(u);
105         const char *prefix2;
106         char *p2;
107         TimerValue *v;
108         char
109                 timespan1[FORMAT_TIMESPAN_MAX];
110
111         p2 = strappend(prefix, "\t");
112         prefix2 = p2 ? p2 : prefix;
113
114         fprintf(f,
115                 "%sTimer State: %s\n"
116                 "%sUnit: %s\n",
117                 prefix, timer_state_to_string(t->state),
118                 prefix, t->unit->meta.id);
119
120         LIST_FOREACH(value, v, t->values)
121                 fprintf(f,
122                         "%s%s: %s\n",
123                         prefix,
124                         timer_base_to_string(v->base),
125                         strna(format_timespan(timespan1, sizeof(timespan1), v->value)));
126
127         free(p2);
128 }
129
130 static void timer_set_state(Timer *t, TimerState state) {
131         TimerState old_state;
132         assert(t);
133
134         old_state = t->state;
135         t->state = state;
136
137         if (state != TIMER_WAITING)
138                 unit_unwatch_timer(UNIT(t), &t->timer_watch);
139
140         if (state != old_state)
141                 log_debug("%s changed %s -> %s",
142                           t->meta.id,
143                           timer_state_to_string(old_state),
144                           timer_state_to_string(state));
145
146         unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
147 }
148
149 static void timer_enter_waiting(Timer *t, bool initial);
150
151 static int timer_coldplug(Unit *u) {
152         Timer *t = TIMER(u);
153
154         assert(t);
155         assert(t->state == TIMER_DEAD);
156
157         if (t->deserialized_state != t->state) {
158
159                 if (t->deserialized_state == TIMER_WAITING ||
160                     t->deserialized_state == TIMER_RUNNING ||
161                     t->deserialized_state == TIMER_ELAPSED)
162                         timer_enter_waiting(t, false);
163                 else
164                         timer_set_state(t, t->deserialized_state);
165         }
166
167         return 0;
168 }
169
170 static void timer_enter_dead(Timer *t, bool success) {
171         assert(t);
172
173         if (!success)
174                 t->failure = true;
175
176         timer_set_state(t, t->failure ? TIMER_MAINTENANCE : TIMER_DEAD);
177 }
178
179 static void timer_enter_waiting(Timer *t, bool initial) {
180         TimerValue *v;
181         usec_t base = 0, delay, n;
182         bool found = false;
183         int r;
184
185         n = now(CLOCK_MONOTONIC);
186
187         LIST_FOREACH(value, v, t->values) {
188
189                 if (v->disabled)
190                         continue;
191
192                 switch (v->base) {
193
194                 case TIMER_ACTIVE:
195                         if (state_translation_table[t->state] == UNIT_ACTIVE) {
196                                 base = t->meta.inactive_exit_timestamp.monotonic;
197                         } else
198                                 base = n;
199                         break;
200
201                 case TIMER_BOOT:
202                         /* CLOCK_MONOTONIC equals the uptime on Linux */
203                         base = 0;
204                         break;
205
206                 case TIMER_STARTUP:
207                         base = t->meta.manager->startup_timestamp.monotonic;
208                         break;
209
210                 case TIMER_UNIT_ACTIVE:
211
212                         if (t->unit->meta.inactive_exit_timestamp.monotonic <= 0)
213                                 continue;
214
215                         base = t->unit->meta.inactive_exit_timestamp.monotonic;
216                         break;
217
218                 case TIMER_UNIT_INACTIVE:
219
220                         if (t->unit->meta.inactive_enter_timestamp.monotonic <= 0)
221                                 continue;
222
223                         base = t->unit->meta.inactive_enter_timestamp.monotonic;
224                         break;
225
226                 default:
227                         assert_not_reached("Unknown timer base");
228                 }
229
230                 v->next_elapse = base + v->value;
231
232                 if (!initial && v->next_elapse < n) {
233                         v->disabled = true;
234                         continue;
235                 }
236
237                 if (!found)
238                         t->next_elapse = v->next_elapse;
239                 else
240                         t->next_elapse = MIN(t->next_elapse, v->next_elapse);
241
242                 found = true;
243         }
244
245         if (!found) {
246                 timer_set_state(t, TIMER_ELAPSED);
247                 return;
248         }
249
250         delay = n < t->next_elapse ? t->next_elapse - n : 0;
251
252         if ((r = unit_watch_timer(UNIT(t), delay, &t->timer_watch)) < 0)
253                 goto fail;
254
255         timer_set_state(t, TIMER_WAITING);
256         return;
257
258 fail:
259         log_warning("%s failed to enter waiting state: %s", t->meta.id, strerror(-r));
260         timer_enter_dead(t, false);
261 }
262
263 static void timer_enter_running(Timer *t) {
264         int r;
265         assert(t);
266
267         if ((r = manager_add_job(t->meta.manager, JOB_START, t->unit, JOB_REPLACE, true, NULL)) < 0)
268                 goto fail;
269
270         timer_set_state(t, TIMER_RUNNING);
271         return;
272
273 fail:
274         log_warning("%s failed to queue unit startup job: %s", t->meta.id, strerror(-r));
275         timer_enter_dead(t, false);
276 }
277
278 static int timer_start(Unit *u) {
279         Timer *t = TIMER(u);
280
281         assert(t);
282         assert(t->state == TIMER_DEAD || t->state == TIMER_MAINTENANCE);
283
284         if (t->unit->meta.load_state != UNIT_LOADED)
285                 return -ENOENT;
286
287         t->failure = false;
288         timer_enter_waiting(t, true);
289         return 0;
290 }
291
292 static int timer_stop(Unit *u) {
293         Timer *t = TIMER(u);
294
295         assert(t);
296         assert(t->state == TIMER_WAITING || t->state == TIMER_RUNNING || t->state == TIMER_ELAPSED);
297
298         timer_enter_dead(t, true);
299         return 0;
300 }
301
302 static int timer_serialize(Unit *u, FILE *f, FDSet *fds) {
303         Timer *t = TIMER(u);
304
305         assert(u);
306         assert(f);
307         assert(fds);
308
309         unit_serialize_item(u, f, "state", timer_state_to_string(t->state));
310
311         return 0;
312 }
313
314 static int timer_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
315         Timer *t = TIMER(u);
316
317         assert(u);
318         assert(key);
319         assert(value);
320         assert(fds);
321
322         if (streq(key, "state")) {
323                 TimerState state;
324
325                 if ((state = timer_state_from_string(value)) < 0)
326                         log_debug("Failed to parse state value %s", value);
327                 else
328                         t->deserialized_state = state;
329         } else
330                 log_debug("Unknown serialization key '%s'", key);
331
332         return 0;
333 }
334
335 static UnitActiveState timer_active_state(Unit *u) {
336         assert(u);
337
338         return state_translation_table[TIMER(u)->state];
339 }
340
341 static const char *timer_sub_state_to_string(Unit *u) {
342         assert(u);
343
344         return timer_state_to_string(TIMER(u)->state);
345 }
346
347 static void timer_timer_event(Unit *u, uint64_t elapsed, Watch *w) {
348         Timer *t = TIMER(u);
349
350         assert(t);
351         assert(elapsed == 1);
352
353         if (t->state != TIMER_WAITING)
354                 return;
355
356         log_debug("Timer elapsed on %s", u->meta.id);
357         timer_enter_running(t);
358 }
359
360 void timer_unit_notify(Unit *u, UnitActiveState new_state) {
361         char *n;
362         int r;
363         Iterator i;
364
365         if (u->meta.type == UNIT_TIMER)
366                 return;
367
368         SET_FOREACH(n, u->meta.names, i) {
369                 char *k;
370                 Unit *p;
371                 Timer *t;
372                 TimerValue *v;
373
374                 if (!(k = unit_name_change_suffix(n, ".timer"))) {
375                         r = -ENOMEM;
376                         goto fail;
377                 }
378
379                 p = manager_get_unit(u->meta.manager, k);
380                 free(k);
381
382                 if (!p)
383                         continue;
384
385                 if (p->meta.load_state != UNIT_LOADED)
386                         continue;
387
388                 t = TIMER(p);
389
390                 if (t->unit != u)
391                         continue;
392
393                 /* Reenable all timers that depend on unit state */
394                 LIST_FOREACH(value, v, t->values)
395                         if (v->base == TIMER_UNIT_ACTIVE ||
396                             v->base == TIMER_UNIT_INACTIVE)
397                                 v->disabled = false;
398
399                 switch (t->state) {
400
401                 case TIMER_WAITING:
402                 case TIMER_ELAPSED:
403
404                         /* Recalculate sleep time */
405                         timer_enter_waiting(t, false);
406                         break;
407
408                 case TIMER_RUNNING:
409
410                         if (UNIT_IS_INACTIVE_OR_MAINTENANCE(new_state)) {
411                                 log_debug("%s got notified about unit deactivation.", t->meta.id);
412                                 timer_enter_waiting(t, false);
413                         }
414
415                         break;
416
417                 case TIMER_DEAD:
418                 case TIMER_MAINTENANCE:
419                         ;
420
421                 default:
422                         assert_not_reached("Unknown timer state");
423                 }
424         }
425
426         return;
427
428 fail:
429         log_error("Failed find timer unit: %s", strerror(-r));
430 }
431
432 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
433         [TIMER_DEAD] = "dead",
434         [TIMER_WAITING] = "waiting",
435         [TIMER_RUNNING] = "running",
436         [TIMER_ELAPSED] = "elapsed",
437         [TIMER_MAINTENANCE] = "maintenance"
438 };
439
440 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
441
442 static const char* const timer_base_table[_TIMER_BASE_MAX] = {
443         [TIMER_ACTIVE] = "OnActive",
444         [TIMER_BOOT] = "OnBoot",
445         [TIMER_STARTUP] = "OnStartup",
446         [TIMER_UNIT_ACTIVE] = "OnUnitActive",
447         [TIMER_UNIT_INACTIVE] = "OnUnitInactive"
448 };
449
450 DEFINE_STRING_TABLE_LOOKUP(timer_base, TimerBase);
451
452 const UnitVTable timer_vtable = {
453         .suffix = ".timer",
454
455         .init = timer_init,
456         .done = timer_done,
457         .load = timer_load,
458
459         .coldplug = timer_coldplug,
460
461         .dump = timer_dump,
462
463         .start = timer_start,
464         .stop = timer_stop,
465
466         .serialize = timer_serialize,
467         .deserialize_item = timer_deserialize_item,
468
469         .active_state = timer_active_state,
470         .sub_state_to_string = timer_sub_state_to_string,
471
472         .timer_event = timer_timer_event,
473
474         .bus_message_handler = bus_timer_message_handler
475 };