1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
7 #include <sys/timerfd.h>
14 #include "load-fragment.h"
15 #include "load-dropin.h"
18 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
19 [UNIT_SERVICE] = &service_vtable,
20 [UNIT_TIMER] = &timer_vtable,
21 [UNIT_SOCKET] = &socket_vtable,
22 [UNIT_TARGET] = &target_vtable,
23 [UNIT_DEVICE] = &device_vtable,
24 [UNIT_MOUNT] = &mount_vtable,
25 [UNIT_AUTOMOUNT] = &automount_vtable,
26 [UNIT_SNAPSHOT] = &snapshot_vtable
29 UnitType unit_name_to_type(const char *n) {
34 for (t = 0; t < _UNIT_TYPE_MAX; t++)
35 if (endswith(n, unit_vtable[t]->suffix))
38 return _UNIT_TYPE_INVALID;
43 "abcdefghijklmnopqrstuvwxyz" \
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
47 bool unit_name_is_valid(const char *n) {
53 if (strlen(n) >= UNIT_NAME_MAX)
56 t = unit_name_to_type(n);
57 if (t < 0 || t >= _UNIT_TYPE_MAX)
60 if (!(e = strrchr(n, '.')))
63 for (i = n; i < e; i++)
64 if (!strchr(VALID_CHARS, *i))
70 char *unit_name_change_suffix(const char *n, const char *suffix) {
75 assert(unit_name_is_valid(n));
78 assert_se(e = strrchr(n, '.'));
82 if (!(r = new(char, a + b + 1)))
86 memcpy(r+a, suffix, b+1);
91 Unit *unit_new(Manager *m) {
96 if (!(u = new0(Unit, 1)))
99 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
105 u->meta.type = _UNIT_TYPE_INVALID;
110 int unit_add_name(Unit *u, const char *text) {
118 if (!unit_name_is_valid(text))
121 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
124 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
127 if (!(s = strdup(text)))
130 if ((r = set_put(u->meta.names, s)) < 0) {
139 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
140 set_remove(u->meta.names, s);
153 void unit_add_to_load_queue(Unit *u) {
156 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
159 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
160 u->meta.in_load_queue = true;
163 static void bidi_set_free(Unit *u, Set *s) {
169 /* Frees the set and makes sure we are dropped from the
170 * inverse pointers */
172 SET_FOREACH(other, s, i) {
175 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
176 set_remove(other->meta.dependencies[d], u);
182 void unit_free(Unit *u) {
189 /* Detach from next 'bigger' objects */
191 SET_FOREACH(t, u->meta.names, i)
192 hashmap_remove_value(u->meta.manager->units, t, u);
194 if (u->meta.in_load_queue)
195 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
197 if (u->meta.load_state == UNIT_LOADED)
198 if (UNIT_VTABLE(u)->done)
199 UNIT_VTABLE(u)->done(u);
201 /* Free data and next 'smaller' objects */
203 job_free(u->meta.job);
205 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
206 bidi_set_free(u, u->meta.dependencies[d]);
208 free(u->meta.description);
210 while ((t = set_steal_first(u->meta.names)))
212 set_free(u->meta.names);
217 UnitActiveState unit_active_state(Unit *u) {
220 if (u->meta.load_state != UNIT_LOADED)
221 return UNIT_INACTIVE;
223 return UNIT_VTABLE(u)->active_state(u);
226 static int ensure_merge(Set **s, Set *other) {
232 return set_merge(*s, other);
234 if (!(*s = set_copy(other)))
240 /* FIXME: Does not rollback on failure! */
241 int unit_merge(Unit *u, Unit *other) {
247 assert(u->meta.manager == other->meta.manager);
249 /* This merges 'other' into 'unit'. FIXME: This does not
250 * rollback on failure. */
252 if (u->meta.type != u->meta.type)
255 if (u->meta.load_state != UNIT_STUB)
259 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
262 /* Merge dependencies */
263 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
264 /* fixme, the inverse mapping is missing */
265 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
271 const char* unit_id(Unit *u) {
277 return set_first(u->meta.names);
280 const char *unit_description(Unit *u) {
283 if (u->meta.description)
284 return u->meta.description;
289 void unit_dump(Unit *u, FILE *f, const char *prefix) {
291 static const char* const load_state_table[_UNIT_LOAD_STATE_MAX] = {
292 [UNIT_STUB] = "stub",
293 [UNIT_LOADED] = "loaded",
294 [UNIT_FAILED] = "failed"
297 static const char* const active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
298 [UNIT_ACTIVE] = "active",
299 [UNIT_INACTIVE] = "inactive",
300 [UNIT_ACTIVATING] = "activating",
301 [UNIT_DEACTIVATING] = "deactivating"
304 static const char* const dependency_table[_UNIT_DEPENDENCY_MAX] = {
305 [UNIT_REQUIRES] = "Requires",
306 [UNIT_SOFT_REQUIRES] = "SoftRequires",
307 [UNIT_WANTS] = "Wants",
308 [UNIT_REQUISITE] = "Requisite",
309 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
310 [UNIT_REQUIRED_BY] = "RequiredBy",
311 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
312 [UNIT_WANTED_BY] = "WantedBy",
313 [UNIT_CONFLICTS] = "Conflicts",
314 [UNIT_BEFORE] = "Before",
315 [UNIT_AFTER] = "After",
327 prefix2 = strappend(prefix, "\t");
333 "%s\tDescription: %s\n"
334 "%s\tUnit Load State: %s\n"
335 "%s\tUnit Active State: %s\n",
337 prefix, unit_description(u),
338 prefix, load_state_table[u->meta.load_state],
339 prefix, active_state_table[unit_active_state(u)]);
341 SET_FOREACH(t, u->meta.names, i)
342 fprintf(f, "%s\tName: %s\n", prefix, t);
344 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
347 if (set_isempty(u->meta.dependencies[d]))
350 SET_FOREACH(other, u->meta.dependencies[d], i)
351 fprintf(f, "%s\t%s: %s\n", prefix, dependency_table[d], unit_id(other));
354 if (UNIT_VTABLE(u)->dump)
355 UNIT_VTABLE(u)->dump(u, f, prefix2);
358 job_dump(u->meta.job, f, prefix2);
363 /* Common implementation for multiple backends */
364 int unit_load_fragment_and_dropin(Unit *u) {
369 /* Load a .socket file */
370 if ((r = unit_load_fragment(u)) < 0)
373 /* Load drop-in directory data */
374 if ((r = unit_load_dropin(u)) < 0)
380 int unit_load(Unit *u) {
385 if (u->meta.in_load_queue) {
386 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
387 u->meta.in_load_queue = false;
390 if (u->meta.load_state != UNIT_STUB)
393 if (UNIT_VTABLE(u)->init)
394 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
397 u->meta.load_state = UNIT_LOADED;
401 u->meta.load_state = UNIT_FAILED;
406 * -EBADR: This unit type does not support starting.
407 * -EALREADY: Unit is already started.
408 * -EAGAIN: An operation is already in progress. Retry later.
410 int unit_start(Unit *u) {
411 UnitActiveState state;
415 if (!UNIT_VTABLE(u)->start)
418 state = unit_active_state(u);
419 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
422 /* We don't suppress calls to ->start() here when we are
423 * already starting, to allow this request to be used as a
424 * "hurry up" call, for example when the unit is in some "auto
425 * restart" state where it waits for a holdoff timer to elapse
426 * before it will start again. */
428 return UNIT_VTABLE(u)->start(u);
431 bool unit_can_start(Unit *u) {
434 return !!UNIT_VTABLE(u)->start;
438 * -EBADR: This unit type does not support stopping.
439 * -EALREADY: Unit is already stopped.
440 * -EAGAIN: An operation is already in progress. Retry later.
442 int unit_stop(Unit *u) {
443 UnitActiveState state;
447 if (!UNIT_VTABLE(u)->stop)
450 state = unit_active_state(u);
451 if (state == UNIT_INACTIVE)
454 if (state == UNIT_DEACTIVATING)
457 return UNIT_VTABLE(u)->stop(u);
461 * -EBADR: This unit type does not support reloading.
462 * -ENOEXEC: Unit is not started.
463 * -EAGAIN: An operation is already in progress. Retry later.
465 int unit_reload(Unit *u) {
466 UnitActiveState state;
470 if (!unit_can_reload(u))
473 state = unit_active_state(u);
474 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
477 if (unit_active_state(u) != UNIT_ACTIVE)
480 return UNIT_VTABLE(u)->reload(u);
483 bool unit_can_reload(Unit *u) {
486 if (!UNIT_VTABLE(u)->reload)
489 if (!UNIT_VTABLE(u)->can_reload)
492 return UNIT_VTABLE(u)->can_reload(u);
495 static void retroactively_start_dependencies(Unit *u) {
500 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
502 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
503 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
504 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
506 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
507 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
508 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
510 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
511 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
512 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
514 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
515 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
516 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
518 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
519 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
520 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
523 static void retroactively_stop_dependencies(Unit *u) {
528 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
530 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
531 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
532 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
535 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
537 assert(os < _UNIT_ACTIVE_STATE_MAX);
538 assert(ns < _UNIT_ACTIVE_STATE_MAX);
539 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
540 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
545 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
546 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
547 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
548 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
552 if (u->meta.job->state == JOB_WAITING)
554 /* So we reached a different state for this
555 * job. Let's see if we can run it now if it
556 * failed previously due to EAGAIN. */
557 job_schedule_run(u->meta.job);
560 assert(u->meta.job->state == JOB_RUNNING);
562 /* Let's check of this state change
563 * constitutes a finished job, or maybe
564 * cotradicts a running job and hence needs to
565 * invalidate jobs. */
567 switch (u->meta.job->type) {
570 case JOB_VERIFY_ACTIVE:
572 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
573 job_finish_and_invalidate(u->meta.job, true);
575 } else if (ns == UNIT_ACTIVATING)
578 job_finish_and_invalidate(u->meta.job, false);
583 case JOB_RELOAD_OR_START:
585 if (ns == UNIT_ACTIVE) {
586 job_finish_and_invalidate(u->meta.job, true);
588 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
591 job_finish_and_invalidate(u->meta.job, false);
597 case JOB_TRY_RESTART:
599 if (ns == UNIT_INACTIVE) {
600 job_finish_and_invalidate(u->meta.job, true);
602 } else if (ns == UNIT_DEACTIVATING)
605 job_finish_and_invalidate(u->meta.job, false);
610 assert_not_reached("Job type unknown");
615 /* If this state change happened without being requested by a
616 * job, then let's retroactively start or stop dependencies */
618 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
619 retroactively_start_dependencies(u);
620 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
621 retroactively_stop_dependencies(u);
624 int unit_watch_fd(Unit *u, int fd, uint32_t events) {
625 struct epoll_event ev;
633 ev.data.u32 = MANAGER_FD;
636 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) >= 0)
640 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_MOD, fd, &ev) >= 0)
646 void unit_unwatch_fd(Unit *u, int fd) {
650 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, fd, NULL) >= 0 || errno == ENOENT);
653 int unit_watch_pid(Unit *u, pid_t pid) {
657 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
660 void unit_unwatch_pid(Unit *u, pid_t pid) {
664 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
667 int unit_watch_timer(Unit *u, usec_t delay, int *id) {
668 struct epoll_event ev;
670 struct itimerspec its;
677 /* This will try to reuse the old timer if there is one */
686 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
693 /* Set absolute time in the past, but not 0, since we
694 * don't want to disarm the timer */
695 its.it_value.tv_sec = 0;
696 its.it_value.tv_nsec = 1;
698 flags = TFD_TIMER_ABSTIME;
700 timespec_store(&its.it_value, delay);
704 /* This will also flush the elapse counter */
705 if (timerfd_settime(fd, flags, &its, NULL) < 0)
711 ev.data.u32 = MANAGER_TIMER;
714 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
722 assert_se(close_nointr(fd) == 0);
727 void unit_unwatch_timer(Unit *u, int *id) {
734 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, *id, NULL) >= 0);
735 assert_se(close_nointr(*id) == 0);
739 bool unit_job_is_applicable(Unit *u, JobType j) {
741 assert(j >= 0 && j < _JOB_TYPE_MAX);
745 case JOB_VERIFY_ACTIVE:
751 case JOB_TRY_RESTART:
752 return unit_can_start(u);
755 return unit_can_reload(u);
757 case JOB_RELOAD_OR_START:
758 return unit_can_reload(u) && unit_can_start(u);
761 assert_not_reached("Invalid job type");
765 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
767 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
768 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
769 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
770 [UNIT_WANTS] = UNIT_WANTED_BY,
771 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
772 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
773 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
774 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
775 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
776 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
777 [UNIT_BEFORE] = UNIT_AFTER,
778 [UNIT_AFTER] = UNIT_BEFORE
783 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
784 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
787 /* We won't allow dependencies on ourselves. We will not
788 * consider them an error however. */
792 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
795 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
798 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
801 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
802 set_remove(u->meta.dependencies[d], other);