]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[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 "serialize.h"
6 #include "special.h"
7 #include "string-util.h"
8 #include "target.h"
9 #include "unit-name.h"
10 #include "unit.h"
11
12 static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = {
13 [TARGET_DEAD] = UNIT_INACTIVE,
14 [TARGET_ACTIVE] = UNIT_ACTIVE
15 };
16
17 static void target_set_state(Target *t, TargetState state) {
18 TargetState old_state;
19 assert(t);
20
21 old_state = t->state;
22 t->state = state;
23
24 if (state != old_state)
25 log_debug("%s changed %s -> %s",
26 UNIT(t)->id,
27 target_state_to_string(old_state),
28 target_state_to_string(state));
29
30 unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0);
31 }
32
33 static int target_add_default_dependencies(Target *t) {
34
35 static const UnitDependency deps[] = {
36 UNIT_REQUIRES,
37 UNIT_REQUISITE,
38 UNIT_WANTS,
39 UNIT_BINDS_TO,
40 UNIT_PART_OF
41 };
42
43 int r;
44 unsigned k;
45
46 assert(t);
47
48 if (!UNIT(t)->default_dependencies)
49 return 0;
50
51 /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting
52 * ordering manually we won't add anything in here to make sure we don't create a loop. */
53
54 for (k = 0; k < ELEMENTSOF(deps); k++) {
55 Unit *other;
56 Iterator i;
57 void *v;
58
59 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
60 r = unit_add_default_target_dependency(other, UNIT(t));
61 if (r < 0)
62 return r;
63 }
64 }
65
66 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
67 return 0;
68
69 /* Make sure targets are unloaded on shutdown */
70 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
71 }
72
73 static int target_load(Unit *u) {
74 Target *t = TARGET(u);
75 int r;
76
77 assert(t);
78
79 r = unit_load_fragment_and_dropin(u);
80 if (r < 0)
81 return r;
82
83 /* This is a new unit? Then let's add in some extras */
84 if (u->load_state == UNIT_LOADED) {
85 r = target_add_default_dependencies(t);
86 if (r < 0)
87 return r;
88 }
89
90 return 0;
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 .load = target_load,
196 .coldplug = target_coldplug,
197
198 .dump = target_dump,
199
200 .start = target_start,
201 .stop = target_stop,
202
203 .serialize = target_serialize,
204 .deserialize_item = target_deserialize_item,
205
206 .active_state = target_active_state,
207 .sub_state_to_string = target_sub_state_to_string,
208
209 .bus_vtable = bus_target_vtable,
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 };