1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/resource.h>
11 #include <sys/socket.h>
14 #include "sd-bus-protocol.h"
16 #include "sd-daemon.h"
20 #include "alloc-util.h"
21 #include "bus-internal.h"
22 #include "bus-label.h"
23 #include "bus-message.h"
26 #include "cgroup-util.h"
31 #include "mountpoint-util.h"
33 #include "parse-util.h"
34 #include "path-util.h"
35 #include "proc-cmdline.h"
36 #include "rlimit-util.h"
37 #include "stdio-util.h"
39 #include "user-util.h"
41 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
42 sd_event
*e
= userdata
;
47 sd_bus_close(sd_bus_message_get_bus(m
));
53 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
62 /* We unregister the name here and then wait for the
63 * NameOwnerChanged signal for this event to arrive before we
64 * quit. We do this in order to make sure that any queued
65 * requests are still processed before we really exit. */
67 r
= sd_bus_get_unique_name(bus
, &unique
);
72 "sender='org.freedesktop.DBus',"
74 "interface='org.freedesktop.DBus',"
75 "member='NameOwnerChanged',"
76 "path='/org/freedesktop/DBus',"
78 "arg1='", unique
, "',",
81 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
85 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
92 int bus_event_loop_with_idle(
97 check_idle_t check_idle
,
109 r
= sd_event_get_state(e
);
112 if (r
== SD_EVENT_FINISHED
)
116 idle
= check_idle(userdata
);
120 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
124 if (r
== 0 && !exiting
&& idle
) {
126 r
= sd_bus_try_close(bus
);
130 /* Fallback for dbus1 connections: we
131 * unregister the name and wait for the
132 * response to come through for it */
133 if (r
== -EOPNOTSUPP
) {
135 /* Inform the service manager that we
136 * are going down, so that it will
137 * queue all further start requests,
138 * instead of assuming we are already
140 sd_notify(false, "STOPPING=1");
142 r
= bus_async_unregister_and_exit(e
, bus
, name
);
158 r
= sd_event_get_exit_code(e
, &code
);
165 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
166 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
167 int r
, has_owner
= 0;
172 r
= sd_bus_call_method(c
,
173 "org.freedesktop.DBus",
174 "/org/freedesktop/dbus",
175 "org.freedesktop.DBus",
184 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
186 return sd_bus_error_set_errno(error
, r
);
191 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
192 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
198 if (good_user
== UID_INVALID
)
201 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
205 /* Don't trust augmented credentials for authorization */
206 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
208 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
212 return sender_uid
== good_user
;
216 sd_bus_message
*call
,
219 const char **details
,
229 /* Tests non-interactively! */
231 r
= check_good_user(call
, good_user
);
235 r
= sd_bus_query_sender_privilege(call
, capability
);
242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
243 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
244 int authorized
= false, challenge
= false;
245 const char *sender
, **k
, **v
;
247 sender
= sd_bus_message_get_sender(call
);
251 r
= sd_bus_message_new_method_call(
254 "org.freedesktop.PolicyKit1",
255 "/org/freedesktop/PolicyKit1/Authority",
256 "org.freedesktop.PolicyKit1.Authority",
257 "CheckAuthorization");
261 r
= sd_bus_message_append(
264 "system-bus-name", 1, "name", "s", sender
,
269 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
273 STRV_FOREACH_PAIR(k
, v
, details
) {
274 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
279 r
= sd_bus_message_close_container(request
);
283 r
= sd_bus_message_append(request
, "us", 0, NULL
);
287 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
289 /* Treat no PK available as access denied */
290 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
291 sd_bus_error_free(e
);
298 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
302 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
310 *_challenge
= challenge
;
321 typedef struct AsyncPolkitQuery
{
322 sd_bus_message
*request
, *reply
;
323 sd_bus_message_handler_t callback
;
329 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
334 sd_bus_slot_unref(q
->slot
);
336 if (q
->registry
&& q
->request
)
337 hashmap_remove(q
->registry
, q
->request
);
339 sd_bus_message_unref(q
->request
);
340 sd_bus_message_unref(q
->reply
);
345 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
346 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
347 AsyncPolkitQuery
*q
= userdata
;
353 q
->slot
= sd_bus_slot_unref(q
->slot
);
354 q
->reply
= sd_bus_message_ref(reply
);
356 r
= sd_bus_message_rewind(q
->request
, true);
358 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
362 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
363 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
366 async_polkit_query_free(q
);
373 int bus_verify_polkit_async(
374 sd_bus_message
*call
,
377 const char **details
,
381 sd_bus_error
*error
) {
384 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
386 const char *sender
, **k
, **v
;
387 sd_bus_message_handler_t callback
;
397 r
= check_good_user(call
, good_user
);
402 q
= hashmap_get(*registry
, call
);
404 int authorized
, challenge
;
406 /* This is the second invocation of this function, and
407 * there's already a response from polkit, let's
411 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
412 const sd_bus_error
*e
;
414 e
= sd_bus_message_get_error(q
->reply
);
416 /* Treat no PK available as access denied */
417 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
))
420 /* Copy error from polkit reply */
421 sd_bus_error_copy(error
, e
);
422 return -sd_bus_error_get_errno(e
);
425 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
427 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
436 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
442 r
= sd_bus_query_sender_privilege(call
, capability
);
449 if (sd_bus_get_current_message(call
->bus
) != call
)
452 callback
= sd_bus_get_current_handler(call
->bus
);
456 userdata
= sd_bus_get_current_userdata(call
->bus
);
458 sender
= sd_bus_message_get_sender(call
);
462 c
= sd_bus_message_get_allow_interactive_authorization(call
);
468 r
= hashmap_ensure_allocated(registry
, NULL
);
472 r
= sd_bus_message_new_method_call(
475 "org.freedesktop.PolicyKit1",
476 "/org/freedesktop/PolicyKit1/Authority",
477 "org.freedesktop.PolicyKit1.Authority",
478 "CheckAuthorization");
482 r
= sd_bus_message_append(
485 "system-bus-name", 1, "name", "s", sender
,
490 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
494 STRV_FOREACH_PAIR(k
, v
, details
) {
495 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
500 r
= sd_bus_message_close_container(pk
);
504 r
= sd_bus_message_append(pk
, "us", interactive
, NULL
);
508 q
= new0(AsyncPolkitQuery
, 1);
512 q
->request
= sd_bus_message_ref(call
);
513 q
->callback
= callback
;
514 q
->userdata
= userdata
;
516 r
= hashmap_put(*registry
, call
, q
);
518 async_polkit_query_free(q
);
522 q
->registry
= *registry
;
524 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
526 async_polkit_query_free(q
);
536 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
538 hashmap_free_with_destructor(registry
, async_polkit_query_free
);
542 int bus_check_peercred(sd_bus
*c
) {
548 fd
= sd_bus_get_fd(c
);
552 r
= getpeercred(fd
, &ucred
);
556 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
562 int bus_connect_system_systemd(sd_bus
**_bus
) {
563 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
569 return sd_bus_default_system(_bus
);
571 /* If we are root then let's talk directly to the system
572 * instance, instead of going via the bus */
574 r
= sd_bus_new(&bus
);
578 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
582 r
= sd_bus_start(bus
);
584 return sd_bus_default_system(_bus
);
586 r
= bus_check_peercred(bus
);
590 *_bus
= TAKE_PTR(bus
);
595 int bus_connect_user_systemd(sd_bus
**_bus
) {
596 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
597 _cleanup_free_
char *ee
= NULL
;
603 e
= secure_getenv("XDG_RUNTIME_DIR");
605 return sd_bus_default_user(_bus
);
607 ee
= bus_address_escape(e
);
611 r
= sd_bus_new(&bus
);
615 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
619 r
= sd_bus_start(bus
);
621 return sd_bus_default_user(_bus
);
623 r
= bus_check_peercred(bus
);
627 *_bus
= TAKE_PTR(bus
);
632 int bus_print_property_value(const char *name
, const char *expected_value
, bool only_value
, const char *value
) {
635 if (expected_value
&& !streq_ptr(expected_value
, value
))
641 printf("%s=%s\n", name
, value
);
646 int bus_print_property_valuef(const char *name
, const char *expected_value
, bool only_value
, const char *fmt
, ...) {
653 if (expected_value
) {
654 _cleanup_free_
char *s
= NULL
;
657 r
= vasprintf(&s
, fmt
, ap
);
662 if (streq_ptr(expected_value
, s
)) {
666 printf("%s=%s\n", name
, s
);
682 static int bus_print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
684 const char *contents
;
690 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
696 case SD_BUS_TYPE_STRING
: {
699 r
= sd_bus_message_read_basic(m
, type
, &s
);
703 if (all
|| !isempty(s
)) {
706 /* This property has a single value, so we need to take
707 * care not to print a new line, everything else is OK. */
708 good
= !strchr(s
, '\n');
709 bus_print_property_value(name
, expected_value
, value
, good
? s
: "[unprintable]");
715 case SD_BUS_TYPE_BOOLEAN
: {
718 r
= sd_bus_message_read_basic(m
, type
, &b
);
722 if (expected_value
&& parse_boolean(expected_value
) != b
)
725 bus_print_property_value(name
, NULL
, value
, yes_no(b
));
729 case SD_BUS_TYPE_UINT64
: {
732 r
= sd_bus_message_read_basic(m
, type
, &u
);
736 /* Yes, heuristics! But we can change this check
737 * should it turn out to not be sufficient */
739 if (endswith(name
, "Timestamp") ||
740 STR_IN_SET(name
, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
741 char timestamp
[FORMAT_TIMESTAMP_MAX
];
744 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
746 bus_print_property_value(name
, expected_value
, value
, strempty(t
));
748 } else if (strstr(name
, "USec")) {
749 char timespan
[FORMAT_TIMESPAN_MAX
];
751 (void) format_timespan(timespan
, sizeof(timespan
), u
, 0);
752 bus_print_property_value(name
, expected_value
, value
, timespan
);
754 } else if (streq(name
, "RestrictNamespaces")) {
755 _cleanup_free_
char *s
= NULL
;
758 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
760 else if (FLAGS_SET(u
, NAMESPACE_FLAGS_ALL
))
763 r
= namespace_flags_to_string(u
, &s
);
770 bus_print_property_value(name
, expected_value
, value
, result
);
772 } else if (streq(name
, "MountFlags")) {
775 result
= mount_propagation_flags_to_string(u
);
779 bus_print_property_value(name
, expected_value
, value
, result
);
781 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
782 _cleanup_free_
char *s
= NULL
;
784 r
= capability_set_to_string_alloc(u
, &s
);
788 bus_print_property_value(name
, expected_value
, value
, s
);
790 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
791 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
792 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
793 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
794 (endswith(name
, "NSec") && u
== (uint64_t) -1))
796 bus_print_property_value(name
, expected_value
, value
, "[not set]");
798 else if ((STR_IN_SET(name
, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
799 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
800 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
801 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
803 bus_print_property_value(name
, expected_value
, value
, "infinity");
804 else if (STR_IN_SET(name
, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u
== (uint64_t) -1)
805 bus_print_property_value(name
, expected_value
, value
, "[no data]");
807 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu64
, u
);
812 case SD_BUS_TYPE_INT64
: {
815 r
= sd_bus_message_read_basic(m
, type
, &i
);
819 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi64
, i
);
823 case SD_BUS_TYPE_UINT32
: {
826 r
= sd_bus_message_read_basic(m
, type
, &u
);
830 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
831 bus_print_property_valuef(name
, expected_value
, value
, "%04o", u
);
833 else if (streq(name
, "UID")) {
834 if (u
== UID_INVALID
)
835 bus_print_property_value(name
, expected_value
, value
, "[not set]");
837 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
838 } else if (streq(name
, "GID")) {
839 if (u
== GID_INVALID
)
840 bus_print_property_value(name
, expected_value
, value
, "[not set]");
842 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
844 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
849 case SD_BUS_TYPE_INT32
: {
852 r
= sd_bus_message_read_basic(m
, type
, &i
);
856 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi32
, i
);
860 case SD_BUS_TYPE_DOUBLE
: {
863 r
= sd_bus_message_read_basic(m
, type
, &d
);
867 bus_print_property_valuef(name
, expected_value
, value
, "%g", d
);
871 case SD_BUS_TYPE_ARRAY
:
872 if (streq(contents
, "s")) {
876 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, contents
);
880 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
886 /* This property has multiple space-separated values, so
887 * neither spaces nor newlines can be allowed in a value. */
888 good
= str
[strcspn(str
, " \n")] == '\0';
890 printf("%s%s", first
? "" : " ", good
? str
: "[unprintable]");
897 if (first
&& all
&& !value
)
902 r
= sd_bus_message_exit_container(m
);
908 } else if (streq(contents
, "y")) {
912 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
922 for (i
= 0; i
< n
; i
++)
923 printf("%02x", u
[i
]);
930 } else if (streq(contents
, "u")) {
934 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
944 for (i
= 0; i
< n
; i
++)
945 printf("%08x", u
[i
]);
959 int bus_message_print_all_properties(
961 bus_message_print_t func
,
965 Set
**found_properties
) {
971 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
975 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
976 _cleanup_free_
char *name_with_equal
= NULL
;
977 const char *name
, *contents
, *expected_value
= NULL
;
979 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &name
);
983 if (found_properties
) {
984 r
= set_ensure_allocated(found_properties
, &string_hash_ops
);
988 r
= set_put(*found_properties
, name
);
989 if (r
< 0 && r
!= -EEXIST
)
993 name_with_equal
= strjoin(name
, "=");
994 if (!name_with_equal
)
997 if (!filter
|| strv_find(filter
, name
) ||
998 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
999 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1003 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1008 r
= func(name
, expected_value
, m
, value
, all
);
1009 if (!func
|| r
== 0)
1010 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
1014 if (all
&& !expected_value
)
1015 printf("%s=[unprintable]\n", name
);
1016 /* skip what we didn't read */
1017 r
= sd_bus_message_skip(m
, contents
);
1022 r
= sd_bus_message_exit_container(m
);
1026 r
= sd_bus_message_skip(m
, "v");
1031 r
= sd_bus_message_exit_container(m
);
1038 r
= sd_bus_message_exit_container(m
);
1045 int bus_print_all_properties(
1049 bus_message_print_t func
,
1053 Set
**found_properties
) {
1055 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1056 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1062 r
= sd_bus_call_method(bus
,
1065 "org.freedesktop.DBus.Properties",
1073 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
1076 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1077 sd_id128_t
*p
= userdata
;
1082 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1089 memcpy((*p
).bytes
, v
, n
);
1096 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
1100 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1106 case SD_BUS_TYPE_STRING
: {
1107 const char **p
= userdata
;
1110 r
= sd_bus_message_read_basic(m
, type
, &s
);
1117 if (flags
& BUS_MAP_STRDUP
)
1118 return free_and_strdup((char **) userdata
, s
);
1124 case SD_BUS_TYPE_ARRAY
: {
1125 _cleanup_strv_free_
char **l
= NULL
;
1126 char ***p
= userdata
;
1128 r
= bus_message_read_strv_extend(m
, &l
);
1132 return strv_free_and_replace(*p
, l
);
1135 case SD_BUS_TYPE_BOOLEAN
: {
1138 r
= sd_bus_message_read_basic(m
, type
, &b
);
1142 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
1143 *(bool*) userdata
= b
;
1145 *(int*) userdata
= b
;
1150 case SD_BUS_TYPE_INT32
:
1151 case SD_BUS_TYPE_UINT32
: {
1152 uint32_t u
, *p
= userdata
;
1154 r
= sd_bus_message_read_basic(m
, type
, &u
);
1162 case SD_BUS_TYPE_INT64
:
1163 case SD_BUS_TYPE_UINT64
: {
1164 uint64_t t
, *p
= userdata
;
1166 r
= sd_bus_message_read_basic(m
, type
, &t
);
1174 case SD_BUS_TYPE_DOUBLE
: {
1175 double d
, *p
= userdata
;
1177 r
= sd_bus_message_read_basic(m
, type
, &d
);
1188 int bus_message_map_all_properties(
1190 const struct bus_properties_map
*map
,
1192 sd_bus_error
*error
,
1200 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1204 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1205 const struct bus_properties_map
*prop
;
1207 const char *contents
;
1211 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1215 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1216 if (streq(map
[i
].member
, member
)) {
1222 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1226 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1230 v
= (uint8_t *)userdata
+ prop
->offset
;
1232 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
1234 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
1238 r
= sd_bus_message_exit_container(m
);
1242 r
= sd_bus_message_skip(m
, "v");
1247 r
= sd_bus_message_exit_container(m
);
1254 return sd_bus_message_exit_container(m
);
1257 int bus_map_all_properties(
1259 const char *destination
,
1261 const struct bus_properties_map
*map
,
1263 sd_bus_error
*error
,
1264 sd_bus_message
**reply
,
1267 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1271 assert(destination
);
1274 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
1276 r
= sd_bus_call_method(
1280 "org.freedesktop.DBus.Properties",
1288 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
1293 *reply
= sd_bus_message_ref(m
);
1298 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
1299 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1302 assert(transport
>= 0);
1303 assert(transport
< _BUS_TRANSPORT_MAX
);
1306 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1307 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1309 switch (transport
) {
1311 case BUS_TRANSPORT_LOCAL
:
1313 r
= sd_bus_default_user(&bus
);
1315 if (sd_booted() <= 0) {
1316 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1317 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
1321 r
= sd_bus_default_system(&bus
);
1325 case BUS_TRANSPORT_REMOTE
:
1326 r
= sd_bus_open_system_remote(&bus
, host
);
1329 case BUS_TRANSPORT_MACHINE
:
1330 r
= sd_bus_open_system_machine(&bus
, host
);
1334 assert_not_reached("Hmm, unknown transport type.");
1339 r
= sd_bus_set_exit_on_disconnect(bus
, true);
1343 *ret
= TAKE_PTR(bus
);
1348 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1351 assert(transport
>= 0);
1352 assert(transport
< _BUS_TRANSPORT_MAX
);
1355 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1356 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1358 switch (transport
) {
1360 case BUS_TRANSPORT_LOCAL
:
1362 r
= bus_connect_user_systemd(bus
);
1364 if (sd_booted() <= 0)
1365 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1366 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1367 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1368 r
= bus_connect_system_systemd(bus
);
1372 case BUS_TRANSPORT_REMOTE
:
1373 r
= sd_bus_open_system_remote(bus
, host
);
1376 case BUS_TRANSPORT_MACHINE
:
1377 r
= sd_bus_open_system_machine(bus
, host
);
1381 assert_not_reached("Hmm, unknown transport type.");
1387 int bus_property_get_bool(
1390 const char *interface
,
1391 const char *property
,
1392 sd_bus_message
*reply
,
1394 sd_bus_error
*error
) {
1396 int b
= *(bool*) userdata
;
1398 return sd_bus_message_append_basic(reply
, 'b', &b
);
1401 int bus_property_set_bool(
1404 const char *interface
,
1405 const char *property
,
1406 sd_bus_message
*value
,
1408 sd_bus_error
*error
) {
1412 r
= sd_bus_message_read(value
, "b", &b
);
1416 *(bool*) userdata
= b
;
1420 int bus_property_get_id128(
1423 const char *interface
,
1424 const char *property
,
1425 sd_bus_message
*reply
,
1427 sd_bus_error
*error
) {
1429 sd_id128_t
*id
= userdata
;
1431 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1432 return sd_bus_message_append(reply
, "ay", 0);
1434 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1437 #if __SIZEOF_SIZE_T__ != 8
1438 int bus_property_get_size(
1441 const char *interface
,
1442 const char *property
,
1443 sd_bus_message
*reply
,
1445 sd_bus_error
*error
) {
1447 uint64_t sz
= *(size_t*) userdata
;
1449 return sd_bus_message_append_basic(reply
, 't', &sz
);
1453 #if __SIZEOF_LONG__ != 8
1454 int bus_property_get_long(
1457 const char *interface
,
1458 const char *property
,
1459 sd_bus_message
*reply
,
1461 sd_bus_error
*error
) {
1463 int64_t l
= *(long*) userdata
;
1465 return sd_bus_message_append_basic(reply
, 'x', &l
);
1468 int bus_property_get_ulong(
1471 const char *interface
,
1472 const char *property
,
1473 sd_bus_message
*reply
,
1475 sd_bus_error
*error
) {
1477 uint64_t ul
= *(unsigned long*) userdata
;
1479 return sd_bus_message_append_basic(reply
, 't', &ul
);
1484 * bus_path_encode_unique() - encode unique object path
1485 * @b: bus connection or NULL
1486 * @prefix: object path prefix
1487 * @sender_id: unique-name of client, or NULL
1488 * @external_id: external ID to be chosen by client, or NULL
1489 * @ret_path: storage for encoded object path pointer
1491 * Whenever we provide a bus API that allows clients to create and manage
1492 * server-side objects, we need to provide a unique name for these objects. If
1493 * we let the server choose the name, we suffer from a race condition: If a
1494 * client creates an object asynchronously, it cannot destroy that object until
1495 * it received the method reply. It cannot know the name of the new object,
1496 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1498 * Therefore, many APIs allow the client to choose the unique name for newly
1499 * created objects. There're two problems to solve, though:
1500 * 1) Object names are usually defined via dbus object paths, which are
1501 * usually globally namespaced. Therefore, multiple clients must be able
1502 * to choose unique object names without interference.
1503 * 2) If multiple libraries share the same bus connection, they must be
1504 * able to choose unique object names without interference.
1505 * The first problem is solved easily by prefixing a name with the
1506 * unique-bus-name of a connection. The server side must enforce this and
1507 * reject any other name. The second problem is solved by providing unique
1508 * suffixes from within sd-bus.
1510 * This helper allows clients to create unique object-paths. It uses the
1511 * template '/prefix/sender_id/external_id' and returns the new path in
1512 * @ret_path (must be freed by the caller).
1513 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1514 * NULL, this function allocates a unique suffix via @b (by requesting a new
1515 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1518 * Returns: 0 on success, negative error code on failure.
1520 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1521 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1522 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1525 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1526 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1527 assert_return(ret_path
, -EINVAL
);
1530 r
= sd_bus_get_unique_name(b
, &sender_id
);
1536 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1537 external_id
= external_buf
;
1540 sender_label
= bus_label_escape(sender_id
);
1544 external_label
= bus_label_escape(external_id
);
1545 if (!external_label
)
1548 p
= path_join(prefix
, sender_label
, external_label
);
1557 * bus_path_decode_unique() - decode unique object path
1558 * @path: object path to decode
1559 * @prefix: object path prefix
1560 * @ret_sender: output parameter for sender-id label
1561 * @ret_external: output parameter for external-id label
1563 * This does the reverse of bus_path_encode_unique() (see its description for
1564 * details). Both trailing labels, sender-id and external-id, are unescaped and
1565 * returned in the given output parameters (the caller must free them).
1567 * Note that this function returns 0 if the path does not match the template
1568 * (see bus_path_encode_unique()), 1 if it matched.
1570 * Returns: Negative error code on failure, 0 if the given object path does not
1571 * match the template (return parameters are set to NULL), 1 if it was
1572 * parsed successfully (return parameters contain allocated labels).
1574 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1576 char *sender
, *external
;
1578 assert(object_path_is_valid(path
));
1579 assert(object_path_is_valid(prefix
));
1581 assert(ret_external
);
1583 p
= object_path_startswith(path
, prefix
);
1586 *ret_external
= NULL
;
1593 *ret_external
= NULL
;
1597 sender
= bus_label_unescape_n(p
, q
- p
);
1598 external
= bus_label_unescape(q
+ 1);
1599 if (!sender
|| !external
) {
1605 *ret_sender
= sender
;
1606 *ret_external
= external
;
1610 int bus_property_get_rlimit(
1613 const char *interface
,
1614 const char *property
,
1615 sd_bus_message
*reply
,
1617 sd_bus_error
*error
) {
1619 const char *is_soft
;
1628 is_soft
= endswith(property
, "Soft");
1630 rl
= *(struct rlimit
**) userdata
;
1632 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1634 struct rlimit buf
= {};
1638 /* Chop off "Soft" suffix */
1639 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1641 /* Skip over any prefix, such as "Default" */
1642 assert_se(p
= strstr(s
, "Limit"));
1644 z
= rlimit_from_string(p
+ 5);
1647 (void) getrlimit(z
, &buf
);
1648 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1651 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1653 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1655 return sd_bus_message_append(reply
, "t", u
);
1658 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1664 /* Continues adding after failure, and returns the first failure. */
1666 STRV_FOREACH(i
, l
) {
1669 k
= sd_bus_track_add_name(t
, *i
);
1670 if (k
< 0 && r
>= 0)
1677 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1678 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1684 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */
1686 r
= sd_bus_new(&bus
);
1691 r
= sd_bus_set_description(bus
, description
);
1696 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1698 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1700 r
= sd_bus_set_address(bus
, e
);
1704 r
= sd_bus_set_bus_client(bus
, true);
1708 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1712 r
= sd_bus_set_watch_bind(bus
, true);
1716 r
= sd_bus_set_connected_signal(bus
, true);
1720 r
= sd_bus_start(bus
);
1724 *ret
= TAKE_PTR(bus
);
1729 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1730 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1736 /* Reply to the specified message with a message containing a dictionary put together from the specified
1739 r
= sd_bus_message_new_method_return(m
, &reply
);
1743 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1747 STRV_FOREACH_PAIR(k
, v
, l
) {
1748 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1753 r
= sd_bus_message_close_container(reply
);
1757 return sd_bus_send(NULL
, reply
, NULL
);
1760 static void bus_message_unref_wrapper(void *m
) {
1761 sd_bus_message_unref(m
);
1764 const struct hash_ops bus_message_hash_ops
= {
1765 .hash
= trivial_hash_func
,
1766 .compare
= trivial_compare_func
,
1767 .free_value
= bus_message_unref_wrapper
,