1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
28 #include "load-fragment.h"
30 #include "dbus-target.h"
32 #include "unit-name.h"
34 static const UnitActiveState state_translation_table
[_TARGET_STATE_MAX
] = {
35 [TARGET_DEAD
] = UNIT_INACTIVE
,
36 [TARGET_ACTIVE
] = UNIT_ACTIVE
39 static void target_set_state(Target
*t
, TargetState state
) {
40 TargetState old_state
;
46 if (state
!= old_state
)
47 log_debug("%s changed %s -> %s",
49 target_state_to_string(old_state
),
50 target_state_to_string(state
));
52 unit_notify(UNIT(t
), state_translation_table
[old_state
], state_translation_table
[state
], true);
55 static int target_add_default_dependencies(Target
*t
) {
57 static const UnitDependency deps
[] = {
59 UNIT_REQUIRES_OVERRIDABLE
,
61 UNIT_REQUISITE_OVERRIDABLE
,
74 /* Imply ordering for requirement dependencies on target
75 * units. Note that when the user created a contradicting
76 * ordering manually we won't add anything in here to make
77 * sure we don't create a loop. */
79 for (k
= 0; k
< ELEMENTSOF(deps
); k
++)
80 SET_FOREACH(other
, UNIT(t
)->dependencies
[deps
[k
]], i
) {
81 r
= unit_add_default_target_dependency(other
, UNIT(t
));
86 /* Make sure targets are unloaded on shutdown */
87 return unit_add_dependency_by_name(UNIT(t
), UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
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
&& u
->default_dependencies
) {
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
);
137 assert(t
->state
== TARGET_DEAD
);
139 target_set_state(t
, TARGET_ACTIVE
);
143 static int target_stop(Unit
*u
) {
144 Target
*t
= TARGET(u
);
147 assert(t
->state
== TARGET_ACTIVE
);
149 target_set_state(t
, TARGET_DEAD
);
153 static int target_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
154 Target
*s
= TARGET(u
);
160 unit_serialize_item(u
, f
, "state", target_state_to_string(s
->state
));
164 static int target_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
165 Target
*s
= TARGET(u
);
172 if (streq(key
, "state")) {
175 state
= target_state_from_string(value
);
177 log_debug("Failed to parse state value %s", value
);
179 s
->deserialized_state
= state
;
182 log_debug("Unknown serialization key '%s'", key
);
187 _pure_
static UnitActiveState
target_active_state(Unit
*u
) {
190 return state_translation_table
[TARGET(u
)->state
];
193 _pure_
static const char *target_sub_state_to_string(Unit
*u
) {
196 return target_state_to_string(TARGET(u
)->state
);
199 static const char* const target_state_table
[_TARGET_STATE_MAX
] = {
200 [TARGET_DEAD
] = "dead",
201 [TARGET_ACTIVE
] = "active"
204 DEFINE_STRING_TABLE_LOOKUP(target_state
, TargetState
);
206 const UnitVTable target_vtable
= {
207 .object_size
= sizeof(Target
),
215 .coldplug
= target_coldplug
,
219 .start
= target_start
,
222 .serialize
= target_serialize
,
223 .deserialize_item
= target_deserialize_item
,
225 .active_state
= target_active_state
,
226 .sub_state_to_string
= target_sub_state_to_string
,
228 .bus_interface
= "org.freedesktop.systemd1.Target",
229 .bus_vtable
= bus_target_vtable
,
231 .status_message_formats
= {
232 .finished_start_job
= {
233 [JOB_DONE
] = "Reached target %s.",
234 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
236 .finished_stop_job
= {
237 [JOB_DONE
] = "Stopped target %s.",