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"
31 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
32 [TARGET_DEAD] = UNIT_INACTIVE,
33 [TARGET_ACTIVE] = UNIT_ACTIVE
36 static void target_set_state(Target *t, TargetState state) {
37 TargetState old_state;
43 if (state != old_state)
44 log_debug("%s changed %s -> %s",
46 target_state_to_string(old_state),
47 target_state_to_string(state));
49 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state]);
52 static int target_coldplug(Unit *u) {
53 Target *t = TARGET(u);
56 assert(t->state == TARGET_DEAD);
58 if (t->deserialized_state != t->state)
59 target_set_state(t, t->deserialized_state);
64 static void target_dump(Unit *u, FILE *f, const char *prefix) {
65 Target *t = TARGET(u);
71 "%sTarget State: %s\n",
72 prefix, target_state_to_string(t->state));
75 static int target_start(Unit *u) {
76 Target *t = TARGET(u);
79 assert(t->state == TARGET_DEAD);
81 target_set_state(t, TARGET_ACTIVE);
85 static int target_stop(Unit *u) {
86 Target *t = TARGET(u);
89 assert(t->state == TARGET_ACTIVE);
91 target_set_state(t, TARGET_DEAD);
95 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
96 Target *s = TARGET(u);
102 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
106 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
107 Target *s = TARGET(u);
114 if (streq(key, "state")) {
117 if ((state = target_state_from_string(value)) < 0)
118 log_debug("Failed to parse state value %s", value);
120 s->deserialized_state = state;
123 log_debug("Unknown serialization key '%s'", key);
128 static UnitActiveState target_active_state(Unit *u) {
131 return state_translation_table[TARGET(u)->state];
134 static const char *target_sub_state_to_string(Unit *u) {
137 return target_state_to_string(TARGET(u)->state);
140 int target_get_runlevel(Target *t) {
142 static const struct {
146 { SPECIAL_RUNLEVEL5_TARGET, '5' },
147 { SPECIAL_RUNLEVEL4_TARGET, '4' },
148 { SPECIAL_RUNLEVEL3_TARGET, '3' },
149 { SPECIAL_RUNLEVEL2_TARGET, '2' },
150 { SPECIAL_RUNLEVEL1_TARGET, '1' },
151 { SPECIAL_RUNLEVEL0_TARGET, '0' },
152 { SPECIAL_RUNLEVEL6_TARGET, '6' },
159 /* Tries to determine if this is a SysV runlevel and returns
160 * it if that is so. */
162 for (i = 0; i < ELEMENTSOF(table); i++)
163 if (unit_has_name(UNIT(t), table[i].special))
164 return table[i].runlevel;
169 static const char* const target_state_table[_TARGET_STATE_MAX] = {
170 [TARGET_DEAD] = "dead",
171 [TARGET_ACTIVE] = "active"
174 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
176 const UnitVTable target_vtable = {
179 .load = unit_load_fragment_and_dropin,
180 .coldplug = target_coldplug,
184 .start = target_start,
187 .serialize = target_serialize,
188 .deserialize_item = target_deserialize_item,
190 .active_state = target_active_state,
191 .sub_state_to_string = target_sub_state_to_string,
193 .bus_message_handler = bus_target_message_handler