]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 LP |
47 | int r; |
48 | ||
98bc2000 | 49 | assert(t); |
a40eb732 | 50 | |
41c237af IP |
51 | if (!UNIT(t)->default_dependencies) |
52 | return 0; | |
53 | ||
eef85c4a LP |
54 | /* Imply ordering for requirement dependencies on target units. Note that when the user created a contradicting |
55 | * ordering manually we won't add anything in here to make sure we don't create a loop. */ | |
bba34eed | 56 | |
fe96c0f8 | 57 | for (size_t k = 0; k < ELEMENTSOF(deps); k++) { |
eef85c4a | 58 | Unit *other; |
eef85c4a LP |
59 | void *v; |
60 | ||
90e74a66 | 61 | HASHMAP_FOREACH_KEY(v, other, UNIT(t)->dependencies[deps[k]]) { |
1850161f LP |
62 | r = unit_add_default_target_dependency(other, UNIT(t)); |
63 | if (r < 0) | |
21256a2b | 64 | return r; |
1850161f | 65 | } |
eef85c4a | 66 | } |
bba34eed | 67 | |
33e28180 LP |
68 | if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET)) |
69 | return 0; | |
70 | ||
b401e1fb | 71 | /* Make sure targets are unloaded on shutdown */ |
5a724170 | 72 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
73 | } |
74 | ||
75 | static int target_load(Unit *u) { | |
76 | Target *t = TARGET(u); | |
77 | int r; | |
78 | ||
79 | assert(t); | |
80 | ||
c3620770 | 81 | r = unit_load_fragment_and_dropin(u, true); |
1850161f | 82 | if (r < 0) |
a40eb732 LP |
83 | return r; |
84 | ||
75193d41 ZJS |
85 | if (u->load_state != UNIT_LOADED) |
86 | return 0; | |
a40eb732 | 87 | |
75193d41 ZJS |
88 | /* This is a new unit? Then let's add in some extras */ |
89 | return target_add_default_dependencies(t); | |
a40eb732 LP |
90 | } |
91 | ||
be847e82 | 92 | static int target_coldplug(Unit *u) { |
a16e1123 LP |
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 | ||
fa068367 LP |
115 | static int target_start(Unit *u) { |
116 | Target *t = TARGET(u); | |
4b58153d | 117 | int r; |
fa068367 LP |
118 | |
119 | assert(t); | |
120 | assert(t->state == TARGET_DEAD); | |
121 | ||
4b58153d LP |
122 | r = unit_acquire_invocation_id(u); |
123 | if (r < 0) | |
124 | return r; | |
125 | ||
fa068367 | 126 | target_set_state(t, TARGET_ACTIVE); |
82a2b6bb | 127 | return 1; |
fa068367 | 128 | } |
c22cbe26 | 129 | |
fa068367 LP |
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); | |
82a2b6bb | 137 | return 1; |
c22cbe26 LP |
138 | } |
139 | ||
a16e1123 LP |
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 | ||
d68c645b | 147 | (void) serialize_item(f, "state", target_state_to_string(s->state)); |
a16e1123 LP |
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 | ||
1850161f LP |
162 | state = target_state_from_string(value); |
163 | if (state < 0) | |
a16e1123 LP |
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 | ||
44a6b1b6 | 174 | _pure_ static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
175 | assert(u); |
176 | ||
177 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
178 | } |
179 | ||
44a6b1b6 | 180 | _pure_ static const char *target_sub_state_to_string(Unit *u) { |
10a94420 LP |
181 | assert(u); |
182 | ||
a16e1123 | 183 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
184 | } |
185 | ||
87f0e418 | 186 | const UnitVTable target_vtable = { |
7d17cfbc | 187 | .object_size = sizeof(Target), |
718db961 | 188 | |
f975e971 LP |
189 | .sections = |
190 | "Unit\0" | |
191 | "Target\0" | |
192 | "Install\0", | |
c22cbe26 | 193 | |
94d1ddbd ZJS |
194 | .can_fail = true, |
195 | ||
a40eb732 | 196 | .load = target_load, |
a16e1123 | 197 | .coldplug = target_coldplug, |
fa068367 LP |
198 | |
199 | .dump = target_dump, | |
200 | ||
201 | .start = target_start, | |
202 | .stop = target_stop, | |
c22cbe26 | 203 | |
a16e1123 LP |
204 | .serialize = target_serialize, |
205 | .deserialize_item = target_deserialize_item, | |
206 | ||
10a94420 | 207 | .active_state = target_active_state, |
4139c1b2 LP |
208 | .sub_state_to_string = target_sub_state_to_string, |
209 | ||
c6918296 MS |
210 | .status_message_formats = { |
211 | .finished_start_job = { | |
212 | [JOB_DONE] = "Reached target %s.", | |
c6918296 MS |
213 | }, |
214 | .finished_stop_job = { | |
215 | [JOB_DONE] = "Stopped target %s.", | |
216 | }, | |
217 | }, | |
c22cbe26 | 218 | }; |