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