]> git.ipfire.org Git - people/ms/systemd.git/blob - unit.c
manager: properly read timerfd elapse counter
[people/ms/systemd.git] / unit.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 <assert.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
27 #include <sys/poll.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30
31 #include "set.h"
32 #include "unit.h"
33 #include "macro.h"
34 #include "strv.h"
35 #include "load-fragment.h"
36 #include "load-dropin.h"
37 #include "log.h"
38
39 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
40 [UNIT_SERVICE] = &service_vtable,
41 [UNIT_TIMER] = &timer_vtable,
42 [UNIT_SOCKET] = &socket_vtable,
43 [UNIT_TARGET] = &target_vtable,
44 [UNIT_DEVICE] = &device_vtable,
45 [UNIT_MOUNT] = &mount_vtable,
46 [UNIT_AUTOMOUNT] = &automount_vtable,
47 [UNIT_SNAPSHOT] = &snapshot_vtable
48 };
49
50 UnitType unit_name_to_type(const char *n) {
51 UnitType t;
52
53 assert(n);
54
55 for (t = 0; t < _UNIT_TYPE_MAX; t++)
56 if (endswith(n, unit_vtable[t]->suffix))
57 return t;
58
59 return _UNIT_TYPE_INVALID;
60 }
61
62 #define VALID_CHARS \
63 "0123456789" \
64 "abcdefghijklmnopqrstuvwxyz" \
65 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
66 "-_.\\"
67
68 bool unit_name_is_valid(const char *n) {
69 UnitType t;
70 const char *e, *i;
71
72 assert(n);
73
74 if (strlen(n) >= UNIT_NAME_MAX)
75 return false;
76
77 t = unit_name_to_type(n);
78 if (t < 0 || t >= _UNIT_TYPE_MAX)
79 return false;
80
81 if (!(e = strrchr(n, '.')))
82 return false;
83
84 if (e == n)
85 return false;
86
87 for (i = n; i < e; i++)
88 if (!strchr(VALID_CHARS, *i))
89 return false;
90
91 return true;
92 }
93
94 char *unit_name_change_suffix(const char *n, const char *suffix) {
95 char *e, *r;
96 size_t a, b;
97
98 assert(n);
99 assert(unit_name_is_valid(n));
100 assert(suffix);
101
102 assert_se(e = strrchr(n, '.'));
103 a = e - n;
104 b = strlen(suffix);
105
106 if (!(r = new(char, a + b + 1)))
107 return NULL;
108
109 memcpy(r, n, a);
110 memcpy(r+a, suffix, b+1);
111
112 return r;
113 }
114
115 Unit *unit_new(Manager *m) {
116 Unit *u;
117
118 assert(m);
119
120 if (!(u = new0(Unit, 1)))
121 return NULL;
122
123 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
124 free(u);
125 return NULL;
126 }
127
128 u->meta.manager = m;
129 u->meta.type = _UNIT_TYPE_INVALID;
130
131 return u;
132 }
133
134 int unit_add_name(Unit *u, const char *text) {
135 UnitType t;
136 char *s;
137 int r;
138
139 assert(u);
140 assert(text);
141
142 if (!unit_name_is_valid(text))
143 return -EINVAL;
144
145 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
146 return -EINVAL;
147
148 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
149 return -EINVAL;
150
151 if (!(s = strdup(text)))
152 return -ENOMEM;
153
154 if ((r = set_put(u->meta.names, s)) < 0) {
155 free(s);
156
157 if (r == -EEXIST)
158 return 0;
159
160 return r;
161 }
162
163 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
164 set_remove(u->meta.names, s);
165 free(s);
166 return r;
167 }
168
169 if (u->meta.type == _UNIT_TYPE_INVALID)
170 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
171
172 u->meta.type = t;
173
174 if (!u->meta.id)
175 u->meta.id = s;
176
177 unit_add_to_dbus_queue(u);
178 return 0;
179 }
180
181 int unit_choose_id(Unit *u, const char *name) {
182 char *s;
183
184 assert(u);
185 assert(name);
186
187 /* Selects one of the names of this unit as the id */
188
189 if (!(s = set_get(u->meta.names, (char*) name)))
190 return -ENOENT;
191
192 u->meta.id = s;
193
194 unit_add_to_dbus_queue(u);
195 return 0;
196 }
197
198 int unit_set_description(Unit *u, const char *description) {
199 char *s;
200
201 assert(u);
202
203 if (!(s = strdup(description)))
204 return -ENOMEM;
205
206 free(u->meta.description);
207 u->meta.description = s;
208
209 unit_add_to_dbus_queue(u);
210 return 0;
211 }
212
213 void unit_add_to_load_queue(Unit *u) {
214 assert(u);
215
216 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
217 return;
218
219 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
220 u->meta.in_load_queue = true;
221 }
222
223 void unit_add_to_dbus_queue(Unit *u) {
224 assert(u);
225
226 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue || set_isempty(u->meta.manager->subscribed))
227 return;
228
229 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
230 u->meta.in_dbus_queue = true;
231 }
232
233 static void bidi_set_free(Unit *u, Set *s) {
234 Iterator i;
235 Unit *other;
236
237 assert(u);
238
239 /* Frees the set and makes sure we are dropped from the
240 * inverse pointers */
241
242 SET_FOREACH(other, s, i) {
243 UnitDependency d;
244
245 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
246 set_remove(other->meta.dependencies[d], u);
247 }
248
249 set_free(s);
250 }
251
252 void unit_free(Unit *u) {
253 UnitDependency d;
254 Iterator i;
255 char *t;
256
257 assert(u);
258
259 bus_unit_send_removed_signal(u);
260
261 /* Detach from next 'bigger' objects */
262
263 SET_FOREACH(t, u->meta.names, i)
264 hashmap_remove_value(u->meta.manager->units, t, u);
265
266 if (u->meta.type != _UNIT_TYPE_INVALID)
267 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
268
269 if (u->meta.in_load_queue)
270 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
271
272 if (u->meta.in_dbus_queue)
273 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
274
275 if (u->meta.load_state == UNIT_LOADED)
276 if (UNIT_VTABLE(u)->done)
277 UNIT_VTABLE(u)->done(u);
278
279 /* Free data and next 'smaller' objects */
280 if (u->meta.job)
281 job_free(u->meta.job);
282
283 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
284 bidi_set_free(u, u->meta.dependencies[d]);
285
286 free(u->meta.description);
287 free(u->meta.load_path);
288
289 while ((t = set_steal_first(u->meta.names)))
290 free(t);
291 set_free(u->meta.names);
292
293 free(u);
294 }
295
296 UnitActiveState unit_active_state(Unit *u) {
297 assert(u);
298
299 if (u->meta.load_state != UNIT_LOADED)
300 return UNIT_INACTIVE;
301
302 return UNIT_VTABLE(u)->active_state(u);
303 }
304
305 static int ensure_merge(Set **s, Set *other) {
306
307 if (!other)
308 return 0;
309
310 if (*s)
311 return set_merge(*s, other);
312
313 if (!(*s = set_copy(other)))
314 return -ENOMEM;
315
316 return 0;
317 }
318
319 /* FIXME: Does not rollback on failure! Needs to fix special unit
320 * pointers. Needs to merge names and dependencies properly.*/
321 int unit_merge(Unit *u, Unit *other) {
322 int r;
323 UnitDependency d;
324
325 assert(u);
326 assert(other);
327 assert(u->meta.manager == other->meta.manager);
328
329 /* This merges 'other' into 'unit'. FIXME: This does not
330 * rollback on failure. */
331
332 if (u->meta.type != u->meta.type)
333 return -EINVAL;
334
335 if (u->meta.load_state != UNIT_STUB)
336 return -EINVAL;
337
338 /* Merge names */
339 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
340 return r;
341
342 /* Merge dependencies */
343 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
344 /* fixme, the inverse mapping is missing */
345 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
346 return r;
347
348 unit_add_to_dbus_queue(u);
349
350 return 0;
351 }
352
353 const char* unit_id(Unit *u) {
354 assert(u);
355
356 if (u->meta.id)
357 return u->meta.id;
358
359 return set_first(u->meta.names);
360 }
361
362 const char *unit_description(Unit *u) {
363 assert(u);
364
365 if (u->meta.description)
366 return u->meta.description;
367
368 return unit_id(u);
369 }
370
371 void unit_dump(Unit *u, FILE *f, const char *prefix) {
372
373 char *t;
374 UnitDependency d;
375 Iterator i;
376 char *p2;
377 const char *prefix2;
378
379 assert(u);
380
381 if (!prefix)
382 prefix = "";
383 p2 = strappend(prefix, "\t");
384 prefix2 = p2 ? p2 : prefix;
385
386 fprintf(f,
387 "%s→ Unit %s:\n"
388 "%s\tDescription: %s\n"
389 "%s\tUnit Load State: %s\n"
390 "%s\tUnit Active State: %s\n"
391 "%s\tRecursive Stop: %s\n"
392 "%s\tStop When Unneeded: %s\n",
393 prefix, unit_id(u),
394 prefix, unit_description(u),
395 prefix, unit_load_state_to_string(u->meta.load_state),
396 prefix, unit_active_state_to_string(unit_active_state(u)),
397 prefix, yes_no(u->meta.recursive_stop),
398 prefix, yes_no(u->meta.stop_when_unneeded));
399
400 if (u->meta.load_path)
401 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
402
403 SET_FOREACH(t, u->meta.names, i)
404 fprintf(f, "%s\tName: %s\n", prefix, t);
405
406 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
407 Unit *other;
408
409 if (set_isempty(u->meta.dependencies[d]))
410 continue;
411
412 SET_FOREACH(other, u->meta.dependencies[d], i)
413 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), unit_id(other));
414 }
415
416 if (UNIT_VTABLE(u)->dump)
417 UNIT_VTABLE(u)->dump(u, f, prefix2);
418
419 if (u->meta.job)
420 job_dump(u->meta.job, f, prefix2);
421
422 free(p2);
423 }
424
425 /* Common implementation for multiple backends */
426 int unit_load_fragment_and_dropin(Unit *u) {
427 int r, ret;
428
429 assert(u);
430
431 /* Load a .socket file */
432 if ((r = unit_load_fragment(u)) < 0)
433 return r;
434
435 ret = r > 0;
436
437 /* Load drop-in directory data */
438 if ((r = unit_load_dropin(u)) < 0)
439 return r;
440
441 return ret;
442 }
443
444 int unit_load(Unit *u) {
445 int r;
446
447 assert(u);
448
449 if (u->meta.in_load_queue) {
450 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
451 u->meta.in_load_queue = false;
452 }
453
454 if (u->meta.load_state != UNIT_STUB)
455 return 0;
456
457 if (UNIT_VTABLE(u)->init)
458 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
459 goto fail;
460
461 u->meta.load_state = UNIT_LOADED;
462 unit_add_to_dbus_queue(u);
463 return 0;
464
465 fail:
466 u->meta.load_state = UNIT_FAILED;
467 unit_add_to_dbus_queue(u);
468 return r;
469 }
470
471 /* Errors:
472 * -EBADR: This unit type does not support starting.
473 * -EALREADY: Unit is already started.
474 * -EAGAIN: An operation is already in progress. Retry later.
475 */
476 int unit_start(Unit *u) {
477 UnitActiveState state;
478
479 assert(u);
480
481 if (!UNIT_VTABLE(u)->start)
482 return -EBADR;
483
484 state = unit_active_state(u);
485 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
486 return -EALREADY;
487
488 /* We don't suppress calls to ->start() here when we are
489 * already starting, to allow this request to be used as a
490 * "hurry up" call, for example when the unit is in some "auto
491 * restart" state where it waits for a holdoff timer to elapse
492 * before it will start again. */
493
494 unit_add_to_dbus_queue(u);
495 return UNIT_VTABLE(u)->start(u);
496 }
497
498 bool unit_can_start(Unit *u) {
499 assert(u);
500
501 return !!UNIT_VTABLE(u)->start;
502 }
503
504 /* Errors:
505 * -EBADR: This unit type does not support stopping.
506 * -EALREADY: Unit is already stopped.
507 * -EAGAIN: An operation is already in progress. Retry later.
508 */
509 int unit_stop(Unit *u) {
510 UnitActiveState state;
511
512 assert(u);
513
514 if (!UNIT_VTABLE(u)->stop)
515 return -EBADR;
516
517 state = unit_active_state(u);
518 if (state == UNIT_INACTIVE)
519 return -EALREADY;
520
521 if (state == UNIT_DEACTIVATING)
522 return 0;
523
524 unit_add_to_dbus_queue(u);
525 return UNIT_VTABLE(u)->stop(u);
526 }
527
528 /* Errors:
529 * -EBADR: This unit type does not support reloading.
530 * -ENOEXEC: Unit is not started.
531 * -EAGAIN: An operation is already in progress. Retry later.
532 */
533 int unit_reload(Unit *u) {
534 UnitActiveState state;
535
536 assert(u);
537
538 if (!unit_can_reload(u))
539 return -EBADR;
540
541 state = unit_active_state(u);
542 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
543 return -EALREADY;
544
545 if (unit_active_state(u) != UNIT_ACTIVE)
546 return -ENOEXEC;
547
548 unit_add_to_dbus_queue(u);
549 return UNIT_VTABLE(u)->reload(u);
550 }
551
552 bool unit_can_reload(Unit *u) {
553 assert(u);
554
555 if (!UNIT_VTABLE(u)->reload)
556 return false;
557
558 if (!UNIT_VTABLE(u)->can_reload)
559 return true;
560
561 return UNIT_VTABLE(u)->can_reload(u);
562 }
563
564 static void unit_check_uneeded(Unit *u) {
565 Iterator i;
566 Unit *other;
567
568 assert(u);
569
570 /* If this service shall be shut down when unneeded then do
571 * so. */
572
573 if (!u->meta.stop_when_unneeded)
574 return;
575
576 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
577 return;
578
579 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
580 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
581 return;
582
583 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRED_BY], i)
584 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
585 return;
586
587 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
588 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
589 return;
590
591 log_debug("Service %s is not needed anymore. Stopping.", unit_id(u));
592
593 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
594 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL);
595 }
596
597 static void retroactively_start_dependencies(Unit *u) {
598 Iterator i;
599 Unit *other;
600
601 assert(u);
602 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
603
604 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
605 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
606 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
607
608 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
609 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
610 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
611
612 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
613 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
614 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
615
616 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
617 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
618 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
619
620 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
621 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
622 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
623 }
624
625 static void retroactively_stop_dependencies(Unit *u) {
626 Iterator i;
627 Unit *other;
628
629 assert(u);
630 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
631
632 if (u->meta.recursive_stop) {
633 /* Pull down units need us recursively if enabled */
634 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
635 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
636 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
637 }
638
639 /* Garbage collect services that might not be needed anymore, if enabled */
640 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
641 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
642 unit_check_uneeded(other);
643 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
644 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
645 unit_check_uneeded(other);
646 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
647 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
648 unit_check_uneeded(other);
649 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
650 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
651 unit_check_uneeded(other);
652 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUISITE], i)
653 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
654 unit_check_uneeded(other);
655 }
656
657 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
658 assert(u);
659 assert(os < _UNIT_ACTIVE_STATE_MAX);
660 assert(ns < _UNIT_ACTIVE_STATE_MAX);
661 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
662 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
663
664 if (os == ns)
665 return;
666
667 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
668 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
669 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
670 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
671
672 if (u->meta.job) {
673
674 if (u->meta.job->state == JOB_WAITING)
675
676 /* So we reached a different state for this
677 * job. Let's see if we can run it now if it
678 * failed previously due to EAGAIN. */
679 job_add_to_run_queue(u->meta.job);
680
681 else {
682 assert(u->meta.job->state == JOB_RUNNING);
683
684 /* Let's check whether this state change
685 * constitutes a finished job, or maybe
686 * cotradicts a running job and hence needs to
687 * invalidate jobs. */
688
689 switch (u->meta.job->type) {
690
691 case JOB_START:
692 case JOB_VERIFY_ACTIVE:
693
694 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
695 job_finish_and_invalidate(u->meta.job, true);
696 return;
697 } else if (ns == UNIT_ACTIVATING)
698 return;
699 else
700 job_finish_and_invalidate(u->meta.job, false);
701
702 break;
703
704 case JOB_RELOAD:
705 case JOB_RELOAD_OR_START:
706
707 if (ns == UNIT_ACTIVE) {
708 job_finish_and_invalidate(u->meta.job, true);
709 return;
710 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
711 return;
712 else
713 job_finish_and_invalidate(u->meta.job, false);
714
715 break;
716
717 case JOB_STOP:
718 case JOB_RESTART:
719 case JOB_TRY_RESTART:
720
721 if (ns == UNIT_INACTIVE) {
722 job_finish_and_invalidate(u->meta.job, true);
723 return;
724 } else if (ns == UNIT_DEACTIVATING)
725 return;
726 else
727 job_finish_and_invalidate(u->meta.job, false);
728
729 break;
730
731 default:
732 assert_not_reached("Job type unknown");
733 }
734 }
735 }
736
737 /* If this state change happened without being requested by a
738 * job, then let's retroactively start or stop dependencies */
739
740 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
741 retroactively_start_dependencies(u);
742 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
743 retroactively_stop_dependencies(u);
744
745 /* Maybe we finished startup and are now ready for being
746 * stopped because unneeded? */
747 unit_check_uneeded(u);
748
749 unit_add_to_dbus_queue(u);
750 }
751
752 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
753 struct epoll_event ev;
754
755 assert(u);
756 assert(fd >= 0);
757 assert(w);
758 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
759
760 zero(ev);
761 ev.data.ptr = w;
762 ev.events = events;
763
764 if (epoll_ctl(u->meta.manager->epoll_fd,
765 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
766 fd,
767 &ev) < 0)
768 return -errno;
769
770 w->fd = fd;
771 w->type = WATCH_FD;
772 w->data.unit = u;
773
774 return 0;
775 }
776
777 void unit_unwatch_fd(Unit *u, Watch *w) {
778 assert(u);
779 assert(w);
780
781 if (w->type == WATCH_INVALID)
782 return;
783
784 assert(w->type == WATCH_FD);
785 assert(w->data.unit == u);
786 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
787
788 w->fd = -1;
789 w->type = WATCH_INVALID;
790 w->data.unit = NULL;
791 }
792
793 int unit_watch_pid(Unit *u, pid_t pid) {
794 assert(u);
795 assert(pid >= 1);
796
797 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
798 }
799
800 void unit_unwatch_pid(Unit *u, pid_t pid) {
801 assert(u);
802 assert(pid >= 1);
803
804 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
805 }
806
807 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
808 struct itimerspec its;
809 int flags, fd;
810 bool ours;
811
812 assert(u);
813 assert(w);
814 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->data.unit == u));
815
816 /* This will try to reuse the old timer if there is one */
817
818 if (w->type == WATCH_TIMER) {
819 ours = false;
820 fd = w->fd;
821 } else {
822 ours = true;
823 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
824 return -errno;
825 }
826
827 zero(its);
828
829 if (delay <= 0) {
830 /* Set absolute time in the past, but not 0, since we
831 * don't want to disarm the timer */
832 its.it_value.tv_sec = 0;
833 its.it_value.tv_nsec = 1;
834
835 flags = TFD_TIMER_ABSTIME;
836 } else {
837 timespec_store(&its.it_value, delay);
838 flags = 0;
839 }
840
841 /* This will also flush the elapse counter */
842 if (timerfd_settime(fd, flags, &its, NULL) < 0)
843 goto fail;
844
845 if (w->type == WATCH_INVALID) {
846 struct epoll_event ev;
847
848 zero(ev);
849 ev.data.ptr = w;
850 ev.events = EPOLLIN;
851
852 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
853 goto fail;
854 }
855
856 w->fd = fd;
857 w->type = WATCH_TIMER;
858 w->data.unit = u;
859
860 return 0;
861
862 fail:
863 if (ours)
864 close_nointr_nofail(fd);
865
866 return -errno;
867 }
868
869 void unit_unwatch_timer(Unit *u, Watch *w) {
870 assert(u);
871 assert(w);
872
873 if (w->type == WATCH_INVALID)
874 return;
875
876 assert(w->type == WATCH_TIMER && w->data.unit == u);
877
878 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
879 assert_se(close_nointr(w->fd) == 0);
880
881 w->fd = -1;
882 w->type = WATCH_INVALID;
883 w->data.unit = NULL;
884 }
885
886 bool unit_job_is_applicable(Unit *u, JobType j) {
887 assert(u);
888 assert(j >= 0 && j < _JOB_TYPE_MAX);
889
890 switch (j) {
891
892 case JOB_VERIFY_ACTIVE:
893 case JOB_START:
894 return true;
895
896 case JOB_STOP:
897 case JOB_RESTART:
898 case JOB_TRY_RESTART:
899 return unit_can_start(u);
900
901 case JOB_RELOAD:
902 return unit_can_reload(u);
903
904 case JOB_RELOAD_OR_START:
905 return unit_can_reload(u) && unit_can_start(u);
906
907 default:
908 assert_not_reached("Invalid job type");
909 }
910 }
911
912 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
913
914 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
915 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
916 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
917 [UNIT_WANTS] = UNIT_WANTED_BY,
918 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
919 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
920 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
921 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
922 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
923 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
924 [UNIT_BEFORE] = UNIT_AFTER,
925 [UNIT_AFTER] = UNIT_BEFORE
926 };
927 int r;
928
929 assert(u);
930 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
931 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
932 assert(other);
933
934 /* We won't allow dependencies on ourselves. We will not
935 * consider them an error however. */
936 if (u == other)
937 return 0;
938
939 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
940 return r;
941
942 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
943 return r;
944
945 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
946 return r;
947
948 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
949 set_remove(u->meta.dependencies[d], other);
950 return r;
951 }
952
953 unit_add_to_dbus_queue(u);
954 return 0;
955 }
956
957 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name) {
958 Unit *other;
959 int r;
960
961 if ((r = manager_load_unit(u->meta.manager, name, &other)) < 0)
962 return r;
963
964 if ((r = unit_add_dependency(u, d, other)) < 0)
965 return r;
966
967 return 0;
968 }
969
970 const char *unit_path(void) {
971 char *e;
972
973 if ((e = getenv("UNIT_PATH")))
974 if (path_is_absolute(e))
975 return e;
976
977 return UNIT_PATH;
978 }
979
980 int set_unit_path(const char *p) {
981 char *cwd, *c;
982 int r;
983
984 /* This is mostly for debug purposes */
985
986 if (path_is_absolute(p)) {
987 if (!(c = strdup(p)))
988 return -ENOMEM;
989 } else {
990 if (!(cwd = get_current_dir_name()))
991 return -errno;
992
993 r = asprintf(&c, "%s/%s", cwd, p);
994 free(cwd);
995
996 if (r < 0)
997 return -ENOMEM;
998 }
999
1000 if (setenv("UNIT_PATH", c, 0) < 0) {
1001 r = -errno;
1002 free(c);
1003 return r;
1004 }
1005
1006 return 0;
1007 }
1008
1009 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix) {
1010 char *r, *t;
1011 const char *f;
1012 size_t a, b, c;
1013
1014 assert(path);
1015
1016 /* Takes a path and a suffix and prefix and makes a nice
1017 * string suitable as unit name of it, escaping all weird
1018 * chars on the way.
1019 *
1020 * / becomes ., and all chars not alloweed in a unit name get
1021 * escaped as \xFF, including \ and ., of course. This
1022 * escaping is hence reversible.
1023 */
1024
1025 if (!prefix)
1026 prefix = "";
1027
1028 if (!suffix)
1029 suffix = "";
1030
1031 a = strlen(prefix);
1032 b = strlen(path);
1033 c = strlen(suffix);
1034
1035 if (!(r = new(char, a+b*4+c+1)))
1036 return NULL;
1037
1038 memcpy(r, prefix, a);
1039
1040 for (f = path, t = r+a; *f; f++) {
1041 if (*f == '/')
1042 *(t++) = '.';
1043 else if (*f == '.' || *f == '\\' || !strchr(VALID_CHARS, *f)) {
1044 *(t++) = '\\';
1045 *(t++) = 'x';
1046 *(t++) = hexchar(*f > 4);
1047 *(t++) = hexchar(*f);
1048 } else
1049 *(t++) = *f;
1050 }
1051
1052 memcpy(t, suffix, c+1);
1053
1054 return r;
1055 }
1056
1057 char *unit_dbus_path(Unit *u) {
1058 char *p, *e;
1059
1060 assert(u);
1061
1062 if (!(e = bus_path_escape(unit_id(u))))
1063 return NULL;
1064
1065 if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
1066 free(e);
1067 return NULL;
1068 }
1069
1070 free(e);
1071 return p;
1072 }
1073
1074 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
1075 [UNIT_SERVICE] = "service",
1076 [UNIT_TIMER] = "timer",
1077 [UNIT_SOCKET] = "socket",
1078 [UNIT_TARGET] = "target",
1079 [UNIT_DEVICE] = "device",
1080 [UNIT_MOUNT] = "mount",
1081 [UNIT_AUTOMOUNT] = "automount",
1082 [UNIT_SNAPSHOT] = "snapshot"
1083 };
1084
1085 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
1086
1087 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
1088 [UNIT_STUB] = "stub",
1089 [UNIT_LOADED] = "loaded",
1090 [UNIT_FAILED] = "failed"
1091 };
1092
1093 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
1094
1095 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
1096 [UNIT_ACTIVE] = "active",
1097 [UNIT_INACTIVE] = "inactive",
1098 [UNIT_ACTIVATING] = "activating",
1099 [UNIT_DEACTIVATING] = "deactivating"
1100 };
1101
1102 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
1103
1104 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1105 [UNIT_REQUIRES] = "Requires",
1106 [UNIT_SOFT_REQUIRES] = "SoftRequires",
1107 [UNIT_WANTS] = "Wants",
1108 [UNIT_REQUISITE] = "Requisite",
1109 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
1110 [UNIT_REQUIRED_BY] = "RequiredBy",
1111 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
1112 [UNIT_WANTED_BY] = "WantedBy",
1113 [UNIT_CONFLICTS] = "Conflicts",
1114 [UNIT_BEFORE] = "Before",
1115 [UNIT_AFTER] = "After",
1116 };
1117
1118 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);