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