1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "string-util.h"
12 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
13 [TARGET_DEAD
] = UNIT_INACTIVE
,
14 [TARGET_ACTIVE
] = UNIT_ACTIVE
,
17 static void target_set_state(Target
*t
, TargetState state
) {
18 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_unit_debug(UNIT(t
), "Changed %s -> %s",
30 target_state_to_string(old_state
), target_state_to_string(state
));
32 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], /* reload_success = */ true);
35 static int target_add_default_dependencies(Target
*t
) {
36 _cleanup_free_ Unit
**others
= NULL
;
41 if (!UNIT(t
)->default_dependencies
)
44 /* Imply ordering for requirement dependencies on target units. Note that when the user created a
45 * contradicting ordering manually we won't add anything in here to make sure we don't create a
48 * Note that quite likely iterating through these dependencies will add new dependencies, which
49 * conflicts with the hashmap-based iteration logic. Hence, instead of iterating through the
50 * dependencies and acting on them as we go, first take an "atomic snapshot" of sorts and iterate
53 n_others
= unit_get_dependency_array(UNIT(t
), UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE
, &others
);
57 FOREACH_ARRAY(i
, others
, n_others
) {
58 r
= unit_add_default_target_dependency(*i
, UNIT(t
));
63 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
66 /* Make sure targets are unloaded on shutdown */
67 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, true, UNIT_DEPENDENCY_DEFAULT
);
70 static int target_load(Unit
*u
) {
71 Target
*t
= ASSERT_PTR(TARGET(u
));
74 r
= unit_load_fragment_and_dropin(u
, true);
78 if (u
->load_state
!= UNIT_LOADED
)
81 /* This is a new unit? Then let's add in some extras */
82 return target_add_default_dependencies(t
);
85 static int target_coldplug(Unit
*u
) {
86 Target
*t
= ASSERT_PTR(TARGET(u
));
88 assert(t
->state
== TARGET_DEAD
);
90 if (t
->deserialized_state
!= t
->state
)
91 target_set_state(t
, t
->deserialized_state
);
96 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
97 Target
*t
= ASSERT_PTR(TARGET(u
));
103 "%sTarget State: %s\n",
104 prefix
, target_state_to_string(t
->state
));
107 static int target_start(Unit
*u
) {
108 Target
*t
= ASSERT_PTR(TARGET(u
));
111 assert(t
->state
== TARGET_DEAD
);
113 r
= unit_acquire_invocation_id(u
);
117 target_set_state(t
, TARGET_ACTIVE
);
121 static int target_stop(Unit
*u
) {
122 Target
*t
= ASSERT_PTR(TARGET(u
));
124 assert(t
->state
== TARGET_ACTIVE
);
126 target_set_state(t
, TARGET_DEAD
);
130 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
131 Target
*t
= ASSERT_PTR(TARGET(u
));
136 (void) serialize_item(f
, "state", target_state_to_string(t
->state
));
140 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
141 Target
*t
= ASSERT_PTR(TARGET(u
));
147 if (streq(key
, "state")) {
150 state
= target_state_from_string(value
);
152 log_unit_debug(u
, "Failed to parse state: %s", value
);
154 t
->deserialized_state
= state
;
157 log_unit_debug(u
, "Unknown serialization key: %s", key
);
162 static UnitActiveState
target_active_state(Unit
*u
) {
163 Target
*t
= ASSERT_PTR(TARGET(u
));
165 return state_translation_table
[t
->state
];
168 static const char *target_sub_state_to_string(Unit
*u
) {
169 Target
*t
= ASSERT_PTR(TARGET(u
));
171 return target_state_to_string(t
->state
);
174 const UnitVTable target_vtable
= {
175 .object_size
= sizeof(Target
),
185 .coldplug
= target_coldplug
,
189 .start
= target_start
,
192 .serialize
= target_serialize
,
193 .deserialize_item
= target_deserialize_item
,
195 .active_state
= target_active_state
,
196 .sub_state_to_string
= target_sub_state_to_string
,
198 .status_message_formats
= {
199 .finished_start_job
= {
200 [JOB_DONE
] = "Reached target %s.",
202 .finished_stop_job
= {
203 [JOB_DONE
] = "Stopped target %s.",
207 .notify_supervisor
= true,