]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
a7334b09 | 2 | |
4139c1b2 | 3 | #include "dbus-target.h" |
07630cea | 4 | #include "log.h" |
d68c645b | 5 | #include "serialize.h" |
514f4ef5 | 6 | #include "special.h" |
07630cea | 7 | #include "string-util.h" |
d68c645b | 8 | #include "target.h" |
b2bb3dbe | 9 | #include "unit-name.h" |
07630cea | 10 | #include "unit.h" |
c22cbe26 | 11 | |
fa068367 LP |
12 | static const UnitActiveState state_translation_table[_TARGET_STATE_MAX] = { |
13 | [TARGET_DEAD] = UNIT_INACTIVE, | |
14 | [TARGET_ACTIVE] = UNIT_ACTIVE | |
15 | }; | |
16 | ||
fa068367 LP |
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 | ||
e537352b | 24 | if (state != old_state) |
40d50879 | 25 | log_debug("%s changed %s -> %s", |
1124fe6f | 26 | UNIT(t)->id, |
a16e1123 LP |
27 | target_state_to_string(old_state), |
28 | target_state_to_string(state)); | |
c22cbe26 | 29 | |
2ad2e41a | 30 | unit_notify(UNIT(t), state_translation_table[old_state], state_translation_table[state], 0); |
fa068367 LP |
31 | } |
32 | ||
a40eb732 | 33 | static int target_add_default_dependencies(Target *t) { |
1850161f | 34 | |
21256a2b LP |
35 | static const UnitDependency deps[] = { |
36 | UNIT_REQUIRES, | |
21256a2b | 37 | UNIT_REQUISITE, |
21256a2b | 38 | UNIT_WANTS, |
1850161f LP |
39 | UNIT_BINDS_TO, |
40 | UNIT_PART_OF | |
21256a2b LP |
41 | }; |
42 | ||
bba34eed | 43 | int r; |
21256a2b | 44 | unsigned k; |
bba34eed | 45 | |
98bc2000 | 46 | assert(t); |
a40eb732 | 47 | |
41c237af IP |
48 | if (!UNIT(t)->default_dependencies) |
49 | return 0; | |
50 | ||
eef85c4a LP |
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. */ | |
bba34eed | 53 | |
eef85c4a LP |
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) { | |
1850161f LP |
60 | r = unit_add_default_target_dependency(other, UNIT(t)); |
61 | if (r < 0) | |
21256a2b | 62 | return r; |
1850161f | 63 | } |
eef85c4a | 64 | } |
bba34eed | 65 | |
33e28180 LP |
66 | if (unit_has_name(UNIT(t), SPECIAL_SHUTDOWN_TARGET)) |
67 | return 0; | |
68 | ||
b401e1fb | 69 | /* Make sure targets are unloaded on shutdown */ |
5a724170 | 70 | return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
71 | } |
72 | ||
73 | static int target_load(Unit *u) { | |
74 | Target *t = TARGET(u); | |
75 | int r; | |
76 | ||
77 | assert(t); | |
78 | ||
1850161f LP |
79 | r = unit_load_fragment_and_dropin(u); |
80 | if (r < 0) | |
a40eb732 LP |
81 | return r; |
82 | ||
83 | /* This is a new unit? Then let's add in some extras */ | |
41c237af | 84 | if (u->load_state == UNIT_LOADED) { |
1850161f LP |
85 | r = target_add_default_dependencies(t); |
86 | if (r < 0) | |
87 | return r; | |
a40eb732 LP |
88 | } |
89 | ||
90 | return 0; | |
91 | } | |
92 | ||
be847e82 | 93 | static int target_coldplug(Unit *u) { |
a16e1123 LP |
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 | ||
fa068367 LP |
116 | static int target_start(Unit *u) { |
117 | Target *t = TARGET(u); | |
4b58153d | 118 | int r; |
fa068367 LP |
119 | |
120 | assert(t); | |
121 | assert(t->state == TARGET_DEAD); | |
122 | ||
4b58153d LP |
123 | r = unit_acquire_invocation_id(u); |
124 | if (r < 0) | |
125 | return r; | |
126 | ||
fa068367 | 127 | target_set_state(t, TARGET_ACTIVE); |
82a2b6bb | 128 | return 1; |
fa068367 | 129 | } |
c22cbe26 | 130 | |
fa068367 LP |
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); | |
82a2b6bb | 138 | return 1; |
c22cbe26 LP |
139 | } |
140 | ||
a16e1123 LP |
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 | ||
d68c645b | 148 | (void) serialize_item(f, "state", target_state_to_string(s->state)); |
a16e1123 LP |
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 | ||
1850161f LP |
163 | state = target_state_from_string(value); |
164 | if (state < 0) | |
a16e1123 LP |
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 | ||
44a6b1b6 | 175 | _pure_ static UnitActiveState target_active_state(Unit *u) { |
fa068367 LP |
176 | assert(u); |
177 | ||
178 | return state_translation_table[TARGET(u)->state]; | |
c22cbe26 LP |
179 | } |
180 | ||
44a6b1b6 | 181 | _pure_ static const char *target_sub_state_to_string(Unit *u) { |
10a94420 LP |
182 | assert(u); |
183 | ||
a16e1123 | 184 | return target_state_to_string(TARGET(u)->state); |
10a94420 LP |
185 | } |
186 | ||
87f0e418 | 187 | const UnitVTable target_vtable = { |
7d17cfbc | 188 | .object_size = sizeof(Target), |
718db961 | 189 | |
f975e971 LP |
190 | .sections = |
191 | "Unit\0" | |
192 | "Target\0" | |
193 | "Install\0", | |
c22cbe26 | 194 | |
a40eb732 | 195 | .load = target_load, |
a16e1123 | 196 | .coldplug = target_coldplug, |
fa068367 LP |
197 | |
198 | .dump = target_dump, | |
199 | ||
200 | .start = target_start, | |
201 | .stop = target_stop, | |
c22cbe26 | 202 | |
a16e1123 LP |
203 | .serialize = target_serialize, |
204 | .deserialize_item = target_deserialize_item, | |
205 | ||
10a94420 | 206 | .active_state = target_active_state, |
4139c1b2 LP |
207 | .sub_state_to_string = target_sub_state_to_string, |
208 | ||
718db961 | 209 | .bus_vtable = bus_target_vtable, |
c6918296 MS |
210 | |
211 | .status_message_formats = { | |
212 | .finished_start_job = { | |
213 | [JOB_DONE] = "Reached target %s.", | |
c6918296 MS |
214 | }, |
215 | .finished_stop_job = { | |
216 | [JOB_DONE] = "Stopped target %s.", | |
217 | }, | |
218 | }, | |
c22cbe26 | 219 | }; |