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) {
58 /* Make sure targets are unloaded on shutdown */
59 return unit_add_dependency_by_name(UNIT(t), UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
62 static int target_add_getty_dependencies(Target *t) {
68 if (!unit_has_name(UNIT(t), SPECIAL_GETTY_TARGET))
71 /* Automatically add in a serial getty on the kernel
73 if (t->meta.manager->console) {
74 log_debug("Automatically adding serial getty for %s", t->meta.manager->console);
75 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, t->meta.manager->console)))
78 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
85 /* Automatically add in a serial getty on the first
86 * virtualizer console */
87 if (access("/sys/class/tty/hvc0", F_OK) == 0) {
88 log_debug("Automatic adding serial getty for hvc0");
89 if (!(n = unit_name_replace_instance(SPECIAL_SERIAL_GETTY_SERVICE, "hvc0")))
92 r = unit_add_two_dependencies_by_name(UNIT(t), UNIT_AFTER, UNIT_WANTS, n, NULL, true);
102 static int target_load(Unit *u) {
103 Target *t = TARGET(u);
108 if ((r = unit_load_fragment_and_dropin(u)) < 0)
111 /* This is a new unit? Then let's add in some extras */
112 if (u->meta.load_state == UNIT_LOADED) {
113 if (u->meta.default_dependencies)
114 if ((r = target_add_default_dependencies(t)) < 0)
117 if ((r = target_add_getty_dependencies(t)) < 0)
124 static int target_coldplug(Unit *u) {
125 Target *t = TARGET(u);
128 assert(t->state == TARGET_DEAD);
130 if (t->deserialized_state != t->state)
131 target_set_state(t, t->deserialized_state);
136 static void target_dump(Unit *u, FILE *f, const char *prefix) {
137 Target *t = TARGET(u);
143 "%sTarget State: %s\n",
144 prefix, target_state_to_string(t->state));
147 static int target_start(Unit *u) {
148 Target *t = TARGET(u);
151 assert(t->state == TARGET_DEAD);
153 target_set_state(t, TARGET_ACTIVE);
157 static int target_stop(Unit *u) {
158 Target *t = TARGET(u);
161 assert(t->state == TARGET_ACTIVE);
163 target_set_state(t, TARGET_DEAD);
167 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
168 Target *s = TARGET(u);
174 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
178 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
179 Target *s = TARGET(u);
186 if (streq(key, "state")) {
189 if ((state = target_state_from_string(value)) < 0)
190 log_debug("Failed to parse state value %s", value);
192 s->deserialized_state = state;
195 log_debug("Unknown serialization key '%s'", key);
200 static UnitActiveState target_active_state(Unit *u) {
203 return state_translation_table[TARGET(u)->state];
206 static const char *target_sub_state_to_string(Unit *u) {
209 return target_state_to_string(TARGET(u)->state);
212 static const char* const target_state_table[_TARGET_STATE_MAX] = {
213 [TARGET_DEAD] = "dead",
214 [TARGET_ACTIVE] = "active"
217 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
219 const UnitVTable target_vtable = {
223 .coldplug = target_coldplug,
227 .start = target_start,
230 .serialize = target_serialize,
231 .deserialize_item = target_deserialize_item,
233 .active_state = target_active_state,
234 .sub_state_to_string = target_sub_state_to_string,
236 .bus_interface = "org.freedesktop.systemd1.Target",
237 .bus_message_handler = bus_target_message_handler