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"
46 const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX] = {
47 [UNIT_SERVICE] = &service_vtable,
48 [UNIT_TIMER] = &timer_vtable,
49 [UNIT_SOCKET] = &socket_vtable,
50 [UNIT_TARGET] = &target_vtable,
51 [UNIT_DEVICE] = &device_vtable,
52 [UNIT_MOUNT] = &mount_vtable,
53 [UNIT_AUTOMOUNT] = &automount_vtable,
54 [UNIT_SNAPSHOT] = &snapshot_vtable,
55 [UNIT_SWAP] = &swap_vtable,
56 [UNIT_PATH] = &path_vtable
59 Unit *unit_new(Manager *m) {
64 if (!(u = new0(Unit, 1)))
67 if (!(u->meta.names = set_new(string_hash_func, string_compare_func))) {
73 u->meta.type = _UNIT_TYPE_INVALID;
74 u->meta.deserialized_job = _JOB_TYPE_INVALID;
75 u->meta.default_dependencies = true;
80 bool unit_has_name(Unit *u, const char *name) {
84 return !!set_get(u->meta.names, (char*) name);
87 int unit_add_name(Unit *u, const char *text) {
95 if (unit_name_is_template(text)) {
96 if (!u->meta.instance)
99 s = unit_name_replace_instance(text, u->meta.instance);
106 if (!unit_name_is_valid(s)) {
111 assert_se((t = unit_name_to_type(s)) >= 0);
113 if (u->meta.type != _UNIT_TYPE_INVALID && t != u->meta.type) {
118 if ((r = unit_name_to_instance(s, &i)) < 0)
121 if (i && unit_vtable[t]->no_instances)
124 /* Ensure that this unit is either instanced or not instanced,
126 if (u->meta.type != _UNIT_TYPE_INVALID && !u->meta.instance != !i) {
131 if (unit_vtable[t]->no_alias &&
132 !set_isempty(u->meta.names) &&
133 !set_get(u->meta.names, s)) {
138 if (hashmap_size(u->meta.manager->units) >= MANAGER_MAX_NAMES) {
143 if ((r = set_put(u->meta.names, s)) < 0) {
149 if ((r = hashmap_put(u->meta.manager->units, s, u)) < 0) {
150 set_remove(u->meta.names, s);
154 if (u->meta.type == _UNIT_TYPE_INVALID) {
158 u->meta.instance = i;
160 LIST_PREPEND(Meta, units_per_type, u->meta.manager->units_per_type[t], &u->meta);
162 if (UNIT_VTABLE(u)->init)
163 UNIT_VTABLE(u)->init(u);
167 unit_add_to_dbus_queue(u);
177 int unit_choose_id(Unit *u, const char *name) {
178 char *s, *t = NULL, *i;
184 if (unit_name_is_template(name)) {
186 if (!u->meta.instance)
189 if (!(t = unit_name_replace_instance(name, u->meta.instance)))
195 /* Selects one of the names of this unit as the id */
196 s = set_get(u->meta.names, (char*) name);
202 if ((r = unit_name_to_instance(s, &i)) < 0)
207 free(u->meta.instance);
208 u->meta.instance = i;
210 unit_add_to_dbus_queue(u);
215 int unit_set_description(Unit *u, const char *description) {
220 if (!(s = strdup(description)))
223 free(u->meta.description);
224 u->meta.description = s;
226 unit_add_to_dbus_queue(u);
230 bool unit_check_gc(Unit *u) {
233 if (u->meta.load_state == UNIT_STUB)
236 if (UNIT_VTABLE(u)->no_gc)
242 if (unit_active_state(u) != UNIT_INACTIVE)
245 if (UNIT_VTABLE(u)->check_gc)
246 if (UNIT_VTABLE(u)->check_gc(u))
252 void unit_add_to_load_queue(Unit *u) {
254 assert(u->meta.type != _UNIT_TYPE_INVALID);
256 if (u->meta.load_state != UNIT_STUB || u->meta.in_load_queue)
259 LIST_PREPEND(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
260 u->meta.in_load_queue = true;
263 void unit_add_to_cleanup_queue(Unit *u) {
266 if (u->meta.in_cleanup_queue)
269 LIST_PREPEND(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
270 u->meta.in_cleanup_queue = true;
273 void unit_add_to_gc_queue(Unit *u) {
276 if (u->meta.in_gc_queue || u->meta.in_cleanup_queue)
279 if (unit_check_gc(u))
282 LIST_PREPEND(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
283 u->meta.in_gc_queue = true;
285 u->meta.manager->n_in_gc_queue ++;
287 if (u->meta.manager->gc_queue_timestamp <= 0)
288 u->meta.manager->gc_queue_timestamp = now(CLOCK_MONOTONIC);
291 void unit_add_to_dbus_queue(Unit *u) {
293 assert(u->meta.type != _UNIT_TYPE_INVALID);
295 if (u->meta.load_state == UNIT_STUB || u->meta.in_dbus_queue)
298 /* Shortcut things if nobody cares */
299 if (!bus_has_subscriber(u->meta.manager)) {
300 u->meta.sent_dbus_new_signal = true;
304 LIST_PREPEND(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
305 u->meta.in_dbus_queue = true;
308 static void bidi_set_free(Unit *u, Set *s) {
314 /* Frees the set and makes sure we are dropped from the
315 * inverse pointers */
317 SET_FOREACH(other, s, i) {
320 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
321 set_remove(other->meta.dependencies[d], u);
323 unit_add_to_gc_queue(other);
329 void unit_free(Unit *u) {
336 bus_unit_send_removed_signal(u);
338 if (u->meta.load_state != UNIT_STUB)
339 if (UNIT_VTABLE(u)->done)
340 UNIT_VTABLE(u)->done(u);
342 SET_FOREACH(t, u->meta.names, i)
343 hashmap_remove_value(u->meta.manager->units, t, u);
346 job_free(u->meta.job);
348 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
349 bidi_set_free(u, u->meta.dependencies[d]);
351 if (u->meta.type != _UNIT_TYPE_INVALID)
352 LIST_REMOVE(Meta, units_per_type, u->meta.manager->units_per_type[u->meta.type], &u->meta);
354 if (u->meta.in_load_queue)
355 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
357 if (u->meta.in_dbus_queue)
358 LIST_REMOVE(Meta, dbus_queue, u->meta.manager->dbus_unit_queue, &u->meta);
360 if (u->meta.in_cleanup_queue)
361 LIST_REMOVE(Meta, cleanup_queue, u->meta.manager->cleanup_queue, &u->meta);
363 if (u->meta.in_gc_queue) {
364 LIST_REMOVE(Meta, gc_queue, u->meta.manager->gc_queue, &u->meta);
365 u->meta.manager->n_in_gc_queue--;
368 cgroup_bonding_free_list(u->meta.cgroup_bondings);
370 free(u->meta.description);
371 free(u->meta.fragment_path);
373 set_free_free(u->meta.names);
375 free(u->meta.instance);
380 UnitActiveState unit_active_state(Unit *u) {
383 if (u->meta.load_state == UNIT_MERGED)
384 return unit_active_state(unit_follow_merge(u));
386 /* After a reload it might happen that a unit is not correctly
387 * loaded but still has a process around. That's why we won't
388 * shortcut failed loading to UNIT_INACTIVE_MAINTENANCE. */
390 return UNIT_VTABLE(u)->active_state(u);
393 const char* unit_sub_state_to_string(Unit *u) {
396 return UNIT_VTABLE(u)->sub_state_to_string(u);
399 static void complete_move(Set **s, Set **other) {
407 set_move(*s, *other);
414 static void merge_names(Unit *u, Unit *other) {
421 complete_move(&u->meta.names, &other->meta.names);
423 set_free_free(other->meta.names);
424 other->meta.names = NULL;
425 other->meta.id = NULL;
427 SET_FOREACH(t, u->meta.names, i)
428 assert_se(hashmap_replace(u->meta.manager->units, t, u) == 0);
431 static void merge_dependencies(Unit *u, Unit *other, UnitDependency d) {
438 assert(d < _UNIT_DEPENDENCY_MAX);
440 SET_FOREACH(back, other->meta.dependencies[d], i) {
443 for (k = 0; k < _UNIT_DEPENDENCY_MAX; k++)
444 if ((r = set_remove_and_put(back->meta.dependencies[k], other, u)) < 0) {
447 set_remove(back->meta.dependencies[k], other);
449 assert(r == -ENOENT);
453 complete_move(&u->meta.dependencies[d], &other->meta.dependencies[d]);
455 set_free(other->meta.dependencies[d]);
456 other->meta.dependencies[d] = NULL;
459 int unit_merge(Unit *u, Unit *other) {
464 assert(u->meta.manager == other->meta.manager);
465 assert(u->meta.type != _UNIT_TYPE_INVALID);
467 other = unit_follow_merge(other);
472 if (u->meta.type != other->meta.type)
475 if (!u->meta.instance != !other->meta.instance)
478 if (other->meta.load_state != UNIT_STUB &&
479 other->meta.load_state != UNIT_FAILED)
485 if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(unit_active_state(other)))
489 merge_names(u, other);
491 /* Merge dependencies */
492 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++)
493 merge_dependencies(u, other, d);
495 other->meta.load_state = UNIT_MERGED;
496 other->meta.merged_into = u;
498 /* If there is still some data attached to the other node, we
499 * don't need it anymore, and can free it. */
500 if (other->meta.load_state != UNIT_STUB)
501 if (UNIT_VTABLE(other)->done)
502 UNIT_VTABLE(other)->done(other);
504 unit_add_to_dbus_queue(u);
505 unit_add_to_cleanup_queue(other);
510 int unit_merge_by_name(Unit *u, const char *name) {
518 if (unit_name_is_template(name)) {
519 if (!u->meta.instance)
522 if (!(s = unit_name_replace_instance(name, u->meta.instance)))
528 if (!(other = manager_get_unit(u->meta.manager, name)))
529 r = unit_add_name(u, name);
531 r = unit_merge(u, other);
537 Unit* unit_follow_merge(Unit *u) {
540 while (u->meta.load_state == UNIT_MERGED)
541 assert_se(u = u->meta.merged_into);
546 int unit_add_exec_dependencies(Unit *u, ExecContext *c) {
552 if (c->std_output != EXEC_OUTPUT_KMSG &&
553 c->std_output != EXEC_OUTPUT_SYSLOG &&
554 c->std_error != EXEC_OUTPUT_KMSG &&
555 c->std_error != EXEC_OUTPUT_SYSLOG)
558 /* If syslog or kernel logging is requested, make sure our own
559 * logging daemon is run first. */
561 if ((r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
564 if (u->meta.manager->running_as == MANAGER_SYSTEM)
565 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET, NULL, true)) < 0)
571 const char *unit_description(Unit *u) {
574 if (u->meta.description)
575 return u->meta.description;
580 void unit_dump(Unit *u, FILE *f, const char *prefix) {
588 timestamp1[FORMAT_TIMESTAMP_MAX],
589 timestamp2[FORMAT_TIMESTAMP_MAX],
590 timestamp3[FORMAT_TIMESTAMP_MAX],
591 timestamp4[FORMAT_TIMESTAMP_MAX],
592 timespan[FORMAT_TIMESPAN_MAX];
596 assert(u->meta.type >= 0);
600 p2 = strappend(prefix, "\t");
601 prefix2 = p2 ? p2 : prefix;
605 "%s\tDescription: %s\n"
607 "%s\tUnit Load State: %s\n"
608 "%s\tUnit Active State: %s\n"
609 "%s\tInactive Exit Timestamp: %s\n"
610 "%s\tActive Enter Timestamp: %s\n"
611 "%s\tActive Exit Timestamp: %s\n"
612 "%s\tInactive Enter Timestamp: %s\n"
613 "%s\tGC Check Good: %s\n"
614 "%s\tNeed Daemon Reload: %s\n",
616 prefix, unit_description(u),
617 prefix, strna(u->meta.instance),
618 prefix, unit_load_state_to_string(u->meta.load_state),
619 prefix, unit_active_state_to_string(unit_active_state(u)),
620 prefix, strna(format_timestamp(timestamp1, sizeof(timestamp1), u->meta.inactive_exit_timestamp.realtime)),
621 prefix, strna(format_timestamp(timestamp2, sizeof(timestamp2), u->meta.active_enter_timestamp.realtime)),
622 prefix, strna(format_timestamp(timestamp3, sizeof(timestamp3), u->meta.active_exit_timestamp.realtime)),
623 prefix, strna(format_timestamp(timestamp4, sizeof(timestamp4), u->meta.inactive_enter_timestamp.realtime)),
624 prefix, yes_no(unit_check_gc(u)),
625 prefix, yes_no(unit_need_daemon_reload(u)));
627 SET_FOREACH(t, u->meta.names, i)
628 fprintf(f, "%s\tName: %s\n", prefix, t);
630 if ((following = unit_following(u)))
631 fprintf(f, "%s\tFollowing: %s\n", prefix, following->meta.id);
633 if (u->meta.fragment_path)
634 fprintf(f, "%s\tFragment Path: %s\n", prefix, u->meta.fragment_path);
636 if (u->meta.job_timeout > 0)
637 fprintf(f, "%s\tJob Timeout: %s\n", prefix, format_timespan(timespan, sizeof(timespan), u->meta.job_timeout));
639 for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) {
642 SET_FOREACH(other, u->meta.dependencies[d], i)
643 fprintf(f, "%s\t%s: %s\n", prefix, unit_dependency_to_string(d), other->meta.id);
646 if (u->meta.load_state == UNIT_LOADED) {
648 "%s\tRecursive Stop: %s\n"
649 "%s\tStopWhenUnneeded: %s\n"
650 "%s\tRefuseManualStart: %s\n"
651 "%s\tRefuseManualStop: %s\n"
652 "%s\tDefaultDependencies: %s\n"
653 "%s\tIgnoreDependencyFailure: %s\n",
654 prefix, yes_no(u->meta.recursive_stop),
655 prefix, yes_no(u->meta.stop_when_unneeded),
656 prefix, yes_no(u->meta.refuse_manual_start),
657 prefix, yes_no(u->meta.refuse_manual_stop),
658 prefix, yes_no(u->meta.default_dependencies),
659 prefix, yes_no(u->meta.ignore_dependency_failure));
661 LIST_FOREACH(by_unit, b, u->meta.cgroup_bondings)
662 fprintf(f, "%s\tControlGroup: %s:%s\n",
663 prefix, b->controller, b->path);
665 if (UNIT_VTABLE(u)->dump)
666 UNIT_VTABLE(u)->dump(u, f, prefix2);
668 } else if (u->meta.load_state == UNIT_MERGED)
670 "%s\tMerged into: %s\n",
671 prefix, u->meta.merged_into->meta.id);
674 job_dump(u->meta.job, f, prefix2);
679 /* Common implementation for multiple backends */
680 int unit_load_fragment_and_dropin(Unit *u) {
685 /* Load a .service file */
686 if ((r = unit_load_fragment(u)) < 0)
689 if (u->meta.load_state == UNIT_STUB)
692 /* Load drop-in directory data */
693 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
699 /* Common implementation for multiple backends */
700 int unit_load_fragment_and_dropin_optional(Unit *u) {
705 /* Same as unit_load_fragment_and_dropin(), but whether
706 * something can be loaded or not doesn't matter. */
708 /* Load a .service file */
709 if ((r = unit_load_fragment(u)) < 0)
712 if (u->meta.load_state == UNIT_STUB)
713 u->meta.load_state = UNIT_LOADED;
715 /* Load drop-in directory data */
716 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
722 /* Common implementation for multiple backends */
723 int unit_load_nop(Unit *u) {
726 if (u->meta.load_state == UNIT_STUB)
727 u->meta.load_state = UNIT_LOADED;
732 int unit_load(Unit *u) {
737 if (u->meta.in_load_queue) {
738 LIST_REMOVE(Meta, load_queue, u->meta.manager->load_queue, &u->meta);
739 u->meta.in_load_queue = false;
742 if (u->meta.type == _UNIT_TYPE_INVALID)
745 if (u->meta.load_state != UNIT_STUB)
748 if (UNIT_VTABLE(u)->load)
749 if ((r = UNIT_VTABLE(u)->load(u)) < 0)
752 if (u->meta.load_state == UNIT_STUB) {
757 assert((u->meta.load_state != UNIT_MERGED) == !u->meta.merged_into);
759 unit_add_to_dbus_queue(unit_follow_merge(u));
760 unit_add_to_gc_queue(u);
765 u->meta.load_state = UNIT_FAILED;
766 unit_add_to_dbus_queue(u);
768 log_notice("Failed to load configuration for %s: %s", u->meta.id, strerror(-r));
774 * -EBADR: This unit type does not support starting.
775 * -EALREADY: Unit is already started.
776 * -EAGAIN: An operation is already in progress. Retry later.
777 * -ECANCELED: Too many requests for now.
779 int unit_start(Unit *u) {
780 UnitActiveState state;
784 if (u->meta.load_state != UNIT_LOADED)
787 /* If this is already (being) started, then this will
788 * succeed. Note that this will even succeed if this unit is
789 * not startable by the user. This is relied on to detect when
790 * we need to wait for units and when waiting is finished. */
791 state = unit_active_state(u);
792 if (UNIT_IS_ACTIVE_OR_RELOADING(state))
795 /* If it is stopped, but we cannot start it, then fail */
796 if (!UNIT_VTABLE(u)->start)
799 /* We don't suppress calls to ->start() here when we are
800 * already starting, to allow this request to be used as a
801 * "hurry up" call, for example when the unit is in some "auto
802 * restart" state where it waits for a holdoff timer to elapse
803 * before it will start again. */
805 unit_add_to_dbus_queue(u);
807 unit_status_printf(u, "Starting %s...\n", unit_description(u));
809 return UNIT_VTABLE(u)->start(u);
812 bool unit_can_start(Unit *u) {
815 return !!UNIT_VTABLE(u)->start;
819 * -EBADR: This unit type does not support stopping.
820 * -EALREADY: Unit is already stopped.
821 * -EAGAIN: An operation is already in progress. Retry later.
823 int unit_stop(Unit *u) {
824 UnitActiveState state;
828 state = unit_active_state(u);
829 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(state))
832 if (!UNIT_VTABLE(u)->stop)
835 unit_add_to_dbus_queue(u);
837 unit_status_printf(u, "Stopping %s...\n", unit_description(u));
839 return UNIT_VTABLE(u)->stop(u);
843 * -EBADR: This unit type does not support reloading.
844 * -ENOEXEC: Unit is not started.
845 * -EAGAIN: An operation is already in progress. Retry later.
847 int unit_reload(Unit *u) {
848 UnitActiveState state;
852 if (u->meta.load_state != UNIT_LOADED)
855 if (!unit_can_reload(u))
858 state = unit_active_state(u);
859 if (unit_active_state(u) == UNIT_RELOADING)
862 if (unit_active_state(u) != UNIT_ACTIVE)
865 unit_add_to_dbus_queue(u);
866 return UNIT_VTABLE(u)->reload(u);
869 bool unit_can_reload(Unit *u) {
872 if (!UNIT_VTABLE(u)->reload)
875 if (!UNIT_VTABLE(u)->can_reload)
878 return UNIT_VTABLE(u)->can_reload(u);
881 static void unit_check_uneeded(Unit *u) {
887 /* If this service shall be shut down when unneeded then do
890 if (!u->meta.stop_when_unneeded)
893 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)))
896 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
897 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
900 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY_OVERRIDABLE], i)
901 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
904 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTED_BY], i)
905 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
908 log_info("Service %s is not needed anymore. Stopping.", u->meta.id);
910 /* Ok, nobody needs us anymore. Sniff. Then let's commit suicide */
911 manager_add_job(u->meta.manager, JOB_STOP, u, JOB_FAIL, true, NULL, NULL);
914 static void retroactively_start_dependencies(Unit *u) {
919 assert(UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u)));
921 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
922 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
923 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
925 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
926 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
927 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
929 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
930 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
931 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
933 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
934 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
935 manager_add_job(u->meta.manager, JOB_START, other, JOB_FAIL, false, NULL, NULL);
937 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTS], i)
938 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
939 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
941 SET_FOREACH(other, u->meta.dependencies[UNIT_CONFLICTED_BY], i)
942 if (!UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(other)))
943 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
946 static void retroactively_stop_dependencies(Unit *u) {
951 assert(UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(u)));
953 if (u->meta.recursive_stop) {
954 /* Pull down units need us recursively if enabled */
955 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRED_BY], i)
956 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
957 manager_add_job(u->meta.manager, JOB_STOP, other, JOB_REPLACE, true, NULL, NULL);
960 /* Garbage collect services that might not be needed anymore, if enabled */
961 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES], i)
962 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
963 unit_check_uneeded(other);
964 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
965 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
966 unit_check_uneeded(other);
967 SET_FOREACH(other, u->meta.dependencies[UNIT_WANTS], i)
968 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
969 unit_check_uneeded(other);
970 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE], i)
971 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
972 unit_check_uneeded(other);
973 SET_FOREACH(other, u->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i)
974 if (!UNIT_IS_INACTIVE_OR_DEACTIVATING(unit_active_state(other)))
975 unit_check_uneeded(other);
978 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns) {
983 assert(os < _UNIT_ACTIVE_STATE_MAX);
984 assert(ns < _UNIT_ACTIVE_STATE_MAX);
986 /* Note that this is called for all low-level state changes,
987 * even if they might map to the same high-level
988 * UnitActiveState! That means that ns == os is OK an expected
989 * behaviour here. For example: if a mount point is remounted
990 * this function will be called too and the utmp code below
993 dual_timestamp_get(&ts);
995 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && !UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
996 u->meta.inactive_exit_timestamp = ts;
997 else if (!UNIT_IS_INACTIVE_OR_MAINTENANCE(os) && UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
998 u->meta.inactive_enter_timestamp = ts;
1000 if (!UNIT_IS_ACTIVE_OR_RELOADING(os) && UNIT_IS_ACTIVE_OR_RELOADING(ns))
1001 u->meta.active_enter_timestamp = ts;
1002 else if (UNIT_IS_ACTIVE_OR_RELOADING(os) && !UNIT_IS_ACTIVE_OR_RELOADING(ns))
1003 u->meta.active_exit_timestamp = ts;
1005 if (UNIT_IS_INACTIVE_OR_MAINTENANCE(ns))
1006 cgroup_bonding_trim_list(u->meta.cgroup_bondings, true);
1008 timer_unit_notify(u, ns);
1009 path_unit_notify(u, ns);
1014 if (u->meta.job->state == JOB_WAITING)
1016 /* So we reached a different state for this
1017 * job. Let's see if we can run it now if it
1018 * failed previously due to EAGAIN. */
1019 job_add_to_run_queue(u->meta.job);
1021 /* Let's check whether this state change constitutes a
1022 * finished job, or maybe cotradicts a running job and
1023 * hence needs to invalidate jobs. */
1025 switch (u->meta.job->type) {
1028 case JOB_VERIFY_ACTIVE:
1030 if (UNIT_IS_ACTIVE_OR_RELOADING(ns))
1031 job_finish_and_invalidate(u->meta.job, true);
1032 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_ACTIVATING) {
1034 job_finish_and_invalidate(u->meta.job, false);
1040 case JOB_RELOAD_OR_START:
1042 if (u->meta.job->state == JOB_RUNNING) {
1043 if (ns == UNIT_ACTIVE)
1044 job_finish_and_invalidate(u->meta.job, true);
1045 else if (ns != UNIT_ACTIVATING && ns != UNIT_RELOADING) {
1047 job_finish_and_invalidate(u->meta.job, false);
1055 case JOB_TRY_RESTART:
1057 if (ns == UNIT_INACTIVE || ns == UNIT_MAINTENANCE)
1058 job_finish_and_invalidate(u->meta.job, true);
1059 else if (u->meta.job->state == JOB_RUNNING && ns != UNIT_DEACTIVATING) {
1061 job_finish_and_invalidate(u->meta.job, false);
1067 assert_not_reached("Job type unknown");
1073 /* If this state change happened without being requested by a
1074 * job, then let's retroactively start or stop
1075 * dependencies. We skip that step when deserializing, since
1076 * we don't want to create any additional jobs just because
1077 * something is already activated. */
1079 if (unexpected && u->meta.manager->n_deserializing <= 0) {
1080 if (UNIT_IS_INACTIVE_OR_DEACTIVATING(os) && UNIT_IS_ACTIVE_OR_ACTIVATING(ns))
1081 retroactively_start_dependencies(u);
1082 else if (UNIT_IS_ACTIVE_OR_ACTIVATING(os) && UNIT_IS_INACTIVE_OR_DEACTIVATING(ns))
1083 retroactively_stop_dependencies(u);
1086 if (ns != os && ns == UNIT_MAINTENANCE) {
1090 SET_FOREACH(other, u->meta.dependencies[UNIT_ON_FAILURE], i)
1091 manager_add_job(u->meta.manager, JOB_START, other, JOB_REPLACE, true, NULL, NULL);
1093 log_notice("Unit %s entered maintenance state.", u->meta.id);
1096 /* Some names are special */
1097 if (UNIT_IS_ACTIVE_OR_RELOADING(ns)) {
1098 if (unit_has_name(u, SPECIAL_DBUS_SERVICE))
1099 /* The bus just might have become available,
1100 * hence try to connect to it, if we aren't
1102 bus_init(u->meta.manager);
1104 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1105 /* The syslog daemon just might have become
1106 * available, hence try to connect to it, if
1107 * we aren't yet connected. */
1110 if (u->meta.type == UNIT_SERVICE &&
1111 !UNIT_IS_ACTIVE_OR_RELOADING(os))
1112 /* Write audit record if we have just finished starting up */
1113 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_START, 1);
1117 if (unit_has_name(u, SPECIAL_SYSLOG_SERVICE))
1118 /* The syslog daemon might just have
1119 * terminated, hence try to disconnect from
1123 /* We don't care about D-Bus here, since we'll get an
1124 * asynchronous notification for it anyway. */
1126 if (u->meta.type == UNIT_SERVICE &&
1127 UNIT_IS_INACTIVE_OR_MAINTENANCE(ns) &&
1128 !UNIT_IS_INACTIVE_OR_MAINTENANCE(os))
1130 /* Write audit record if we have just finished shutting down */
1131 manager_send_unit_audit(u->meta.manager, u, AUDIT_SERVICE_STOP, ns == UNIT_INACTIVE);
1134 /* Maybe we finished startup and are now ready for being
1135 * stopped because unneeded? */
1136 unit_check_uneeded(u);
1138 unit_add_to_dbus_queue(u);
1139 unit_add_to_gc_queue(u);
1142 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w) {
1143 struct epoll_event ev;
1148 assert(w->type == WATCH_INVALID || (w->type == WATCH_FD && w->fd == fd && w->data.unit == u));
1154 if (epoll_ctl(u->meta.manager->epoll_fd,
1155 w->type == WATCH_INVALID ? EPOLL_CTL_ADD : EPOLL_CTL_MOD,
1167 void unit_unwatch_fd(Unit *u, Watch *w) {
1171 if (w->type == WATCH_INVALID)
1174 assert(w->type == WATCH_FD);
1175 assert(w->data.unit == u);
1176 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1179 w->type = WATCH_INVALID;
1180 w->data.unit = NULL;
1183 int unit_watch_pid(Unit *u, pid_t pid) {
1187 /* Watch a specific PID. We only support one unit watching
1188 * each PID for now. */
1190 return hashmap_put(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1193 void unit_unwatch_pid(Unit *u, pid_t pid) {
1197 hashmap_remove_value(u->meta.manager->watch_pids, LONG_TO_PTR(pid), u);
1200 int unit_watch_timer(Unit *u, usec_t delay, Watch *w) {
1201 struct itimerspec its;
1207 assert(w->type == WATCH_INVALID || (w->type == WATCH_UNIT_TIMER && w->data.unit == u));
1209 /* This will try to reuse the old timer if there is one */
1211 if (w->type == WATCH_UNIT_TIMER) {
1212 assert(w->data.unit == u);
1217 } else if (w->type == WATCH_INVALID) {
1220 if ((fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC)) < 0)
1223 assert_not_reached("Invalid watch type");
1228 /* Set absolute time in the past, but not 0, since we
1229 * don't want to disarm the timer */
1230 its.it_value.tv_sec = 0;
1231 its.it_value.tv_nsec = 1;
1233 flags = TFD_TIMER_ABSTIME;
1235 timespec_store(&its.it_value, delay);
1239 /* This will also flush the elapse counter */
1240 if (timerfd_settime(fd, flags, &its, NULL) < 0)
1243 if (w->type == WATCH_INVALID) {
1244 struct epoll_event ev;
1248 ev.events = EPOLLIN;
1250 if (epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0)
1254 w->type = WATCH_UNIT_TIMER;
1262 close_nointr_nofail(fd);
1267 void unit_unwatch_timer(Unit *u, Watch *w) {
1271 if (w->type == WATCH_INVALID)
1274 assert(w->type == WATCH_UNIT_TIMER);
1275 assert(w->data.unit == u);
1278 assert_se(epoll_ctl(u->meta.manager->epoll_fd, EPOLL_CTL_DEL, w->fd, NULL) >= 0);
1279 close_nointr_nofail(w->fd);
1282 w->type = WATCH_INVALID;
1283 w->data.unit = NULL;
1286 bool unit_job_is_applicable(Unit *u, JobType j) {
1288 assert(j >= 0 && j < _JOB_TYPE_MAX);
1292 case JOB_VERIFY_ACTIVE:
1298 case JOB_TRY_RESTART:
1299 return unit_can_start(u);
1302 return unit_can_reload(u);
1304 case JOB_RELOAD_OR_START:
1305 return unit_can_reload(u) && unit_can_start(u);
1308 assert_not_reached("Invalid job type");
1312 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference) {
1314 static const UnitDependency inverse_table[_UNIT_DEPENDENCY_MAX] = {
1315 [UNIT_REQUIRES] = UNIT_REQUIRED_BY,
1316 [UNIT_REQUIRES_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1317 [UNIT_WANTS] = UNIT_WANTED_BY,
1318 [UNIT_REQUISITE] = UNIT_REQUIRED_BY,
1319 [UNIT_REQUISITE_OVERRIDABLE] = UNIT_REQUIRED_BY_OVERRIDABLE,
1320 [UNIT_REQUIRED_BY] = _UNIT_DEPENDENCY_INVALID,
1321 [UNIT_REQUIRED_BY_OVERRIDABLE] = _UNIT_DEPENDENCY_INVALID,
1322 [UNIT_WANTED_BY] = _UNIT_DEPENDENCY_INVALID,
1323 [UNIT_CONFLICTS] = UNIT_CONFLICTED_BY,
1324 [UNIT_CONFLICTED_BY] = UNIT_CONFLICTS,
1325 [UNIT_BEFORE] = UNIT_AFTER,
1326 [UNIT_AFTER] = UNIT_BEFORE,
1327 [UNIT_ON_FAILURE] = _UNIT_DEPENDENCY_INVALID,
1328 [UNIT_REFERENCES] = UNIT_REFERENCED_BY,
1329 [UNIT_REFERENCED_BY] = UNIT_REFERENCES
1331 int r, q = 0, v = 0, w = 0;
1334 assert(d >= 0 && d < _UNIT_DEPENDENCY_MAX);
1337 /* We won't allow dependencies on ourselves. We will not
1338 * consider them an error however. */
1342 if (UNIT_VTABLE(u)->no_requires &&
1343 (d == UNIT_REQUIRES ||
1344 d == UNIT_REQUIRES_OVERRIDABLE ||
1345 d == UNIT_REQUISITE ||
1346 d == UNIT_REQUISITE_OVERRIDABLE)) {
1350 if ((r = set_ensure_allocated(&u->meta.dependencies[d], trivial_hash_func, trivial_compare_func)) < 0)
1353 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1354 if ((r = set_ensure_allocated(&other->meta.dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func)) < 0)
1358 if ((r = set_ensure_allocated(&u->meta.dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func)) < 0 ||
1359 (r = set_ensure_allocated(&other->meta.dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func)) < 0)
1362 if ((q = set_put(u->meta.dependencies[d], other)) < 0)
1365 if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID)
1366 if ((v = set_put(other->meta.dependencies[inverse_table[d]], u)) < 0) {
1371 if (add_reference) {
1372 if ((w = set_put(u->meta.dependencies[UNIT_REFERENCES], other)) < 0) {
1377 if ((r = set_put(other->meta.dependencies[UNIT_REFERENCED_BY], u)) < 0)
1381 unit_add_to_dbus_queue(u);
1386 set_remove(u->meta.dependencies[d], other);
1389 set_remove(other->meta.dependencies[inverse_table[d]], u);
1392 set_remove(u->meta.dependencies[UNIT_REFERENCES], other);
1397 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference) {
1402 if ((r = unit_add_dependency(u, d, other, add_reference)) < 0)
1405 if ((r = unit_add_dependency(u, e, other, add_reference)) < 0)
1411 static const char *resolve_template(Unit *u, const char *name, const char*path, char **p) {
1415 assert(name || path);
1418 name = file_name_from_path(path);
1420 if (!unit_name_is_template(name)) {
1425 if (u->meta.instance)
1426 s = unit_name_replace_instance(name, u->meta.instance);
1430 if (!(i = unit_name_to_prefix(u->meta.id)))
1433 s = unit_name_replace_instance(name, i);
1444 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1450 assert(name || path);
1452 if (!(name = resolve_template(u, name, path, &s)))
1455 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1458 r = unit_add_dependency(u, d, other, add_reference);
1465 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1471 assert(name || path);
1473 if (!(name = resolve_template(u, name, path, &s)))
1476 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1479 r = unit_add_two_dependencies(u, d, e, other, add_reference);
1486 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) {
1492 assert(name || path);
1494 if (!(name = resolve_template(u, name, path, &s)))
1497 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1500 r = unit_add_dependency(other, d, u, add_reference);
1507 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) {
1513 assert(name || path);
1515 if (!(name = resolve_template(u, name, path, &s)))
1518 if ((r = manager_load_unit(u->meta.manager, name, path, NULL, &other)) < 0)
1521 if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0)
1529 int set_unit_path(const char *p) {
1533 /* This is mostly for debug purposes */
1535 if (path_is_absolute(p)) {
1536 if (!(c = strdup(p)))
1539 if (!(cwd = get_current_dir_name()))
1542 r = asprintf(&c, "%s/%s", cwd, p);
1549 if (setenv("SYSTEMD_UNIT_PATH", c, 0) < 0) {
1558 char *unit_dbus_path(Unit *u) {
1566 if (!(e = bus_path_escape(u->meta.id)))
1569 p = strappend("/org/freedesktop/systemd1/unit/", e);
1575 int unit_add_cgroup(Unit *u, CGroupBonding *b) {
1585 if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
1588 /* Ensure this hasn't been added yet */
1591 l = hashmap_get(u->meta.manager->cgroup_bondings, b->path);
1592 LIST_PREPEND(CGroupBonding, by_path, l, b);
1594 if ((r = hashmap_replace(u->meta.manager->cgroup_bondings, b->path, l)) < 0) {
1595 LIST_REMOVE(CGroupBonding, by_path, l, b);
1599 LIST_PREPEND(CGroupBonding, by_unit, u->meta.cgroup_bondings, b);
1605 static char *default_cgroup_path(Unit *u) {
1611 if (u->meta.instance) {
1614 if (!(t = unit_name_template(u->meta.id)))
1617 r = asprintf(&p, "%s/%s/%s", u->meta.manager->cgroup_hierarchy, t, u->meta.instance);
1620 r = asprintf(&p, "%s/%s", u->meta.manager->cgroup_hierarchy, u->meta.id);
1622 return r < 0 ? NULL : p;
1625 int unit_add_cgroup_from_text(Unit *u, const char *name) {
1626 char *controller = NULL, *path = NULL;
1627 CGroupBonding *b = NULL;
1633 if ((r = cg_split_spec(name, &controller, &path)) < 0)
1637 path = default_cgroup_path(u);
1640 controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
1642 if (!path || !controller) {
1649 if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
1654 if (!(b = new0(CGroupBonding, 1))) {
1659 b->controller = controller;
1662 b->clean_up = false;
1664 if ((r = unit_add_cgroup(u, b)) < 0)
1677 int unit_add_default_cgroup(Unit *u) {
1683 /* Adds in the default cgroup data, if it wasn't specified yet */
1685 if (unit_get_default_cgroup(u))
1688 if (!(b = new0(CGroupBonding, 1)))
1691 if (!(b->path = default_cgroup_path(u)))
1697 if ((r = unit_add_cgroup(u, b)) < 0)
1704 free(b->controller);
1710 CGroupBonding* unit_get_default_cgroup(Unit *u) {
1713 return cgroup_bonding_find_list(u->meta.cgroup_bondings, SYSTEMD_CGROUP_CONTROLLER);
1716 int unit_load_related_unit(Unit *u, const char *type, Unit **_found) {
1724 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1727 assert(!unit_has_name(u, t));
1729 r = manager_load_unit(u->meta.manager, t, NULL, NULL, _found);
1732 assert(r < 0 || *_found != u);
1737 int unit_get_related_unit(Unit *u, const char *type, Unit **_found) {
1745 if (!(t = unit_name_change_suffix(u->meta.id, type)))
1748 assert(!unit_has_name(u, t));
1750 found = manager_get_unit(u->meta.manager, t);
1760 static char *specifier_prefix_and_instance(char specifier, void *data, void *userdata) {
1764 return unit_name_to_prefix_and_instance(u->meta.id);
1767 static char *specifier_prefix(char specifier, void *data, void *userdata) {
1771 return unit_name_to_prefix(u->meta.id);
1774 static char *specifier_prefix_unescaped(char specifier, void *data, void *userdata) {
1780 if (!(p = unit_name_to_prefix(u->meta.id)))
1783 r = unit_name_unescape(p);
1789 static char *specifier_instance_unescaped(char specifier, void *data, void *userdata) {
1793 if (u->meta.instance)
1794 return unit_name_unescape(u->meta.instance);
1799 char *unit_name_printf(Unit *u, const char* format) {
1802 * This will use the passed string as format string and
1803 * replace the following specifiers:
1805 * %n: the full id of the unit (foo@bar.waldo)
1806 * %N: the id of the unit without the suffix (foo@bar)
1807 * %p: the prefix (foo)
1808 * %i: the instance (bar)
1811 const Specifier table[] = {
1812 { 'n', specifier_string, u->meta.id },
1813 { 'N', specifier_prefix_and_instance, NULL },
1814 { 'p', specifier_prefix, NULL },
1815 { 'i', specifier_string, u->meta.instance },
1822 return specifier_printf(format, table, u);
1825 char *unit_full_printf(Unit *u, const char *format) {
1827 /* This is similar to unit_name_printf() but also supports
1830 const Specifier table[] = {
1831 { 'n', specifier_string, u->meta.id },
1832 { 'N', specifier_prefix_and_instance, NULL },
1833 { 'p', specifier_prefix, NULL },
1834 { 'P', specifier_prefix_unescaped, NULL },
1835 { 'i', specifier_string, u->meta.instance },
1836 { 'I', specifier_instance_unescaped, NULL },
1843 return specifier_printf(format, table, u);
1846 char **unit_full_printf_strv(Unit *u, char **l) {
1850 /* Applies unit_full_printf to every entry in l */
1855 if (!(r = new(char*, n+1)))
1858 for (i = l, j = r; *i; i++, j++)
1859 if (!(*j = unit_full_printf(u, *i)))
1875 int unit_watch_bus_name(Unit *u, const char *name) {
1879 /* Watch a specific name on the bus. We only support one unit
1880 * watching each name for now. */
1882 return hashmap_put(u->meta.manager->watch_bus, name, u);
1885 void unit_unwatch_bus_name(Unit *u, const char *name) {
1889 hashmap_remove_value(u->meta.manager->watch_bus, name, u);
1892 bool unit_can_serialize(Unit *u) {
1895 return UNIT_VTABLE(u)->serialize && UNIT_VTABLE(u)->deserialize_item;
1898 int unit_serialize(Unit *u, FILE *f, FDSet *fds) {
1905 if (!unit_can_serialize(u))
1908 if ((r = UNIT_VTABLE(u)->serialize(u, f, fds)) < 0)
1912 unit_serialize_item(u, f, "job", job_type_to_string(u->meta.job->type));
1919 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *format, ...) {
1930 va_start(ap, format);
1931 vfprintf(f, format, ap);
1937 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value) {
1943 fprintf(f, "%s=%s\n", key, value);
1946 int unit_deserialize(Unit *u, FILE *f, FDSet *fds) {
1953 if (!unit_can_serialize(u))
1957 char line[1024], *l, *v;
1960 if (!fgets(line, sizeof(line), f)) {
1972 k = strcspn(l, "=");
1980 if (streq(l, "job")) {
1983 if ((type = job_type_from_string(v)) < 0)
1984 log_debug("Failed to parse job type value %s", v);
1986 u->meta.deserialized_job = type;
1991 if ((r = UNIT_VTABLE(u)->deserialize_item(u, l, v, fds)) < 0)
1996 int unit_add_node_link(Unit *u, const char *what, bool wants) {
2006 /* Adds in links to the device node that this unit is based on */
2008 if (!is_device_path(what))
2011 if (!(e = unit_name_build_escape(what+1, NULL, ".device")))
2014 r = manager_load_unit(u->meta.manager, e, NULL, NULL, &device);
2020 if ((r = unit_add_two_dependencies(u, UNIT_AFTER, UNIT_REQUIRES, device, true)) < 0)
2024 if ((r = unit_add_dependency(device, UNIT_WANTS, u, false)) < 0)
2030 int unit_coldplug(Unit *u) {
2035 if (UNIT_VTABLE(u)->coldplug)
2036 if ((r = UNIT_VTABLE(u)->coldplug(u)) < 0)
2039 if (u->meta.deserialized_job >= 0) {
2040 if ((r = manager_add_job(u->meta.manager, u->meta.deserialized_job, u, JOB_FAIL, false, NULL, NULL)) < 0)
2043 u->meta.deserialized_job = _JOB_TYPE_INVALID;
2049 void unit_status_printf(Unit *u, const char *format, ...) {
2055 if (!UNIT_VTABLE(u)->show_status)
2058 if (u->meta.manager->running_as != MANAGER_SYSTEM)
2061 if (!u->meta.manager->show_status)
2064 if (!manager_is_booting_or_shutting_down(u->meta.manager))
2067 va_start(ap, format);
2068 status_vprintf(format, ap);
2072 bool unit_need_daemon_reload(Unit *u) {
2077 if (!u->meta.fragment_path)
2081 if (stat(u->meta.fragment_path, &st) < 0)
2082 /* What, cannot access this anymore? */
2086 u->meta.fragment_mtime &&
2087 timespec_load(&st.st_mtim) != u->meta.fragment_mtime;
2090 void unit_reset_maintenance(Unit *u) {
2093 if (UNIT_VTABLE(u)->reset_maintenance)
2094 UNIT_VTABLE(u)->reset_maintenance(u);
2097 Unit *unit_following(Unit *u) {
2100 if (UNIT_VTABLE(u)->following)
2101 return UNIT_VTABLE(u)->following(u);
2106 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
2107 [UNIT_SERVICE] = "service",
2108 [UNIT_TIMER] = "timer",
2109 [UNIT_SOCKET] = "socket",
2110 [UNIT_TARGET] = "target",
2111 [UNIT_DEVICE] = "device",
2112 [UNIT_MOUNT] = "mount",
2113 [UNIT_AUTOMOUNT] = "automount",
2114 [UNIT_SNAPSHOT] = "snapshot",
2115 [UNIT_SWAP] = "swap"
2118 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
2120 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
2121 [UNIT_STUB] = "stub",
2122 [UNIT_LOADED] = "loaded",
2123 [UNIT_FAILED] = "failed",
2124 [UNIT_MERGED] = "merged"
2127 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
2129 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
2130 [UNIT_ACTIVE] = "active",
2131 [UNIT_RELOADING] = "reloading",
2132 [UNIT_INACTIVE] = "inactive",
2133 [UNIT_MAINTENANCE] = "maintenance",
2134 [UNIT_ACTIVATING] = "activating",
2135 [UNIT_DEACTIVATING] = "deactivating"
2138 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
2140 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
2141 [UNIT_REQUIRES] = "Requires",
2142 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
2143 [UNIT_WANTS] = "Wants",
2144 [UNIT_REQUISITE] = "Requisite",
2145 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
2146 [UNIT_REQUIRED_BY] = "RequiredBy",
2147 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
2148 [UNIT_WANTED_BY] = "WantedBy",
2149 [UNIT_CONFLICTS] = "Conflicts",
2150 [UNIT_CONFLICTED_BY] = "ConflictedBy",
2151 [UNIT_BEFORE] = "Before",
2152 [UNIT_AFTER] = "After",
2153 [UNIT_REFERENCES] = "References",
2154 [UNIT_REFERENCED_BY] = "ReferencedBy",
2155 [UNIT_ON_FAILURE] = "OnFailure"
2158 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
2160 static const char* const kill_mode_table[_KILL_MODE_MAX] = {
2161 [KILL_CONTROL_GROUP] = "control-group",
2162 [KILL_PROCESS_GROUP] = "process-group",
2163 [KILL_PROCESS] = "process",
2164 [KILL_NONE] = "none"
2167 DEFINE_STRING_TABLE_LOOKUP(kill_mode, KillMode);