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-label.h"
20 #include "bus-message.h"
23 #include "cgroup-util.h"
24 #include "mountpoint-util.h"
26 #include "parse-util.h"
27 #include "path-util.h"
28 #include "rlimit-util.h"
29 #include "socket-util.h"
30 #include "stdio-util.h"
32 #include "user-util.h"
34 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
35 sd_event
*e
= userdata
;
40 sd_bus_close(sd_bus_message_get_bus(m
));
46 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
55 /* We unregister the name here and then wait for the
56 * NameOwnerChanged signal for this event to arrive before we
57 * quit. We do this in order to make sure that any queued
58 * requests are still processed before we really exit. */
60 r
= sd_bus_get_unique_name(bus
, &unique
);
65 "sender='org.freedesktop.DBus',"
67 "interface='org.freedesktop.DBus',"
68 "member='NameOwnerChanged',"
69 "path='/org/freedesktop/DBus',"
71 "arg1='", unique
, "',",
74 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
78 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
85 int bus_event_loop_with_idle(
90 check_idle_t check_idle
,
102 r
= sd_event_get_state(e
);
105 if (r
== SD_EVENT_FINISHED
)
109 idle
= check_idle(userdata
);
113 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
117 if (r
== 0 && !exiting
&& idle
) {
119 r
= sd_bus_try_close(bus
);
123 /* Fallback for dbus1 connections: we
124 * unregister the name and wait for the
125 * response to come through for it */
126 if (r
== -EOPNOTSUPP
) {
128 /* Inform the service manager that we
129 * are going down, so that it will
130 * queue all further start requests,
131 * instead of assuming we are already
133 sd_notify(false, "STOPPING=1");
135 r
= bus_async_unregister_and_exit(e
, bus
, name
);
151 r
= sd_event_get_exit_code(e
, &code
);
158 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
159 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
160 int r
, has_owner
= 0;
165 r
= sd_bus_call_method(c
,
166 "org.freedesktop.DBus",
167 "/org/freedesktop/dbus",
168 "org.freedesktop.DBus",
177 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
179 return sd_bus_error_set_errno(error
, r
);
184 int bus_check_peercred(sd_bus
*c
) {
190 fd
= sd_bus_get_fd(c
);
194 r
= getpeercred(fd
, &ucred
);
198 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
204 int bus_connect_system_systemd(sd_bus
**_bus
) {
205 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
211 return sd_bus_default_system(_bus
);
213 /* If we are root then let's talk directly to the system
214 * instance, instead of going via the bus */
216 r
= sd_bus_new(&bus
);
220 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
224 r
= sd_bus_start(bus
);
226 return sd_bus_default_system(_bus
);
228 r
= bus_check_peercred(bus
);
232 *_bus
= TAKE_PTR(bus
);
237 int bus_connect_user_systemd(sd_bus
**_bus
) {
238 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
239 _cleanup_free_
char *ee
= NULL
;
245 e
= secure_getenv("XDG_RUNTIME_DIR");
247 return sd_bus_default_user(_bus
);
249 ee
= bus_address_escape(e
);
253 r
= sd_bus_new(&bus
);
257 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
261 r
= sd_bus_start(bus
);
263 return sd_bus_default_user(_bus
);
265 r
= bus_check_peercred(bus
);
269 *_bus
= TAKE_PTR(bus
);
274 int bus_print_property_value(const char *name
, const char *expected_value
, bool only_value
, const char *value
) {
277 if (expected_value
&& !streq_ptr(expected_value
, value
))
283 printf("%s=%s\n", name
, value
);
288 int bus_print_property_valuef(const char *name
, const char *expected_value
, bool only_value
, const char *fmt
, ...) {
295 if (expected_value
) {
296 _cleanup_free_
char *s
= NULL
;
299 r
= vasprintf(&s
, fmt
, ap
);
304 if (streq_ptr(expected_value
, s
)) {
308 printf("%s=%s\n", name
, s
);
324 static int bus_print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
326 const char *contents
;
332 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
338 case SD_BUS_TYPE_STRING
: {
341 r
= sd_bus_message_read_basic(m
, type
, &s
);
345 if (all
|| !isempty(s
)) {
348 /* This property has a single value, so we need to take
349 * care not to print a new line, everything else is OK. */
350 good
= !strchr(s
, '\n');
351 bus_print_property_value(name
, expected_value
, value
, good
? s
: "[unprintable]");
357 case SD_BUS_TYPE_BOOLEAN
: {
360 r
= sd_bus_message_read_basic(m
, type
, &b
);
364 if (expected_value
&& parse_boolean(expected_value
) != b
)
367 bus_print_property_value(name
, NULL
, value
, yes_no(b
));
371 case SD_BUS_TYPE_UINT64
: {
374 r
= sd_bus_message_read_basic(m
, type
, &u
);
378 /* Yes, heuristics! But we can change this check
379 * should it turn out to not be sufficient */
381 if (endswith(name
, "Timestamp") ||
382 STR_IN_SET(name
, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
383 char timestamp
[FORMAT_TIMESTAMP_MAX
];
386 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
388 bus_print_property_value(name
, expected_value
, value
, strempty(t
));
390 } else if (strstr(name
, "USec")) {
391 char timespan
[FORMAT_TIMESPAN_MAX
];
393 (void) format_timespan(timespan
, sizeof(timespan
), u
, 0);
394 bus_print_property_value(name
, expected_value
, value
, timespan
);
396 } else if (streq(name
, "RestrictNamespaces")) {
397 _cleanup_free_
char *s
= NULL
;
400 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
402 else if (FLAGS_SET(u
, NAMESPACE_FLAGS_ALL
))
405 r
= namespace_flags_to_string(u
, &s
);
412 bus_print_property_value(name
, expected_value
, value
, result
);
414 } else if (streq(name
, "MountFlags")) {
417 result
= mount_propagation_flags_to_string(u
);
421 bus_print_property_value(name
, expected_value
, value
, result
);
423 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
424 _cleanup_free_
char *s
= NULL
;
426 r
= capability_set_to_string_alloc(u
, &s
);
430 bus_print_property_value(name
, expected_value
, value
, s
);
432 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
433 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
434 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
435 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
436 (endswith(name
, "NSec") && u
== (uint64_t) -1))
438 bus_print_property_value(name
, expected_value
, value
, "[not set]");
440 else if ((STR_IN_SET(name
, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
441 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
442 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
443 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
445 bus_print_property_value(name
, expected_value
, value
, "infinity");
446 else if (STR_IN_SET(name
, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u
== (uint64_t) -1)
447 bus_print_property_value(name
, expected_value
, value
, "[no data]");
449 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu64
, u
);
454 case SD_BUS_TYPE_INT64
: {
457 r
= sd_bus_message_read_basic(m
, type
, &i
);
461 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi64
, i
);
465 case SD_BUS_TYPE_UINT32
: {
468 r
= sd_bus_message_read_basic(m
, type
, &u
);
472 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
473 bus_print_property_valuef(name
, expected_value
, value
, "%04o", u
);
475 else if (streq(name
, "UID")) {
476 if (u
== UID_INVALID
)
477 bus_print_property_value(name
, expected_value
, value
, "[not set]");
479 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
480 } else if (streq(name
, "GID")) {
481 if (u
== GID_INVALID
)
482 bus_print_property_value(name
, expected_value
, value
, "[not set]");
484 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
486 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
491 case SD_BUS_TYPE_INT32
: {
494 r
= sd_bus_message_read_basic(m
, type
, &i
);
498 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi32
, i
);
502 case SD_BUS_TYPE_DOUBLE
: {
505 r
= sd_bus_message_read_basic(m
, type
, &d
);
509 bus_print_property_valuef(name
, expected_value
, value
, "%g", d
);
513 case SD_BUS_TYPE_ARRAY
:
514 if (streq(contents
, "s")) {
518 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, contents
);
522 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
528 /* This property has multiple space-separated values, so
529 * neither spaces nor newlines can be allowed in a value. */
530 good
= str
[strcspn(str
, " \n")] == '\0';
532 printf("%s%s", first
? "" : " ", good
? str
: "[unprintable]");
539 if (first
&& all
&& !value
)
544 r
= sd_bus_message_exit_container(m
);
550 } else if (streq(contents
, "y")) {
554 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
564 for (i
= 0; i
< n
; i
++)
565 printf("%02x", u
[i
]);
572 } else if (streq(contents
, "u")) {
576 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
586 for (i
= 0; i
< n
; i
++)
587 printf("%08x", u
[i
]);
601 int bus_message_print_all_properties(
603 bus_message_print_t func
,
607 Set
**found_properties
) {
613 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
617 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
618 _cleanup_free_
char *name_with_equal
= NULL
;
619 const char *name
, *contents
, *expected_value
= NULL
;
621 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &name
);
625 if (found_properties
) {
626 r
= set_ensure_allocated(found_properties
, &string_hash_ops
);
630 r
= set_put(*found_properties
, name
);
631 if (r
< 0 && r
!= -EEXIST
)
635 name_with_equal
= strjoin(name
, "=");
636 if (!name_with_equal
)
639 if (!filter
|| strv_find(filter
, name
) ||
640 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
641 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
645 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
650 r
= func(name
, expected_value
, m
, value
, all
);
652 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
656 if (all
&& !expected_value
)
657 printf("%s=[unprintable]\n", name
);
658 /* skip what we didn't read */
659 r
= sd_bus_message_skip(m
, contents
);
664 r
= sd_bus_message_exit_container(m
);
668 r
= sd_bus_message_skip(m
, "v");
673 r
= sd_bus_message_exit_container(m
);
680 r
= sd_bus_message_exit_container(m
);
687 int bus_print_all_properties(
691 bus_message_print_t func
,
695 Set
**found_properties
) {
697 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
698 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
704 r
= sd_bus_call_method(bus
,
707 "org.freedesktop.DBus.Properties",
715 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
718 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
719 sd_id128_t
*p
= userdata
;
724 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
731 memcpy((*p
).bytes
, v
, n
);
738 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
742 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
748 case SD_BUS_TYPE_STRING
:
749 case SD_BUS_TYPE_OBJECT_PATH
: {
750 const char **p
= userdata
;
753 r
= sd_bus_message_read_basic(m
, type
, &s
);
760 if (flags
& BUS_MAP_STRDUP
)
761 return free_and_strdup((char **) userdata
, s
);
767 case SD_BUS_TYPE_ARRAY
: {
768 _cleanup_strv_free_
char **l
= NULL
;
769 char ***p
= userdata
;
771 r
= bus_message_read_strv_extend(m
, &l
);
775 return strv_extend_strv(p
, l
, false);
778 case SD_BUS_TYPE_BOOLEAN
: {
781 r
= sd_bus_message_read_basic(m
, type
, &b
);
785 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
786 *(bool*) userdata
= b
;
788 *(int*) userdata
= b
;
793 case SD_BUS_TYPE_INT32
:
794 case SD_BUS_TYPE_UINT32
: {
795 uint32_t u
, *p
= userdata
;
797 r
= sd_bus_message_read_basic(m
, type
, &u
);
805 case SD_BUS_TYPE_INT64
:
806 case SD_BUS_TYPE_UINT64
: {
807 uint64_t t
, *p
= userdata
;
809 r
= sd_bus_message_read_basic(m
, type
, &t
);
817 case SD_BUS_TYPE_DOUBLE
: {
818 double d
, *p
= userdata
;
820 r
= sd_bus_message_read_basic(m
, type
, &d
);
831 int bus_message_map_all_properties(
833 const struct bus_properties_map
*map
,
843 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
847 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
848 const struct bus_properties_map
*prop
;
850 const char *contents
;
854 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
858 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
859 if (streq(map
[i
].member
, member
)) {
865 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
869 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
873 v
= (uint8_t *)userdata
+ prop
->offset
;
875 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
877 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
881 r
= sd_bus_message_exit_container(m
);
885 r
= sd_bus_message_skip(m
, "v");
890 r
= sd_bus_message_exit_container(m
);
897 return sd_bus_message_exit_container(m
);
900 int bus_map_all_properties(
902 const char *destination
,
904 const struct bus_properties_map
*map
,
907 sd_bus_message
**reply
,
910 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
917 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
919 r
= sd_bus_call_method(
923 "org.freedesktop.DBus.Properties",
931 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
936 *reply
= sd_bus_message_ref(m
);
941 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
942 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
945 assert(transport
>= 0);
946 assert(transport
< _BUS_TRANSPORT_MAX
);
949 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
950 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
954 case BUS_TRANSPORT_LOCAL
:
956 r
= sd_bus_default_user(&bus
);
958 if (sd_booted() <= 0) {
959 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
960 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
964 r
= sd_bus_default_system(&bus
);
968 case BUS_TRANSPORT_REMOTE
:
969 r
= sd_bus_open_system_remote(&bus
, host
);
972 case BUS_TRANSPORT_MACHINE
:
973 r
= sd_bus_open_system_machine(&bus
, host
);
977 assert_not_reached("Hmm, unknown transport type.");
982 r
= sd_bus_set_exit_on_disconnect(bus
, true);
986 *ret
= TAKE_PTR(bus
);
991 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
994 assert(transport
>= 0);
995 assert(transport
< _BUS_TRANSPORT_MAX
);
998 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
999 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1001 switch (transport
) {
1003 case BUS_TRANSPORT_LOCAL
:
1005 r
= bus_connect_user_systemd(bus
);
1007 if (sd_booted() <= 0)
1008 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1009 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1010 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1011 r
= bus_connect_system_systemd(bus
);
1015 case BUS_TRANSPORT_REMOTE
:
1016 r
= sd_bus_open_system_remote(bus
, host
);
1019 case BUS_TRANSPORT_MACHINE
:
1020 r
= sd_bus_open_system_machine(bus
, host
);
1024 assert_not_reached("Hmm, unknown transport type.");
1030 int bus_property_get_bool(
1033 const char *interface
,
1034 const char *property
,
1035 sd_bus_message
*reply
,
1037 sd_bus_error
*error
) {
1039 int b
= *(bool*) userdata
;
1041 return sd_bus_message_append_basic(reply
, 'b', &b
);
1044 int bus_property_set_bool(
1047 const char *interface
,
1048 const char *property
,
1049 sd_bus_message
*value
,
1051 sd_bus_error
*error
) {
1055 r
= sd_bus_message_read(value
, "b", &b
);
1059 *(bool*) userdata
= b
;
1063 int bus_property_get_id128(
1066 const char *interface
,
1067 const char *property
,
1068 sd_bus_message
*reply
,
1070 sd_bus_error
*error
) {
1072 sd_id128_t
*id
= userdata
;
1074 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1075 return sd_bus_message_append(reply
, "ay", 0);
1077 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1080 #if __SIZEOF_SIZE_T__ != 8
1081 int bus_property_get_size(
1084 const char *interface
,
1085 const char *property
,
1086 sd_bus_message
*reply
,
1088 sd_bus_error
*error
) {
1090 uint64_t sz
= *(size_t*) userdata
;
1092 return sd_bus_message_append_basic(reply
, 't', &sz
);
1096 #if __SIZEOF_LONG__ != 8
1097 int bus_property_get_long(
1100 const char *interface
,
1101 const char *property
,
1102 sd_bus_message
*reply
,
1104 sd_bus_error
*error
) {
1106 int64_t l
= *(long*) userdata
;
1108 return sd_bus_message_append_basic(reply
, 'x', &l
);
1111 int bus_property_get_ulong(
1114 const char *interface
,
1115 const char *property
,
1116 sd_bus_message
*reply
,
1118 sd_bus_error
*error
) {
1120 uint64_t ul
= *(unsigned long*) userdata
;
1122 return sd_bus_message_append_basic(reply
, 't', &ul
);
1127 * bus_path_encode_unique() - encode unique object path
1128 * @b: bus connection or NULL
1129 * @prefix: object path prefix
1130 * @sender_id: unique-name of client, or NULL
1131 * @external_id: external ID to be chosen by client, or NULL
1132 * @ret_path: storage for encoded object path pointer
1134 * Whenever we provide a bus API that allows clients to create and manage
1135 * server-side objects, we need to provide a unique name for these objects. If
1136 * we let the server choose the name, we suffer from a race condition: If a
1137 * client creates an object asynchronously, it cannot destroy that object until
1138 * it received the method reply. It cannot know the name of the new object,
1139 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1141 * Therefore, many APIs allow the client to choose the unique name for newly
1142 * created objects. There're two problems to solve, though:
1143 * 1) Object names are usually defined via dbus object paths, which are
1144 * usually globally namespaced. Therefore, multiple clients must be able
1145 * to choose unique object names without interference.
1146 * 2) If multiple libraries share the same bus connection, they must be
1147 * able to choose unique object names without interference.
1148 * The first problem is solved easily by prefixing a name with the
1149 * unique-bus-name of a connection. The server side must enforce this and
1150 * reject any other name. The second problem is solved by providing unique
1151 * suffixes from within sd-bus.
1153 * This helper allows clients to create unique object-paths. It uses the
1154 * template '/prefix/sender_id/external_id' and returns the new path in
1155 * @ret_path (must be freed by the caller).
1156 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1157 * NULL, this function allocates a unique suffix via @b (by requesting a new
1158 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1161 * Returns: 0 on success, negative error code on failure.
1163 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1164 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1165 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1168 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1169 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1170 assert_return(ret_path
, -EINVAL
);
1173 r
= sd_bus_get_unique_name(b
, &sender_id
);
1179 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1180 external_id
= external_buf
;
1183 sender_label
= bus_label_escape(sender_id
);
1187 external_label
= bus_label_escape(external_id
);
1188 if (!external_label
)
1191 p
= path_join(prefix
, sender_label
, external_label
);
1200 * bus_path_decode_unique() - decode unique object path
1201 * @path: object path to decode
1202 * @prefix: object path prefix
1203 * @ret_sender: output parameter for sender-id label
1204 * @ret_external: output parameter for external-id label
1206 * This does the reverse of bus_path_encode_unique() (see its description for
1207 * details). Both trailing labels, sender-id and external-id, are unescaped and
1208 * returned in the given output parameters (the caller must free them).
1210 * Note that this function returns 0 if the path does not match the template
1211 * (see bus_path_encode_unique()), 1 if it matched.
1213 * Returns: Negative error code on failure, 0 if the given object path does not
1214 * match the template (return parameters are set to NULL), 1 if it was
1215 * parsed successfully (return parameters contain allocated labels).
1217 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1219 char *sender
, *external
;
1221 assert(object_path_is_valid(path
));
1222 assert(object_path_is_valid(prefix
));
1224 assert(ret_external
);
1226 p
= object_path_startswith(path
, prefix
);
1229 *ret_external
= NULL
;
1236 *ret_external
= NULL
;
1240 sender
= bus_label_unescape_n(p
, q
- p
);
1241 external
= bus_label_unescape(q
+ 1);
1242 if (!sender
|| !external
) {
1248 *ret_sender
= sender
;
1249 *ret_external
= external
;
1253 int bus_property_get_rlimit(
1256 const char *interface
,
1257 const char *property
,
1258 sd_bus_message
*reply
,
1260 sd_bus_error
*error
) {
1262 const char *is_soft
;
1271 is_soft
= endswith(property
, "Soft");
1273 rl
= *(struct rlimit
**) userdata
;
1275 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1277 struct rlimit buf
= {};
1281 /* Chop off "Soft" suffix */
1282 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1284 /* Skip over any prefix, such as "Default" */
1285 assert_se(p
= strstr(s
, "Limit"));
1287 z
= rlimit_from_string(p
+ 5);
1290 (void) getrlimit(z
, &buf
);
1291 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1294 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1296 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1298 return sd_bus_message_append(reply
, "t", u
);
1301 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1307 /* Continues adding after failure, and returns the first failure. */
1309 STRV_FOREACH(i
, l
) {
1312 k
= sd_bus_track_add_name(t
, *i
);
1313 if (k
< 0 && r
>= 0)
1320 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1321 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1327 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1330 r
= sd_bus_new(&bus
);
1335 r
= sd_bus_set_description(bus
, description
);
1340 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1342 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1344 r
= sd_bus_set_address(bus
, e
);
1348 r
= sd_bus_set_bus_client(bus
, true);
1352 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1356 r
= sd_bus_set_watch_bind(bus
, true);
1360 r
= sd_bus_set_connected_signal(bus
, true);
1364 r
= sd_bus_start(bus
);
1368 *ret
= TAKE_PTR(bus
);
1373 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1374 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1380 /* Reply to the specified message with a message containing a dictionary put together from the
1383 r
= sd_bus_message_new_method_return(m
, &reply
);
1387 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1391 STRV_FOREACH_PAIR(k
, v
, l
) {
1392 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1397 r
= sd_bus_message_close_container(reply
);
1401 return sd_bus_send(NULL
, reply
, NULL
);
1404 static void bus_message_unref_wrapper(void *m
) {
1405 sd_bus_message_unref(m
);
1408 const struct hash_ops bus_message_hash_ops
= {
1409 .hash
= trivial_hash_func
,
1410 .compare
= trivial_compare_func
,
1411 .free_value
= bus_message_unref_wrapper
,