]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/target.c
core: use ASSERT_PTR(CAST(u)) everywhere
[thirdparty/systemd.git] / src / core / target.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
4139c1b2 3#include "dbus-target.h"
6fcbec6f 4#include "dbus-unit.h"
07630cea 5#include "log.h"
d68c645b 6#include "serialize.h"
514f4ef5 7#include "special.h"
07630cea 8#include "string-util.h"
d68c645b 9#include "target.h"
b2bb3dbe 10#include "unit-name.h"
07630cea 11#include "unit.h"
c22cbe26 12
fa068367
LP
13static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
14 [TARGET_DEAD] = UNIT_INACTIVE,
15 [TARGET_ACTIVE] = UNIT_ACTIVE
16};
17
fa068367
LP
18static void target_set_state(Target *t, TargetState state) {
19 TargetState old_state;
20 assert(t);
21
6fcbec6f
LP
22 if (t->state != state)
23 bus_unit_send_pending_change_signal(UNIT(t), false);
24
fa068367
LP
25 old_state = t->state;
26 t->state = state;
27
e537352b 28 if (state != old_state)
40d50879 29 log_debug("%s changed %s -> %s",
1124fe6f 30 UNIT(t)->id,
a16e1123
LP
31 target_state_to_string(old_state),
32 target_state_to_string(state));
c22cbe26 33
96b09de5 34 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], /* reload_success = */ true);
fa068367
LP
35}
36
a40eb732 37static int target_add_default_dependencies(Target *t) {
15ed3c3a
LP
38 _cleanup_free_ Unit **others = NULL;
39 int r, n_others;
bba34eed 40
98bc2000 41 assert(t);
a40eb732 42
41c237af
IP
43 if (!UNIT(t)->default_dependencies)
44 return 0;
45
15ed3c3a
LP
46 /* Imply ordering for requirement dependencies on target units. Note that when the user created a
47 * contradicting ordering manually we won't add anything in here to make sure we don't create a
48 * loop.
49 *
50 * Note that quite likely iterating through these dependencies will add new dependencies, which
51 * conflicts with the hashmap-based iteration logic. Hence, instead of iterating through the
52 * dependencies and acting on them as we go, first take an "atomic snapshot" of sorts and iterate
53 * through that. */
54
55 n_others = unit_get_dependency_array(UNIT(t), UNIT_ATOM_ADD_DEFAULT_TARGET_DEPENDENCY_QUEUE, &others);
56 if (n_others < 0)
57 return n_others;
58
59 for (int i = 0; i < n_others; i++) {
60 r = unit_add_default_target_dependency(others[i], UNIT(t));
61 if (r < 0)
62 return r;
eef85c4a 63 }
bba34eed 64
33e28180
LP
65 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
66 return 0;
67
b401e1fb 68 /* Make sure targets are unloaded on shutdown */
3835b9aa 69 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
a40eb732
LP
70}
71
72static int target_load(Unit *u) {
e9fa1bf7 73 Target *t = ASSERT_PTR(TARGET(u));
a40eb732
LP
74 int r;
75
c3620770 76 r = unit_load_fragment_and_dropin(u, true);
1850161f 77 if (r < 0)
a40eb732
LP
78 return r;
79
75193d41
ZJS
80 if (u->load_state != UNIT_LOADED)
81 return 0;
a40eb732 82
75193d41
ZJS
83 /* This is a new unit? Then let's add in some extras */
84 return target_add_default_dependencies(t);
a40eb732
LP
85}
86
be847e82 87static int target_coldplug(Unit *u) {
e9fa1bf7 88 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 89
a16e1123
LP
90 assert(t->state == TARGET_DEAD);
91
92 if (t->deserialized_state != t->state)
93 target_set_state(t, t->deserialized_state);
94
95 return 0;
96}
97
98static void target_dump(Unit *u, FILE *f, const char *prefix) {
e9fa1bf7 99 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 100
a16e1123 101 assert(f);
e9fa1bf7 102 assert(prefix);
a16e1123
LP
103
104 fprintf(f,
105 "%sTarget State: %s\n",
106 prefix, target_state_to_string(t->state));
107}
108
fa068367 109static int target_start(Unit *u) {
e9fa1bf7 110 Target *t = ASSERT_PTR(TARGET(u));
4b58153d 111 int r;
fa068367 112
fa068367
LP
113 assert(t->state == TARGET_DEAD);
114
4b58153d
LP
115 r = unit_acquire_invocation_id(u);
116 if (r < 0)
117 return r;
118
fa068367 119 target_set_state(t, TARGET_ACTIVE);
82a2b6bb 120 return 1;
fa068367 121}
c22cbe26 122
fa068367 123static int target_stop(Unit *u) {
e9fa1bf7 124 Target *t = ASSERT_PTR(TARGET(u));
fa068367 125
fa068367
LP
126 assert(t->state == TARGET_ACTIVE);
127
128 target_set_state(t, TARGET_DEAD);
82a2b6bb 129 return 1;
c22cbe26
LP
130}
131
a16e1123 132static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
e9fa1bf7 133 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 134
a16e1123
LP
135 assert(f);
136 assert(fds);
137
e9fa1bf7 138 (void) serialize_item(f, "state", target_state_to_string(t->state));
a16e1123
LP
139 return 0;
140}
141
142static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
e9fa1bf7 143 Target *t = ASSERT_PTR(TARGET(u));
a16e1123 144
a16e1123
LP
145 assert(key);
146 assert(value);
147 assert(fds);
148
149 if (streq(key, "state")) {
150 TargetState state;
151
1850161f
LP
152 state = target_state_from_string(value);
153 if (state < 0)
e9fa1bf7 154 log_unit_debug(u, "Failed to parse state: %s", value);
a16e1123 155 else
e9fa1bf7 156 t->deserialized_state = state;
a16e1123
LP
157
158 } else
e9fa1bf7 159 log_unit_debug(u, "Unknown serialization key: %s", key);
a16e1123
LP
160
161 return 0;
162}
163
d1e8e8b5 164static UnitActiveState target_active_state(Unit *u) {
e9fa1bf7 165 Target *t = ASSERT_PTR(TARGET(u));
fa068367 166
e9fa1bf7 167 return state_translation_table[t->state];
c22cbe26
LP
168}
169
d1e8e8b5 170static const char *target_sub_state_to_string(Unit *u) {
e9fa1bf7 171 Target *t = ASSERT_PTR(TARGET(u));
10a94420 172
e9fa1bf7 173 return target_state_to_string(t->state);
10a94420
LP
174}
175
87f0e418 176const UnitVTable target_vtable = {
7d17cfbc 177 .object_size = sizeof(Target),
718db961 178
f975e971
LP
179 .sections =
180 "Unit\0"
181 "Target\0"
182 "Install\0",
c22cbe26 183
94d1ddbd
ZJS
184 .can_fail = true,
185
a40eb732 186 .load = target_load,
a16e1123 187 .coldplug = target_coldplug,
fa068367
LP
188
189 .dump = target_dump,
190
191 .start = target_start,
192 .stop = target_stop,
c22cbe26 193
a16e1123
LP
194 .serialize = target_serialize,
195 .deserialize_item = target_deserialize_item,
196
10a94420 197 .active_state = target_active_state,
4139c1b2
LP
198 .sub_state_to_string = target_sub_state_to_string,
199
c6918296
MS
200 .status_message_formats = {
201 .finished_start_job = {
202 [JOB_DONE] = "Reached target %s.",
c6918296
MS
203 },
204 .finished_stop_job = {
205 [JOB_DONE] = "Stopped target %s.",
206 },
207 },
b2d6bb5b
LP
208
209 .notify_supervisor = true,
c22cbe26 210};