1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
25 #include <sys/epoll.h>
26 #include <sys/timerfd.h>
36 #include "load-fragment.h"
37 #include "load-dropin.h"
39 #include "unit-name.h"
40 #include "specifier.h"
41 #include "dbus-unit.h"
43 #include "cgroup-util.h"
45 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
46 [UNIT_SERVICE] = &service_vtable,
47 [UNIT_TIMER] = &timer_vtable,
48 [UNIT_SOCKET] = &socket_vtable,
49 [UNIT_TARGET] = &target_vtable,
50 [UNIT_DEVICE] = &device_vtable,
51 [UNIT_MOUNT] = &mount_vtable,
52 [UNIT_AUTOMOUNT] = &automount_vtable,
53 [UNIT_SNAPSHOT] = &snapshot_vtable,
54 [UNIT_SWAP] = &swap_vtable,
55 [UNIT_PATH] = &path_vtable
58 Unit *unit_new(Manager *m) {
63 if (!(u = new0(Unit, 1)))
66 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
72 u->meta.type = _UNIT_TYPE_INVALID;
73 u->meta.deserialized_job = _JOB_TYPE_INVALID;
74 u->meta.default_dependencies = true;
79 bool unit_has_name(Unit *u, const char *name) {
83 return !!set_get(u->meta.names, (char*) name);
86 int unit_add_name(Unit *u, const char *text) {
94 if (unit_name_is_template(text)) {
95 if (!u->meta.instance)
98 s = unit_name_replace_instance(text, u->meta.instance);
105 if (!unit_name_is_valid(s)) {
110 assert_se((t = unit_name_to_type(s)) >= 0);
112 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
117 if ((r = unit_name_to_instance(s, &i)) < 0)
120 if (i && unit_vtable[t]->no_instances)
123 /* Ensure that this unit is either instanced or not instanced,
125 if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
130 if (unit_vtable[t]->no_alias &&
131 !set_isempty(u->meta.names) &&
132 !set_get(u->meta.names, s)) {
137 if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
142 if ((r = set_put(u->meta.names, s)) < 0) {
148 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
149 set_remove(u->meta.names, s);
153 if (u->meta.type == _UNIT_TYPE_INVALID) {
157 u->meta.instance = i;
159 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
161 if (UNIT_VTABLE(u)->init)
162 UNIT_VTABLE(u)->init(u);
166 unit_add_to_dbus_queue(u);
176 int unit_choose_id(Unit *u, const char *name) {
177 char *s, *t = NULL, *i;
183 if (unit_name_is_template(name)) {
185 if (!u->meta.instance)
188 if (!(t = unit_name_replace_instance(name, u->meta.instance)))
194 /* Selects one of the names of this unit as the id */
195 s = set_get(u->meta.names, (char*) name);
201 if ((r = unit_name_to_instance(s, &i)) < 0)
206 free(u->meta.instance);
207 u->meta.instance = i;
209 unit_add_to_dbus_queue(u);
214 int unit_set_description(Unit *u, const char *description) {
219 if (!(s = strdup(description)))
222 free(u->meta.description);
223 u->meta.description = s;
225 unit_add_to_dbus_queue(u);
229 bool unit_check_gc(Unit *u) {
232 if (u->meta.load_state == UNIT_STUB)
235 if (UNIT_VTABLE(u)->no_gc)
241 if (unit_active_state(u) != UNIT_INACTIVE)
244 if (UNIT_VTABLE(u)->check_gc)
245 if (UNIT_VTABLE(u)->check_gc(u))
251 void unit_add_to_load_queue(Unit *u) {
253 assert(u->meta.type != _UNIT_TYPE_INVALID);
255 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
258 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
259 u->meta.in_load_queue = true;
262 void unit_add_to_cleanup_queue(Unit *u) {
265 if (u->meta.in_cleanup_queue)
268 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
269 u->meta.in_cleanup_queue = true;
272 void unit_add_to_gc_queue(Unit *u) {
275 if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
278 if (unit_check_gc(u))
281 LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
282 u->meta.in_gc_queue = true;
284 u->meta.manager->n_in_gc_queue ++;
286 if (u->meta.manager->gc_queue_timestamp <= 0)
287 u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
290 void unit_add_to_dbus_queue(Unit *u) {
292 assert(u->meta.type != _UNIT_TYPE_INVALID);
294 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
297 /* Shortcut things if nobody cares */
298 if (!bus_has_subscriber(u->meta.manager)) {
299 u->meta.sent_dbus_new_signal = true;
303 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
304 u->meta.in_dbus_queue = true;
307 static void bidi_set_free(Unit *u, Set *s) {
313 /* Frees the set and makes sure we are dropped from the
314 * inverse pointers */
316 SET_FOREACH(other, s, i) {
319 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
320 set_remove(other->meta.dependencies[d], u);
322 unit_add_to_gc_queue(other);
328 void unit_free(Unit *u) {
335 bus_unit_send_removed_signal(u);
337 if (u->meta.load_state != UNIT_STUB)
338 if (UNIT_VTABLE(u)->done)
339 UNIT_VTABLE(u)->done(u);
341 SET_FOREACH(t, u->meta.names, i)
342 hashmap_remove_value(u->meta.manager->units, t, u);
345 job_free(u->meta.job);
347 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
348 bidi_set_free(u, u->meta.dependencies[d]);
350 if (u->meta.type != _UNIT_TYPE_INVALID)
351 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
353 if (u->meta.in_load_queue)
354 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
356 if (u->meta.in_dbus_queue)
357 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
359 if (u->meta.in_cleanup_queue)
360 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
362 if (u->meta.in_gc_queue) {
363 LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
364 u->meta.manager->n_in_gc_queue--;
367 cgroup_bonding_free_list(u->meta.cgroup_bondings);
369 free(u->meta.description);
370 free(u->meta.fragment_path);
372 set_free_free(u->meta.names);
374 free(u->meta.instance);
379 UnitActiveState unit_active_state(Unit *u) {
382 if (u->meta.load_state == UNIT_MERGED)
383 return unit_active_state(unit_follow_merge(u));
385 /* After a reload it might happen that a unit is not correctly
386 * loaded but still has a process around. That's why we won't
387 * shortcut failed loading to UNIT_INACTIVE_MAINTENANCE. */
389 return UNIT_VTABLE(u)->active_state(u);
392 const char* unit_sub_state_to_string(Unit *u) {
395 return UNIT_VTABLE(u)->sub_state_to_string(u);
398 static void complete_move(Set **s, Set **other) {
406 set_move(*s, *other);
413 static void merge_names(Unit *u, Unit *other) {
420 complete_move(&u->meta.names, &other->meta.names);
422 set_free_free(other->meta.names);
423 other->meta.names = NULL;
424 other->meta.id = NULL;
426 SET_FOREACH(t, u->meta.names, i)
427 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
430 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
437 assert(d < _UNIT_DEPENDENCY_MAX);
439 SET_FOREACH(back, other->meta.dependencies[d], i) {
442 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
443 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
446 set_remove(back->meta.dependencies[k], other);
448 assert(r == -ENOENT);
452 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
454 set_free(other->meta.dependencies[d]);
455 other->meta.dependencies[d] = NULL;
458 int unit_merge(Unit *u, Unit *other) {
463 assert(u->meta.manager == other->meta.manager);
464 assert(u->meta.type != _UNIT_TYPE_INVALID);
466 other = unit_follow_merge(other);
471 if (u->meta.type != other->meta.type)
474 if (!u->meta.instance != !other->meta.instance)
477 if (other->meta.load_state != UNIT_STUB &&
478 other->meta.load_state != UNIT_FAILED)
484 if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other)))
488 merge_names(u, other);
490 /* Merge dependencies */
491 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
492 merge_dependencies(u, other, d);
494 other->meta.load_state = UNIT_MERGED;
495 other->meta.merged_into = u;
497 /* If there is still some data attached to the other node, we
498 * don't need it anymore, and can free it. */
499 if (other->meta.load_state != UNIT_STUB)
500 if (UNIT_VTABLE(other)->done)
501 UNIT_VTABLE(other)->done(other);
503 unit_add_to_dbus_queue(u);
504 unit_add_to_cleanup_queue(other);
509 int unit_merge_by_name(Unit *u, const char *name) {
517 if (unit_name_is_template(name)) {
518 if (!u->meta.instance)
521 if (!(s = unit_name_replace_instance(name, u->meta.instance)))
527 if (!(other = manager_get_unit(u->meta.manager, name)))
528 r = unit_add_name(u, name);
530 r = unit_merge(u, other);
536 Unit* unit_follow_merge(Unit *u) {
539 while (u->meta.load_state == UNIT_MERGED)
540 assert_se(u = u->meta.merged_into);
545 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
551 if (c->std_output != EXEC_OUTPUT_KMSG &&
552 c->std_output != EXEC_OUTPUT_SYSLOG &&
553 c->std_error != EXEC_OUTPUT_KMSG &&
554 c->std_error != EXEC_OUTPUT_SYSLOG)
557 /* If syslog or kernel logging is requested, make sure our own
558 * logging daemon is run first. */
560 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
563 if (u->meta.manager->running_as == MANAGER_SYSTEM)
564 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
570 const char *unit_description(Unit *u) {
573 if (u->meta.description)
574 return u->meta.description;
579 void unit_dump(Unit *u, FILE *f, const char *prefix) {
587 timestamp1[FORMAT_TIMESTAMP_MAX],
588 timestamp2[FORMAT_TIMESTAMP_MAX],
589 timestamp3[FORMAT_TIMESTAMP_MAX],
590 timestamp4[FORMAT_TIMESTAMP_MAX],
591 timespan[FORMAT_TIMESPAN_MAX];
595 assert(u->meta.type >= 0);
599 p2 = strappend(prefix, "\t");
600 prefix2 = p2 ? p2 : prefix;
604 "%s\tDescription: %s\n"
606 "%s\tUnit Load State: %s\n"
607 "%s\tUnit Active State: %s\n"
608 "%s\tInactive Exit Timestamp: %s\n"
609 "%s\tActive Enter Timestamp: %s\n"
610 "%s\tActive Exit Timestamp: %s\n"
611 "%s\tInactive Enter Timestamp: %s\n"
612 "%s\tGC Check Good: %s\n"
613 "%s\tNeed Daemon Reload: %s\n",
615 prefix, unit_description(u),
616 prefix, strna(u->meta.instance),
617 prefix, unit_load_state_to_string(u->meta.load_state),
618 prefix, unit_active_state_to_string(unit_active_state(u)),
619 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)),
620 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)),
621 prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)),
622 prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)),
623 prefix, yes_no(unit_check_gc(u)),
624 prefix, yes_no(unit_need_daemon_reload(u)));
626 SET_FOREACH(t, u->meta.names, i)
627 fprintf(f, "%s\tName: %s\n", prefix, t);
629 if ((following = unit_following(u)))
630 fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id);
632 if (u->meta.fragment_path)
633 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
635 if (u->meta.job_timeout > 0)
636 fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout));
638 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
641 SET_FOREACH(other, u->meta.dependencies[d], i)
642 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
645 if (u->meta.load_state == UNIT_LOADED) {
647 "%s\tRecursive Stop: %s\n"
648 "%s\tStopWhenUnneeded: %s\n"
649 "%s\tOnlyByDependency: %s\n"
650 "%s\tDefaultDependencies: %s\n"
651 "%s\tIgnoreDependencyFailure: %s\n",
652 prefix, yes_no(u->meta.recursive_stop),
653 prefix, yes_no(u->meta.stop_when_unneeded),
654 prefix, yes_no(u->meta.only_by_dependency),
655 prefix, yes_no(u->meta.default_dependencies),
656 prefix, yes_no(u->meta.ignore_dependency_failure));
658 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
659 fprintf(f, "%s\tControlGroup: %s:%s\n",
660 prefix, b->controller, b->path);
662 if (UNIT_VTABLE(u)->dump)
663 UNIT_VTABLE(u)->dump(u, f, prefix2);
665 } else if (u->meta.load_state == UNIT_MERGED)
667 "%s\tMerged into: %s\n",
668 prefix, u->meta.merged_into->meta.id);
671 job_dump(u->meta.job, f, prefix2);
676 /* Common implementation for multiple backends */
677 int unit_load_fragment_and_dropin(Unit *u) {
682 /* Load a .service file */
683 if ((r = unit_load_fragment(u)) < 0)
686 if (u->meta.load_state == UNIT_STUB)
689 /* Load drop-in directory data */
690 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
696 /* Common implementation for multiple backends */
697 int unit_load_fragment_and_dropin_optional(Unit *u) {
702 /* Same as unit_load_fragment_and_dropin(), but whether
703 * something can be loaded or not doesn't matter. */
705 /* Load a .service file */
706 if ((r = unit_load_fragment(u)) < 0)
709 if (u->meta.load_state == UNIT_STUB)
710 u->meta.load_state = UNIT_LOADED;
712 /* Load drop-in directory data */
713 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
719 /* Common implementation for multiple backends */
720 int unit_load_nop(Unit *u) {
723 if (u->meta.load_state == UNIT_STUB)
724 u->meta.load_state = UNIT_LOADED;
729 int unit_load(Unit *u) {
734 if (u->meta.in_load_queue) {
735 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
736 u->meta.in_load_queue = false;
739 if (u->meta.type == _UNIT_TYPE_INVALID)
742 if (u->meta.load_state != UNIT_STUB)
745 if (UNIT_VTABLE(u)->load)
746 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
749 if (u->meta.load_state == UNIT_STUB) {
754 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
756 unit_add_to_dbus_queue(unit_follow_merge(u));
757 unit_add_to_gc_queue(u);
762 u->meta.load_state = UNIT_FAILED;
763 unit_add_to_dbus_queue(u);
765 log_notice("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
771 * -EBADR: This unit type does not support starting.
772 * -EALREADY: Unit is already started.
773 * -EAGAIN: An operation is already in progress. Retry later.
774 * -ECANCELED: Too many requests for now.
776 int unit_start(Unit *u) {
777 UnitActiveState state;
781 if (u->meta.load_state != UNIT_LOADED)
784 /* If this is already (being) started, then this will
785 * succeed. Note that this will even succeed if this unit is
786 * not startable by the user. This is relied on to detect when
787 * we need to wait for units and when waiting is finished. */
788 state = unit_active_state(u);
789 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
792 /* If it is stopped, but we cannot start it, then fail */
793 if (!UNIT_VTABLE(u)->start)
796 /* We don't suppress calls to ->start() here when we are
797 * already starting, to allow this request to be used as a
798 * "hurry up" call, for example when the unit is in some "auto
799 * restart" state where it waits for a holdoff timer to elapse
800 * before it will start again. */
802 unit_add_to_dbus_queue(u);
804 unit_status_printf(u, "Starting %s...\n", unit_description(u));
806 return UNIT_VTABLE(u)->start(u);
809 bool unit_can_start(Unit *u) {
812 return !!UNIT_VTABLE(u)->start;
816 * -EBADR: This unit type does not support stopping.
817 * -EALREADY: Unit is already stopped.
818 * -EAGAIN: An operation is already in progress. Retry later.
820 int unit_stop(Unit *u) {
821 UnitActiveState state;
825 state = unit_active_state(u);
826 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state))
829 if (!UNIT_VTABLE(u)->stop)
832 unit_add_to_dbus_queue(u);
834 unit_status_printf(u, "Stopping %s...\n", unit_description(u));
836 return UNIT_VTABLE(u)->stop(u);
840 * -EBADR: This unit type does not support reloading.
841 * -ENOEXEC: Unit is not started.
842 * -EAGAIN: An operation is already in progress. Retry later.
844 int unit_reload(Unit *u) {
845 UnitActiveState state;
849 if (u->meta.load_state != UNIT_LOADED)
852 if (!unit_can_reload(u))
855 state = unit_active_state(u);
856 if (unit_active_state(u) == UNIT_RELOADING)
859 if (unit_active_state(u) != UNIT_ACTIVE)
862 unit_add_to_dbus_queue(u);
863 return UNIT_VTABLE(u)->reload(u);
866 bool unit_can_reload(Unit *u) {
869 if (!UNIT_VTABLE(u)->reload)
872 if (!UNIT_VTABLE(u)->can_reload)
875 return UNIT_VTABLE(u)->can_reload(u);
878 static void unit_check_uneeded(Unit *u) {
884 /* If this service shall be shut down when unneeded then do
887 if (!u->meta.stop_when_unneeded)
890 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
893 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
894 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
897 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
898 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
901 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
902 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
905 log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
907 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
908 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL);
911 static void retroactively_start_dependencies(Unit *u) {
916 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
918 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
919 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
920 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
922 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
923 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
924 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
926 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
927 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
928 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
930 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
931 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
932 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
934 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
935 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
936 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
939 static void retroactively_stop_dependencies(Unit *u) {
944 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
946 if (u->meta.recursive_stop) {
947 /* Pull down units need us recursively if enabled */
948 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
949 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
950 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
953 /* Garbage collect services that might not be needed anymore, if enabled */
954 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
955 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
956 unit_check_uneeded(other);
957 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
958 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
959 unit_check_uneeded(other);
960 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
961 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
962 unit_check_uneeded(other);
963 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
964 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
965 unit_check_uneeded(other);
966 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
967 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
968 unit_check_uneeded(other);
971 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
976 assert(os < _UNIT_ACTIVE_STATE_MAX);
977 assert(ns < _UNIT_ACTIVE_STATE_MAX);
979 /* Note that this is called for all low-level state changes,
980 * even if they might map to the same high-level
981 * UnitActiveState! That means that ns == os is OK an expected
982 * behaviour here. For example: if a mount point is remounted
983 * this function will be called too and the utmp code below
986 dual_timestamp_get(&ts);
988 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
989 u->meta.inactive_exit_timestamp = ts;
990 else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
991 u->meta.inactive_enter_timestamp = ts;
993 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
994 u->meta.active_enter_timestamp = ts;
995 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
996 u->meta.active_exit_timestamp = ts;
998 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
999 cgroup_bonding_trim_list(u->meta.cgroup_bondings, true);
1001 timer_unit_notify(u, ns);
1002 path_unit_notify(u, ns);
1007 if (u->meta.job->state == JOB_WAITING)
1009 /* So we reached a different state for this
1010 * job. Let's see if we can run it now if it
1011 * failed previously due to EAGAIN. */
1012 job_add_to_run_queue(u->meta.job);
1014 /* Let's check whether this state change constitutes a
1015 * finished job, or maybe cotradicts a running job and
1016 * hence needs to invalidate jobs. */
1018 switch (u->meta.job->type) {
1021 case JOB_VERIFY_ACTIVE:
1023 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
1024 job_finish_and_invalidate(u->meta.job, true);
1025 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
1027 job_finish_and_invalidate(u->meta.job, false);
1033 case JOB_RELOAD_OR_START:
1035 if (u->meta.job->state == JOB_RUNNING) {
1036 if (ns == UNIT_ACTIVE)
1037 job_finish_and_invalidate(u->meta.job, true);
1038 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
1040 job_finish_and_invalidate(u->meta.job, false);
1048 case JOB_TRY_RESTART:
1050 if (ns == UNIT_INACTIVE || ns == UNIT_MAINTENANCE)
1051 job_finish_and_invalidate(u->meta.job, true);
1052 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
1054 job_finish_and_invalidate(u->meta.job, false);
1060 assert_not_reached("Job type unknown");
1066 /* If this state change happened without being requested by a
1067 * job, then let's retroactively start or stop
1068 * dependencies. We skip that step when deserializing, since
1069 * we don't want to create any additional jobs just because
1070 * something is already activated. */
1072 if (unexpected && u->meta.manager->n_deserializing <= 0) {
1073 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
1074 retroactively_start_dependencies(u);
1075 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1076 retroactively_stop_dependencies(u);
1079 if (ns != os && ns == UNIT_MAINTENANCE) {
1083 SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i)
1084 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1086 log_notice("Unit %s entered maintenance state.", u->meta.id);
1089 /* Some names are special */
1090 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1091 if (unit_has_name(u, SPECIAL_DBUS_SERVICE)) {
1092 /* The bus just might have become available,
1093 * hence try to connect to it, if we aren't
1095 bus_init(u->meta.manager);
1098 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1099 /* The syslog daemon just might have become
1100 * available, hence try to connect to it, if
1101 * we aren't yet connected. */
1104 if (u->meta.type == UNIT_MOUNT)
1105 /* Another directory became available, let's
1106 * check if that is enough to write our utmp
1108 manager_write_utmp_reboot(u->meta.manager);
1110 if (u->meta.type == UNIT_TARGET)
1111 /* A target got activated, maybe this is a runlevel? */
1112 manager_write_utmp_runlevel(u->meta.manager, u);
1114 } else if (!UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1116 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1117 /* The syslog daemon might just have
1118 * terminated, hence try to disconnect from
1122 /* We don't care about D-Bus here, since we'll get an
1123 * asynchronous notification for it anyway. */
1126 /* Maybe we finished startup and are now ready for being
1127 * stopped because unneeded? */
1128 unit_check_uneeded(u);
1130 unit_add_to_dbus_queue(u);
1131 unit_add_to_gc_queue(u);
1134 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
1135 struct epoll_event ev;
1140 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
1146 if (epoll_ctl(u->meta.manager->epoll_fd,
1147 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1159 void unit_unwatch_fd(Unit *u, Watch *w) {
1163 if (w->type == WATCH_INVALID)
1166 assert(w->type == WATCH_FD);
1167 assert(w->data.unit == u);
1168 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1171 w->type = WATCH_INVALID;
1172 w->data.unit = NULL;
1175 int unit_watch_pid(Unit *u, pid_t pid) {
1179 /* Watch a specific PID. We only support one unit watching
1180 * each PID for now. */
1182 return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1185 void unit_unwatch_pid(Unit *u, pid_t pid) {
1189 hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1192 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1193 struct itimerspec its;
1199 assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u));
1201 /* This will try to reuse the old timer if there is one */
1203 if (w->type == WATCH_UNIT_TIMER) {
1204 assert(w->data.unit == u);
1209 } else if (w->type == WATCH_INVALID) {
1212 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1215 assert_not_reached("Invalid watch type");
1220 /* Set absolute time in the past, but not 0, since we
1221 * don't want to disarm the timer */
1222 its.it_value.tv_sec = 0;
1223 its.it_value.tv_nsec = 1;
1225 flags = TFD_TIMER_ABSTIME;
1227 timespec_store(&its.it_value, delay);
1231 /* This will also flush the elapse counter */
1232 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1235 if (w->type == WATCH_INVALID) {
1236 struct epoll_event ev;
1240 ev.events = EPOLLIN;
1242 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1246 w->type = WATCH_UNIT_TIMER;
1254 close_nointr_nofail(fd);
1259 void unit_unwatch_timer(Unit *u, Watch *w) {
1263 if (w->type == WATCH_INVALID)
1266 assert(w->type == WATCH_UNIT_TIMER);
1267 assert(w->data.unit == u);
1270 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1271 close_nointr_nofail(w->fd);
1274 w->type = WATCH_INVALID;
1275 w->data.unit = NULL;
1278 bool unit_job_is_applicable(Unit *u, JobType j) {
1280 assert(j >= 0 && j < _JOB_TYPE_MAX);
1284 case JOB_VERIFY_ACTIVE:
1290 case JOB_TRY_RESTART:
1291 return unit_can_start(u);
1294 return unit_can_reload(u);
1296 case JOB_RELOAD_OR_START:
1297 return unit_can_reload(u) && unit_can_start(u);
1300 assert_not_reached("Invalid job type");
1304 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
1306 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1307 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1308 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1309 [UNIT_WANTS] = UNIT_WANTED_BY,
1310 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1311 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1312 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1313 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
1314 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1315 [UNIT_CONFLICTS] = UNIT_CONFLICTS,
1316 [UNIT_BEFORE] = UNIT_AFTER,
1317 [UNIT_AFTER] = UNIT_BEFORE,
1318 [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID,
1319 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1320 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
1322 int r, q = 0, v = 0, w = 0;
1325 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1328 /* We won't allow dependencies on ourselves. We will not
1329 * consider them an error however. */
1333 if (UNIT_VTABLE(u)->no_requires &&
1334 (d == UNIT_REQUIRES ||
1335 d == UNIT_REQUIRES_OVERRIDABLE ||
1336 d == UNIT_REQUISITE ||
1337 d == UNIT_REQUISITE_OVERRIDABLE)) {
1341 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1344 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1345 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1349 if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1350 (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1353 if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1356 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1357 if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1362 if (add_reference) {
1363 if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
1368 if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
1372 unit_add_to_dbus_queue(u);
1377 set_remove(u->meta.dependencies[d], other);
1380 set_remove(other->meta.dependencies[inverse_table[d]], u);
1383 set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
1388 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
1393 if ((r = unit_add_dependency(u, d, other, add_reference)) < 0)
1396 if ((r = unit_add_dependency(u, e, other, add_reference)) < 0)
1402 static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1406 assert(name || path);
1409 name = file_name_from_path(path);
1411 if (!unit_name_is_template(name)) {
1416 if (u->meta.instance)
1417 s = unit_name_replace_instance(name, u->meta.instance);
1421 if (!(i = unit_name_to_prefix(u->meta.id)))
1424 s = unit_name_replace_instance(name, i);
1435 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1441 assert(name || path);
1443 if (!(name = resolve_template(u, name, path, &s)))
1446 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1449 r = unit_add_dependency(u, d, other, add_reference);
1456 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1462 assert(name || path);
1464 if (!(name = resolve_template(u, name, path, &s)))
1467 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1470 r = unit_add_two_dependencies(u, d, e, other, add_reference);
1477 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1483 assert(name || path);
1485 if (!(name = resolve_template(u, name, path, &s)))
1488 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1491 r = unit_add_dependency(other, d, u, add_reference);
1498 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1504 assert(name || path);
1506 if (!(name = resolve_template(u, name, path, &s)))
1509 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1512 if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
1520 int set_unit_path(const char *p) {
1524 /* This is mostly for debug purposes */
1526 if (path_is_absolute(p)) {
1527 if (!(c = strdup(p)))
1530 if (!(cwd = get_current_dir_name()))
1533 r = asprintf(&c, "%s/%s", cwd, p);
1540 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1549 char *unit_dbus_path(Unit *u) {
1554 if (!(e = bus_path_escape(u->meta.id)))
1557 p = strappend("/org/freedesktop/systemd1/unit/", e);
1563 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1573 if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
1576 /* Ensure this hasn't been added yet */
1579 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1580 LIST_PREPEND(CGroupBonding, by_path, l, b);
1582 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1583 LIST_REMOVE(CGroupBonding, by_path, l, b);
1587 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1593 static char *default_cgroup_path(Unit *u) {
1599 if (u->meta.instance) {
1602 if (!(t = unit_name_template(u->meta.id)))
1605 r = asprintf(&p, "%s/%s/%s", u->meta.manager->cgroup_hierarchy, t, u->meta.instance);
1608 r = asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, u->meta.id);
1610 return r < 0 ? NULL : p;
1613 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1614 char *controller = NULL, *path = NULL;
1615 CGroupBonding *b = NULL;
1621 if ((r = cg_split_spec(name, &controller, &path)) < 0)
1625 path = default_cgroup_path(u);
1628 controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
1630 if (!path || !controller) {
1637 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1642 if (!(b = new0(CGroupBonding, 1))) {
1647 b->controller = controller;
1650 b->clean_up = false;
1652 if ((r = unit_add_cgroup(u, b)) < 0)
1665 int unit_add_default_cgroup(Unit *u) {
1671 /* Adds in the default cgroup data, if it wasn't specified yet */
1673 if (unit_get_default_cgroup(u))
1676 if (!(b = new0(CGroupBonding, 1)))
1679 if (!(b->path = default_cgroup_path(u)))
1685 if ((r = unit_add_cgroup(u, b)) < 0)
1692 free(b->controller);
1698 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1701 return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER);
1704 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1712 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1715 assert(!unit_has_name(u, t));
1717 r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found);
1720 assert(r < 0 || *_found != u);
1725 int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
1733 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1736 assert(!unit_has_name(u, t));
1738 found = manager_get_unit(u->meta.manager, t);
1748 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
1752 return unit_name_to_prefix_and_instance(u->meta.id);
1755 static char *specifier_prefix(char specifier, void *data, void *userdata) {
1759 return unit_name_to_prefix(u->meta.id);
1762 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
1768 if (!(p = unit_name_to_prefix(u->meta.id)))
1771 r = unit_name_unescape(p);
1777 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
1781 if (u->meta.instance)
1782 return unit_name_unescape(u->meta.instance);
1787 char *unit_name_printf(Unit *u, const char* format) {
1790 * This will use the passed string as format string and
1791 * replace the following specifiers:
1793 * %n: the full id of the unit (foo@bar.waldo)
1794 * %N: the id of the unit without the suffix (foo@bar)
1795 * %p: the prefix (foo)
1796 * %i: the instance (bar)
1799 const Specifier table[] = {
1800 { 'n', specifier_string, u->meta.id },
1801 { 'N', specifier_prefix_and_instance, NULL },
1802 { 'p', specifier_prefix, NULL },
1803 { 'i', specifier_string, u->meta.instance },
1810 return specifier_printf(format, table, u);
1813 char *unit_full_printf(Unit *u, const char *format) {
1815 /* This is similar to unit_name_printf() but also supports
1818 const Specifier table[] = {
1819 { 'n', specifier_string, u->meta.id },
1820 { 'N', specifier_prefix_and_instance, NULL },
1821 { 'p', specifier_prefix, NULL },
1822 { 'P', specifier_prefix_unescaped, NULL },
1823 { 'i', specifier_string, u->meta.instance },
1824 { 'I', specifier_instance_unescaped, NULL },
1831 return specifier_printf(format, table, u);
1834 char **unit_full_printf_strv(Unit *u, char **l) {
1838 /* Applies unit_full_printf to every entry in l */
1843 if (!(r = new(char*, n+1)))
1846 for (i = l, j = r; *i; i++, j++)
1847 if (!(*j = unit_full_printf(u, *i)))
1863 int unit_watch_bus_name(Unit *u, const char *name) {
1867 /* Watch a specific name on the bus. We only support one unit
1868 * watching each name for now. */
1870 return hashmap_put(u->meta.manager->watch_bus, name, u);
1873 void unit_unwatch_bus_name(Unit *u, const char *name) {
1877 hashmap_remove_value(u->meta.manager->watch_bus, name, u);
1880 bool unit_can_serialize(Unit *u) {
1883 return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
1886 int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
1893 if (!unit_can_serialize(u))
1896 if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
1900 unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type));
1907 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
1918 va_start(ap, format);
1919 vfprintf(f, format, ap);
1925 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
1931 fprintf(f, "%s=%s\n", key, value);
1934 int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
1941 if (!unit_can_serialize(u))
1945 char line[1024], *l, *v;
1948 if (!fgets(line, sizeof(line), f)) {
1960 k = strcspn(l, "=");
1968 if (streq(l, "job")) {
1971 if ((type = job_type_from_string(v)) < 0)
1972 log_debug("Failed to parse job type value %s", v);
1974 u->meta.deserialized_job = type;
1979 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
1984 int unit_add_node_link(Unit *u, const char *what, bool wants) {
1994 /* Adds in links to the device node that this unit is based on */
1996 if (!is_device_path(what))
1999 if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
2002 r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device);
2008 if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, device, true)) < 0)
2012 if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0)
2018 int unit_coldplug(Unit *u) {
2023 if (UNIT_VTABLE(u)->coldplug)
2024 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
2027 if (u->meta.deserialized_job >= 0) {
2028 if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_FAIL, false, NULL, NULL)) < 0)
2031 u->meta.deserialized_job = _JOB_TYPE_INVALID;
2037 void unit_status_printf(Unit *u, const char *format, ...) {
2043 if (!UNIT_VTABLE(u)->show_status)
2046 if (u->meta.manager->running_as != MANAGER_SYSTEM)
2049 if (!u->meta.manager->show_status)
2052 if (!manager_is_booting_or_shutting_down(u->meta.manager))
2055 va_start(ap, format);
2056 status_vprintf(format, ap);
2060 bool unit_need_daemon_reload(Unit *u) {
2065 if (!u->meta.fragment_path)
2069 if (stat(u->meta.fragment_path, &st) < 0)
2070 /* What, cannot access this anymore? */
2074 u->meta.fragment_mtime &&
2075 timespec_load(&st.st_mtim) != u->meta.fragment_mtime;
2078 void unit_reset_maintenance(Unit *u) {
2081 if (UNIT_VTABLE(u)->reset_maintenance)
2082 UNIT_VTABLE(u)->reset_maintenance(u);
2085 Unit *unit_following(Unit *u) {
2088 if (UNIT_VTABLE(u)->following)
2089 return UNIT_VTABLE(u)->following(u);
2094 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
2095 [UNIT_SERVICE] = "service",
2096 [UNIT_TIMER] = "timer",
2097 [UNIT_SOCKET] = "socket",
2098 [UNIT_TARGET] = "target",
2099 [UNIT_DEVICE] = "device",
2100 [UNIT_MOUNT] = "mount",
2101 [UNIT_AUTOMOUNT] = "automount",
2102 [UNIT_SNAPSHOT] = "snapshot",
2103 [UNIT_SWAP] = "swap"
2106 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
2108 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
2109 [UNIT_STUB] = "stub",
2110 [UNIT_LOADED] = "loaded",
2111 [UNIT_FAILED] = "failed",
2112 [UNIT_MERGED] = "merged"
2115 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
2117 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
2118 [UNIT_ACTIVE] = "active",
2119 [UNIT_RELOADING] = "reloading",
2120 [UNIT_INACTIVE] = "inactive",
2121 [UNIT_MAINTENANCE] = "maintenance",
2122 [UNIT_ACTIVATING] = "activating",
2123 [UNIT_DEACTIVATING] = "deactivating"
2126 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
2128 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
2129 [UNIT_REQUIRES] = "Requires",
2130 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
2131 [UNIT_WANTS] = "Wants",
2132 [UNIT_REQUISITE] = "Requisite",
2133 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
2134 [UNIT_REQUIRED_BY] = "RequiredBy",
2135 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
2136 [UNIT_WANTED_BY] = "WantedBy",
2137 [UNIT_CONFLICTS] = "Conflicts",
2138 [UNIT_BEFORE] = "Before",
2139 [UNIT_AFTER] = "After",
2140 [UNIT_REFERENCES] = "References",
2141 [UNIT_REFERENCED_BY] = "ReferencedBy",
2142 [UNIT_ON_FAILURE] = "OnFailure"
2145 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
2147 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
2148 [KILL_CONTROL_GROUP] = "control-group",
2149 [KILL_PROCESS_GROUP] = "process-group",
2150 [KILL_PROCESS] = "process",
2151 [KILL_NONE] = "none"
2154 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);