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]);
55 static int target_add_default_dependencies(Target *t) {
60 /* Imply ordering for requirement dependencies on target
61 * units. Note that when the user created a contradicting
62 * ordering manually we won't add anything in here to make
63 * sure we don't create a loop. */
65 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES], i)
66 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
67 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
69 SET_FOREACH(other, t->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i)
70 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
71 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
73 SET_FOREACH(other, t->meta.dependencies[UNIT_WANTS], i)
74 if (!set_get(t->meta.dependencies[UNIT_BEFORE], other))
75 if ((r = unit_add_dependency(UNIT(t), UNIT_AFTER, other, true)) < 0)
81 static int target_add_getty_dependencies(Target *t) {
87 if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET))
90 /* Automatically add in a serial getty on the kernel
92 if (t->meta.manager->console) {
93 log_debug("Automatically adding serial getty for %s", t->meta.manager->console);
94 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, t->meta.manager->console)))
97 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
104 /* Automatically add in a serial getty on the first
105 * virtualizer console */
106 if (access("/sys/class/tty/hvc0", F_OK) == 0) {
107 log_debug("Automatic adding serial getty for hvc0");
108 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0")))
111 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
121 static int target_load(Unit *u) {
122 Target *t = TARGET(u);
127 if ((r = unit_load_fragment_and_dropin(u)) < 0)
130 /* This is a new unit? Then let's add in some extras */
131 if (u->meta.load_state == UNIT_LOADED) {
132 if (u->meta.default_dependencies)
133 if ((r = target_add_default_dependencies(t)) < 0)
136 if ((r = target_add_getty_dependencies(t)) < 0)
143 static int target_coldplug(Unit *u) {
144 Target *t = TARGET(u);
147 assert(t->state == TARGET_DEAD);
149 if (t->deserialized_state != t->state)
150 target_set_state(t, t->deserialized_state);
155 static void target_dump(Unit *u, FILE *f, const char *prefix) {
156 Target *t = TARGET(u);
162 "%sTarget State: %s\n",
163 prefix, target_state_to_string(t->state));
166 static int target_start(Unit *u) {
167 Target *t = TARGET(u);
170 assert(t->state == TARGET_DEAD);
172 target_set_state(t, TARGET_ACTIVE);
176 static int target_stop(Unit *u) {
177 Target *t = TARGET(u);
180 assert(t->state == TARGET_ACTIVE);
182 target_set_state(t, TARGET_DEAD);
186 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
187 Target *s = TARGET(u);
193 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
197 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
198 Target *s = TARGET(u);
205 if (streq(key, "state")) {
208 if ((state = target_state_from_string(value)) < 0)
209 log_debug("Failed to parse state value %s", value);
211 s->deserialized_state = state;
214 log_debug("Unknown serialization key '%s'", key);
219 static UnitActiveState target_active_state(Unit *u) {
222 return state_translation_table[TARGET(u)->state];
225 static const char *target_sub_state_to_string(Unit *u) {
228 return target_state_to_string(TARGET(u)->state);
231 static const char* const target_state_table[_TARGET_STATE_MAX] = {
232 [TARGET_DEAD] = "dead",
233 [TARGET_ACTIVE] = "active"
236 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
238 const UnitVTable target_vtable = {
242 .coldplug = target_coldplug,
246 .start = target_start,
249 .serialize = target_serialize,
250 .deserialize_item = target_deserialize_item,
252 .active_state = target_active_state,
253 .sub_state_to_string = target_sub_state_to_string,
255 .bus_interface = "org.freedesktop.systemd1.Target",
256 .bus_message_handler = bus_target_message_handler