]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
40ca29a1 | 2 | |
a8fbdf54 TA |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
5 | #include <inttypes.h> | |
a8fbdf54 | 6 | #include <stdlib.h> |
a8fbdf54 TA |
7 | #include <sys/ioctl.h> |
8 | #include <sys/resource.h> | |
0c842e0a | 9 | #include <sys/socket.h> |
a8fbdf54 | 10 | #include <unistd.h> |
0c842e0a | 11 | |
4f5dd394 | 12 | #include "sd-bus.h" |
ebd011d9 LP |
13 | #include "sd-daemon.h" |
14 | #include "sd-event.h" | |
a8fbdf54 | 15 | #include "sd-id128.h" |
d53d9474 | 16 | |
73d3ac8e | 17 | #include "bus-common-errors.h" |
d53d9474 LP |
18 | #include "bus-internal.h" |
19 | #include "bus-label.h" | |
3ffd4af2 | 20 | #include "bus-util.h" |
657ee2d8 | 21 | #include "path-util.h" |
269e4d2d | 22 | #include "socket-util.h" |
15a5e950 | 23 | #include "stdio-util.h" |
40ca29a1 | 24 | |
19070062 | 25 | static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { |
40ca29a1 LP |
26 | sd_event *e = userdata; |
27 | ||
40ca29a1 LP |
28 | assert(m); |
29 | assert(e); | |
30 | ||
19070062 | 31 | sd_bus_close(sd_bus_message_get_bus(m)); |
6203e07a | 32 | sd_event_exit(e, 0); |
b27adf35 | 33 | |
40ca29a1 LP |
34 | return 1; |
35 | } | |
36 | ||
fb293b3c ZJS |
37 | int bus_log_address_error(int r, BusTransport transport) { |
38 | bool hint = transport == BUS_TRANSPORT_LOCAL && r == -ENOMEDIUM; | |
39 | ||
237fbb67 | 40 | return log_error_errno(r, |
fb293b3c ZJS |
41 | hint ? "Failed to set bus address: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" : |
42 | "Failed to set bus address: %m"); | |
237fbb67 ZJS |
43 | } |
44 | ||
45 | int bus_log_connect_error(int r) { | |
46 | return log_error_errno(r, | |
47 | r == -ENOMEDIUM ? "Failed to connect to bus: $DBUS_SESSION_BUS_ADDRESS and $XDG_RUNTIME_DIR not defined (consider using --machine=<user>@.host --user to connect to bus of other user)" : | |
48 | ERRNO_IS_PRIVILEGE(r) ? "Failed to connect to bus: Operation not permitted (consider using --machine=<user>@.host --user to connect to bus of other user)" : | |
49 | "Failed to connect to bus: %m"); | |
50 | } | |
51 | ||
6203e07a | 52 | int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) { |
75152a4d | 53 | const char *match; |
11846aa7 | 54 | const char *unique; |
40ca29a1 LP |
55 | int r; |
56 | ||
57 | assert(e); | |
58 | assert(bus); | |
59 | assert(name); | |
60 | ||
6203e07a LP |
61 | /* We unregister the name here and then wait for the |
62 | * NameOwnerChanged signal for this event to arrive before we | |
63 | * quit. We do this in order to make sure that any queued | |
64 | * requests are still processed before we really exit. */ | |
65 | ||
11846aa7 | 66 | r = sd_bus_get_unique_name(bus, &unique); |
40ca29a1 LP |
67 | if (r < 0) |
68 | return r; | |
69 | ||
75152a4d LP |
70 | match = strjoina( |
71 | "sender='org.freedesktop.DBus'," | |
72 | "type='signal'," | |
73 | "interface='org.freedesktop.DBus'," | |
74 | "member='NameOwnerChanged'," | |
75 | "path='/org/freedesktop/DBus'," | |
76 | "arg0='", name, "',", | |
77 | "arg1='", unique, "',", | |
78 | "arg2=''"); | |
79 | ||
80 | r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e); | |
40ca29a1 LP |
81 | if (r < 0) |
82 | return r; | |
83 | ||
75152a4d | 84 | r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL); |
40ca29a1 LP |
85 | if (r < 0) |
86 | return r; | |
87 | ||
40ca29a1 LP |
88 | return 0; |
89 | } | |
90 | ||
37224a5f LP |
91 | int bus_event_loop_with_idle( |
92 | sd_event *e, | |
93 | sd_bus *bus, | |
94 | const char *name, | |
95 | usec_t timeout, | |
96 | check_idle_t check_idle, | |
97 | void *userdata) { | |
40ca29a1 | 98 | bool exiting = false; |
6203e07a | 99 | int r, code; |
40ca29a1 LP |
100 | |
101 | assert(e); | |
102 | assert(bus); | |
103 | assert(name); | |
104 | ||
105 | for (;;) { | |
37224a5f LP |
106 | bool idle; |
107 | ||
40ca29a1 LP |
108 | r = sd_event_get_state(e); |
109 | if (r < 0) | |
110 | return r; | |
40ca29a1 LP |
111 | if (r == SD_EVENT_FINISHED) |
112 | break; | |
113 | ||
37224a5f LP |
114 | if (check_idle) |
115 | idle = check_idle(userdata); | |
116 | else | |
117 | idle = true; | |
118 | ||
f5fbe71d | 119 | r = sd_event_run(e, exiting || !idle ? UINT64_MAX : timeout); |
40ca29a1 LP |
120 | if (r < 0) |
121 | return r; | |
122 | ||
a8ba6cd1 | 123 | if (r == 0 && !exiting && idle) { |
99cde098 ZJS |
124 | /* Inform the service manager that we are going down, so that it will queue all |
125 | * further start requests, instead of assuming we are already running. */ | |
126 | sd_notify(false, "STOPPING=1"); | |
b27adf35 | 127 | |
99cde098 | 128 | r = bus_async_unregister_and_exit(e, bus, name); |
40ca29a1 LP |
129 | if (r < 0) |
130 | return r; | |
131 | ||
99cde098 ZJS |
132 | exiting = true; |
133 | continue; | |
40ca29a1 LP |
134 | } |
135 | } | |
136 | ||
6203e07a LP |
137 | r = sd_event_get_exit_code(e, &code); |
138 | if (r < 0) | |
139 | return r; | |
140 | ||
141 | return code; | |
40ca29a1 LP |
142 | } |
143 | ||
5fd38859 | 144 | int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) { |
4afd3348 | 145 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL; |
5fd38859 DH |
146 | int r, has_owner = 0; |
147 | ||
148 | assert(c); | |
149 | assert(name); | |
150 | ||
151 | r = sd_bus_call_method(c, | |
152 | "org.freedesktop.DBus", | |
153 | "/org/freedesktop/dbus", | |
154 | "org.freedesktop.DBus", | |
155 | "NameHasOwner", | |
156 | error, | |
157 | &rep, | |
158 | "s", | |
159 | name); | |
160 | if (r < 0) | |
161 | return r; | |
162 | ||
163 | r = sd_bus_message_read_basic(rep, 'b', &has_owner); | |
164 | if (r < 0) | |
165 | return sd_bus_error_set_errno(error, r); | |
166 | ||
167 | return has_owner; | |
168 | } | |
169 | ||
73d3ac8e ZJS |
170 | bool bus_error_is_unknown_service(const sd_bus_error *error) { |
171 | return sd_bus_error_has_names(error, | |
172 | SD_BUS_ERROR_SERVICE_UNKNOWN, | |
173 | SD_BUS_ERROR_NAME_HAS_NO_OWNER, | |
174 | BUS_ERROR_NO_SUCH_UNIT); | |
175 | } | |
176 | ||
718db961 | 177 | int bus_check_peercred(sd_bus *c) { |
0c842e0a | 178 | struct ucred ucred; |
3e641e36 | 179 | int fd, r; |
0c842e0a TG |
180 | |
181 | assert(c); | |
182 | ||
183 | fd = sd_bus_get_fd(c); | |
0f8bd8de LP |
184 | if (fd < 0) |
185 | return fd; | |
0c842e0a | 186 | |
3e641e36 LP |
187 | r = getpeercred(fd, &ucred); |
188 | if (r < 0) | |
189 | return r; | |
0c842e0a TG |
190 | |
191 | if (ucred.uid != 0 && ucred.uid != geteuid()) | |
192 | return -EPERM; | |
193 | ||
194 | return 1; | |
195 | } | |
196 | ||
266f3e26 | 197 | int bus_connect_system_systemd(sd_bus **_bus) { |
b1a4981a | 198 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
0c842e0a | 199 | int r; |
0c842e0a TG |
200 | |
201 | assert(_bus); | |
202 | ||
0f8bd8de | 203 | if (geteuid() != 0) |
266f3e26 | 204 | return sd_bus_default_system(_bus); |
a1da8583 | 205 | |
a132bef0 ZJS |
206 | /* If we are root then let's talk directly to the system |
207 | * instance, instead of going via the bus */ | |
a6aa8912 LP |
208 | |
209 | r = sd_bus_new(&bus); | |
0f8bd8de LP |
210 | if (r < 0) |
211 | return r; | |
a1da8583 | 212 | |
a6aa8912 LP |
213 | r = sd_bus_set_address(bus, "unix:path=/run/systemd/private"); |
214 | if (r < 0) | |
215 | return r; | |
216 | ||
217 | r = sd_bus_start(bus); | |
218 | if (r < 0) | |
266f3e26 | 219 | return sd_bus_default_system(_bus); |
a6aa8912 | 220 | |
0f8bd8de | 221 | r = bus_check_peercred(bus); |
a1da8583 TG |
222 | if (r < 0) |
223 | return r; | |
224 | ||
1cc6c93a | 225 | *_bus = TAKE_PTR(bus); |
0f8bd8de | 226 | |
a1da8583 TG |
227 | return 0; |
228 | } | |
229 | ||
266f3e26 | 230 | int bus_connect_user_systemd(sd_bus **_bus) { |
b1a4981a | 231 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
a6aa8912 | 232 | _cleanup_free_ char *ee = NULL; |
41dd15e4 LP |
233 | const char *e; |
234 | int r; | |
235 | ||
41dd15e4 LP |
236 | assert(_bus); |
237 | ||
238 | e = secure_getenv("XDG_RUNTIME_DIR"); | |
537220d9 | 239 | if (!e) |
266f3e26 | 240 | return sd_bus_default_user(_bus); |
537220d9 | 241 | |
a6aa8912 LP |
242 | ee = bus_address_escape(e); |
243 | if (!ee) | |
537220d9 | 244 | return -ENOMEM; |
41dd15e4 LP |
245 | |
246 | r = sd_bus_new(&bus); | |
247 | if (r < 0) | |
248 | return r; | |
249 | ||
605405c6 | 250 | bus->address = strjoin("unix:path=", ee, "/systemd/private"); |
a6aa8912 LP |
251 | if (!bus->address) |
252 | return -ENOMEM; | |
41dd15e4 LP |
253 | |
254 | r = sd_bus_start(bus); | |
255 | if (r < 0) | |
266f3e26 | 256 | return sd_bus_default_user(_bus); |
41dd15e4 LP |
257 | |
258 | r = bus_check_peercred(bus); | |
259 | if (r < 0) | |
260 | return r; | |
261 | ||
1cc6c93a | 262 | *_bus = TAKE_PTR(bus); |
41dd15e4 LP |
263 | |
264 | return 0; | |
265 | } | |
266 | ||
1b630835 LP |
267 | int bus_connect_transport( |
268 | BusTransport transport, | |
269 | const char *host, | |
270 | bool user, | |
271 | sd_bus **ret) { | |
272 | ||
b1a4981a | 273 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d21ed1ea LP |
274 | int r; |
275 | ||
276 | assert(transport >= 0); | |
277 | assert(transport < _BUS_TRANSPORT_MAX); | |
38303498 | 278 | assert(ret); |
d21ed1ea LP |
279 | |
280 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
1b630835 | 281 | assert_return(transport != BUS_TRANSPORT_REMOTE || !user, -EOPNOTSUPP); |
d21ed1ea LP |
282 | |
283 | switch (transport) { | |
284 | ||
285 | case BUS_TRANSPORT_LOCAL: | |
286 | if (user) | |
38303498 | 287 | r = sd_bus_default_user(&bus); |
fb507898 | 288 | else { |
d7a0f1f4 | 289 | if (sd_booted() <= 0) |
fb507898 | 290 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ |
d7a0f1f4 FS |
291 | return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), |
292 | "System has not been booted with systemd as init system (PID 1). Can't operate."); | |
fb507898 YW |
293 | r = sd_bus_default_system(&bus); |
294 | } | |
d21ed1ea LP |
295 | break; |
296 | ||
297 | case BUS_TRANSPORT_REMOTE: | |
38303498 | 298 | r = sd_bus_open_system_remote(&bus, host); |
41dd15e4 LP |
299 | break; |
300 | ||
de33fc62 | 301 | case BUS_TRANSPORT_MACHINE: |
1b630835 LP |
302 | if (user) |
303 | r = sd_bus_open_user_machine(&bus, host); | |
304 | else | |
305 | r = sd_bus_open_system_machine(&bus, host); | |
41dd15e4 LP |
306 | break; |
307 | ||
308 | default: | |
04499a70 | 309 | assert_not_reached(); |
41dd15e4 | 310 | } |
38303498 LP |
311 | if (r < 0) |
312 | return r; | |
41dd15e4 | 313 | |
38303498 LP |
314 | r = sd_bus_set_exit_on_disconnect(bus, true); |
315 | if (r < 0) | |
316 | return r; | |
317 | ||
1cc6c93a | 318 | *ret = TAKE_PTR(bus); |
38303498 | 319 | return 0; |
41dd15e4 LP |
320 | } |
321 | ||
266f3e26 | 322 | int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) { |
41dd15e4 LP |
323 | int r; |
324 | ||
325 | assert(transport >= 0); | |
326 | assert(transport < _BUS_TRANSPORT_MAX); | |
327 | assert(bus); | |
328 | ||
329 | assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL); | |
15411c0c | 330 | assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP); |
41dd15e4 LP |
331 | |
332 | switch (transport) { | |
333 | ||
334 | case BUS_TRANSPORT_LOCAL: | |
335 | if (user) | |
266f3e26 | 336 | r = bus_connect_user_systemd(bus); |
fb507898 | 337 | else { |
baaa35ad | 338 | if (sd_booted() <= 0) |
fb507898 | 339 | /* Print a friendly message when the local system is actually not running systemd as PID 1. */ |
baaa35ad ZJS |
340 | return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN), |
341 | "System has not been booted with systemd as init system (PID 1). Can't operate."); | |
fb507898 YW |
342 | r = bus_connect_system_systemd(bus); |
343 | } | |
41dd15e4 LP |
344 | break; |
345 | ||
346 | case BUS_TRANSPORT_REMOTE: | |
3db729cb | 347 | r = sd_bus_open_system_remote(bus, host); |
d21ed1ea LP |
348 | break; |
349 | ||
de33fc62 LP |
350 | case BUS_TRANSPORT_MACHINE: |
351 | r = sd_bus_open_system_machine(bus, host); | |
d21ed1ea LP |
352 | break; |
353 | ||
354 | default: | |
04499a70 | 355 | assert_not_reached(); |
d21ed1ea LP |
356 | } |
357 | ||
358 | return r; | |
359 | } | |
e6504030 | 360 | |
98a4c30b DH |
361 | /** |
362 | * bus_path_encode_unique() - encode unique object path | |
363 | * @b: bus connection or NULL | |
364 | * @prefix: object path prefix | |
365 | * @sender_id: unique-name of client, or NULL | |
366 | * @external_id: external ID to be chosen by client, or NULL | |
367 | * @ret_path: storage for encoded object path pointer | |
368 | * | |
369 | * Whenever we provide a bus API that allows clients to create and manage | |
370 | * server-side objects, we need to provide a unique name for these objects. If | |
371 | * we let the server choose the name, we suffer from a race condition: If a | |
372 | * client creates an object asynchronously, it cannot destroy that object until | |
373 | * it received the method reply. It cannot know the name of the new object, | |
374 | * thus, it cannot destroy it. Furthermore, it enforces a round-trip. | |
375 | * | |
376 | * Therefore, many APIs allow the client to choose the unique name for newly | |
377 | * created objects. There're two problems to solve, though: | |
378 | * 1) Object names are usually defined via dbus object paths, which are | |
379 | * usually globally namespaced. Therefore, multiple clients must be able | |
380 | * to choose unique object names without interference. | |
381 | * 2) If multiple libraries share the same bus connection, they must be | |
382 | * able to choose unique object names without interference. | |
383 | * The first problem is solved easily by prefixing a name with the | |
384 | * unique-bus-name of a connection. The server side must enforce this and | |
385 | * reject any other name. The second problem is solved by providing unique | |
386 | * suffixes from within sd-bus. | |
387 | * | |
388 | * This helper allows clients to create unique object-paths. It uses the | |
389 | * template '/prefix/sender_id/external_id' and returns the new path in | |
390 | * @ret_path (must be freed by the caller). | |
391 | * If @sender_id is NULL, the unique-name of @b is used. If @external_id is | |
392 | * NULL, this function allocates a unique suffix via @b (by requesting a new | |
393 | * cookie). If both @sender_id and @external_id are given, @b can be passed as | |
394 | * NULL. | |
395 | * | |
396 | * Returns: 0 on success, negative error code on failure. | |
397 | */ | |
398 | int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) { | |
399 | _cleanup_free_ char *sender_label = NULL, *external_label = NULL; | |
400 | char external_buf[DECIMAL_STR_MAX(uint64_t)], *p; | |
401 | int r; | |
402 | ||
403 | assert_return(b || (sender_id && external_id), -EINVAL); | |
5453a4b1 | 404 | assert_return(sd_bus_object_path_is_valid(prefix), -EINVAL); |
98a4c30b DH |
405 | assert_return(ret_path, -EINVAL); |
406 | ||
407 | if (!sender_id) { | |
408 | r = sd_bus_get_unique_name(b, &sender_id); | |
409 | if (r < 0) | |
410 | return r; | |
411 | } | |
412 | ||
413 | if (!external_id) { | |
414 | xsprintf(external_buf, "%"PRIu64, ++b->cookie); | |
415 | external_id = external_buf; | |
416 | } | |
417 | ||
418 | sender_label = bus_label_escape(sender_id); | |
419 | if (!sender_label) | |
420 | return -ENOMEM; | |
421 | ||
422 | external_label = bus_label_escape(external_id); | |
423 | if (!external_label) | |
424 | return -ENOMEM; | |
425 | ||
657ee2d8 | 426 | p = path_join(prefix, sender_label, external_label); |
98a4c30b DH |
427 | if (!p) |
428 | return -ENOMEM; | |
429 | ||
430 | *ret_path = p; | |
431 | return 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * bus_path_decode_unique() - decode unique object path | |
436 | * @path: object path to decode | |
437 | * @prefix: object path prefix | |
438 | * @ret_sender: output parameter for sender-id label | |
439 | * @ret_external: output parameter for external-id label | |
440 | * | |
441 | * This does the reverse of bus_path_encode_unique() (see its description for | |
442 | * details). Both trailing labels, sender-id and external-id, are unescaped and | |
443 | * returned in the given output parameters (the caller must free them). | |
444 | * | |
445 | * Note that this function returns 0 if the path does not match the template | |
446 | * (see bus_path_encode_unique()), 1 if it matched. | |
447 | * | |
448 | * Returns: Negative error code on failure, 0 if the given object path does not | |
449 | * match the template (return parameters are set to NULL), 1 if it was | |
450 | * parsed successfully (return parameters contain allocated labels). | |
451 | */ | |
452 | int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) { | |
453 | const char *p, *q; | |
454 | char *sender, *external; | |
455 | ||
5453a4b1 ZJS |
456 | assert(sd_bus_object_path_is_valid(path)); |
457 | assert(sd_bus_object_path_is_valid(prefix)); | |
98a4c30b DH |
458 | assert(ret_sender); |
459 | assert(ret_external); | |
460 | ||
461 | p = object_path_startswith(path, prefix); | |
462 | if (!p) { | |
463 | *ret_sender = NULL; | |
464 | *ret_external = NULL; | |
465 | return 0; | |
466 | } | |
467 | ||
468 | q = strchr(p, '/'); | |
469 | if (!q) { | |
470 | *ret_sender = NULL; | |
471 | *ret_external = NULL; | |
472 | return 0; | |
473 | } | |
474 | ||
475 | sender = bus_label_unescape_n(p, q - p); | |
476 | external = bus_label_unescape(q + 1); | |
477 | if (!sender || !external) { | |
478 | free(sender); | |
479 | free(external); | |
480 | return -ENOMEM; | |
481 | } | |
482 | ||
483 | *ret_sender = sender; | |
484 | *ret_external = external; | |
485 | return 1; | |
486 | } | |
057171ef | 487 | |
984794ba LP |
488 | int bus_track_add_name_many(sd_bus_track *t, char **l) { |
489 | int r = 0; | |
490 | char **i; | |
491 | ||
492 | assert(t); | |
493 | ||
494 | /* Continues adding after failure, and returns the first failure. */ | |
495 | ||
496 | STRV_FOREACH(i, l) { | |
497 | int k; | |
498 | ||
499 | k = sd_bus_track_add_name(t, *i); | |
500 | if (k < 0 && r >= 0) | |
501 | r = k; | |
502 | } | |
503 | ||
504 | return r; | |
505 | } | |
d7afd945 | 506 | |
0ddf50ff | 507 | int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) { |
b1a4981a | 508 | _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; |
d7afd945 LP |
509 | const char *e; |
510 | int r; | |
511 | ||
512 | assert(ret); | |
513 | ||
61252bae ZJS |
514 | /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal |
515 | * turned on. */ | |
d7afd945 LP |
516 | |
517 | r = sd_bus_new(&bus); | |
518 | if (r < 0) | |
519 | return r; | |
520 | ||
0ddf50ff YW |
521 | if (description) { |
522 | r = sd_bus_set_description(bus, description); | |
523 | if (r < 0) | |
524 | return r; | |
525 | } | |
526 | ||
d7afd945 LP |
527 | e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS"); |
528 | if (!e) | |
529 | e = DEFAULT_SYSTEM_BUS_ADDRESS; | |
530 | ||
531 | r = sd_bus_set_address(bus, e); | |
532 | if (r < 0) | |
533 | return r; | |
534 | ||
535 | r = sd_bus_set_bus_client(bus, true); | |
536 | if (r < 0) | |
537 | return r; | |
538 | ||
d7afd945 LP |
539 | r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS); |
540 | if (r < 0) | |
541 | return r; | |
542 | ||
543 | r = sd_bus_set_watch_bind(bus, true); | |
544 | if (r < 0) | |
545 | return r; | |
546 | ||
547 | r = sd_bus_set_connected_signal(bus, true); | |
548 | if (r < 0) | |
549 | return r; | |
550 | ||
551 | r = sd_bus_start(bus); | |
552 | if (r < 0) | |
553 | return r; | |
554 | ||
1cc6c93a | 555 | *ret = TAKE_PTR(bus); |
d7afd945 LP |
556 | |
557 | return 0; | |
558 | } | |
906cb2eb | 559 | |
19017acb LP |
560 | int bus_reply_pair_array(sd_bus_message *m, char **l) { |
561 | _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; | |
562 | char **k, **v; | |
563 | int r; | |
564 | ||
565 | assert(m); | |
566 | ||
61252bae ZJS |
567 | /* Reply to the specified message with a message containing a dictionary put together from the |
568 | * specified strv */ | |
19017acb LP |
569 | |
570 | r = sd_bus_message_new_method_return(m, &reply); | |
571 | if (r < 0) | |
572 | return r; | |
573 | ||
574 | r = sd_bus_message_open_container(reply, 'a', "{ss}"); | |
575 | if (r < 0) | |
576 | return r; | |
577 | ||
578 | STRV_FOREACH_PAIR(k, v, l) { | |
579 | r = sd_bus_message_append(reply, "{ss}", *k, *v); | |
580 | if (r < 0) | |
581 | return r; | |
582 | } | |
583 | ||
584 | r = sd_bus_message_close_container(reply); | |
585 | if (r < 0) | |
586 | return r; | |
587 | ||
588 | return sd_bus_send(NULL, reply, NULL); | |
589 | } | |
2a66c2a1 LP |
590 | |
591 | static void bus_message_unref_wrapper(void *m) { | |
592 | sd_bus_message_unref(m); | |
593 | } | |
594 | ||
595 | const struct hash_ops bus_message_hash_ops = { | |
596 | .hash = trivial_hash_func, | |
597 | .compare = trivial_compare_func, | |
598 | .free_value = bus_message_unref_wrapper, | |
599 | }; |