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_add_default_dependencies(Target
*t
) {
58 /* Imply ordering for requirement dependencies
61 SET_FOREACH(other
, t
->meta
.dependencies
[UNIT_REQUIRES
], i
)
62 if ((r
= unit_add_dependency(UNIT(t
), UNIT_AFTER
, other
, true)) < 0)
64 SET_FOREACH(other
, t
->meta
.dependencies
[UNIT_REQUIRES_OVERRIDABLE
], i
)
65 if ((r
= unit_add_dependency(UNIT(t
), UNIT_AFTER
, other
, true)) < 0)
67 SET_FOREACH(other
, t
->meta
.dependencies
[UNIT_WANTS
], i
)
68 if ((r
= unit_add_dependency(UNIT(t
), UNIT_AFTER
, other
, true)) < 0)
74 static int target_load(Unit
*u
) {
75 Target
*t
= TARGET(u
);
80 if ((r
= unit_load_fragment_and_dropin(u
)) < 0)
83 /* This is a new unit? Then let's add in some extras */
84 if (u
->meta
.load_state
== UNIT_LOADED
) {
85 if (u
->meta
.default_dependencies
)
86 if ((r
= target_add_default_dependencies(t
)) < 0)
93 static int target_coldplug(Unit
*u
) {
94 Target
*t
= TARGET(u
);
97 assert(t
->state
== TARGET_DEAD
);
99 if (t
->deserialized_state
!= t
->state
)
100 target_set_state(t
, t
->deserialized_state
);
105 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
106 Target
*t
= TARGET(u
);
112 "%sTarget State: %s\n",
113 prefix
, target_state_to_string(t
->state
));
116 static int target_start(Unit
*u
) {
117 Target
*t
= TARGET(u
);
120 assert(t
->state
== TARGET_DEAD
);
122 target_set_state(t
, TARGET_ACTIVE
);
126 static int target_stop(Unit
*u
) {
127 Target
*t
= TARGET(u
);
130 assert(t
->state
== TARGET_ACTIVE
);
132 target_set_state(t
, TARGET_DEAD
);
136 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
137 Target
*s
= TARGET(u
);
143 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
147 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
148 Target
*s
= TARGET(u
);
155 if (streq(key
, "state")) {
158 if ((state
= target_state_from_string(value
)) < 0)
159 log_debug("Failed to parse state value %s", value
);
161 s
->deserialized_state
= state
;
164 log_debug("Unknown serialization key '%s'", key
);
169 static UnitActiveState
target_active_state(Unit
*u
) {
172 return state_translation_table
[TARGET(u
)->state
];
175 static const char *target_sub_state_to_string(Unit
*u
) {
178 return target_state_to_string(TARGET(u
)->state
);
181 int target_get_runlevel(Target
*t
) {
183 static const struct {
187 { SPECIAL_RUNLEVEL5_TARGET
, '5' },
188 { SPECIAL_RUNLEVEL4_TARGET
, '4' },
189 { SPECIAL_RUNLEVEL3_TARGET
, '3' },
190 { SPECIAL_RUNLEVEL2_TARGET
, '2' },
191 { SPECIAL_RESCUE_TARGET
, '1' },
192 { SPECIAL_POWEROFF_TARGET
, '0' },
193 { SPECIAL_REBOOT_TARGET
, '6' },
200 /* Tries to determine if this is a SysV runlevel and returns
201 * it if that is so. */
203 for (i
= 0; i
< ELEMENTSOF(table
); i
++)
204 if (unit_has_name(UNIT(t
), table
[i
].special
))
205 return table
[i
].runlevel
;
210 static const char* const target_state_table
[_TARGET_STATE_MAX
] = {
211 [TARGET_DEAD
] = "dead",
212 [TARGET_ACTIVE
] = "active"
215 DEFINE_STRING_TABLE_LOOKUP(target_state
, TargetState
);
217 const UnitVTable target_vtable
= {
221 .coldplug
= target_coldplug
,
225 .start
= target_start
,
228 .serialize
= target_serialize
,
229 .deserialize_item
= target_deserialize_item
,
231 .active_state
= target_active_state
,
232 .sub_state_to_string
= target_sub_state_to_string
,
234 .bus_message_handler
= bus_target_message_handler