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