1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "dbus-target.h"
8 #include "string-util.h"
10 #include "unit-name.h"
13 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
14 [TARGET_DEAD
] = UNIT_INACTIVE
,
15 [TARGET_ACTIVE
] = UNIT_ACTIVE
18 static void target_set_state(Target
*t
, TargetState state
) {
19 TargetState old_state
;
22 if (t
->state
!= state
)
23 bus_unit_send_pending_change_signal(UNIT(t
), false);
28 if (state
!= old_state
)
29 log_debug("%s changed %s -> %s",
31 target_state_to_string(old_state
),
32 target_state_to_string(state
));
34 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
37 static int target_add_default_dependencies(Target
*t
) {
39 static const UnitDependency deps
[] = {
52 if (!UNIT(t
)->default_dependencies
)
55 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
56 * ordering manually we won't add anything in here to make sure we don't create a loop. */
58 for (k
= 0; k
< ELEMENTSOF(deps
); k
++) {
63 HASHMAP_FOREACH_KEY(v
, other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
64 r
= unit_add_default_target_dependency(other
, UNIT(t
));
70 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
73 /* Make sure targets are unloaded on shutdown */
74 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
77 static int target_load(Unit
*u
) {
78 Target
*t
= TARGET(u
);
83 r
= unit_load_fragment_and_dropin(u
, true);
87 if (u
->load_state
!= UNIT_LOADED
)
90 /* This is a new unit? Then let's add in some extras */
91 return target_add_default_dependencies(t
);
94 static int target_coldplug(Unit
*u
) {
95 Target
*t
= TARGET(u
);
98 assert(t
->state
== TARGET_DEAD
);
100 if (t
->deserialized_state
!= t
->state
)
101 target_set_state(t
, t
->deserialized_state
);
106 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
107 Target
*t
= TARGET(u
);
113 "%sTarget State: %s\n",
114 prefix
, target_state_to_string(t
->state
));
117 static int target_start(Unit
*u
) {
118 Target
*t
= TARGET(u
);
122 assert(t
->state
== TARGET_DEAD
);
124 r
= unit_acquire_invocation_id(u
);
128 target_set_state(t
, TARGET_ACTIVE
);
132 static int target_stop(Unit
*u
) {
133 Target
*t
= TARGET(u
);
136 assert(t
->state
== TARGET_ACTIVE
);
138 target_set_state(t
, TARGET_DEAD
);
142 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
143 Target
*s
= TARGET(u
);
149 (void) serialize_item(f
, "state", target_state_to_string(s
->state
));
153 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
154 Target
*s
= TARGET(u
);
161 if (streq(key
, "state")) {
164 state
= target_state_from_string(value
);
166 log_debug("Failed to parse state value %s", value
);
168 s
->deserialized_state
= state
;
171 log_debug("Unknown serialization key '%s'", key
);
176 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
179 return state_translation_table
[TARGET(u
)->state
];
182 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
185 return target_state_to_string(TARGET(u
)->state
);
188 const UnitVTable target_vtable
= {
189 .object_size
= sizeof(Target
),
199 .coldplug
= target_coldplug
,
203 .start
= target_start
,
206 .serialize
= target_serialize
,
207 .deserialize_item
= target_deserialize_item
,
209 .active_state
= target_active_state
,
210 .sub_state_to_string
= target_sub_state_to_string
,
212 .status_message_formats
= {
213 .finished_start_job
= {
214 [JOB_DONE
] = "Reached target %s.",
216 .finished_stop_job
= {
217 [JOB_DONE
] = "Stopped target %s.",