1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "dbus-target.h"
6 #include "string-util.h"
11 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
12 [TARGET_DEAD
] = UNIT_INACTIVE
,
13 [TARGET_ACTIVE
] = UNIT_ACTIVE
16 static void target_set_state(Target
*t
, TargetState state
) {
17 TargetState old_state
;
23 if (state
!= old_state
)
24 log_debug("%s changed %s -> %s",
26 target_state_to_string(old_state
),
27 target_state_to_string(state
));
29 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
32 static int target_add_default_dependencies(Target
*t
) {
34 static const UnitDependency deps
[] = {
47 if (!UNIT(t
)->default_dependencies
)
50 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
51 * ordering manually we won't add anything in here to make sure we don't create a loop. */
53 for (k
= 0; k
< ELEMENTSOF(deps
); k
++) {
58 HASHMAP_FOREACH_KEY(v
, other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
59 r
= unit_add_default_target_dependency(other
, UNIT(t
));
65 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
68 /* Make sure targets are unloaded on shutdown */
69 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
72 static int target_load(Unit
*u
) {
73 Target
*t
= TARGET(u
);
78 r
= unit_load_fragment_and_dropin(u
);
82 /* This is a new unit? Then let's add in some extras */
83 if (u
->load_state
== UNIT_LOADED
) {
84 r
= target_add_default_dependencies(t
);
92 static int target_coldplug(Unit
*u
) {
93 Target
*t
= TARGET(u
);
96 assert(t
->state
== TARGET_DEAD
);
98 if (t
->deserialized_state
!= t
->state
)
99 target_set_state(t
, t
->deserialized_state
);
104 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
105 Target
*t
= TARGET(u
);
111 "%sTarget State: %s\n",
112 prefix
, target_state_to_string(t
->state
));
115 static int target_start(Unit
*u
) {
116 Target
*t
= TARGET(u
);
120 assert(t
->state
== TARGET_DEAD
);
122 r
= unit_acquire_invocation_id(u
);
126 target_set_state(t
, TARGET_ACTIVE
);
130 static int target_stop(Unit
*u
) {
131 Target
*t
= TARGET(u
);
134 assert(t
->state
== TARGET_ACTIVE
);
136 target_set_state(t
, TARGET_DEAD
);
140 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
141 Target
*s
= TARGET(u
);
147 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
151 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
152 Target
*s
= TARGET(u
);
159 if (streq(key
, "state")) {
162 state
= target_state_from_string(value
);
164 log_debug("Failed to parse state value %s", value
);
166 s
->deserialized_state
= state
;
169 log_debug("Unknown serialization key '%s'", key
);
174 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
177 return state_translation_table
[TARGET(u
)->state
];
180 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
183 return target_state_to_string(TARGET(u
)->state
);
186 const UnitVTable target_vtable
= {
187 .object_size
= sizeof(Target
),
195 .coldplug
= target_coldplug
,
199 .start
= target_start
,
202 .serialize
= target_serialize
,
203 .deserialize_item
= target_deserialize_item
,
205 .active_state
= target_active_state
,
206 .sub_state_to_string
= target_sub_state_to_string
,
208 .bus_vtable
= bus_target_vtable
,
210 .status_message_formats
= {
211 .finished_start_job
= {
212 [JOB_DONE
] = "Reached target %s.",
214 .finished_stop_job
= {
215 [JOB_DONE
] = "Stopped target %s.",