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