1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/resource.h>
9 #include <sys/socket.h>
13 #include "sd-daemon.h"
17 #include "alloc-util.h"
18 #include "bus-internal.h"
19 #include "bus-introspect.h"
20 #include "bus-label.h"
21 #include "bus-message.h"
24 #include "cgroup-util.h"
26 #include "mountpoint-util.h"
28 #include "parse-util.h"
29 #include "path-util.h"
30 #include "rlimit-util.h"
31 #include "socket-util.h"
32 #include "stdio-util.h"
34 #include "user-util.h"
36 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
37 sd_event
*e
= userdata
;
42 sd_bus_close(sd_bus_message_get_bus(m
));
48 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
57 /* We unregister the name here and then wait for the
58 * NameOwnerChanged signal for this event to arrive before we
59 * quit. We do this in order to make sure that any queued
60 * requests are still processed before we really exit. */
62 r
= sd_bus_get_unique_name(bus
, &unique
);
67 "sender='org.freedesktop.DBus',"
69 "interface='org.freedesktop.DBus',"
70 "member='NameOwnerChanged',"
71 "path='/org/freedesktop/DBus',"
73 "arg1='", unique
, "',",
76 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
80 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
87 int bus_event_loop_with_idle(
92 check_idle_t check_idle
,
104 r
= sd_event_get_state(e
);
107 if (r
== SD_EVENT_FINISHED
)
111 idle
= check_idle(userdata
);
115 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
119 if (r
== 0 && !exiting
&& idle
) {
120 /* Inform the service manager that we are going down, so that it will queue all
121 * further start requests, instead of assuming we are already running. */
122 sd_notify(false, "STOPPING=1");
124 r
= bus_async_unregister_and_exit(e
, bus
, name
);
133 r
= sd_event_get_exit_code(e
, &code
);
140 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
141 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
142 int r
, has_owner
= 0;
147 r
= sd_bus_call_method(c
,
148 "org.freedesktop.DBus",
149 "/org/freedesktop/dbus",
150 "org.freedesktop.DBus",
159 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
161 return sd_bus_error_set_errno(error
, r
);
166 int bus_check_peercred(sd_bus
*c
) {
172 fd
= sd_bus_get_fd(c
);
176 r
= getpeercred(fd
, &ucred
);
180 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
186 int bus_connect_system_systemd(sd_bus
**_bus
) {
187 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
193 return sd_bus_default_system(_bus
);
195 /* If we are root then let's talk directly to the system
196 * instance, instead of going via the bus */
198 r
= sd_bus_new(&bus
);
202 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
206 r
= sd_bus_start(bus
);
208 return sd_bus_default_system(_bus
);
210 r
= bus_check_peercred(bus
);
214 *_bus
= TAKE_PTR(bus
);
219 int bus_connect_user_systemd(sd_bus
**_bus
) {
220 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
221 _cleanup_free_
char *ee
= NULL
;
227 e
= secure_getenv("XDG_RUNTIME_DIR");
229 return sd_bus_default_user(_bus
);
231 ee
= bus_address_escape(e
);
235 r
= sd_bus_new(&bus
);
239 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
243 r
= sd_bus_start(bus
);
245 return sd_bus_default_user(_bus
);
247 r
= bus_check_peercred(bus
);
251 *_bus
= TAKE_PTR(bus
);
256 int bus_print_property_value(const char *name
, const char *expected_value
, bool only_value
, const char *value
) {
259 if (expected_value
&& !streq_ptr(expected_value
, value
))
265 printf("%s=%s\n", name
, value
);
270 int bus_print_property_valuef(const char *name
, const char *expected_value
, bool only_value
, const char *fmt
, ...) {
277 if (expected_value
) {
278 _cleanup_free_
char *s
= NULL
;
281 r
= vasprintf(&s
, fmt
, ap
);
286 if (streq_ptr(expected_value
, s
)) {
290 printf("%s=%s\n", name
, s
);
306 static int bus_print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
308 const char *contents
;
314 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
320 case SD_BUS_TYPE_STRING
: {
323 r
= sd_bus_message_read_basic(m
, type
, &s
);
327 if (all
|| !isempty(s
)) {
330 /* This property has a single value, so we need to take
331 * care not to print a new line, everything else is OK. */
332 good
= !strchr(s
, '\n');
333 bus_print_property_value(name
, expected_value
, value
, good
? s
: "[unprintable]");
339 case SD_BUS_TYPE_BOOLEAN
: {
342 r
= sd_bus_message_read_basic(m
, type
, &b
);
346 if (expected_value
&& parse_boolean(expected_value
) != b
)
349 bus_print_property_value(name
, NULL
, value
, yes_no(b
));
353 case SD_BUS_TYPE_UINT64
: {
356 r
= sd_bus_message_read_basic(m
, type
, &u
);
360 /* Yes, heuristics! But we can change this check
361 * should it turn out to not be sufficient */
363 if (endswith(name
, "Timestamp") ||
364 STR_IN_SET(name
, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
365 char timestamp
[FORMAT_TIMESTAMP_MAX
];
368 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
370 bus_print_property_value(name
, expected_value
, value
, strempty(t
));
372 } else if (strstr(name
, "USec")) {
373 char timespan
[FORMAT_TIMESPAN_MAX
];
375 (void) format_timespan(timespan
, sizeof(timespan
), u
, 0);
376 bus_print_property_value(name
, expected_value
, value
, timespan
);
378 } else if (streq(name
, "CoredumpFilter")) {
379 char buf
[STRLEN("0xFFFFFFFF")];
381 xsprintf(buf
, "0x%"PRIx64
, u
);
382 bus_print_property_value(name
, expected_value
, value
, buf
);
384 } else if (streq(name
, "RestrictNamespaces")) {
385 _cleanup_free_
char *s
= NULL
;
388 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
390 else if (FLAGS_SET(u
, NAMESPACE_FLAGS_ALL
))
393 r
= namespace_flags_to_string(u
, &s
);
397 result
= strempty(s
);
400 bus_print_property_value(name
, expected_value
, value
, result
);
402 } else if (streq(name
, "MountFlags")) {
405 result
= mount_propagation_flags_to_string(u
);
409 bus_print_property_value(name
, expected_value
, value
, result
);
411 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
412 _cleanup_free_
char *s
= NULL
;
414 r
= capability_set_to_string_alloc(u
, &s
);
418 bus_print_property_value(name
, expected_value
, value
, s
);
420 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
421 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
422 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
423 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
424 (endswith(name
, "NSec") && u
== (uint64_t) -1))
426 bus_print_property_value(name
, expected_value
, value
, "[not set]");
428 else if ((STR_IN_SET(name
, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
429 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
430 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
431 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
433 bus_print_property_value(name
, expected_value
, value
, "infinity");
434 else if (STR_IN_SET(name
, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u
== (uint64_t) -1)
435 bus_print_property_value(name
, expected_value
, value
, "[no data]");
437 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu64
, u
);
442 case SD_BUS_TYPE_INT64
: {
445 r
= sd_bus_message_read_basic(m
, type
, &i
);
449 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi64
, i
);
453 case SD_BUS_TYPE_UINT32
: {
456 r
= sd_bus_message_read_basic(m
, type
, &u
);
460 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
461 bus_print_property_valuef(name
, expected_value
, value
, "%04o", u
);
463 else if (streq(name
, "UID")) {
464 if (u
== UID_INVALID
)
465 bus_print_property_value(name
, expected_value
, value
, "[not set]");
467 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
468 } else if (streq(name
, "GID")) {
469 if (u
== GID_INVALID
)
470 bus_print_property_value(name
, expected_value
, value
, "[not set]");
472 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
474 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
479 case SD_BUS_TYPE_INT32
: {
482 r
= sd_bus_message_read_basic(m
, type
, &i
);
486 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi32
, i
);
490 case SD_BUS_TYPE_DOUBLE
: {
493 r
= sd_bus_message_read_basic(m
, type
, &d
);
497 bus_print_property_valuef(name
, expected_value
, value
, "%g", d
);
501 case SD_BUS_TYPE_ARRAY
:
502 if (streq(contents
, "s")) {
506 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, contents
);
510 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
511 _cleanup_free_
char *e
= NULL
;
513 e
= shell_maybe_quote(str
, ESCAPE_BACKSLASH_ONELINE
);
529 if (first
&& all
&& !value
)
534 r
= sd_bus_message_exit_container(m
);
540 } else if (streq(contents
, "y")) {
544 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
554 for (i
= 0; i
< n
; i
++)
555 printf("%02x", u
[i
]);
562 } else if (streq(contents
, "u")) {
566 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
576 for (i
= 0; i
< n
; i
++)
577 printf("%08x", u
[i
]);
591 int bus_message_print_all_properties(
593 bus_message_print_t func
,
597 Set
**found_properties
) {
603 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
607 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
608 _cleanup_free_
char *name_with_equal
= NULL
;
609 const char *name
, *contents
, *expected_value
= NULL
;
611 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &name
);
615 if (found_properties
) {
616 r
= set_ensure_allocated(found_properties
, &string_hash_ops
);
620 r
= set_put(*found_properties
, name
);
621 if (r
< 0 && r
!= -EEXIST
)
625 name_with_equal
= strjoin(name
, "=");
626 if (!name_with_equal
)
629 if (!filter
|| strv_find(filter
, name
) ||
630 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
631 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
635 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
640 r
= func(name
, expected_value
, m
, value
, all
);
642 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
646 if (all
&& !expected_value
)
647 printf("%s=[unprintable]\n", name
);
648 /* skip what we didn't read */
649 r
= sd_bus_message_skip(m
, contents
);
654 r
= sd_bus_message_exit_container(m
);
658 r
= sd_bus_message_skip(m
, "v");
663 r
= sd_bus_message_exit_container(m
);
670 r
= sd_bus_message_exit_container(m
);
677 int bus_print_all_properties(
681 bus_message_print_t func
,
685 Set
**found_properties
) {
687 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
688 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
694 r
= sd_bus_call_method(bus
,
697 "org.freedesktop.DBus.Properties",
705 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
708 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
709 sd_id128_t
*p
= userdata
;
714 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
721 memcpy((*p
).bytes
, v
, n
);
728 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
732 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
738 case SD_BUS_TYPE_STRING
:
739 case SD_BUS_TYPE_OBJECT_PATH
: {
740 const char **p
= userdata
;
743 r
= sd_bus_message_read_basic(m
, type
, &s
);
750 if (flags
& BUS_MAP_STRDUP
)
751 return free_and_strdup((char **) userdata
, s
);
757 case SD_BUS_TYPE_ARRAY
: {
758 _cleanup_strv_free_
char **l
= NULL
;
759 char ***p
= userdata
;
761 r
= bus_message_read_strv_extend(m
, &l
);
765 return strv_extend_strv(p
, l
, false);
768 case SD_BUS_TYPE_BOOLEAN
: {
771 r
= sd_bus_message_read_basic(m
, type
, &b
);
775 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
776 *(bool*) userdata
= b
;
778 *(int*) userdata
= b
;
783 case SD_BUS_TYPE_INT32
:
784 case SD_BUS_TYPE_UINT32
: {
785 uint32_t u
, *p
= userdata
;
787 r
= sd_bus_message_read_basic(m
, type
, &u
);
795 case SD_BUS_TYPE_INT64
:
796 case SD_BUS_TYPE_UINT64
: {
797 uint64_t t
, *p
= userdata
;
799 r
= sd_bus_message_read_basic(m
, type
, &t
);
807 case SD_BUS_TYPE_DOUBLE
: {
808 double d
, *p
= userdata
;
810 r
= sd_bus_message_read_basic(m
, type
, &d
);
821 int bus_message_map_all_properties(
823 const struct bus_properties_map
*map
,
833 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
837 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
838 const struct bus_properties_map
*prop
;
840 const char *contents
;
844 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
848 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
849 if (streq(map
[i
].member
, member
)) {
855 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
859 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
863 v
= (uint8_t *)userdata
+ prop
->offset
;
865 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
867 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
871 r
= sd_bus_message_exit_container(m
);
875 r
= sd_bus_message_skip(m
, "v");
880 r
= sd_bus_message_exit_container(m
);
887 return sd_bus_message_exit_container(m
);
890 int bus_map_all_properties(
892 const char *destination
,
894 const struct bus_properties_map
*map
,
897 sd_bus_message
**reply
,
900 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
907 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
909 r
= sd_bus_call_method(
913 "org.freedesktop.DBus.Properties",
921 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
926 *reply
= sd_bus_message_ref(m
);
931 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
932 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
935 assert(transport
>= 0);
936 assert(transport
< _BUS_TRANSPORT_MAX
);
939 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
940 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
944 case BUS_TRANSPORT_LOCAL
:
946 r
= sd_bus_default_user(&bus
);
948 if (sd_booted() <= 0) {
949 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
950 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
954 r
= sd_bus_default_system(&bus
);
958 case BUS_TRANSPORT_REMOTE
:
959 r
= sd_bus_open_system_remote(&bus
, host
);
962 case BUS_TRANSPORT_MACHINE
:
963 r
= sd_bus_open_system_machine(&bus
, host
);
967 assert_not_reached("Hmm, unknown transport type.");
972 r
= sd_bus_set_exit_on_disconnect(bus
, true);
976 *ret
= TAKE_PTR(bus
);
981 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
984 assert(transport
>= 0);
985 assert(transport
< _BUS_TRANSPORT_MAX
);
988 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
989 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
993 case BUS_TRANSPORT_LOCAL
:
995 r
= bus_connect_user_systemd(bus
);
997 if (sd_booted() <= 0)
998 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
999 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1000 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1001 r
= bus_connect_system_systemd(bus
);
1005 case BUS_TRANSPORT_REMOTE
:
1006 r
= sd_bus_open_system_remote(bus
, host
);
1009 case BUS_TRANSPORT_MACHINE
:
1010 r
= sd_bus_open_system_machine(bus
, host
);
1014 assert_not_reached("Hmm, unknown transport type.");
1020 int bus_property_get_bool(
1023 const char *interface
,
1024 const char *property
,
1025 sd_bus_message
*reply
,
1027 sd_bus_error
*error
) {
1029 int b
= *(bool*) userdata
;
1031 return sd_bus_message_append_basic(reply
, 'b', &b
);
1034 int bus_property_set_bool(
1037 const char *interface
,
1038 const char *property
,
1039 sd_bus_message
*value
,
1041 sd_bus_error
*error
) {
1045 r
= sd_bus_message_read(value
, "b", &b
);
1049 *(bool*) userdata
= b
;
1053 int bus_property_get_id128(
1056 const char *interface
,
1057 const char *property
,
1058 sd_bus_message
*reply
,
1060 sd_bus_error
*error
) {
1062 sd_id128_t
*id
= userdata
;
1064 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1065 return sd_bus_message_append(reply
, "ay", 0);
1067 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1070 #if __SIZEOF_SIZE_T__ != 8
1071 int bus_property_get_size(
1074 const char *interface
,
1075 const char *property
,
1076 sd_bus_message
*reply
,
1078 sd_bus_error
*error
) {
1080 uint64_t sz
= *(size_t*) userdata
;
1082 return sd_bus_message_append_basic(reply
, 't', &sz
);
1086 #if __SIZEOF_LONG__ != 8
1087 int bus_property_get_long(
1090 const char *interface
,
1091 const char *property
,
1092 sd_bus_message
*reply
,
1094 sd_bus_error
*error
) {
1096 int64_t l
= *(long*) userdata
;
1098 return sd_bus_message_append_basic(reply
, 'x', &l
);
1101 int bus_property_get_ulong(
1104 const char *interface
,
1105 const char *property
,
1106 sd_bus_message
*reply
,
1108 sd_bus_error
*error
) {
1110 uint64_t ul
= *(unsigned long*) userdata
;
1112 return sd_bus_message_append_basic(reply
, 't', &ul
);
1117 * bus_path_encode_unique() - encode unique object path
1118 * @b: bus connection or NULL
1119 * @prefix: object path prefix
1120 * @sender_id: unique-name of client, or NULL
1121 * @external_id: external ID to be chosen by client, or NULL
1122 * @ret_path: storage for encoded object path pointer
1124 * Whenever we provide a bus API that allows clients to create and manage
1125 * server-side objects, we need to provide a unique name for these objects. If
1126 * we let the server choose the name, we suffer from a race condition: If a
1127 * client creates an object asynchronously, it cannot destroy that object until
1128 * it received the method reply. It cannot know the name of the new object,
1129 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1131 * Therefore, many APIs allow the client to choose the unique name for newly
1132 * created objects. There're two problems to solve, though:
1133 * 1) Object names are usually defined via dbus object paths, which are
1134 * usually globally namespaced. Therefore, multiple clients must be able
1135 * to choose unique object names without interference.
1136 * 2) If multiple libraries share the same bus connection, they must be
1137 * able to choose unique object names without interference.
1138 * The first problem is solved easily by prefixing a name with the
1139 * unique-bus-name of a connection. The server side must enforce this and
1140 * reject any other name. The second problem is solved by providing unique
1141 * suffixes from within sd-bus.
1143 * This helper allows clients to create unique object-paths. It uses the
1144 * template '/prefix/sender_id/external_id' and returns the new path in
1145 * @ret_path (must be freed by the caller).
1146 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1147 * NULL, this function allocates a unique suffix via @b (by requesting a new
1148 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1151 * Returns: 0 on success, negative error code on failure.
1153 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1154 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1155 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1158 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1159 assert_return(sd_bus_object_path_is_valid(prefix
), -EINVAL
);
1160 assert_return(ret_path
, -EINVAL
);
1163 r
= sd_bus_get_unique_name(b
, &sender_id
);
1169 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1170 external_id
= external_buf
;
1173 sender_label
= bus_label_escape(sender_id
);
1177 external_label
= bus_label_escape(external_id
);
1178 if (!external_label
)
1181 p
= path_join(prefix
, sender_label
, external_label
);
1190 * bus_path_decode_unique() - decode unique object path
1191 * @path: object path to decode
1192 * @prefix: object path prefix
1193 * @ret_sender: output parameter for sender-id label
1194 * @ret_external: output parameter for external-id label
1196 * This does the reverse of bus_path_encode_unique() (see its description for
1197 * details). Both trailing labels, sender-id and external-id, are unescaped and
1198 * returned in the given output parameters (the caller must free them).
1200 * Note that this function returns 0 if the path does not match the template
1201 * (see bus_path_encode_unique()), 1 if it matched.
1203 * Returns: Negative error code on failure, 0 if the given object path does not
1204 * match the template (return parameters are set to NULL), 1 if it was
1205 * parsed successfully (return parameters contain allocated labels).
1207 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1209 char *sender
, *external
;
1211 assert(sd_bus_object_path_is_valid(path
));
1212 assert(sd_bus_object_path_is_valid(prefix
));
1214 assert(ret_external
);
1216 p
= object_path_startswith(path
, prefix
);
1219 *ret_external
= NULL
;
1226 *ret_external
= NULL
;
1230 sender
= bus_label_unescape_n(p
, q
- p
);
1231 external
= bus_label_unescape(q
+ 1);
1232 if (!sender
|| !external
) {
1238 *ret_sender
= sender
;
1239 *ret_external
= external
;
1243 int bus_property_get_rlimit(
1246 const char *interface
,
1247 const char *property
,
1248 sd_bus_message
*reply
,
1250 sd_bus_error
*error
) {
1252 const char *is_soft
;
1261 is_soft
= endswith(property
, "Soft");
1263 rl
= *(struct rlimit
**) userdata
;
1265 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1267 struct rlimit buf
= {};
1271 /* Chop off "Soft" suffix */
1272 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1274 /* Skip over any prefix, such as "Default" */
1275 assert_se(p
= strstr(s
, "Limit"));
1277 z
= rlimit_from_string(p
+ 5);
1280 (void) getrlimit(z
, &buf
);
1281 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1284 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1286 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1288 return sd_bus_message_append(reply
, "t", u
);
1291 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1297 /* Continues adding after failure, and returns the first failure. */
1299 STRV_FOREACH(i
, l
) {
1302 k
= sd_bus_track_add_name(t
, *i
);
1303 if (k
< 0 && r
>= 0)
1310 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1311 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1317 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1320 r
= sd_bus_new(&bus
);
1325 r
= sd_bus_set_description(bus
, description
);
1330 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1332 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1334 r
= sd_bus_set_address(bus
, e
);
1338 r
= sd_bus_set_bus_client(bus
, true);
1342 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1346 r
= sd_bus_set_watch_bind(bus
, true);
1350 r
= sd_bus_set_connected_signal(bus
, true);
1354 r
= sd_bus_start(bus
);
1358 *ret
= TAKE_PTR(bus
);
1363 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1364 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1370 /* Reply to the specified message with a message containing a dictionary put together from the
1373 r
= sd_bus_message_new_method_return(m
, &reply
);
1377 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1381 STRV_FOREACH_PAIR(k
, v
, l
) {
1382 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1387 r
= sd_bus_message_close_container(reply
);
1391 return sd_bus_send(NULL
, reply
, NULL
);
1394 static void bus_message_unref_wrapper(void *m
) {
1395 sd_bus_message_unref(m
);
1398 const struct hash_ops bus_message_hash_ops
= {
1399 .hash
= trivial_hash_func
,
1400 .compare
= trivial_compare_func
,
1401 .free_value
= bus_message_unref_wrapper
,
1404 /* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface
1405 * strings encapsulated within a single struct.
1407 int bus_call_method_async(
1410 const BusLocator
*locator
,
1412 sd_bus_message_handler_t callback
,
1414 const char *types
, ...) {
1421 va_start(ap
, types
);
1422 r
= sd_bus_call_method_asyncv(bus
, slot
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, userdata
, types
, ap
);
1428 int bus_call_method(
1430 const BusLocator
*locator
,
1432 sd_bus_error
*error
,
1433 sd_bus_message
**reply
,
1434 const char *types
, ...) {
1441 va_start(ap
, types
);
1442 r
= sd_bus_call_methodv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, reply
, types
, ap
);
1448 int bus_get_property(
1450 const BusLocator
*locator
,
1452 sd_bus_error
*error
,
1453 sd_bus_message
**reply
,
1458 return sd_bus_get_property(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, reply
, type
);
1461 int bus_get_property_trivial(
1463 const BusLocator
*locator
,
1465 sd_bus_error
*error
,
1466 char type
, void *ptr
) {
1470 return sd_bus_get_property_trivial(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, type
, ptr
);
1473 int bus_get_property_string(
1475 const BusLocator
*locator
,
1477 sd_bus_error
*error
,
1482 return sd_bus_get_property_string(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, ret
);
1485 int bus_get_property_strv(
1487 const BusLocator
*locator
,
1489 sd_bus_error
*error
,
1494 return sd_bus_get_property_strv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, ret
);
1497 int bus_set_property(
1499 const BusLocator
*locator
,
1501 sd_bus_error
*error
,
1502 const char *type
, ...) {
1510 r
= sd_bus_set_propertyv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, type
, ap
);
1516 int bus_match_signal(
1519 const BusLocator
*locator
,
1521 sd_bus_message_handler_t callback
,
1526 return sd_bus_match_signal(bus
, ret
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, userdata
);
1529 int bus_match_signal_async(
1532 const BusLocator
*locator
,
1534 sd_bus_message_handler_t callback
,
1535 sd_bus_message_handler_t install_callback
,
1540 return sd_bus_match_signal_async(bus
, ret
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, install_callback
, userdata
);
1543 int bus_message_new_method_call(
1546 const BusLocator
*locator
,
1547 const char *member
) {
1551 return sd_bus_message_new_method_call(bus
, m
, locator
->destination
, locator
->path
, locator
->interface
, member
);
1554 int bus_add_implementation(sd_bus
*bus
, const BusObjectImplementation
*impl
, void *userdata
) {
1557 log_debug("Registering bus object implementation for path=%s iface=%s", impl
->path
, impl
->interface
);
1559 for (const sd_bus_vtable
**p
= impl
->vtables
; p
&& *p
; p
++) {
1560 r
= sd_bus_add_object_vtable(bus
, NULL
,
1566 return log_error_errno(r
, "Failed to register bus path %s with interface %s: %m",
1571 for (const BusObjectVtablePair
*p
= impl
->fallback_vtables
; p
&& p
->vtable
; p
++) {
1572 r
= sd_bus_add_fallback_vtable(bus
, NULL
,
1579 return log_error_errno(r
, "Failed to register bus path %s with interface %s: %m",
1584 if (impl
->node_enumerator
) {
1585 r
= sd_bus_add_node_enumerator(bus
, NULL
,
1587 impl
->node_enumerator
,
1590 return log_error_errno(r
, "Failed to add node enumerator for %s: %m",
1594 if (impl
->manager
) {
1595 r
= sd_bus_add_object_manager(bus
, NULL
, impl
->path
);
1597 return log_error_errno(r
, "Failed to add object manager for %s: %m", impl
->path
);
1600 for (size_t i
= 0; impl
->children
&& impl
->children
[i
]; i
++) {
1601 r
= bus_add_implementation(bus
, impl
->children
[i
], userdata
);
1609 static const BusObjectImplementation
* find_implementation(
1610 const char *pattern
,
1611 const BusObjectImplementation
* const* bus_objects
) {
1613 for (size_t i
= 0; bus_objects
&& bus_objects
[i
]; i
++) {
1614 const BusObjectImplementation
*impl
= bus_objects
[i
];
1616 if (STR_IN_SET(pattern
, impl
->path
, impl
->interface
))
1619 impl
= find_implementation(pattern
, impl
->children
);
1627 static int bus_introspect_implementation(
1628 struct introspect
*intro
,
1629 const BusObjectImplementation
*impl
) {
1632 for (const sd_bus_vtable
**p
= impl
->vtables
; p
&& *p
; p
++) {
1633 r
= introspect_write_interface(intro
, impl
->interface
, *p
);
1635 return log_error_errno(r
, "Failed to write introspection data: %m");
1638 for (const BusObjectVtablePair
*p
= impl
->fallback_vtables
; p
&& p
->vtable
; p
++) {
1639 r
= introspect_write_interface(intro
, impl
->interface
, p
->vtable
);
1641 return log_error_errno(r
, "Failed to write introspection data: %m");
1647 static void list_paths(
1649 const BusObjectImplementation
* const* bus_objects
) {
1651 for (size_t i
= 0; bus_objects
[i
]; i
++) {
1652 fprintf(out
, "%s\t%s\n", bus_objects
[i
]->path
, bus_objects
[i
]->interface
);
1653 if (bus_objects
[i
]->children
)
1654 list_paths(out
, bus_objects
[i
]->children
);
1658 int bus_introspect_implementations(
1660 const char *pattern
,
1661 const BusObjectImplementation
* const* bus_objects
) {
1663 const BusObjectImplementation
*impl
, *main_impl
= NULL
;
1664 _cleanup_free_
char *s
= NULL
;
1667 if (streq(pattern
, "list")) {
1668 list_paths(out
, bus_objects
);
1672 struct introspect intro
= {};
1673 bool is_interface
= sd_bus_interface_name_is_valid(pattern
);
1675 impl
= find_implementation(pattern
, bus_objects
);
1677 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1679 is_interface
? "Interface" : "Object path",
1682 /* We use trusted=false here to get all the @org.freedesktop.systemd1.Privileged annotations. */
1683 r
= introspect_begin(&intro
, false);
1685 return log_error_errno(r
, "Failed to write introspection data: %m");
1687 r
= introspect_write_default_interfaces(&intro
, impl
->manager
);
1689 return log_error_errno(r
, "Failed to write introspection data: %m");
1691 /* Check if there is a non-fallback path that applies to the given interface, also
1692 * print it. This is useful in the case of units: o.fd.systemd1.Service is declared
1693 * as a fallback vtable for o/fd/systemd1/unit, and we also want to print
1694 * o.fd.systemd1.Unit, which is the non-fallback implementation. */
1695 if (impl
->fallback_vtables
&& is_interface
)
1696 main_impl
= find_implementation(impl
->path
, bus_objects
);
1699 bus_introspect_implementation(&intro
, main_impl
);
1701 if (impl
!= main_impl
)
1702 bus_introspect_implementation(&intro
, impl
);
1704 _cleanup_set_free_ Set
*nodes
= NULL
;
1706 for (size_t i
= 0; impl
->children
&& impl
->children
[i
]; i
++) {
1707 r
= set_put_strdup(&nodes
, impl
->children
[i
]->path
);
1712 r
= introspect_write_child_nodes(&intro
, nodes
, impl
->path
);
1716 r
= introspect_finish(&intro
, &s
);
1718 return log_error_errno(r
, "Failed to write introspection data: %m");