]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
tree-wide: remove Lennart's copyright lines
[thirdparty/systemd.git] / src / core / target.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include "dbus-target.h"
4 #include "log.h"
5 #include "special.h"
6 #include "string-util.h"
7 #include "unit-name.h"
8 #include "unit.h"
9 #include "target.h"
10
11 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
12 [TARGET_DEAD] = UNIT_INACTIVE,
13 [TARGET_ACTIVE] = UNIT_ACTIVE
14 };
15
16 static void target_set_state(Target *t, TargetState state) {
17 TargetState old_state;
18 assert(t);
19
20 old_state = t->state;
21 t->state = state;
22
23 if (state != old_state)
24 log_debug("%s changed %s -> %s",
25 UNIT(t)->id,
26 target_state_to_string(old_state),
27 target_state_to_string(state));
28
29 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
30 }
31
32 static int target_add_default_dependencies(Target *t) {
33
34 static const UnitDependency deps[] = {
35 UNIT_REQUIRES,
36 UNIT_REQUISITE,
37 UNIT_WANTS,
38 UNIT_BINDS_TO,
39 UNIT_PART_OF
40 };
41
42 int r;
43 unsigned k;
44
45 assert(t);
46
47 if (!UNIT(t)->default_dependencies)
48 return 0;
49
50 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
51 * ordering manually we won't add anything in here to make sure we don't create a loop. */
52
53 for (k = 0; k < ELEMENTSOF(deps); k++) {
54 Unit *other;
55 Iterator i;
56 void *v;
57
58 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
59 r = unit_add_default_target_dependency(other, UNIT(t));
60 if (r < 0)
61 return r;
62 }
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, NULL, 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);
79 if (r < 0)
80 return r;
81
82 /* This is a new unit? Then let's add in some extras */
83 if (u->load_state == UNIT_LOADED) {
84 r = target_add_default_dependencies(t);
85 if (r < 0)
86 return r;
87 }
88
89 return 0;
90 }
91
92 static int target_coldplug(Unit *u) {
93 Target *t = TARGET(u);
94
95 assert(t);
96 assert(t->state == TARGET_DEAD);
97
98 if (t->deserialized_state != t->state)
99 target_set_state(t, t->deserialized_state);
100
101 return 0;
102 }
103
104 static void target_dump(Unit *u, FILE *f, const char *prefix) {
105 Target *t = TARGET(u);
106
107 assert(t);
108 assert(f);
109
110 fprintf(f,
111 "%sTarget State: %s\n",
112 prefix, target_state_to_string(t->state));
113 }
114
115 static int target_start(Unit *u) {
116 Target *t = TARGET(u);
117 int r;
118
119 assert(t);
120 assert(t->state == TARGET_DEAD);
121
122 r = unit_acquire_invocation_id(u);
123 if (r < 0)
124 return r;
125
126 target_set_state(t, TARGET_ACTIVE);
127 return 1;
128 }
129
130 static int target_stop(Unit *u) {
131 Target *t = TARGET(u);
132
133 assert(t);
134 assert(t->state == TARGET_ACTIVE);
135
136 target_set_state(t, TARGET_DEAD);
137 return 1;
138 }
139
140 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
141 Target *s = TARGET(u);
142
143 assert(s);
144 assert(f);
145 assert(fds);
146
147 unit_serialize_item(u, f, "state", target_state_to_string(s->state));
148 return 0;
149 }
150
151 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
152 Target *s = TARGET(u);
153
154 assert(u);
155 assert(key);
156 assert(value);
157 assert(fds);
158
159 if (streq(key, "state")) {
160 TargetState state;
161
162 state = target_state_from_string(value);
163 if (state < 0)
164 log_debug("Failed to parse state value %s", value);
165 else
166 s->deserialized_state = state;
167
168 } else
169 log_debug("Unknown serialization key '%s'", key);
170
171 return 0;
172 }
173
174 _pure_ static UnitActiveState target_active_state(Unit *u) {
175 assert(u);
176
177 return state_translation_table[TARGET(u)->state];
178 }
179
180 _pure_ static const char *target_sub_state_to_string(Unit *u) {
181 assert(u);
182
183 return target_state_to_string(TARGET(u)->state);
184 }
185
186 const UnitVTable target_vtable = {
187 .object_size = sizeof(Target),
188
189 .sections =
190 "Unit\0"
191 "Target\0"
192 "Install\0",
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 .bus_vtable = bus_target_vtable,
209
210 .status_message_formats = {
211 .finished_start_job = {
212 [JOB_DONE] = "Reached target %s.",
213 },
214 .finished_stop_job = {
215 [JOB_DONE] = "Stopped target %s.",
216 },
217 },
218 };