]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
core: add IgnoreOnSoftReboot= unit option
[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], /* reload_success = */ true);
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 if (!UNIT(t)->ignore_on_soft_reboot)
70 return unit_add_two_dependencies_by_name(
71 UNIT(t),
72 UNIT_BEFORE, UNIT_CONFLICTS,
73 SPECIAL_SHUTDOWN_TARGET, true,
74 UNIT_DEPENDENCY_DEFAULT);
75
76 /* Unless we are meant to survive soft reboot, in which case we need to conflict with
77 * non-soft-reboot targets. */
78 return unit_add_dependencies_on_real_shutdown_targets(UNIT(t));
79 }
80
81 static int target_load(Unit *u) {
82 Target *t = TARGET(u);
83 int r;
84
85 assert(t);
86
87 r = unit_load_fragment_and_dropin(u, true);
88 if (r < 0)
89 return r;
90
91 if (u->load_state != UNIT_LOADED)
92 return 0;
93
94 /* This is a new unit? Then let's add in some extras */
95 return target_add_default_dependencies(t);
96 }
97
98 static int target_coldplug(Unit *u) {
99 Target *t = TARGET(u);
100
101 assert(t);
102 assert(t->state == TARGET_DEAD);
103
104 if (t->deserialized_state != t->state)
105 target_set_state(t, t->deserialized_state);
106
107 return 0;
108 }
109
110 static void target_dump(Unit *u, FILE *f, const char *prefix) {
111 Target *t = TARGET(u);
112
113 assert(t);
114 assert(f);
115
116 fprintf(f,
117 "%sTarget State: %s\n",
118 prefix, target_state_to_string(t->state));
119 }
120
121 static int target_start(Unit *u) {
122 Target *t = TARGET(u);
123 int r;
124
125 assert(t);
126 assert(t->state == TARGET_DEAD);
127
128 r = unit_acquire_invocation_id(u);
129 if (r < 0)
130 return r;
131
132 target_set_state(t, TARGET_ACTIVE);
133 return 1;
134 }
135
136 static int target_stop(Unit *u) {
137 Target *t = TARGET(u);
138
139 assert(t);
140 assert(t->state == TARGET_ACTIVE);
141
142 target_set_state(t, TARGET_DEAD);
143 return 1;
144 }
145
146 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
147 Target *s = TARGET(u);
148
149 assert(s);
150 assert(f);
151 assert(fds);
152
153 (void) serialize_item(f, "state", target_state_to_string(s->state));
154 return 0;
155 }
156
157 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
158 Target *s = TARGET(u);
159
160 assert(s);
161 assert(u);
162 assert(key);
163 assert(value);
164 assert(fds);
165
166 if (streq(key, "state")) {
167 TargetState state;
168
169 state = target_state_from_string(value);
170 if (state < 0)
171 log_debug("Failed to parse state value %s", value);
172 else
173 s->deserialized_state = state;
174
175 } else
176 log_debug("Unknown serialization key '%s'", key);
177
178 return 0;
179 }
180
181 _pure_ static UnitActiveState target_active_state(Unit *u) {
182 assert(u);
183
184 return state_translation_table[TARGET(u)->state];
185 }
186
187 _pure_ static const char *target_sub_state_to_string(Unit *u) {
188 assert(u);
189
190 return target_state_to_string(TARGET(u)->state);
191 }
192
193 const UnitVTable target_vtable = {
194 .object_size = sizeof(Target),
195
196 .sections =
197 "Unit\0"
198 "Target\0"
199 "Install\0",
200
201 .can_fail = true,
202
203 .load = target_load,
204 .coldplug = target_coldplug,
205
206 .dump = target_dump,
207
208 .start = target_start,
209 .stop = target_stop,
210
211 .serialize = target_serialize,
212 .deserialize_item = target_deserialize_item,
213
214 .active_state = target_active_state,
215 .sub_state_to_string = target_sub_state_to_string,
216
217 .status_message_formats = {
218 .finished_start_job = {
219 [JOB_DONE] = "Reached target %s.",
220 },
221 .finished_stop_job = {
222 [JOB_DONE] = "Stopped target %s.",
223 },
224 },
225 };