]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
core: reduce scope of variants
[thirdparty/systemd.git] / src / core / target.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
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
39 static const UnitDependency deps[] = {
40 UNIT_REQUIRES,
41 UNIT_REQUISITE,
42 UNIT_WANTS,
43 UNIT_BINDS_TO,
44 UNIT_PART_OF
45 };
46
47 int r;
48 unsigned k;
49
50 assert(t);
51
52 if (!UNIT(t)->default_dependencies)
53 return 0;
54
55 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
56 * ordering manually we won't add anything in here to make sure we don't create a loop. */
57
58 for (k = 0; k < ELEMENTSOF(deps); k++) {
59 Unit *other;
60 void *v;
61
62 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]]) {
63 r = unit_add_default_target_dependency(other, UNIT(t));
64 if (r < 0)
65 return r;
66 }
67 }
68
69 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
70 return 0;
71
72 /* Make sure targets are unloaded on shutdown */
73 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
74 }
75
76 static int target_load(Unit *u) {
77 Target *t = TARGET(u);
78 int r;
79
80 assert(t);
81
82 r = unit_load_fragment_and_dropin(u, true);
83 if (r < 0)
84 return r;
85
86 if (u->load_state != UNIT_LOADED)
87 return 0;
88
89 /* This is a new unit? Then let's add in some extras */
90 return target_add_default_dependencies(t);
91 }
92
93 static int target_coldplug(Unit *u) {
94 Target *t = TARGET(u);
95
96 assert(t);
97 assert(t->state == TARGET_DEAD);
98
99 if (t->deserialized_state != t->state)
100 target_set_state(t, t->deserialized_state);
101
102 return 0;
103 }
104
105 static void target_dump(Unit *u, FILE *f, const char *prefix) {
106 Target *t = TARGET(u);
107
108 assert(t);
109 assert(f);
110
111 fprintf(f,
112 "%sTarget State: %s\n",
113 prefix, target_state_to_string(t->state));
114 }
115
116 static int target_start(Unit *u) {
117 Target *t = TARGET(u);
118 int r;
119
120 assert(t);
121 assert(t->state == TARGET_DEAD);
122
123 r = unit_acquire_invocation_id(u);
124 if (r < 0)
125 return r;
126
127 target_set_state(t, TARGET_ACTIVE);
128 return 1;
129 }
130
131 static int target_stop(Unit *u) {
132 Target *t = TARGET(u);
133
134 assert(t);
135 assert(t->state == TARGET_ACTIVE);
136
137 target_set_state(t, TARGET_DEAD);
138 return 1;
139 }
140
141 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
142 Target *s = TARGET(u);
143
144 assert(s);
145 assert(f);
146 assert(fds);
147
148 (void) serialize_item(f, "state", target_state_to_string(s->state));
149 return 0;
150 }
151
152 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
153 Target *s = TARGET(u);
154
155 assert(u);
156 assert(key);
157 assert(value);
158 assert(fds);
159
160 if (streq(key, "state")) {
161 TargetState state;
162
163 state = target_state_from_string(value);
164 if (state < 0)
165 log_debug("Failed to parse state value %s", value);
166 else
167 s->deserialized_state = state;
168
169 } else
170 log_debug("Unknown serialization key '%s'", key);
171
172 return 0;
173 }
174
175 _pure_ static UnitActiveState target_active_state(Unit *u) {
176 assert(u);
177
178 return state_translation_table[TARGET(u)->state];
179 }
180
181 _pure_ static const char *target_sub_state_to_string(Unit *u) {
182 assert(u);
183
184 return target_state_to_string(TARGET(u)->state);
185 }
186
187 const UnitVTable target_vtable = {
188 .object_size = sizeof(Target),
189
190 .sections =
191 "Unit\0"
192 "Target\0"
193 "Install\0",
194
195 .can_fail = true,
196
197 .load = target_load,
198 .coldplug = target_coldplug,
199
200 .dump = target_dump,
201
202 .start = target_start,
203 .stop = target_stop,
204
205 .serialize = target_serialize,
206 .deserialize_item = target_deserialize_item,
207
208 .active_state = target_active_state,
209 .sub_state_to_string = target_sub_state_to_string,
210
211 .status_message_formats = {
212 .finished_start_job = {
213 [JOB_DONE] = "Reached target %s.",
214 },
215 .finished_stop_job = {
216 [JOB_DONE] = "Stopped target %s.",
217 },
218 },
219 };