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_put(found_properties
, &string_hash_ops
, name
);
617 if (r
< 0 && r
!= -EEXIST
)
621 name_with_equal
= strjoin(name
, "=");
622 if (!name_with_equal
)
625 if (!filter
|| strv_find(filter
, name
) ||
626 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
627 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
631 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
636 r
= func(name
, expected_value
, m
, value
, all
);
638 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
642 if (all
&& !expected_value
)
643 printf("%s=[unprintable]\n", name
);
644 /* skip what we didn't read */
645 r
= sd_bus_message_skip(m
, contents
);
650 r
= sd_bus_message_exit_container(m
);
654 r
= sd_bus_message_skip(m
, "v");
659 r
= sd_bus_message_exit_container(m
);
666 r
= sd_bus_message_exit_container(m
);
673 int bus_print_all_properties(
677 bus_message_print_t func
,
681 Set
**found_properties
) {
683 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
684 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
690 r
= sd_bus_call_method(bus
,
693 "org.freedesktop.DBus.Properties",
701 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
704 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
705 sd_id128_t
*p
= userdata
;
710 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
717 memcpy((*p
).bytes
, v
, n
);
724 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
728 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
734 case SD_BUS_TYPE_STRING
:
735 case SD_BUS_TYPE_OBJECT_PATH
: {
736 const char **p
= userdata
;
739 r
= sd_bus_message_read_basic(m
, type
, &s
);
746 if (flags
& BUS_MAP_STRDUP
)
747 return free_and_strdup((char **) userdata
, s
);
753 case SD_BUS_TYPE_ARRAY
: {
754 _cleanup_strv_free_
char **l
= NULL
;
755 char ***p
= userdata
;
757 r
= bus_message_read_strv_extend(m
, &l
);
761 return strv_extend_strv(p
, l
, false);
764 case SD_BUS_TYPE_BOOLEAN
: {
767 r
= sd_bus_message_read_basic(m
, type
, &b
);
771 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
772 *(bool*) userdata
= b
;
774 *(int*) userdata
= b
;
779 case SD_BUS_TYPE_INT32
:
780 case SD_BUS_TYPE_UINT32
: {
781 uint32_t u
, *p
= userdata
;
783 r
= sd_bus_message_read_basic(m
, type
, &u
);
791 case SD_BUS_TYPE_INT64
:
792 case SD_BUS_TYPE_UINT64
: {
793 uint64_t t
, *p
= userdata
;
795 r
= sd_bus_message_read_basic(m
, type
, &t
);
803 case SD_BUS_TYPE_DOUBLE
: {
804 double d
, *p
= userdata
;
806 r
= sd_bus_message_read_basic(m
, type
, &d
);
817 int bus_message_map_all_properties(
819 const struct bus_properties_map
*map
,
829 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
833 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
834 const struct bus_properties_map
*prop
;
836 const char *contents
;
840 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
844 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
845 if (streq(map
[i
].member
, member
)) {
851 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
855 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
859 v
= (uint8_t *)userdata
+ prop
->offset
;
861 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
863 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
867 r
= sd_bus_message_exit_container(m
);
871 r
= sd_bus_message_skip(m
, "v");
876 r
= sd_bus_message_exit_container(m
);
883 return sd_bus_message_exit_container(m
);
886 int bus_map_all_properties(
888 const char *destination
,
890 const struct bus_properties_map
*map
,
893 sd_bus_message
**reply
,
896 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
903 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
905 r
= sd_bus_call_method(
909 "org.freedesktop.DBus.Properties",
917 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
922 *reply
= sd_bus_message_ref(m
);
927 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
928 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
931 assert(transport
>= 0);
932 assert(transport
< _BUS_TRANSPORT_MAX
);
935 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
936 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
940 case BUS_TRANSPORT_LOCAL
:
942 r
= sd_bus_default_user(&bus
);
944 if (sd_booted() <= 0) {
945 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
946 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
950 r
= sd_bus_default_system(&bus
);
954 case BUS_TRANSPORT_REMOTE
:
955 r
= sd_bus_open_system_remote(&bus
, host
);
958 case BUS_TRANSPORT_MACHINE
:
959 r
= sd_bus_open_system_machine(&bus
, host
);
963 assert_not_reached("Hmm, unknown transport type.");
968 r
= sd_bus_set_exit_on_disconnect(bus
, true);
972 *ret
= TAKE_PTR(bus
);
977 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
980 assert(transport
>= 0);
981 assert(transport
< _BUS_TRANSPORT_MAX
);
984 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
985 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
989 case BUS_TRANSPORT_LOCAL
:
991 r
= bus_connect_user_systemd(bus
);
993 if (sd_booted() <= 0)
994 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
995 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
996 "System has not been booted with systemd as init system (PID 1). Can't operate.");
997 r
= bus_connect_system_systemd(bus
);
1001 case BUS_TRANSPORT_REMOTE
:
1002 r
= sd_bus_open_system_remote(bus
, host
);
1005 case BUS_TRANSPORT_MACHINE
:
1006 r
= sd_bus_open_system_machine(bus
, host
);
1010 assert_not_reached("Hmm, unknown transport type.");
1016 int bus_property_get_bool(
1019 const char *interface
,
1020 const char *property
,
1021 sd_bus_message
*reply
,
1023 sd_bus_error
*error
) {
1025 int b
= *(bool*) userdata
;
1027 return sd_bus_message_append_basic(reply
, 'b', &b
);
1030 int bus_property_set_bool(
1033 const char *interface
,
1034 const char *property
,
1035 sd_bus_message
*value
,
1037 sd_bus_error
*error
) {
1041 r
= sd_bus_message_read(value
, "b", &b
);
1045 *(bool*) userdata
= b
;
1049 int bus_property_get_id128(
1052 const char *interface
,
1053 const char *property
,
1054 sd_bus_message
*reply
,
1056 sd_bus_error
*error
) {
1058 sd_id128_t
*id
= userdata
;
1060 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1061 return sd_bus_message_append(reply
, "ay", 0);
1063 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1066 #if __SIZEOF_SIZE_T__ != 8
1067 int bus_property_get_size(
1070 const char *interface
,
1071 const char *property
,
1072 sd_bus_message
*reply
,
1074 sd_bus_error
*error
) {
1076 uint64_t sz
= *(size_t*) userdata
;
1078 return sd_bus_message_append_basic(reply
, 't', &sz
);
1082 #if __SIZEOF_LONG__ != 8
1083 int bus_property_get_long(
1086 const char *interface
,
1087 const char *property
,
1088 sd_bus_message
*reply
,
1090 sd_bus_error
*error
) {
1092 int64_t l
= *(long*) userdata
;
1094 return sd_bus_message_append_basic(reply
, 'x', &l
);
1097 int bus_property_get_ulong(
1100 const char *interface
,
1101 const char *property
,
1102 sd_bus_message
*reply
,
1104 sd_bus_error
*error
) {
1106 uint64_t ul
= *(unsigned long*) userdata
;
1108 return sd_bus_message_append_basic(reply
, 't', &ul
);
1113 * bus_path_encode_unique() - encode unique object path
1114 * @b: bus connection or NULL
1115 * @prefix: object path prefix
1116 * @sender_id: unique-name of client, or NULL
1117 * @external_id: external ID to be chosen by client, or NULL
1118 * @ret_path: storage for encoded object path pointer
1120 * Whenever we provide a bus API that allows clients to create and manage
1121 * server-side objects, we need to provide a unique name for these objects. If
1122 * we let the server choose the name, we suffer from a race condition: If a
1123 * client creates an object asynchronously, it cannot destroy that object until
1124 * it received the method reply. It cannot know the name of the new object,
1125 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1127 * Therefore, many APIs allow the client to choose the unique name for newly
1128 * created objects. There're two problems to solve, though:
1129 * 1) Object names are usually defined via dbus object paths, which are
1130 * usually globally namespaced. Therefore, multiple clients must be able
1131 * to choose unique object names without interference.
1132 * 2) If multiple libraries share the same bus connection, they must be
1133 * able to choose unique object names without interference.
1134 * The first problem is solved easily by prefixing a name with the
1135 * unique-bus-name of a connection. The server side must enforce this and
1136 * reject any other name. The second problem is solved by providing unique
1137 * suffixes from within sd-bus.
1139 * This helper allows clients to create unique object-paths. It uses the
1140 * template '/prefix/sender_id/external_id' and returns the new path in
1141 * @ret_path (must be freed by the caller).
1142 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1143 * NULL, this function allocates a unique suffix via @b (by requesting a new
1144 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1147 * Returns: 0 on success, negative error code on failure.
1149 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1150 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1151 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1154 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1155 assert_return(sd_bus_object_path_is_valid(prefix
), -EINVAL
);
1156 assert_return(ret_path
, -EINVAL
);
1159 r
= sd_bus_get_unique_name(b
, &sender_id
);
1165 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1166 external_id
= external_buf
;
1169 sender_label
= bus_label_escape(sender_id
);
1173 external_label
= bus_label_escape(external_id
);
1174 if (!external_label
)
1177 p
= path_join(prefix
, sender_label
, external_label
);
1186 * bus_path_decode_unique() - decode unique object path
1187 * @path: object path to decode
1188 * @prefix: object path prefix
1189 * @ret_sender: output parameter for sender-id label
1190 * @ret_external: output parameter for external-id label
1192 * This does the reverse of bus_path_encode_unique() (see its description for
1193 * details). Both trailing labels, sender-id and external-id, are unescaped and
1194 * returned in the given output parameters (the caller must free them).
1196 * Note that this function returns 0 if the path does not match the template
1197 * (see bus_path_encode_unique()), 1 if it matched.
1199 * Returns: Negative error code on failure, 0 if the given object path does not
1200 * match the template (return parameters are set to NULL), 1 if it was
1201 * parsed successfully (return parameters contain allocated labels).
1203 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1205 char *sender
, *external
;
1207 assert(sd_bus_object_path_is_valid(path
));
1208 assert(sd_bus_object_path_is_valid(prefix
));
1210 assert(ret_external
);
1212 p
= object_path_startswith(path
, prefix
);
1215 *ret_external
= NULL
;
1222 *ret_external
= NULL
;
1226 sender
= bus_label_unescape_n(p
, q
- p
);
1227 external
= bus_label_unescape(q
+ 1);
1228 if (!sender
|| !external
) {
1234 *ret_sender
= sender
;
1235 *ret_external
= external
;
1239 int bus_property_get_rlimit(
1242 const char *interface
,
1243 const char *property
,
1244 sd_bus_message
*reply
,
1246 sd_bus_error
*error
) {
1248 const char *is_soft
;
1257 is_soft
= endswith(property
, "Soft");
1259 rl
= *(struct rlimit
**) userdata
;
1261 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1263 struct rlimit buf
= {};
1267 /* Chop off "Soft" suffix */
1268 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1270 /* Skip over any prefix, such as "Default" */
1271 assert_se(p
= strstr(s
, "Limit"));
1273 z
= rlimit_from_string(p
+ 5);
1276 (void) getrlimit(z
, &buf
);
1277 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1280 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1282 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1284 return sd_bus_message_append(reply
, "t", u
);
1287 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1293 /* Continues adding after failure, and returns the first failure. */
1295 STRV_FOREACH(i
, l
) {
1298 k
= sd_bus_track_add_name(t
, *i
);
1299 if (k
< 0 && r
>= 0)
1306 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1307 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1313 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1316 r
= sd_bus_new(&bus
);
1321 r
= sd_bus_set_description(bus
, description
);
1326 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1328 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1330 r
= sd_bus_set_address(bus
, e
);
1334 r
= sd_bus_set_bus_client(bus
, true);
1338 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1342 r
= sd_bus_set_watch_bind(bus
, true);
1346 r
= sd_bus_set_connected_signal(bus
, true);
1350 r
= sd_bus_start(bus
);
1354 *ret
= TAKE_PTR(bus
);
1359 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1360 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1366 /* Reply to the specified message with a message containing a dictionary put together from the
1369 r
= sd_bus_message_new_method_return(m
, &reply
);
1373 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1377 STRV_FOREACH_PAIR(k
, v
, l
) {
1378 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1383 r
= sd_bus_message_close_container(reply
);
1387 return sd_bus_send(NULL
, reply
, NULL
);
1390 static void bus_message_unref_wrapper(void *m
) {
1391 sd_bus_message_unref(m
);
1394 const struct hash_ops bus_message_hash_ops
= {
1395 .hash
= trivial_hash_func
,
1396 .compare
= trivial_compare_func
,
1397 .free_value
= bus_message_unref_wrapper
,
1400 /* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface
1401 * strings encapsulated within a single struct.
1403 int bus_call_method_async(
1406 const BusLocator
*locator
,
1408 sd_bus_message_handler_t callback
,
1410 const char *types
, ...) {
1417 va_start(ap
, types
);
1418 r
= sd_bus_call_method_asyncv(bus
, slot
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, userdata
, types
, ap
);
1424 int bus_call_method(
1426 const BusLocator
*locator
,
1428 sd_bus_error
*error
,
1429 sd_bus_message
**reply
,
1430 const char *types
, ...) {
1437 va_start(ap
, types
);
1438 r
= sd_bus_call_methodv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, reply
, types
, ap
);
1444 int bus_get_property(
1446 const BusLocator
*locator
,
1448 sd_bus_error
*error
,
1449 sd_bus_message
**reply
,
1454 return sd_bus_get_property(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, reply
, type
);
1457 int bus_get_property_trivial(
1459 const BusLocator
*locator
,
1461 sd_bus_error
*error
,
1462 char type
, void *ptr
) {
1466 return sd_bus_get_property_trivial(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, type
, ptr
);
1469 int bus_get_property_string(
1471 const BusLocator
*locator
,
1473 sd_bus_error
*error
,
1478 return sd_bus_get_property_string(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, ret
);
1481 int bus_get_property_strv(
1483 const BusLocator
*locator
,
1485 sd_bus_error
*error
,
1490 return sd_bus_get_property_strv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, ret
);
1493 int bus_set_property(
1495 const BusLocator
*locator
,
1497 sd_bus_error
*error
,
1498 const char *type
, ...) {
1506 r
= sd_bus_set_propertyv(bus
, locator
->destination
, locator
->path
, locator
->interface
, member
, error
, type
, ap
);
1512 int bus_match_signal(
1515 const BusLocator
*locator
,
1517 sd_bus_message_handler_t callback
,
1522 return sd_bus_match_signal(bus
, ret
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, userdata
);
1525 int bus_match_signal_async(
1528 const BusLocator
*locator
,
1530 sd_bus_message_handler_t callback
,
1531 sd_bus_message_handler_t install_callback
,
1536 return sd_bus_match_signal_async(bus
, ret
, locator
->destination
, locator
->path
, locator
->interface
, member
, callback
, install_callback
, userdata
);
1539 int bus_message_new_method_call(
1542 const BusLocator
*locator
,
1543 const char *member
) {
1547 return sd_bus_message_new_method_call(bus
, m
, locator
->destination
, locator
->path
, locator
->interface
, member
);
1550 int bus_add_implementation(sd_bus
*bus
, const BusObjectImplementation
*impl
, void *userdata
) {
1553 log_debug("Registering bus object implementation for path=%s iface=%s", impl
->path
, impl
->interface
);
1555 for (const sd_bus_vtable
**p
= impl
->vtables
; p
&& *p
; p
++) {
1556 r
= sd_bus_add_object_vtable(bus
, NULL
,
1562 return log_error_errno(r
, "Failed to register bus path %s with interface %s: %m",
1567 for (const BusObjectVtablePair
*p
= impl
->fallback_vtables
; p
&& p
->vtable
; p
++) {
1568 r
= sd_bus_add_fallback_vtable(bus
, NULL
,
1575 return log_error_errno(r
, "Failed to register bus path %s with interface %s: %m",
1580 if (impl
->node_enumerator
) {
1581 r
= sd_bus_add_node_enumerator(bus
, NULL
,
1583 impl
->node_enumerator
,
1586 return log_error_errno(r
, "Failed to add node enumerator for %s: %m",
1590 if (impl
->manager
) {
1591 r
= sd_bus_add_object_manager(bus
, NULL
, impl
->path
);
1593 return log_error_errno(r
, "Failed to add object manager for %s: %m", impl
->path
);
1596 for (size_t i
= 0; impl
->children
&& impl
->children
[i
]; i
++) {
1597 r
= bus_add_implementation(bus
, impl
->children
[i
], userdata
);
1605 static const BusObjectImplementation
* find_implementation(
1606 const char *pattern
,
1607 const BusObjectImplementation
* const* bus_objects
) {
1609 for (size_t i
= 0; bus_objects
&& bus_objects
[i
]; i
++) {
1610 const BusObjectImplementation
*impl
= bus_objects
[i
];
1612 if (STR_IN_SET(pattern
, impl
->path
, impl
->interface
))
1615 impl
= find_implementation(pattern
, impl
->children
);
1623 static int bus_introspect_implementation(
1624 struct introspect
*intro
,
1625 const BusObjectImplementation
*impl
) {
1628 for (const sd_bus_vtable
**p
= impl
->vtables
; p
&& *p
; p
++) {
1629 r
= introspect_write_interface(intro
, impl
->interface
, *p
);
1631 return log_error_errno(r
, "Failed to write introspection data: %m");
1634 for (const BusObjectVtablePair
*p
= impl
->fallback_vtables
; p
&& p
->vtable
; p
++) {
1635 r
= introspect_write_interface(intro
, impl
->interface
, p
->vtable
);
1637 return log_error_errno(r
, "Failed to write introspection data: %m");
1643 static void list_paths(
1645 const BusObjectImplementation
* const* bus_objects
) {
1647 for (size_t i
= 0; bus_objects
[i
]; i
++) {
1648 fprintf(out
, "%s\t%s\n", bus_objects
[i
]->path
, bus_objects
[i
]->interface
);
1649 if (bus_objects
[i
]->children
)
1650 list_paths(out
, bus_objects
[i
]->children
);
1654 int bus_introspect_implementations(
1656 const char *pattern
,
1657 const BusObjectImplementation
* const* bus_objects
) {
1659 const BusObjectImplementation
*impl
, *main_impl
= NULL
;
1660 _cleanup_free_
char *s
= NULL
;
1663 if (streq(pattern
, "list")) {
1664 list_paths(out
, bus_objects
);
1668 struct introspect intro
= {};
1669 bool is_interface
= sd_bus_interface_name_is_valid(pattern
);
1671 impl
= find_implementation(pattern
, bus_objects
);
1673 return log_error_errno(SYNTHETIC_ERRNO(ENOENT
),
1675 is_interface
? "Interface" : "Object path",
1678 /* We use trusted=false here to get all the @org.freedesktop.systemd1.Privileged annotations. */
1679 r
= introspect_begin(&intro
, false);
1681 return log_error_errno(r
, "Failed to write introspection data: %m");
1683 r
= introspect_write_default_interfaces(&intro
, impl
->manager
);
1685 return log_error_errno(r
, "Failed to write introspection data: %m");
1687 /* Check if there is a non-fallback path that applies to the given interface, also
1688 * print it. This is useful in the case of units: o.fd.systemd1.Service is declared
1689 * as a fallback vtable for o/fd/systemd1/unit, and we also want to print
1690 * o.fd.systemd1.Unit, which is the non-fallback implementation. */
1691 if (impl
->fallback_vtables
&& is_interface
)
1692 main_impl
= find_implementation(impl
->path
, bus_objects
);
1695 bus_introspect_implementation(&intro
, main_impl
);
1697 if (impl
!= main_impl
)
1698 bus_introspect_implementation(&intro
, impl
);
1700 _cleanup_set_free_ Set
*nodes
= NULL
;
1702 for (size_t i
= 0; impl
->children
&& impl
->children
[i
]; i
++) {
1703 r
= set_put_strdup(&nodes
, impl
->children
[i
]->path
);
1708 r
= introspect_write_child_nodes(&intro
, nodes
, impl
->path
);
1712 r
= introspect_finish(&intro
, &s
);
1714 return log_error_errno(r
, "Failed to write introspection data: %m");