Debian Codegraph @

src/shared/bus-wait-for-units.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-bus.h"
4
5 #include "alloc-util.h"
6 #include "bus-error.h"
7 #include "bus-map-properties.h"
8 #include "bus-wait-for-units.h"
9 #include "hashmap.h"
10 #include "log.h"
11 #include "string-util.h"
12 #include "unit-def.h"
13
14 typedef struct WaitForItem {
15 BusWaitForUnits *parent;
16
17 BusWaitForUnitsFlags flags;
18
19 char *bus_path;
20
21 sd_bus_slot *slot_get_all;
22 sd_bus_slot *slot_properties_changed;
23
24 bus_wait_for_units_unit_callback_t unit_callback;
25 void *userdata;
26
27 char *active_state;
28 uint32_t job_id;
29 char *clean_result;
30 char *live_mount_result;
31 } WaitForItem;
32
33 typedef struct BusWaitForUnits {
34 sd_bus *bus;
35 sd_bus_slot *slot_disconnected;
36
37 Hashmap *items;
38
39 BusWaitForUnitsState state;
40 bool has_failed:1;
41 } BusWaitForUnits;
42
43 static WaitForItem* wait_for_item_free(WaitForItem *item) {
44 int r;
45
46 if (!item)
47 return NULL;
48
49 if (item->parent) {
50 if (FLAGS_SET(item->flags, BUS_WAIT_REFFED) && item->bus_path && item->parent->bus) {
51 r = sd_bus_call_method_async(
52 item->parent->bus,
53 NULL,
54 "org.freedesktop.systemd1",
55 item->bus_path,
56 "org.freedesktop.systemd1.Unit",
57 "Unref",
58 NULL,
59 NULL,
60 NULL);
61 if (r < 0)
62 log_debug_errno(r, "Failed to drop reference to unit %s, ignoring: %m", item->bus_path);
63 }
64
65 assert_se(hashmap_remove_value(item->parent->items, item->bus_path, item));
66 }
67
68 sd_bus_slot_unref(item->slot_properties_changed);
69 sd_bus_slot_unref(item->slot_get_all);
70
71 free(item->bus_path);
72 free(item->active_state);
73 free(item->clean_result);
74 free(item->live_mount_result);
75
76 return mfree(item);
77 }
78
79 DEFINE_TRIVIAL_CLEANUP_FUNC(WaitForItem*, wait_for_item_free);
80
81 static void call_unit_callback_and_wait(BusWaitForUnits *d, WaitForItem *item, bool good) {
82 if (item->unit_callback)
83 item->unit_callback(d, item->bus_path, good, item->userdata);
84
85 wait_for_item_free(item);
86 }
87
88 static void bus_wait_for_units_clear(BusWaitForUnits *d) {
89 WaitForItem *item;
90
91 assert(d);
92
93 d->slot_disconnected = sd_bus_slot_unref(d->slot_disconnected);
94 d->bus = sd_bus_unref(d->bus);
95
96 while ((item = hashmap_first(d->items)))
97 call_unit_callback_and_wait(d, item, false);
98
99 d->items = hashmap_free(d->items);
100 }
101
102 static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) {
103 BusWaitForUnits *d = ASSERT_PTR(userdata);
104
105 assert(m);
106
107 log_warning("D-Bus connection terminated while waiting for unit.");
108
109 bus_wait_for_units_clear(d);
110 sd_bus_close(sd_bus_message_get_bus(m));
111
112 return 0;
113 }
114
115 int bus_wait_for_units_new(sd_bus *bus, BusWaitForUnits **ret) {
116 _cleanup_(bus_wait_for_units_freep) BusWaitForUnits *d = NULL;
117 int r;
118
119 assert(bus);
120 assert(ret);
121
122 d = new(BusWaitForUnits, 1);
123 if (!d)
124 return -ENOMEM;
125
126 *d = (BusWaitForUnits) {
127 .state = BUS_WAIT_SUCCESS,
128 .bus = sd_bus_ref(bus),
129 };
130
131 r = sd_bus_match_signal_async(
132 bus,
133 &d->slot_disconnected,
134 "org.freedesktop.DBus.Local",
135 NULL,
136 "org.freedesktop.DBus.Local",
137 "Disconnected",
138 match_disconnected, NULL, d);
139 if (r < 0)
140 return r;
141
142 *ret = TAKE_PTR(d);
143 return 0;
144 }
145
146 BusWaitForUnits* bus_wait_for_units_free(BusWaitForUnits *d) {
147 if (!d)
148 return NULL;
149
150 bus_wait_for_units_clear(d);
151 sd_bus_slot_unref(d->slot_disconnected);
152 sd_bus_unref(d->bus);
153
154 return mfree(d);
155 }
156
157 static bool bus_wait_for_units_is_ready(BusWaitForUnits *d) {
158 assert(d);
159
160 if (!d->bus) /* Disconnected? */
161 return true;
162
163 return hashmap_isempty(d->items);
164 }
165
166 static void bus_wait_for_units_check_ready(BusWaitForUnits *d) {
167 assert(d);
168
169 if (!bus_wait_for_units_is_ready(d))
170 return;
171
172 d->state = d->has_failed ? BUS_WAIT_FAILURE : BUS_WAIT_SUCCESS;
173 }
174
175 static void wait_for_item_check_ready(WaitForItem *item) {
176 BusWaitForUnits *d;
177
178 assert(item);
179 assert_se(d = item->parent);
180
181 if (FLAGS_SET(item->flags, BUS_WAIT_FOR_MAINTENANCE_END)) {
182
183 if (item->clean_result && !streq(item->clean_result, "success"))
184 d->has_failed = true;
185
186 if (item->live_mount_result && !streq(item->live_mount_result, "success"))
187 d->has_failed = true;
188
189 if (!item->active_state || streq(item->active_state, "maintenance"))
190 return;
191 }
192
193 if (FLAGS_SET(item->flags, BUS_WAIT_NO_JOB) && item->job_id != 0)
194 return;
195
196 if (FLAGS_SET(item->flags, BUS_WAIT_FOR_INACTIVE)) {
197
198 if (streq_ptr(item->active_state, "failed"))
199 d->has_failed = true;
200 else if (!streq_ptr(item->active_state, "inactive"))
201 return;
202 }
203
204 call_unit_callback_and_wait(d, item, true);
205 bus_wait_for_units_check_ready(d);
206 }
207
208 static int wait_for_item_parse_properties(WaitForItem *item, sd_bus_message *m) {
209
210 static const struct bus_properties_map map[] = {
211 { "ActiveState", "s", NULL, offsetof(WaitForItem, active_state) },
212 { "Job", "(uo)", bus_map_job_id, offsetof(WaitForItem, job_id) },
213 { "CleanResult", "s", NULL, offsetof(WaitForItem, clean_result) },
214 { "LiveMountResult", "s", NULL, offsetof(WaitForItem, live_mount_result) },
215 {}
216 };
217
218 int r;
219
220 assert(item);
221 assert(m);
222
223 r = bus_message_map_all_properties(m, map, BUS_MAP_STRDUP, NULL, item);
224 if (r < 0)
225 return r;
226
227 wait_for_item_check_ready(item);
228 return 0;
229 }
230
231 static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) {
232 WaitForItem *item = ASSERT_PTR(userdata);
233 const char *interface;
234 int r;
235
236 r = sd_bus_message_read(m, "s", &interface);
237 if (r < 0) {
238 log_debug_errno(r, "Failed to parse PropertiesChanged signal: %m");
239 return 0;
240 }
241
242 if (!streq(interface, "org.freedesktop.systemd1.Unit"))
243 return 0;
244
245 r = wait_for_item_parse_properties(item, m);
246 if (r < 0)
247 log_debug_errno(r, "Failed to process PropertiesChanged signal: %m");
248
249 return 0;
250 }
251
252 static int on_get_all_properties(sd_bus_message *m, void *userdata, sd_bus_error *reterr_error) {
253 WaitForItem *item = ASSERT_PTR(userdata);
254 const sd_bus_error *e;
255 int r;
256
257 e = sd_bus_message_get_error(m);
258 if (e) {
259 BusWaitForUnits *d = item->parent;
260
261 d->has_failed = true;
262
263 r = sd_bus_error_get_errno(e);
264 log_debug_errno(r, "GetAll() failed for %s: %s",
265 item->bus_path, bus_error_message(e, r));
266
267 call_unit_callback_and_wait(d, item, false);
268 bus_wait_for_units_check_ready(d);
269 return 0;
270 }
271
272 r = wait_for_item_parse_properties(item, m);
273 if (r < 0)
274 log_debug_errno(r, "Failed to process GetAll method reply: %m");
275
276 return 0;
277 }
278
279 int bus_wait_for_units_add_unit(
280 BusWaitForUnits *d,
281 const char *unit,
282 BusWaitForUnitsFlags flags,
283 bus_wait_for_units_unit_callback_t callback,
284 void *userdata) {
285
286 _cleanup_(wait_for_item_freep) WaitForItem *item = NULL;
287 _cleanup_free_ char *bus_path = NULL;
288 int r;
289
290 assert(d);
291 assert(unit);
292 assert((flags & _BUS_WAIT_FOR_TARGET) != 0);
293
294 bus_path = unit_dbus_path_from_name(unit);
295 if (!bus_path)
296 return -ENOMEM;
297
298 if (hashmap_contains(d->items, bus_path))
299 return 0;
300
301 item = new(WaitForItem, 1);
302 if (!item)
303 return -ENOMEM;
304
305 *item = (WaitForItem) {
306 .flags = flags,
307 .bus_path = TAKE_PTR(bus_path),
308 .unit_callback = callback,
309 .userdata = userdata,
310 .job_id = UINT32_MAX,
311 };
312
313 if (!FLAGS_SET(item->flags, BUS_WAIT_REFFED)) {
314 r = sd_bus_call_method_async(
315 d->bus,
316 NULL,
317 "org.freedesktop.systemd1",
318 item->bus_path,
319 "org.freedesktop.systemd1.Unit",
320 "Ref",
321 NULL,
322 NULL,
323 NULL);
324 if (r < 0)
325 return log_debug_errno(r, "Failed to add reference to unit %s: %m", unit);
326
327 item->flags |= BUS_WAIT_REFFED;
328 }
329
330 r = sd_bus_match_signal_async(
331 d->bus,
332 &item->slot_properties_changed,
333 "org.freedesktop.systemd1",
334 item->bus_path,
335 "org.freedesktop.DBus.Properties",
336 "PropertiesChanged",
337 on_properties_changed,
338 NULL,
339 item);
340 if (r < 0)
341 return log_debug_errno(r, "Failed to request match for PropertiesChanged signal: %m");
342
343 r = sd_bus_call_method_async(
344 d->bus,
345 &item->slot_get_all,
346 "org.freedesktop.systemd1",
347 item->bus_path,
348 "org.freedesktop.DBus.Properties",
349 "GetAll",
350 on_get_all_properties,
351 item,
352 "s", FLAGS_SET(item->flags, BUS_WAIT_FOR_MAINTENANCE_END) ? NULL : "org.freedesktop.systemd1.Unit");
353 if (r < 0)
354 return log_debug_errno(r, "Failed to request properties of unit %s: %m", unit);
355
356 r = hashmap_ensure_put(&d->items, &string_hash_ops, item->bus_path, item);
357 if (r < 0)
358 return r;
359 assert(r > 0);
360
361 d->state = BUS_WAIT_RUNNING;
362 item->parent = d;
363 TAKE_PTR(item);
364
365 return 1;
366 }
367
368 int bus_wait_for_units_run(BusWaitForUnits *d) {
369 int r;
370
371 assert(d);
372
373 while (d->state == BUS_WAIT_RUNNING) {
374
375 r = sd_bus_process(d->bus, NULL);
376 if (r < 0)
377 return r;
378 if (r > 0)
379 continue;
380
381 r = sd_bus_wait(d->bus, UINT64_MAX);
382 if (r < 0)
383 return r;
384 }
385
386 return d->state;
387 }
388
389 BusWaitForUnitsState bus_wait_for_units_state(BusWaitForUnits *d) {
390 assert(d);
391
392 return d->state;
393 }