]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/target.c
Merge pull request #15265 from fbuihuu/mount-fixes
[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 Iterator i;
61 void *v;
62
63 HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]], i) {
64 r = unit_add_default_target_dependency(other, UNIT(t));
65 if (r < 0)
66 return r;
67 }
68 }
69
70 if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET))
71 return 0;
72
73 /* Make sure targets are unloaded on shutdown */
74 return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT);
75 }
76
77 static int target_load(Unit *u) {
78 Target *t = TARGET(u);
79 int r;
80
81 assert(t);
82
83 r = unit_load_fragment_and_dropin(u, true);
84 if (r < 0)
85 return r;
86
87 if (u->load_state != UNIT_LOADED)
88 return 0;
89
90 /* This is a new unit? Then let's add in some extras */
91 return target_add_default_dependencies(t);
92 }
93
94 static int target_coldplug(Unit *u) {
95 Target *t = TARGET(u);
96
97 assert(t);
98 assert(t->state == TARGET_DEAD);
99
100 if (t->deserialized_state != t->state)
101 target_set_state(t, t->deserialized_state);
102
103 return 0;
104 }
105
106 static void target_dump(Unit *u, FILE *f, const char *prefix) {
107 Target *t = TARGET(u);
108
109 assert(t);
110 assert(f);
111
112 fprintf(f,
113 "%sTarget State: %s\n",
114 prefix, target_state_to_string(t->state));
115 }
116
117 static int target_start(Unit *u) {
118 Target *t = TARGET(u);
119 int r;
120
121 assert(t);
122 assert(t->state == TARGET_DEAD);
123
124 r = unit_acquire_invocation_id(u);
125 if (r < 0)
126 return r;
127
128 target_set_state(t, TARGET_ACTIVE);
129 return 1;
130 }
131
132 static int target_stop(Unit *u) {
133 Target *t = TARGET(u);
134
135 assert(t);
136 assert(t->state == TARGET_ACTIVE);
137
138 target_set_state(t, TARGET_DEAD);
139 return 1;
140 }
141
142 static int target_serialize(Unit *u, FILE *f, FDSet *fds) {
143 Target *s = TARGET(u);
144
145 assert(s);
146 assert(f);
147 assert(fds);
148
149 (void) serialize_item(f, "state", target_state_to_string(s->state));
150 return 0;
151 }
152
153 static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
154 Target *s = TARGET(u);
155
156 assert(u);
157 assert(key);
158 assert(value);
159 assert(fds);
160
161 if (streq(key, "state")) {
162 TargetState state;
163
164 state = target_state_from_string(value);
165 if (state < 0)
166 log_debug("Failed to parse state value %s", value);
167 else
168 s->deserialized_state = state;
169
170 } else
171 log_debug("Unknown serialization key '%s'", key);
172
173 return 0;
174 }
175
176 _pure_ static UnitActiveState target_active_state(Unit *u) {
177 assert(u);
178
179 return state_translation_table[TARGET(u)->state];
180 }
181
182 _pure_ static const char *target_sub_state_to_string(Unit *u) {
183 assert(u);
184
185 return target_state_to_string(TARGET(u)->state);
186 }
187
188 const UnitVTable target_vtable = {
189 .object_size = sizeof(Target),
190
191 .sections =
192 "Unit\0"
193 "Target\0"
194 "Install\0",
195
196 .load = target_load,
197 .coldplug = target_coldplug,
198
199 .dump = target_dump,
200
201 .start = target_start,
202 .stop = target_stop,
203
204 .serialize = target_serialize,
205 .deserialize_item = target_deserialize_item,
206
207 .active_state = target_active_state,
208 .sub_state_to_string = target_sub_state_to_string,
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 };