]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
Merge pull request #21264 from medhefgo/boot-lto
[thirdparty/systemd.git] / src / core / target.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "dbus-target.h"
4 #include "dbus-unit.h"
5 #include "log.h"
6 #include "serialize.h"
7 #include "special.h"
8 #include "string-util.h"
9 #include "target.h"
10 #include "unit-name.h"
11 #include "unit.h"
12
13 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
14 [TARGET_DEAD] = UNIT_INACTIVE,
15 [TARGET_ACTIVE] = UNIT_ACTIVE
16 };
17
18 static void target_set_state(Target *t, TargetState state) {
19 TargetState old_state;
20 assert(t);
21
22 if (t->state != state)
23 bus_unit_send_pending_change_signal(UNIT(t), false);
24
25 old_state = t->state;
26 t->state = state;
27
28 if (state != old_state)
29 log_debug("%s changed %s -> %s",
30 UNIT(t)->id,
31 target_state_to_string(old_state),
32 target_state_to_string(state));
33
34 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
35 }
36
37 static int target_add_default_dependencies(Target *t) {
38 _cleanup_free_ Unit **others = NULL;
39 int r, n_others;
40
41 assert(t);
42
43 if (!UNIT(t)->default_dependencies)
44 return 0;
45
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;
63 }
64
65 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
66 return 0;
67
68 /* Make sure targets are unloaded on shutdown */
69 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
70 }
71
72 static int target_load(Unit *u) {
73 Target *t = TARGET(u);
74 int r;
75
76 assert(t);
77
78 r = unit_load_fragment_and_dropin(u, true);
79 if (r < 0)
80 return r;
81
82 if (u->load_state != UNIT_LOADED)
83 return 0;
84
85 /* This is a new unit? Then let's add in some extras */
86 return target_add_default_dependencies(t);
87 }
88
89 static int target_coldplug(Unit *u) {
90 Target *t = TARGET(u);
91
92 assert(t);
93 assert(t->state == TARGET_DEAD);
94
95 if (t->deserialized_state != t->state)
96 target_set_state(t, t->deserialized_state);
97
98 return 0;
99 }
100
101 static void target_dump(Unit *u, FILE *f, const char *prefix) {
102 Target *t = TARGET(u);
103
104 assert(t);
105 assert(f);
106
107 fprintf(f,
108 "%sTarget State: %s\n",
109 prefix, target_state_to_string(t->state));
110 }
111
112 static int target_start(Unit *u) {
113 Target *t = TARGET(u);
114 int r;
115
116 assert(t);
117 assert(t->state == TARGET_DEAD);
118
119 r = unit_acquire_invocation_id(u);
120 if (r < 0)
121 return r;
122
123 target_set_state(t, TARGET_ACTIVE);
124 return 1;
125 }
126
127 static int target_stop(Unit *u) {
128 Target *t = TARGET(u);
129
130 assert(t);
131 assert(t->state == TARGET_ACTIVE);
132
133 target_set_state(t, TARGET_DEAD);
134 return 1;
135 }
136
137 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
138 Target *s = TARGET(u);
139
140 assert(s);
141 assert(f);
142 assert(fds);
143
144 (void) serialize_item(f, "state", target_state_to_string(s->state));
145 return 0;
146 }
147
148 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
149 Target *s = TARGET(u);
150
151 assert(s);
152 assert(u);
153 assert(key);
154 assert(value);
155 assert(fds);
156
157 if (streq(key, "state")) {
158 TargetState state;
159
160 state = target_state_from_string(value);
161 if (state < 0)
162 log_debug("Failed to parse state value %s", value);
163 else
164 s->deserialized_state = state;
165
166 } else
167 log_debug("Unknown serialization key '%s'", key);
168
169 return 0;
170 }
171
172 _pure_ static UnitActiveState target_active_state(Unit *u) {
173 assert(u);
174
175 return state_translation_table[TARGET(u)->state];
176 }
177
178 _pure_ static const char *target_sub_state_to_string(Unit *u) {
179 assert(u);
180
181 return target_state_to_string(TARGET(u)->state);
182 }
183
184 const UnitVTable target_vtable = {
185 .object_size = sizeof(Target),
186
187 .sections =
188 "Unit\0"
189 "Target\0"
190 "Install\0",
191
192 .can_fail = true,
193
194 .load = target_load,
195 .coldplug = target_coldplug,
196
197 .dump = target_dump,
198
199 .start = target_start,
200 .stop = target_stop,
201
202 .serialize = target_serialize,
203 .deserialize_item = target_deserialize_item,
204
205 .active_state = target_active_state,
206 .sub_state_to_string = target_sub_state_to_string,
207
208 .status_message_formats = {
209 .finished_start_job = {
210 [JOB_DONE] = "Reached target %s.",
211 },
212 .finished_stop_job = {
213 [JOB_DONE] = "Stopped target %s.",
214 },
215 },
216 };