Bug Summary

File:build-scan/../src/shared/bus-wait-for-units.c
Warning:line 349, column 25
Potential leak of memory pointed to by 'item'

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-unknown-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name bus-wait-for-units.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -menable-no-infs -menable-no-nans -menable-unsafe-fp-math -fno-signed-zeros -mreassociate -freciprocal-math -fdenormal-fp-math=preserve-sign,preserve-sign -ffp-contract=fast -fno-rounding-math -ffast-math -ffinite-math-only -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -fno-split-dwarf-inlining -debugger-tuning=gdb -resource-dir /usr/lib64/clang/12.0.0 -include config.h -I src/shared/libsystemd-shared-239.a.p -I src/shared -I ../src/shared -I src/basic -I ../src/basic -I src/systemd -I ../src/systemd -I src/journal -I ../src/journal -I src/journal-remote -I ../src/journal-remote -I src/nspawn -I ../src/nspawn -I src/resolve -I ../src/resolve -I src/timesync -I ../src/timesync -I ../src/time-wait-sync -I src/login -I ../src/login -I src/udev -I ../src/udev -I src/libudev -I ../src/libudev -I src/core -I ../src/core -I ../src/libsystemd/sd-bus -I ../src/libsystemd/sd-device -I ../src/libsystemd/sd-hwdb -I ../src/libsystemd/sd-id128 -I ../src/libsystemd/sd-netlink -I ../src/libsystemd/sd-network -I src/libsystemd-network -I ../src/libsystemd-network -I . -I .. -I /usr/include/blkid -D _FILE_OFFSET_BITS=64 -internal-isystem /usr/local/include -internal-isystem /usr/lib64/clang/12.0.0/include -internal-externc-isystem /include -internal-externc-isystem /usr/include -Wwrite-strings -Wno-unused-parameter -Wno-missing-field-initializers -Wno-unused-result -Wno-format-signedness -Wno-error=nonnull -std=gnu99 -fconst-strings -fdebug-compilation-dir /home/mrc0mmand/repos/@redhat-plumbers/systemd-rhel8/build-scan -ferror-limit 19 -fvisibility default -stack-protector 2 -fgnuc-version=4.2.1 -fcolor-diagnostics -analyzer-output=html -faddrsig -o /tmp/scan-build-2021-07-16-221226-1465241-1 -x c ../src/shared/bus-wait-for-units.c

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

1/* SPDX-License-Identifier: LGPL-2.1+ */
2
3#include "bus-util.h"
4#include "bus-wait-for-units.h"
5#include "hashmap.h"
6#include "string-util.h"
7#include "strv.h"
8#include "unit-def.h"
9
10typedef struct WaitForItem {
11 BusWaitForUnits *parent;
12
13 BusWaitForUnitsFlags flags;
14
15 char *bus_path;
16
17 sd_bus_slot *slot_get_all;
18 sd_bus_slot *slot_properties_changed;
19
20 bus_wait_for_units_unit_callback unit_callback;
21 void *userdata;
22
23 char *active_state;
24 uint32_t job_id;
25 char *clean_result;
26} WaitForItem;
27
28typedef struct BusWaitForUnits {
29 sd_bus *bus;
30 sd_bus_slot *slot_disconnected;
31
32 Hashmap *items;
33
34 bus_wait_for_units_ready_callback ready_callback;
35 void *userdata;
36
37 WaitForItem *current;
38
39 BusWaitForUnitsState state;
40 bool_Bool has_failed:1;
41} BusWaitForUnits;
42
43static WaitForItem *wait_for_item_free(WaitForItem *item) {
44 int r;
45
46 if (!item)
47 return NULL((void*)0);
48
49 if (item->parent) {
50 if (FLAGS_SET(item->flags, BUS_WAIT_REFFED)(((item->flags) & (BUS_WAIT_REFFED)) == (BUS_WAIT_REFFED
))
&& item->bus_path && item->parent->bus) {
51 r = sd_bus_call_method_async(
52 item->parent->bus,
53 NULL((void*)0),
54 "org.freedesktop.systemd1",
55 item->bus_path,
56 "org.freedesktop.systemd1.Unit",
57 "Unref",
58 NULL((void*)0),
59 NULL((void*)0),
60 NULL((void*)0));
61 if (r < 0)
62 log_debug_errno(r, "Failed to drop reference to unit %s, ignoring: %m", item->bus_path)({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 62, __func__, "Failed to drop reference to unit %s, ignoring: %m"
, item->bus_path) : -abs(_e); })
;
63 }
64
65 assert_se(hashmap_remove(item->parent->items, item->bus_path) == item)do { if ((__builtin_expect(!!(!(hashmap_remove(item->parent
->items, item->bus_path) == item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("hashmap_remove(item->parent->items, item->bus_path) == item"
), "../src/shared/bus-wait-for-units.c", 65, __PRETTY_FUNCTION__
); } while (0)
;
66
67 if (item->parent->current == item)
68 item->parent->current = NULL((void*)0);
69 }
70
71 sd_bus_slot_unref(item->slot_properties_changed);
72 sd_bus_slot_unref(item->slot_get_all);
73
74 free(item->bus_path);
75 free(item->active_state);
76 free(item->clean_result);
77
78 return mfree(item);
79}
80
81DEFINE_TRIVIAL_CLEANUP_FUNC(WaitForItem*, wait_for_item_free)static inline void wait_for_item_freep(WaitForItem* *p) { if (
*p) wait_for_item_free(*p); }
;
82
83static void call_unit_callback_and_wait(BusWaitForUnits *d, WaitForItem *item, bool_Bool good) {
84 d->current = item;
85
86 if (item->unit_callback)
87 item->unit_callback(d, item->bus_path, good, item->userdata);
88
89 wait_for_item_free(item);
90}
91
92static void bus_wait_for_units_clear(BusWaitForUnits *d) {
93 WaitForItem *item;
94
95 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 95, __PRETTY_FUNCTION__); } while (0)
;
96
97 d->slot_disconnected = sd_bus_slot_unref(d->slot_disconnected);
98 d->bus = sd_bus_unref(d->bus);
99
100 while ((item = hashmap_first(d->items)))
101 call_unit_callback_and_wait(d, item, false0);
102
103 d->items = hashmap_free(d->items);
104}
105
106static int match_disconnected(sd_bus_message *m, void *userdata, sd_bus_error *error) {
107 BusWaitForUnits *d = userdata;
108
109 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/shared/bus-wait-for-units.c"
, 109, __PRETTY_FUNCTION__); } while (0)
;
110 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 110, __PRETTY_FUNCTION__); } while (0)
;
111
112 log_error("Warning! D-Bus connection terminated.")({ int _level = (((3))), _e = ((0)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 112, __func__, "Warning! D-Bus connection terminated."
) : -abs(_e); })
;
113
114 bus_wait_for_units_clear(d);
115
116 if (d->ready_callback)
117 d->ready_callback(d, false0, d->userdata);
118 else /* If no ready callback is specified close the connection so that the event loop exits */
119 sd_bus_close(sd_bus_message_get_bus(m));
120
121 return 0;
122}
123
124int bus_wait_for_units_new(sd_bus *bus, BusWaitForUnits **ret) {
125 _cleanup_(bus_wait_for_units_freep)__attribute__((cleanup(bus_wait_for_units_freep))) BusWaitForUnits *d = NULL((void*)0);
126 int r;
127
128 assert(bus)do { if ((__builtin_expect(!!(!(bus)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("bus"), "../src/shared/bus-wait-for-units.c"
, 128, __PRETTY_FUNCTION__); } while (0)
;
129 assert(ret)do { if ((__builtin_expect(!!(!(ret)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("ret"), "../src/shared/bus-wait-for-units.c"
, 129, __PRETTY_FUNCTION__); } while (0)
;
130
131 d = new(BusWaitForUnits, 1)((BusWaitForUnits*) malloc_multiply(sizeof(BusWaitForUnits), (
1)))
;
132 if (!d)
133 return -ENOMEM12;
134
135 *d = (BusWaitForUnits) {
136 .state = BUS_WAIT_SUCCESS,
137 .bus = sd_bus_ref(bus),
138 };
139
140 r = sd_bus_match_signal_async(
141 bus,
142 &d->slot_disconnected,
143 "org.freedesktop.DBus.Local",
144 NULL((void*)0),
145 "org.freedesktop.DBus.Local",
146 "Disconnected",
147 match_disconnected, NULL((void*)0), d);
148 if (r < 0)
149 return r;
150
151 *ret = TAKE_PTR(d)({ typeof(d) _ptr_ = (d); (d) = ((void*)0); _ptr_; });
152 return 0;
153}
154
155BusWaitForUnits* bus_wait_for_units_free(BusWaitForUnits *d) {
156 if (!d)
157 return NULL((void*)0);
158
159 bus_wait_for_units_clear(d);
160 sd_bus_slot_unref(d->slot_disconnected);
161 sd_bus_unref(d->bus);
162
163 return mfree(d);
164}
165
166static bool_Bool bus_wait_for_units_is_ready(BusWaitForUnits *d) {
167 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 167, __PRETTY_FUNCTION__); } while (0)
;
168
169 if (!d->bus) /* Disconnected? */
170 return true1;
171
172 return hashmap_isempty(d->items);
173}
174
175void bus_wait_for_units_set_ready_callback(BusWaitForUnits *d, bus_wait_for_units_ready_callback callback, void *userdata) {
176 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 176, __PRETTY_FUNCTION__); } while (0)
;
177
178 d->ready_callback = callback;
179 d->userdata = userdata;
180}
181
182static void bus_wait_for_units_check_ready(BusWaitForUnits *d) {
183 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 183, __PRETTY_FUNCTION__); } while (0)
;
184
185 if (!bus_wait_for_units_is_ready(d))
186 return;
187
188 d->state = d->has_failed ? BUS_WAIT_FAILURE : BUS_WAIT_SUCCESS;
189
190 if (d->ready_callback)
191 d->ready_callback(d, d->state, d->userdata);
192}
193
194static void wait_for_item_check_ready(WaitForItem *item) {
195 BusWaitForUnits *d;
196
197 assert(item)do { if ((__builtin_expect(!!(!(item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("item"), "../src/shared/bus-wait-for-units.c"
, 197, __PRETTY_FUNCTION__); } while (0)
;
198 assert_se(d = item->parent)do { if ((__builtin_expect(!!(!(d = item->parent)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d = item->parent"), "../src/shared/bus-wait-for-units.c"
, 198, __PRETTY_FUNCTION__); } while (0)
;
199
200 if (FLAGS_SET(item->flags, BUS_WAIT_FOR_MAINTENANCE_END)(((item->flags) & (BUS_WAIT_FOR_MAINTENANCE_END)) == (
BUS_WAIT_FOR_MAINTENANCE_END))
) {
201
202 if (item->clean_result && !streq(item->clean_result, "success")(strcmp((item->clean_result),("success")) == 0))
203 d->has_failed = true1;
204
205 if (!item->active_state || streq(item->active_state, "maintenance")(strcmp((item->active_state),("maintenance")) == 0))
206 return;
207 }
208
209 if (FLAGS_SET(item->flags, BUS_WAIT_NO_JOB)(((item->flags) & (BUS_WAIT_NO_JOB)) == (BUS_WAIT_NO_JOB
))
&& item->job_id != 0)
210 return;
211
212 if (FLAGS_SET(item->flags, BUS_WAIT_FOR_INACTIVE)(((item->flags) & (BUS_WAIT_FOR_INACTIVE)) == (BUS_WAIT_FOR_INACTIVE
))
) {
213
214 if (streq_ptr(item->active_state, "failed"))
215 d->has_failed = true1;
216 else if (!streq_ptr(item->active_state, "inactive"))
217 return;
218 }
219
220 call_unit_callback_and_wait(d, item, true1);
221 bus_wait_for_units_check_ready(d);
222}
223
224static int property_map_job(
225 sd_bus *bus,
226 const char *member,
227 sd_bus_message *m,
228 sd_bus_error *error,
229 void *userdata) {
230
231 WaitForItem *item = userdata;
232 const char *path;
233 uint32_t id;
234 int r;
235
236 assert(item)do { if ((__builtin_expect(!!(!(item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("item"), "../src/shared/bus-wait-for-units.c"
, 236, __PRETTY_FUNCTION__); } while (0)
;
237
238 r = sd_bus_message_read(m, "(uo)", &id, &path);
239 if (r < 0)
240 return r;
241
242 item->job_id = id;
243 return 0;
244}
245
246static int wait_for_item_parse_properties(WaitForItem *item, sd_bus_message *m) {
247
248 static const struct bus_properties_map map[] = {
249 { "ActiveState", "s", NULL((void*)0), offsetof(WaitForItem, active_state)__builtin_offsetof(WaitForItem, active_state) },
250 { "Job", "(uo)", property_map_job, 0 },
251 { "CleanResult", "s", NULL((void*)0), offsetof(WaitForItem, clean_result)__builtin_offsetof(WaitForItem, clean_result) },
252 {}
253 };
254
255 int r;
256
257 assert(item)do { if ((__builtin_expect(!!(!(item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("item"), "../src/shared/bus-wait-for-units.c"
, 257, __PRETTY_FUNCTION__); } while (0)
;
258 assert(m)do { if ((__builtin_expect(!!(!(m)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("m"), "../src/shared/bus-wait-for-units.c"
, 258, __PRETTY_FUNCTION__); } while (0)
;
259
260 r = bus_message_map_all_properties(m, map, BUS_MAP_STRDUP, NULL((void*)0), item);
261 if (r < 0)
262 return r;
263
264 wait_for_item_check_ready(item);
265 return 0;
266}
267
268static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error *error) {
269 WaitForItem *item = userdata;
270 const char *interface;
271 int r;
272
273 assert(item)do { if ((__builtin_expect(!!(!(item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("item"), "../src/shared/bus-wait-for-units.c"
, 273, __PRETTY_FUNCTION__); } while (0)
;
274
275 r = sd_bus_message_read(m, "s", &interface);
276 if (r < 0) {
277 log_debug_errno(r, "Failed to parse PropertiesChanged signal: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 277, __func__, "Failed to parse PropertiesChanged signal: %m"
) : -abs(_e); })
;
278 return 0;
279 }
280
281 if (!streq(interface, "org.freedesktop.systemd1.Unit")(strcmp((interface),("org.freedesktop.systemd1.Unit")) == 0))
282 return 0;
283
284 r = wait_for_item_parse_properties(item, m);
285 if (r < 0)
286 log_debug_errno(r, "Failed to process PropertiesChanged signal: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 286, __func__, "Failed to process PropertiesChanged signal: %m"
) : -abs(_e); })
;
287
288 return 0;
289}
290
291static int on_get_all_properties(sd_bus_message *m, void *userdata, sd_bus_error *error) {
292 WaitForItem *item = userdata;
293 int r;
294
295 assert(item)do { if ((__builtin_expect(!!(!(item)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("item"), "../src/shared/bus-wait-for-units.c"
, 295, __PRETTY_FUNCTION__); } while (0)
;
296
297 if (sd_bus_error_is_set(error)) {
298 BusWaitForUnits *d = item->parent;
299
300 d->has_failed = true1;
301
302 log_debug_errno(sd_bus_error_get_errno(error), "GetAll() failed for %s: %s",({ int _level = ((7)), _e = ((sd_bus_error_get_errno(error)))
, _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm
) >= ((_level) & 0x07)) ? log_internal_realm(((_realm)
<< 10 | (_level)), _e, "../src/shared/bus-wait-for-units.c"
, 303, __func__, "GetAll() failed for %s: %s", item->bus_path
, error->message) : -abs(_e); })
303 item->bus_path, error->message)({ int _level = ((7)), _e = ((sd_bus_error_get_errno(error)))
, _realm = (LOG_REALM_SYSTEMD); (log_get_max_level_realm(_realm
) >= ((_level) & 0x07)) ? log_internal_realm(((_realm)
<< 10 | (_level)), _e, "../src/shared/bus-wait-for-units.c"
, 303, __func__, "GetAll() failed for %s: %s", item->bus_path
, error->message) : -abs(_e); })
;
304
305 call_unit_callback_and_wait(d, item, false0);
306 bus_wait_for_units_check_ready(d);
307 return 0;
308 }
309
310 r = wait_for_item_parse_properties(item, m);
311 if (r < 0)
312 log_debug_errno(r, "Failed to process GetAll method reply: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 312, __func__, "Failed to process GetAll method reply: %m"
) : -abs(_e); })
;
313
314 return 0;
315}
316
317int bus_wait_for_units_add_unit(
318 BusWaitForUnits *d,
319 const char *unit,
320 BusWaitForUnitsFlags flags,
321 bus_wait_for_units_unit_callback callback,
322 void *userdata) {
323
324 _cleanup_(wait_for_item_freep)__attribute__((cleanup(wait_for_item_freep))) WaitForItem *item = NULL((void*)0);
325 int r;
326
327 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 327, __PRETTY_FUNCTION__); } while (0)
;
1
Assuming 'd' is non-null
2
Taking false branch
3
Loop condition is false. Exiting loop
328 assert(unit)do { if ((__builtin_expect(!!(!(unit)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("unit"), "../src/shared/bus-wait-for-units.c"
, 328, __PRETTY_FUNCTION__); } while (0)
;
4
Assuming 'unit' is non-null
5
Taking false branch
6
Loop condition is false. Exiting loop
329
330 assert(flags != 0)do { if ((__builtin_expect(!!(!(flags != 0)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("flags != 0"), "../src/shared/bus-wait-for-units.c"
, 330, __PRETTY_FUNCTION__); } while (0)
;
7
Assuming 'flags' is not equal to 0
8
Taking false branch
9
Loop condition is false. Exiting loop
331
332 r = hashmap_ensure_allocated(&d->items, &string_hash_ops)internal_hashmap_ensure_allocated(&d->items, &string_hash_ops
)
;
333 if (r < 0)
10
Assuming 'r' is >= 0
11
Taking false branch
334 return r;
335
336 item = new(WaitForItem, 1)((WaitForItem*) malloc_multiply(sizeof(WaitForItem), (1)));
12
Calling 'malloc_multiply'
15
Returned allocated memory
337 if (!item)
16
Assuming 'item' is non-null
17
Taking false branch
338 return -ENOMEM12;
339
340 *item = (WaitForItem) {
341 .flags = flags,
342 .bus_path = unit_dbus_path_from_name(unit),
343 .unit_callback = callback,
344 .userdata = userdata,
345 .job_id = UINT32_MAX(4294967295U),
346 };
347
348 if (!item->bus_path)
18
Assuming field 'bus_path' is null
19
Taking true branch
349 return -ENOMEM12;
20
Potential leak of memory pointed to by 'item'
350
351 if (!FLAGS_SET(item->flags, BUS_WAIT_REFFED)(((item->flags) & (BUS_WAIT_REFFED)) == (BUS_WAIT_REFFED
))
) {
352 r = sd_bus_call_method_async(
353 d->bus,
354 NULL((void*)0),
355 "org.freedesktop.systemd1",
356 item->bus_path,
357 "org.freedesktop.systemd1.Unit",
358 "Ref",
359 NULL((void*)0),
360 NULL((void*)0),
361 NULL((void*)0));
362 if (r < 0)
363 return log_debug_errno(r, "Failed to add reference to unit %s: %m", unit)({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 363, __func__, "Failed to add reference to unit %s: %m"
, unit) : -abs(_e); })
;
364
365
366 item->flags |= BUS_WAIT_REFFED;
367 }
368
369 r = sd_bus_match_signal_async(
370 d->bus,
371 &item->slot_properties_changed,
372 "org.freedesktop.systemd1",
373 item->bus_path,
374 "org.freedesktop.DBus.Properties",
375 "PropertiesChanged",
376 on_properties_changed,
377 NULL((void*)0),
378 item);
379 if (r < 0)
380 return log_debug_errno(r, "Failed to request match for PropertiesChanged signal: %m")({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 380, __func__, "Failed to request match for PropertiesChanged signal: %m"
) : -abs(_e); })
;
381
382 r = sd_bus_call_method_async(
383 d->bus,
384 &item->slot_get_all,
385 "org.freedesktop.systemd1",
386 item->bus_path,
387 "org.freedesktop.DBus.Properties",
388 "GetAll",
389 on_get_all_properties,
390 item,
391 "s", FLAGS_SET(item->flags, BUS_WAIT_FOR_MAINTENANCE_END)(((item->flags) & (BUS_WAIT_FOR_MAINTENANCE_END)) == (
BUS_WAIT_FOR_MAINTENANCE_END))
? NULL((void*)0) : "org.freedesktop.systemd1.Unit");
392 if (r < 0)
393 return log_debug_errno(r, "Failed to request properties of unit %s: %m", unit)({ int _level = ((7)), _e = ((r)), _realm = (LOG_REALM_SYSTEMD
); (log_get_max_level_realm(_realm) >= ((_level) & 0x07
)) ? log_internal_realm(((_realm) << 10 | (_level)), _e
, "../src/shared/bus-wait-for-units.c", 393, __func__, "Failed to request properties of unit %s: %m"
, unit) : -abs(_e); })
;
394
395 r = hashmap_put(d->items, item->bus_path, item);
396 if (r < 0)
397 return r;
398
399 d->state = BUS_WAIT_RUNNING;
400 item->parent = d;
401 TAKE_PTR(item)({ typeof(item) _ptr_ = (item); (item) = ((void*)0); _ptr_; }
)
;
402 return 0;
403}
404
405int bus_wait_for_units_run(BusWaitForUnits *d) {
406 int r;
407
408 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 408, __PRETTY_FUNCTION__); } while (0)
;
409
410 while (d->state == BUS_WAIT_RUNNING) {
411
412 r = sd_bus_process(d->bus, NULL((void*)0));
413 if (r < 0)
414 return r;
415 if (r > 0)
416 continue;
417
418 r = sd_bus_wait(d->bus, (uint64_t) -1);
419 if (r < 0)
420 return r;
421 }
422
423 return d->state;
424}
425
426BusWaitForUnitsState bus_wait_for_units_state(BusWaitForUnits *d) {
427 assert(d)do { if ((__builtin_expect(!!(!(d)),0))) log_assert_failed_realm
(LOG_REALM_SYSTEMD, ("d"), "../src/shared/bus-wait-for-units.c"
, 427, __PRETTY_FUNCTION__); } while (0)
;
428
429 return d->state;
430}

../src/basic/alloc-util.h

1/* SPDX-License-Identifier: LGPL-2.1+ */
2#pragma once
3
4#include <alloca.h>
5#include <stddef.h>
6#include <stdlib.h>
7#include <string.h>
8
9#include "macro.h"
10
11#define new(t, n)((t*) malloc_multiply(sizeof(t), (n))) ((t*) malloc_multiply(sizeof(t), (n)))
12
13#define new0(t, n)((t*) calloc((n), sizeof(t))) ((t*) calloc((n), sizeof(t)))
14
15#define newa(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 15, __PRETTY_FUNCTION__); } while
(0); (t*) __builtin_alloca (sizeof(t)*(n)); })
\
16 ({ \
17 assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 17, __PRETTY_FUNCTION__); } while
(0)
; \
18 (t*) alloca(sizeof(t)*(n))__builtin_alloca (sizeof(t)*(n)); \
19 })
20
21#define newa0(t, n)({ do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 21, __PRETTY_FUNCTION__); } while
(0); (t*) ({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_
= __builtin_alloca (_len_); (void *) memset(_new_, 0, _len_)
; }); })
\
22 ({ \
23 assert(!size_multiply_overflow(sizeof(t), n))do { if ((__builtin_expect(!!(!(!size_multiply_overflow(sizeof
(t), n))),0))) log_assert_failed_realm(LOG_REALM_SYSTEMD, ("!size_multiply_overflow(sizeof(t), n)"
), "../src/basic/alloc-util.h", 23, __PRETTY_FUNCTION__); } while
(0)
; \
24 (t*) alloca0(sizeof(t)*(n))({ char *_new_; size_t _len_ = sizeof(t)*(n); _new_ = __builtin_alloca
(_len_); (void *) memset(_new_, 0, _len_); })
; \
25 })
26
27#define newdup(t, p, n)((t*) memdup_multiply(p, sizeof(t), (n))) ((t*) memdup_multiply(p, sizeof(t), (n)))
28
29#define newdup_suffix0(t, p, n)((t*) memdup_suffix0_multiply(p, sizeof(t), (n))) ((t*) memdup_suffix0_multiply(p, sizeof(t), (n)))
30
31#define malloc0(n)(calloc(1, (n))) (calloc(1, (n)))
32
33static inline void *mfree(void *memory) {
34 free(memory);
35 return NULL((void*)0);
36}
37
38#define free_and_replace(a, b)({ free(a); (a) = (b); (b) = ((void*)0); 0; }) \
39 ({ \
40 free(a); \
41 (a) = (b); \
42 (b) = NULL((void*)0); \
43 0; \
44 })
45
46void* memdup(const void *p, size_t l) _alloc_(2);
47void* memdup_suffix0(const void *p, size_t l) _alloc_(2);
48
49static inline void freep(void *p) {
50 free(*(void**) p);
51}
52
53#define _cleanup_free___attribute__((cleanup(freep))) _cleanup_(freep)__attribute__((cleanup(freep)))
54
55static inline bool_Bool size_multiply_overflow(size_t size, size_t need) {
56 return _unlikely_(need != 0 && size > (SIZE_MAX / need))(__builtin_expect(!!(need != 0 && size > ((18446744073709551615UL
) / need)),0))
;
57}
58
59_malloc___attribute__ ((malloc)) _alloc_(1, 2) static inline void *malloc_multiply(size_t size, size_t need) {
60 if (size_multiply_overflow(size, need))
13
Taking false branch
61 return NULL((void*)0);
62
63 return malloc(size * need);
14
Memory is allocated
64}
65
66#if !HAVE_REALLOCARRAY1
67_alloc_(2, 3) static inline void *reallocarray(void *p, size_t need, size_t size) {
68 if (size_multiply_overflow(size, need))
69 return NULL((void*)0);
70
71 return realloc(p, size * need);
72}
73#endif
74
75_alloc_(2, 3) static inline void *memdup_multiply(const void *p, size_t size, size_t need) {
76 if (size_multiply_overflow(size, need))
77 return NULL((void*)0);
78
79 return memdup(p, size * need);
80}
81
82_alloc_(2, 3) static inline void *memdup_suffix0_multiply(const void *p, size_t size, size_t need) {
83 if (size_multiply_overflow(size, need))
84 return NULL((void*)0);
85
86 return memdup_suffix0(p, size * need);
87}
88
89void* greedy_realloc(void **p, size_t *allocated, size_t need, size_t size);
90void* greedy_realloc0(void **p, size_t *allocated, size_t need, size_t size);
91
92#define GREEDY_REALLOC(array, allocated, need)greedy_realloc((void**) &(array), &(allocated), (need
), sizeof((array)[0]))
\
93 greedy_realloc((void**) &(array), &(allocated), (need), sizeof((array)[0]))
94
95#define GREEDY_REALLOC0(array, allocated, need)greedy_realloc0((void**) &(array), &(allocated), (need
), sizeof((array)[0]))
\
96 greedy_realloc0((void**) &(array), &(allocated), (need), sizeof((array)[0]))
97
98#define alloca0(n)({ char *_new_; size_t _len_ = n; _new_ = __builtin_alloca (_len_
); (void *) memset(_new_, 0, _len_); })
\
99 ({ \
100 char *_new_; \
101 size_t _len_ = n; \
102 _new_ = alloca(_len_)__builtin_alloca (_len_); \
103 (void *) memset(_new_, 0, _len_); \
104 })
105
106/* It's not clear what alignment glibc/gcc alloca() guarantee, hence provide a guaranteed safe version */
107#define alloca_align(size, align)({ void *_ptr_; size_t _mask_ = (align) - 1; _ptr_ = __builtin_alloca
((size) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) &
~_mask_); })
\
108 ({ \
109 void *_ptr_; \
110 size_t _mask_ = (align) - 1; \
111 _ptr_ = alloca((size) + _mask_)__builtin_alloca ((size) + _mask_); \
112 (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_); \
113 })
114
115#define alloca0_align(size, align)({ void *_new_; size_t _size_ = (size); _new_ = ({ void *_ptr_
; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca ((_size_
) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) & ~_mask_
); }); (void*)memset(_new_, 0, _size_); })
\
116 ({ \
117 void *_new_; \
118 size_t _size_ = (size); \
119 _new_ = alloca_align(_size_, (align))({ void *_ptr_; size_t _mask_ = ((align)) - 1; _ptr_ = __builtin_alloca
((_size_) + _mask_); (void*)(((uintptr_t)_ptr_ + _mask_) &
~_mask_); })
; \
120 (void*)memset(_new_, 0, _size_); \
121 })
122
123/* Takes inspiration from Rusts's Option::take() method: reads and returns a pointer, but at the same time resets it to
124 * NULL. See: https://doc.rust-lang.org/std/option/enum.Option.html#method.take */
125#define TAKE_PTR(ptr)({ typeof(ptr) _ptr_ = (ptr); (ptr) = ((void*)0); _ptr_; }) \
126 ({ \
127 typeof(ptr) _ptr_ = (ptr); \
128 (ptr) = NULL((void*)0); \
129 _ptr_; \
130 })