1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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/>.
28 #include "load-fragment.h"
30 #include "dbus-target.h"
32 #include "unit-name.h"
34 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
35 [TARGET_DEAD] = UNIT_INACTIVE,
36 [TARGET_ACTIVE] = UNIT_ACTIVE
39 static void target_set_state(Target *t, TargetState state) {
40 TargetState old_state;
46 if (state != old_state)
47 log_debug("%s changed %s -> %s",
49 target_state_to_string(old_state),
50 target_state_to_string(state));
52 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], true);
55 static int target_add_default_dependencies(Target *t) {
56 static const UnitDependency deps[] = {
58 UNIT_REQUIRES_OVERRIDABLE,
60 UNIT_REQUISITE_OVERRIDABLE,
72 /* Imply ordering for requirement dependencies on target
73 * units. Note that when the user created a contradicting
74 * ordering manually we won't add anything in here to make
75 * sure we don't create a loop. */
77 for (k = 0; k < ELEMENTSOF(deps); k++)
78 SET_FOREACH(other, UNIT(t)->dependencies[deps[k]], i)
79 if ((r = unit_add_default_target_dependency(other, UNIT(t))) < 0)
82 /* Make sure targets are unloaded on shutdown */
83 return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
86 static int target_load(Unit *u) {
87 Target *t = TARGET(u);
92 if ((r = unit_load_fragment_and_dropin(u)) < 0)
95 /* This is a new unit? Then let's add in some extras */
96 if (u->load_state == UNIT_LOADED) {
97 if (u->default_dependencies)
98 if ((r = target_add_default_dependencies(t)) < 0)
105 static int target_coldplug(Unit *u) {
106 Target *t = TARGET(u);
109 assert(t->state == TARGET_DEAD);
111 if (t->deserialized_state != t->state)
112 target_set_state(t, t->deserialized_state);
117 static void target_dump(Unit *u, FILE *f, const char *prefix) {
118 Target *t = TARGET(u);
124 "%sTarget State: %s\n",
125 prefix, target_state_to_string(t->state));
128 static int target_start(Unit *u) {
129 Target *t = TARGET(u);
132 assert(t->state == TARGET_DEAD);
134 target_set_state(t, TARGET_ACTIVE);
138 static int target_stop(Unit *u) {
139 Target *t = TARGET(u);
142 assert(t->state == TARGET_ACTIVE);
144 target_set_state(t, TARGET_DEAD);
148 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
149 Target *s = TARGET(u);
155 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
159 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
160 Target *s = TARGET(u);
167 if (streq(key, "state")) {
170 if ((state = target_state_from_string(value)) < 0)
171 log_debug("Failed to parse state value %s", value);
173 s->deserialized_state = state;
176 log_debug("Unknown serialization key '%s'", key);
181 static UnitActiveState target_active_state(Unit *u) {
184 return state_translation_table[TARGET(u)->state];
187 static const char *target_sub_state_to_string(Unit *u) {
190 return target_state_to_string(TARGET(u)->state);
193 static const char* const target_state_table[_TARGET_STATE_MAX] = {
194 [TARGET_DEAD] = "dead",
195 [TARGET_ACTIVE] = "active"
198 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
200 const UnitVTable target_vtable = {
202 .object_size = sizeof(Target),
209 .coldplug = target_coldplug,
213 .start = target_start,
216 .serialize = target_serialize,
217 .deserialize_item = target_deserialize_item,
219 .active_state = target_active_state,
220 .sub_state_to_string = target_sub_state_to_string,
222 .bus_interface = "org.freedesktop.systemd1.Target",
223 .bus_message_handler = bus_target_message_handler