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/>.
27 #include "load-fragment.h"
29 #include "dbus-target.h"
32 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
33 [TARGET_DEAD] = UNIT_INACTIVE,
34 [TARGET_ACTIVE] = UNIT_ACTIVE
37 static void target_set_state(Target *t, TargetState state) {
38 TargetState old_state;
44 if (state != old_state)
45 log_debug("%s changed %s -> %s",
47 target_state_to_string(old_state),
48 target_state_to_string(state));
50 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
53 static int target_add_default_dependencies(Target *t) {
58 /* Imply ordering for requirement dependencies on target
59 * units. Note that when the user created a contradicting
60 * ordering manually we won't add anything in here to make
61 * sure we don't create a loop. */
63 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i)
64 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
65 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
67 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
68 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
69 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
71 SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i)
72 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
73 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
79 static int target_load(Unit *u) {
80 Target *t = TARGET(u);
85 if ((r = unit_load_fragment_and_dropin(u)) < 0)
88 /* This is a new unit? Then let's add in some extras */
89 if (u->meta.load_state == UNIT_LOADED) {
90 if (u->meta.default_dependencies)
91 if ((r = target_add_default_dependencies(t)) < 0)
98 static int target_coldplug(Unit *u) {
99 Target *t = TARGET(u);
102 assert(t->state == TARGET_DEAD);
104 if (t->deserialized_state != t->state)
105 target_set_state(t, t->deserialized_state);
110 static void target_dump(Unit *u, FILE *f, const char *prefix) {
111 Target *t = TARGET(u);
117 "%sTarget State: %s\n",
118 prefix, target_state_to_string(t->state));
121 static int target_start(Unit *u) {
122 Target *t = TARGET(u);
125 assert(t->state == TARGET_DEAD);
127 target_set_state(t, TARGET_ACTIVE);
131 static int target_stop(Unit *u) {
132 Target *t = TARGET(u);
135 assert(t->state == TARGET_ACTIVE);
137 target_set_state(t, TARGET_DEAD);
141 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
142 Target *s = TARGET(u);
148 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
152 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
153 Target *s = TARGET(u);
160 if (streq(key, "state")) {
163 if ((state = target_state_from_string(value)) < 0)
164 log_debug("Failed to parse state value %s", value);
166 s->deserialized_state = state;
169 log_debug("Unknown serialization key '%s'", key);
174 static UnitActiveState target_active_state(Unit *u) {
177 return state_translation_table[TARGET(u)->state];
180 static const char *target_sub_state_to_string(Unit *u) {
183 return target_state_to_string(TARGET(u)->state);
186 static const char* const target_state_table[_TARGET_STATE_MAX] = {
187 [TARGET_DEAD] = "dead",
188 [TARGET_ACTIVE] = "active"
191 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
193 const UnitVTable target_vtable = {
197 .coldplug = target_coldplug,
201 .start = target_start,
204 .serialize = target_serialize,
205 .deserialize_item = target_deserialize_item,
207 .active_state = target_active_state,
208 .sub_state_to_string = target_sub_state_to_string,
210 .bus_interface = "org.freedesktop.systemd1.Target",
211 .bus_message_handler = bus_target_message_handler