1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
7 #include <sys/timerfd.h>
16 #include "load-fragment.h"
17 #include "load-dropin.h"
20 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
21 [UNIT_SERVICE] = &service_vtable,
22 [UNIT_TIMER] = &timer_vtable,
23 [UNIT_SOCKET] = &socket_vtable,
24 [UNIT_TARGET] = &target_vtable,
25 [UNIT_DEVICE] = &device_vtable,
26 [UNIT_MOUNT] = &mount_vtable,
27 [UNIT_AUTOMOUNT] = &automount_vtable,
28 [UNIT_SNAPSHOT] = &snapshot_vtable
31 UnitType unit_name_to_type(const char *n) {
36 for (t = 0; t < _UNIT_TYPE_MAX; t++)
37 if (endswith(n, unit_vtable[t]->suffix))
40 return _UNIT_TYPE_INVALID;
45 "abcdefghijklmnopqrstuvwxyz" \
46 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" \
49 bool unit_name_is_valid(const char *n) {
55 if (strlen(n) >= UNIT_NAME_MAX)
58 t = unit_name_to_type(n);
59 if (t < 0 || t >= _UNIT_TYPE_MAX)
62 if (!(e = strrchr(n, '.')))
68 for (i = n; i < e; i++)
69 if (!strchr(VALID_CHARS, *i))
75 char *unit_name_change_suffix(const char *n, const char *suffix) {
80 assert(unit_name_is_valid(n));
83 assert_se(e = strrchr(n, '.'));
87 if (!(r = new(char, a + b + 1)))
91 memcpy(r+a, suffix, b+1);
96 Unit *unit_new(Manager *m) {
101 if (!(u = new0(Unit, 1)))
104 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
110 u->meta.type = _UNIT_TYPE_INVALID;
115 int unit_add_name(Unit *u, const char *text) {
123 if (!unit_name_is_valid(text))
126 if ((t = unit_name_to_type(text)) == _UNIT_TYPE_INVALID)
129 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type)
132 if (!(s = strdup(text)))
135 if ((r = set_put(u->meta.names, s)) < 0) {
144 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
145 set_remove(u->meta.names, s);
158 void unit_add_to_load_queue(Unit *u) {
161 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
164 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
165 u->meta.in_load_queue = true;
168 static void bidi_set_free(Unit *u, Set *s) {
174 /* Frees the set and makes sure we are dropped from the
175 * inverse pointers */
177 SET_FOREACH(other, s, i) {
180 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
181 set_remove(other->meta.dependencies[d], u);
187 void unit_free(Unit *u) {
194 /* Detach from next 'bigger' objects */
196 SET_FOREACH(t, u->meta.names, i)
197 hashmap_remove_value(u->meta.manager->units, t, u);
199 if (u->meta.in_load_queue)
200 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
202 if (u->meta.load_state == UNIT_LOADED)
203 if (UNIT_VTABLE(u)->done)
204 UNIT_VTABLE(u)->done(u);
206 /* Free data and next 'smaller' objects */
208 job_free(u->meta.job);
210 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
211 bidi_set_free(u, u->meta.dependencies[d]);
213 free(u->meta.description);
214 free(u->meta.load_path);
216 while ((t = set_steal_first(u->meta.names)))
218 set_free(u->meta.names);
223 UnitActiveState unit_active_state(Unit *u) {
226 if (u->meta.load_state != UNIT_LOADED)
227 return UNIT_INACTIVE;
229 return UNIT_VTABLE(u)->active_state(u);
232 static int ensure_merge(Set **s, Set *other) {
238 return set_merge(*s, other);
240 if (!(*s = set_copy(other)))
246 /* FIXME: Does not rollback on failure! */
247 int unit_merge(Unit *u, Unit *other) {
253 assert(u->meta.manager == other->meta.manager);
255 /* This merges 'other' into 'unit'. FIXME: This does not
256 * rollback on failure. */
258 if (u->meta.type != u->meta.type)
261 if (u->meta.load_state != UNIT_STUB)
265 if ((r = ensure_merge(&u->meta.names, other->meta.names)) < 0)
268 /* Merge dependencies */
269 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
270 /* fixme, the inverse mapping is missing */
271 if ((r = ensure_merge(&u->meta.dependencies[d], other->meta.dependencies[d])) < 0)
277 const char* unit_id(Unit *u) {
283 return set_first(u->meta.names);
286 const char *unit_description(Unit *u) {
289 if (u->meta.description)
290 return u->meta.description;
295 void unit_dump(Unit *u, FILE *f, const char *prefix) {
297 static const char* const load_state_table[_UNIT_LOAD_STATE_MAX] = {
298 [UNIT_STUB] = "stub",
299 [UNIT_LOADED] = "loaded",
300 [UNIT_FAILED] = "failed"
303 static const char* const active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
304 [UNIT_ACTIVE] = "active",
305 [UNIT_INACTIVE] = "inactive",
306 [UNIT_ACTIVATING] = "activating",
307 [UNIT_DEACTIVATING] = "deactivating"
310 static const char* const dependency_table[_UNIT_DEPENDENCY_MAX] = {
311 [UNIT_REQUIRES] = "Requires",
312 [UNIT_SOFT_REQUIRES] = "SoftRequires",
313 [UNIT_WANTS] = "Wants",
314 [UNIT_REQUISITE] = "Requisite",
315 [UNIT_SOFT_REQUISITE] = "SoftRequisite",
316 [UNIT_REQUIRED_BY] = "RequiredBy",
317 [UNIT_SOFT_REQUIRED_BY] = "SoftRequiredBy",
318 [UNIT_WANTED_BY] = "WantedBy",
319 [UNIT_CONFLICTS] = "Conflicts",
320 [UNIT_BEFORE] = "Before",
321 [UNIT_AFTER] = "After",
333 prefix2 = strappend(prefix, "\t");
339 "%s\tDescription: %s\n"
340 "%s\tUnit Load State: %s\n"
341 "%s\tUnit Active State: %s\n",
343 prefix, unit_description(u),
344 prefix, load_state_table[u->meta.load_state],
345 prefix, active_state_table[unit_active_state(u)]);
347 if (u->meta.load_path)
348 fprintf(f, "%s\tLoad Path: %s\n", prefix, u->meta.load_path);
350 SET_FOREACH(t, u->meta.names, i)
351 fprintf(f, "%s\tName: %s\n", prefix, t);
353 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
356 if (set_isempty(u->meta.dependencies[d]))
359 SET_FOREACH(other, u->meta.dependencies[d], i)
360 fprintf(f, "%s\t%s: %s\n", prefix, dependency_table[d], unit_id(other));
363 if (UNIT_VTABLE(u)->dump)
364 UNIT_VTABLE(u)->dump(u, f, prefix2);
367 job_dump(u->meta.job, f, prefix2);
372 /* Common implementation for multiple backends */
373 int unit_load_fragment_and_dropin(Unit *u) {
378 /* Load a .socket file */
379 if ((r = unit_load_fragment(u)) < 0)
382 /* Load drop-in directory data */
383 if ((r = unit_load_dropin(u)) < 0)
389 int unit_load(Unit *u) {
394 if (u->meta.in_load_queue) {
395 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
396 u->meta.in_load_queue = false;
399 if (u->meta.load_state != UNIT_STUB)
402 if (UNIT_VTABLE(u)->init)
403 if ((r = UNIT_VTABLE(u)->init(u)) < 0)
406 u->meta.load_state = UNIT_LOADED;
410 u->meta.load_state = UNIT_FAILED;
415 * -EBADR: This unit type does not support starting.
416 * -EALREADY: Unit is already started.
417 * -EAGAIN: An operation is already in progress. Retry later.
419 int unit_start(Unit *u) {
420 UnitActiveState state;
424 if (!UNIT_VTABLE(u)->start)
427 state = unit_active_state(u);
428 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
431 /* We don't suppress calls to ->start() here when we are
432 * already starting, to allow this request to be used as a
433 * "hurry up" call, for example when the unit is in some "auto
434 * restart" state where it waits for a holdoff timer to elapse
435 * before it will start again. */
437 return UNIT_VTABLE(u)->start(u);
440 bool unit_can_start(Unit *u) {
443 return !!UNIT_VTABLE(u)->start;
447 * -EBADR: This unit type does not support stopping.
448 * -EALREADY: Unit is already stopped.
449 * -EAGAIN: An operation is already in progress. Retry later.
451 int unit_stop(Unit *u) {
452 UnitActiveState state;
456 if (!UNIT_VTABLE(u)->stop)
459 state = unit_active_state(u);
460 if (state == UNIT_INACTIVE)
463 if (state == UNIT_DEACTIVATING)
466 return UNIT_VTABLE(u)->stop(u);
470 * -EBADR: This unit type does not support reloading.
471 * -ENOEXEC: Unit is not started.
472 * -EAGAIN: An operation is already in progress. Retry later.
474 int unit_reload(Unit *u) {
475 UnitActiveState state;
479 if (!unit_can_reload(u))
482 state = unit_active_state(u);
483 if (unit_active_state(u) == UNIT_ACTIVE_RELOADING)
486 if (unit_active_state(u) != UNIT_ACTIVE)
489 return UNIT_VTABLE(u)->reload(u);
492 bool unit_can_reload(Unit *u) {
495 if (!UNIT_VTABLE(u)->reload)
498 if (!UNIT_VTABLE(u)->can_reload)
501 return UNIT_VTABLE(u)->can_reload(u);
504 static void retroactively_start_dependencies(Unit *u) {
509 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
511 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
512 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
513 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
515 SET_FOREACH(other, u->meta.dependencies[UNIT_SOFT_REQUIRES], i)
516 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
517 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
519 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
520 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
521 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL);
523 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
524 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
525 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL);
527 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
528 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
529 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
532 static void retroactively_stop_dependencies(Unit *u) {
537 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
539 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
540 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
541 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL);
544 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
546 assert(os < _UNIT_ACTIVE_STATE_MAX);
547 assert(ns < _UNIT_ACTIVE_STATE_MAX);
548 assert(!(os == UNIT_ACTIVE && ns == UNIT_ACTIVATING));
549 assert(!(os == UNIT_INACTIVE && ns == UNIT_DEACTIVATING));
554 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
555 u->meta.active_enter_timestamp = now(CLOCK_REALTIME);
556 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
557 u->meta.active_exit_timestamp = now(CLOCK_REALTIME);
561 if (u->meta.job->state == JOB_WAITING)
563 /* So we reached a different state for this
564 * job. Let's see if we can run it now if it
565 * failed previously due to EAGAIN. */
566 job_schedule_run(u->meta.job);
569 assert(u->meta.job->state == JOB_RUNNING);
571 /* Let's check of this state change
572 * constitutes a finished job, or maybe
573 * cotradicts a running job and hence needs to
574 * invalidate jobs. */
576 switch (u->meta.job->type) {
579 case JOB_VERIFY_ACTIVE:
581 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
582 job_finish_and_invalidate(u->meta.job, true);
584 } else if (ns == UNIT_ACTIVATING)
587 job_finish_and_invalidate(u->meta.job, false);
592 case JOB_RELOAD_OR_START:
594 if (ns == UNIT_ACTIVE) {
595 job_finish_and_invalidate(u->meta.job, true);
597 } else if (ns == UNIT_ACTIVATING || ns == UNIT_ACTIVE_RELOADING)
600 job_finish_and_invalidate(u->meta.job, false);
606 case JOB_TRY_RESTART:
608 if (ns == UNIT_INACTIVE) {
609 job_finish_and_invalidate(u->meta.job, true);
611 } else if (ns == UNIT_DEACTIVATING)
614 job_finish_and_invalidate(u->meta.job, false);
619 assert_not_reached("Job type unknown");
624 /* If this state change happened without being requested by a
625 * job, then let's retroactively start or stop dependencies */
627 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
628 retroactively_start_dependencies(u);
629 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
630 retroactively_stop_dependencies(u);
633 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
634 struct epoll_event ev;
639 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->unit == u));
645 if (epoll_ctl(u->meta.manager->epoll_fd,
646 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
658 void unit_unwatch_fd(Unit *u, Watch *w) {
662 if (w->type == WATCH_INVALID)
665 assert(w->type == WATCH_FD && w->unit == u);
666 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
669 w->type = WATCH_INVALID;
673 int unit_watch_pid(Unit *u, pid_t pid) {
677 return hashmap_put(u->meta.manager->watch_pids, UINT32_TO_PTR(pid), u);
680 void unit_unwatch_pid(Unit *u, pid_t pid) {
684 hashmap_remove(u->meta.manager->watch_pids, UINT32_TO_PTR(pid));
687 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
688 struct itimerspec its;
694 assert(w->type == WATCH_INVALID || (w->type == WATCH_TIMER && w->unit == u));
696 /* This will try to reuse the old timer if there is one */
698 if (w->type == WATCH_TIMER) {
703 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
710 /* Set absolute time in the past, but not 0, since we
711 * don't want to disarm the timer */
712 its.it_value.tv_sec = 0;
713 its.it_value.tv_nsec = 1;
715 flags = TFD_TIMER_ABSTIME;
717 timespec_store(&its.it_value, delay);
721 /* This will also flush the elapse counter */
722 if (timerfd_settime(fd, flags, &its, NULL) < 0)
725 if (w->type == WATCH_INVALID) {
726 struct epoll_event ev;
732 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
737 w->type = WATCH_TIMER;
744 assert_se(close_nointr(fd) == 0);
749 void unit_unwatch_timer(Unit *u, Watch *w) {
753 if (w->type == WATCH_INVALID)
756 assert(w->type == WATCH_TIMER && w->unit == u);
758 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
759 assert_se(close_nointr(w->fd) == 0);
762 w->type = WATCH_INVALID;
766 bool unit_job_is_applicable(Unit *u, JobType j) {
768 assert(j >= 0 && j < _JOB_TYPE_MAX);
772 case JOB_VERIFY_ACTIVE:
778 case JOB_TRY_RESTART:
779 return unit_can_start(u);
782 return unit_can_reload(u);
784 case JOB_RELOAD_OR_START:
785 return unit_can_reload(u) && unit_can_start(u);
788 assert_not_reached("Invalid job type");
792 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other) {
794 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
795 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
796 [UNIT_SOFT_REQUIRES] = UNIT_SOFT_REQUIRED_BY,
797 [UNIT_WANTS] = UNIT_WANTED_BY,
798 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
799 [UNIT_SOFT_REQUISITE] = UNIT_SOFT_REQUIRED_BY,
800 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
801 [UNIT_SOFT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
802 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
803 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
804 [UNIT_BEFORE] = UNIT_AFTER,
805 [UNIT_AFTER] = UNIT_BEFORE
810 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
811 assert(inverse_table[d] != _UNIT_DEPENDENCY_INVALID);
814 /* We won't allow dependencies on ourselves. We will not
815 * consider them an error however. */
819 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
822 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
825 if ((r = set_put(u->meta.dependencies[d], other)) < 0)
828 if ((r = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
829 set_remove(u->meta.dependencies[d], other);
836 const char *unit_path(void) {
839 if ((e = getenv("UNIT_PATH")))
840 if (path_is_absolute(e))
846 int set_unit_path(const char *p) {
850 /* This is mostly for debug purposes */
852 if (path_is_absolute(p)) {
853 if (!(c = strdup(p)))
856 if (!(cwd = get_current_dir_name()))
859 r = asprintf(&c, "%s/%s", cwd, p);
866 if (setenv("UNIT_PATH", c, 0) < 0) {