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