2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include "dbus-target.h"
23 #include "string-util.h"
24 #include "unit-name.h"
28 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
29 [TARGET_DEAD
] = UNIT_INACTIVE
,
30 [TARGET_ACTIVE
] = UNIT_ACTIVE
33 static void target_set_state(Target
*t
, TargetState state
) {
34 TargetState old_state
;
40 if (state
!= old_state
)
41 log_debug("%s changed %s -> %s",
43 target_state_to_string(old_state
),
44 target_state_to_string(state
));
46 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
49 static int target_add_default_dependencies(Target
*t
) {
51 static const UnitDependency deps
[] = {
66 if (!UNIT(t
)->default_dependencies
)
69 /* Imply ordering for requirement dependencies on target
70 * units. Note that when the user created a contradicting
71 * ordering manually we won't add anything in here to make
72 * sure we don't create a loop. */
74 for (k
= 0; k
< ELEMENTSOF(deps
); k
++)
75 SET_FOREACH(other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
76 r
= unit_add_default_target_dependency(other
, UNIT(t
));
81 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
84 /* Make sure targets are unloaded on shutdown */
85 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
88 static int target_load(Unit
*u
) {
89 Target
*t
= TARGET(u
);
94 r
= unit_load_fragment_and_dropin(u
);
98 /* This is a new unit? Then let's add in some extras */
99 if (u
->load_state
== UNIT_LOADED
) {
100 r
= target_add_default_dependencies(t
);
108 static int target_coldplug(Unit
*u
) {
109 Target
*t
= TARGET(u
);
112 assert(t
->state
== TARGET_DEAD
);
114 if (t
->deserialized_state
!= t
->state
)
115 target_set_state(t
, t
->deserialized_state
);
120 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
121 Target
*t
= TARGET(u
);
127 "%sTarget State: %s\n",
128 prefix
, target_state_to_string(t
->state
));
131 static int target_start(Unit
*u
) {
132 Target
*t
= TARGET(u
);
136 assert(t
->state
== TARGET_DEAD
);
138 r
= unit_acquire_invocation_id(u
);
142 target_set_state(t
, TARGET_ACTIVE
);
146 static int target_stop(Unit
*u
) {
147 Target
*t
= TARGET(u
);
150 assert(t
->state
== TARGET_ACTIVE
);
152 target_set_state(t
, TARGET_DEAD
);
156 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
157 Target
*s
= TARGET(u
);
163 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
167 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
168 Target
*s
= TARGET(u
);
175 if (streq(key
, "state")) {
178 state
= target_state_from_string(value
);
180 log_debug("Failed to parse state value %s", value
);
182 s
->deserialized_state
= state
;
185 log_debug("Unknown serialization key '%s'", key
);
190 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
193 return state_translation_table
[TARGET(u
)->state
];
196 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
199 return target_state_to_string(TARGET(u
)->state
);
202 const UnitVTable target_vtable
= {
203 .object_size
= sizeof(Target
),
211 .coldplug
= target_coldplug
,
215 .start
= target_start
,
218 .serialize
= target_serialize
,
219 .deserialize_item
= target_deserialize_item
,
221 .active_state
= target_active_state
,
222 .sub_state_to_string
= target_sub_state_to_string
,
224 .bus_vtable
= bus_target_vtable
,
226 .status_message_formats
= {
227 .finished_start_job
= {
228 [JOB_DONE
] = "Reached target %s.",
230 .finished_stop_job
= {
231 [JOB_DONE
] = "Stopped target %s.",