1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
6 #include "dbus-target.h"
9 #include "string-util.h"
10 #include "unit-name.h"
14 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
15 [TARGET_DEAD
] = UNIT_INACTIVE
,
16 [TARGET_ACTIVE
] = UNIT_ACTIVE
19 static void target_set_state(Target
*t
, TargetState state
) {
20 TargetState old_state
;
26 if (state
!= old_state
)
27 log_debug("%s changed %s -> %s",
29 target_state_to_string(old_state
),
30 target_state_to_string(state
));
32 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], 0);
35 static int target_add_default_dependencies(Target
*t
) {
37 static const UnitDependency deps
[] = {
50 if (!UNIT(t
)->default_dependencies
)
53 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
54 * ordering manually we won't add anything in here to make sure we don't create a loop. */
56 for (k
= 0; k
< ELEMENTSOF(deps
); k
++) {
61 HASHMAP_FOREACH_KEY(v
, other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
62 r
= unit_add_default_target_dependency(other
, UNIT(t
));
68 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
71 /* Make sure targets are unloaded on shutdown */
72 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
75 static int target_load(Unit
*u
) {
76 Target
*t
= TARGET(u
);
81 r
= unit_load_fragment_and_dropin(u
);
85 /* This is a new unit? Then let's add in some extras */
86 if (u
->load_state
== UNIT_LOADED
) {
87 r
= target_add_default_dependencies(t
);
95 static int target_coldplug(Unit
*u
) {
96 Target
*t
= TARGET(u
);
99 assert(t
->state
== TARGET_DEAD
);
101 if (t
->deserialized_state
!= t
->state
)
102 target_set_state(t
, t
->deserialized_state
);
107 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
108 Target
*t
= TARGET(u
);
114 "%sTarget State: %s\n",
115 prefix
, target_state_to_string(t
->state
));
118 static int target_start(Unit
*u
) {
119 Target
*t
= TARGET(u
);
123 assert(t
->state
== TARGET_DEAD
);
125 r
= unit_acquire_invocation_id(u
);
129 target_set_state(t
, TARGET_ACTIVE
);
133 static int target_stop(Unit
*u
) {
134 Target
*t
= TARGET(u
);
137 assert(t
->state
== TARGET_ACTIVE
);
139 target_set_state(t
, TARGET_DEAD
);
143 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
144 Target
*s
= TARGET(u
);
150 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
154 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
155 Target
*s
= TARGET(u
);
162 if (streq(key
, "state")) {
165 state
= target_state_from_string(value
);
167 log_debug("Failed to parse state value %s", value
);
169 s
->deserialized_state
= state
;
172 log_debug("Unknown serialization key '%s'", key
);
177 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
180 return state_translation_table
[TARGET(u
)->state
];
183 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
186 return target_state_to_string(TARGET(u
)->state
);
189 const UnitVTable target_vtable
= {
190 .object_size
= sizeof(Target
),
198 .coldplug
= target_coldplug
,
202 .start
= target_start
,
205 .serialize
= target_serialize
,
206 .deserialize_item
= target_deserialize_item
,
208 .active_state
= target_active_state
,
209 .sub_state_to_string
= target_sub_state_to_string
,
211 .bus_vtable
= bus_target_vtable
,
213 .status_message_formats
= {
214 .finished_start_job
= {
215 [JOB_DONE
] = "Reached target %s.",
217 .finished_stop_job
= {
218 [JOB_DONE
] = "Stopped target %s.",