1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include "dbus-target.h"
24 #include "string-util.h"
25 #include "unit-name.h"
29 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
30 [TARGET_DEAD
] = UNIT_INACTIVE
,
31 [TARGET_ACTIVE
] = UNIT_ACTIVE
34 static void target_set_state(Target
*t
, TargetState state
) {
35 TargetState old_state
;
41 if (state
!= old_state
)
42 log_debug("%s changed %s -> %s",
44 target_state_to_string(old_state
),
45 target_state_to_string(state
));
47 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
50 static int target_add_default_dependencies(Target
*t
) {
52 static const UnitDependency deps
[] = {
65 if (!UNIT(t
)->default_dependencies
)
68 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
69 * ordering manually we won't add anything in here to make sure we don't create a loop. */
71 for (k
= 0; k
< ELEMENTSOF(deps
); k
++) {
76 HASHMAP_FOREACH_KEY(v
, other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
77 r
= unit_add_default_target_dependency(other
, UNIT(t
));
83 if (unit_has_name(UNIT(t
), SPECIAL_SHUTDOWN_TARGET
))
86 /* Make sure targets are unloaded on shutdown */
87 return unit_add_two_dependencies_by_name(UNIT(t
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true, UNIT_DEPENDENCY_DEFAULT
);
90 static int target_load(Unit
*u
) {
91 Target
*t
= TARGET(u
);
96 r
= unit_load_fragment_and_dropin(u
);
100 /* This is a new unit? Then let's add in some extras */
101 if (u
->load_state
== UNIT_LOADED
) {
102 r
= target_add_default_dependencies(t
);
110 static int target_coldplug(Unit
*u
) {
111 Target
*t
= TARGET(u
);
114 assert(t
->state
== TARGET_DEAD
);
116 if (t
->deserialized_state
!= t
->state
)
117 target_set_state(t
, t
->deserialized_state
);
122 static void target_dump(Unit
*u
, FILE *f
, const char *prefix
) {
123 Target
*t
= TARGET(u
);
129 "%sTarget State: %s\n",
130 prefix
, target_state_to_string(t
->state
));
133 static int target_start(Unit
*u
) {
134 Target
*t
= TARGET(u
);
138 assert(t
->state
== TARGET_DEAD
);
140 r
= unit_acquire_invocation_id(u
);
144 target_set_state(t
, TARGET_ACTIVE
);
148 static int target_stop(Unit
*u
) {
149 Target
*t
= TARGET(u
);
152 assert(t
->state
== TARGET_ACTIVE
);
154 target_set_state(t
, TARGET_DEAD
);
158 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
159 Target
*s
= TARGET(u
);
165 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
169 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
170 Target
*s
= TARGET(u
);
177 if (streq(key
, "state")) {
180 state
= target_state_from_string(value
);
182 log_debug("Failed to parse state value %s", value
);
184 s
->deserialized_state
= state
;
187 log_debug("Unknown serialization key '%s'", key
);
192 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
195 return state_translation_table
[TARGET(u
)->state
];
198 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
201 return target_state_to_string(TARGET(u
)->state
);
204 const UnitVTable target_vtable
= {
205 .object_size
= sizeof(Target
),
213 .coldplug
= target_coldplug
,
217 .start
= target_start
,
220 .serialize
= target_serialize
,
221 .deserialize_item
= target_deserialize_item
,
223 .active_state
= target_active_state
,
224 .sub_state_to_string
= target_sub_state_to_string
,
226 .bus_vtable
= bus_target_vtable
,
228 .status_message_formats
= {
229 .finished_start_job
= {
230 [JOB_DONE
] = "Reached target %s.",
232 .finished_stop_job
= {
233 [JOB_DONE
] = "Stopped target %s.",