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/>.
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_coldplug(Unit *u) {
54 Target *t = TARGET(u);
57 assert(t->state == TARGET_DEAD);
59 if (t->deserialized_state != t->state)
60 target_set_state(t, t->deserialized_state);
65 static void target_dump(Unit *u, FILE *f, const char *prefix) {
66 Target *t = TARGET(u);
72 "%sTarget State: %s\n",
73 prefix, target_state_to_string(t->state));
76 static int target_start(Unit *u) {
77 Target *t = TARGET(u);
80 assert(t->state == TARGET_DEAD);
82 target_set_state(t, TARGET_ACTIVE);
86 static int target_stop(Unit *u) {
87 Target *t = TARGET(u);
90 assert(t->state == TARGET_ACTIVE);
92 target_set_state(t, TARGET_DEAD);
96 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
97 Target *s = TARGET(u);
103 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
107 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
108 Target *s = TARGET(u);
115 if (streq(key, "state")) {
118 if ((state = target_state_from_string(value)) < 0)
119 log_debug("Failed to parse state value %s", value);
121 s->deserialized_state = state;
124 log_debug("Unknown serialization key '%s'", key);
129 static UnitActiveState target_active_state(Unit *u) {
132 return state_translation_table[TARGET(u)->state];
135 static const char *target_sub_state_to_string(Unit *u) {
138 return target_state_to_string(TARGET(u)->state);
141 int target_get_runlevel(Target *t) {
143 static const struct {
147 { SPECIAL_RUNLEVEL5_TARGET, '5' },
148 { SPECIAL_RUNLEVEL4_TARGET, '4' },
149 { SPECIAL_RUNLEVEL3_TARGET, '3' },
150 { SPECIAL_RUNLEVEL2_TARGET, '2' },
151 { SPECIAL_RESCUE_TARGET, '1' },
152 { SPECIAL_POWEROFF_TARGET, '0' },
153 { SPECIAL_REBOOT_TARGET, '6' },
160 /* Tries to determine if this is a SysV runlevel and returns
161 * it if that is so. */
163 for (i = 0; i < ELEMENTSOF(table); i++)
164 if (unit_has_name(UNIT(t), table[i].special))
165 return table[i].runlevel;
170 static const char* const target_state_table[_TARGET_STATE_MAX] = {
171 [TARGET_DEAD] = "dead",
172 [TARGET_ACTIVE] = "active"
175 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
177 const UnitVTable target_vtable = {
180 .load = unit_load_fragment_and_dropin,
181 .coldplug = target_coldplug,
185 .start = target_start,
188 .serialize = target_serialize,
189 .deserialize_item = target_deserialize_item,
191 .active_state = target_active_state,
192 .sub_state_to_string = target_sub_state_to_string,
194 .bus_message_handler = bus_target_message_handler