]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
4139c1b2 | 3 | #include "dbus-target.h" |
6fcbec6f | 4 | #include "dbus-unit.h" |
07630cea | 5 | #include "log.h" |
d68c645b | 6 | #include "serialize.h" |
514f4ef5 | 7 | #include "special.h" |
07630cea | 8 | #include "string-util.h" |
d68c645b | 9 | #include "target.h" |
b2bb3dbe | 10 | #include "unit-name.h" |
07630cea | 11 | #include "unit.h" |
c22cbe26 | 12 | |
fa068367 LP |
13 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
14 | [TARGET_DEAD] = UNIT_INACTIVE, | |
15 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
16 | }; | |
17 | ||
fa068367 LP |
18 | static void target_set_state(Target *t, TargetState state) { |
19 | TargetState old_state; | |
20 | assert(t); | |
21 | ||
6fcbec6f LP |
22 | if (t->state != state) |
23 | bus_unit_send_pending_change_signal(UNIT(t), false); | |
24 | ||
fa068367 LP |
25 | old_state = t->state; |
26 | t->state = state; | |
27 | ||
e537352b | 28 | if (state != old_state) |
40d50879 | 29 | log_debug("%s changed %s -> %s", |
1124fe6f | 30 | UNIT(t)->id, |
a16e1123 LP |
31 | target_state_to_string(old_state), |
32 | target_state_to_string(state)); | |
c22cbe26 | 33 | |
2ad2e41a | 34 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0); |
fa068367 LP |
35 | } |
36 | ||
a40eb732 | 37 | static int target_add_default_dependencies(Target *t) { |
1850161f | 38 | |
21256a2b LP |
39 | static const UnitDependency deps[] = { |
40 | UNIT_REQUIRES, | |
21256a2b | 41 | UNIT_REQUISITE, |
21256a2b | 42 | UNIT_WANTS, |
1850161f LP |
43 | UNIT_BINDS_TO, |
44 | UNIT_PART_OF | |
21256a2b LP |
45 | }; |
46 | ||
bba34eed | 47 | int r; |
21256a2b | 48 | unsigned k; |
bba34eed | 49 | |
98bc2000 | 50 | assert(t); |
a40eb732 | 51 | |
41c237af IP |
52 | if (!UNIT(t)->default_dependencies) |
53 | return 0; | |
54 | ||
eef85c4a LP |
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. */ | |
bba34eed | 57 | |
eef85c4a LP |
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) { | |
1850161f LP |
64 | r = unit_add_default_target_dependency(other, UNIT(t)); |
65 | if (r < 0) | |
21256a2b | 66 | return r; |
1850161f | 67 | } |
eef85c4a | 68 | } |
bba34eed | 69 | |
33e28180 LP |
70 | if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET)) |
71 | return 0; | |
72 | ||
b401e1fb | 73 | /* Make sure targets are unloaded on shutdown */ |
5a724170 | 74 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
75 | } |
76 | ||
77 | static int target_load(Unit *u) { | |
78 | Target *t = TARGET(u); | |
79 | int r; | |
80 | ||
81 | assert(t); | |
82 | ||
1850161f LP |
83 | r = unit_load_fragment_and_dropin(u); |
84 | if (r < 0) | |
a40eb732 LP |
85 | return r; |
86 | ||
87 | /* This is a new unit? Then let's add in some extras */ | |
41c237af | 88 | if (u->load_state == UNIT_LOADED) { |
1850161f LP |
89 | r = target_add_default_dependencies(t); |
90 | if (r < 0) | |
91 | return r; | |
a40eb732 LP |
92 | } |
93 | ||
94 | return 0; | |
95 | } | |
96 | ||
be847e82 | 97 | static int target_coldplug(Unit *u) { |
a16e1123 LP |
98 | Target *t = TARGET(u); |
99 | ||
100 | assert(t); | |
101 | assert(t->state == TARGET_DEAD); | |
102 | ||
103 | if (t->deserialized_state != t->state) | |
104 | target_set_state(t, t->deserialized_state); | |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
109 | static void target_dump(Unit *u, FILE *f, const char *prefix) { | |
110 | Target *t = TARGET(u); | |
111 | ||
112 | assert(t); | |
113 | assert(f); | |
114 | ||
115 | fprintf(f, | |
116 | "%sTarget State: %s\n", | |
117 | prefix, target_state_to_string(t->state)); | |
118 | } | |
119 | ||
fa068367 LP |
120 | static int target_start(Unit *u) { |
121 | Target *t = TARGET(u); | |
4b58153d | 122 | int r; |
fa068367 LP |
123 | |
124 | assert(t); | |
125 | assert(t->state == TARGET_DEAD); | |
126 | ||
4b58153d LP |
127 | r = unit_acquire_invocation_id(u); |
128 | if (r < 0) | |
129 | return r; | |
130 | ||
fa068367 | 131 | target_set_state(t, TARGET_ACTIVE); |
82a2b6bb | 132 | return 1; |
fa068367 | 133 | } |
c22cbe26 | 134 | |
fa068367 LP |
135 | static int target_stop(Unit *u) { |
136 | Target *t = TARGET(u); | |
137 | ||
138 | assert(t); | |
139 | assert(t->state == TARGET_ACTIVE); | |
140 | ||
141 | target_set_state(t, TARGET_DEAD); | |
82a2b6bb | 142 | return 1; |
c22cbe26 LP |
143 | } |
144 | ||
a16e1123 LP |
145 | static int target_serialize(Unit *u, FILE *f, FDSet *fds) { |
146 | Target *s = TARGET(u); | |
147 | ||
148 | assert(s); | |
149 | assert(f); | |
150 | assert(fds); | |
151 | ||
d68c645b | 152 | (void) serialize_item(f, "state", target_state_to_string(s->state)); |
a16e1123 LP |
153 | return 0; |
154 | } | |
155 | ||
156 | static int target_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
157 | Target *s = TARGET(u); | |
158 | ||
159 | assert(u); | |
160 | assert(key); | |
161 | assert(value); | |
162 | assert(fds); | |
163 | ||
164 | if (streq(key, "state")) { | |
165 | TargetState state; | |
166 | ||
1850161f LP |
167 | state = target_state_from_string(value); |
168 | if (state < 0) | |
a16e1123 LP |
169 | log_debug("Failed to parse state value %s", value); |
170 | else | |
171 | s->deserialized_state = state; | |
172 | ||
173 | } else | |
174 | log_debug("Unknown serialization key '%s'", key); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
44a6b1b6 | 179 | _pure_ static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
180 | assert(u); |
181 | ||
182 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
183 | } |
184 | ||
44a6b1b6 | 185 | _pure_ static const char *target_sub_state_to_string(Unit *u) { |
10a94420 LP |
186 | assert(u); |
187 | ||
a16e1123 | 188 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
189 | } |
190 | ||
87f0e418 | 191 | const UnitVTable target_vtable = { |
7d17cfbc | 192 | .object_size = sizeof(Target), |
718db961 | 193 | |
f975e971 LP |
194 | .sections = |
195 | "Unit\0" | |
196 | "Target\0" | |
197 | "Install\0", | |
c22cbe26 | 198 | |
a40eb732 | 199 | .load = target_load, |
a16e1123 | 200 | .coldplug = target_coldplug, |
fa068367 LP |
201 | |
202 | .dump = target_dump, | |
203 | ||
204 | .start = target_start, | |
205 | .stop = target_stop, | |
c22cbe26 | 206 | |
a16e1123 LP |
207 | .serialize = target_serialize, |
208 | .deserialize_item = target_deserialize_item, | |
209 | ||
10a94420 | 210 | .active_state = target_active_state, |
4139c1b2 LP |
211 | .sub_state_to_string = target_sub_state_to_string, |
212 | ||
718db961 | 213 | .bus_vtable = bus_target_vtable, |
c6918296 MS |
214 | |
215 | .status_message_formats = { | |
216 | .finished_start_job = { | |
217 | [JOB_DONE] = "Reached target %s.", | |
c6918296 MS |
218 | }, |
219 | .finished_stop_job = { | |
220 | [JOB_DONE] = "Stopped target %s.", | |
221 | }, | |
222 | }, | |
c22cbe26 | 223 | }; |