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 "proc-cmdline.h"
35 #include "rlimit-util.h"
36 #include "stdio-util.h"
38 #include "user-util.h"
40 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
41 sd_event
*e
= userdata
;
46 sd_bus_close(sd_bus_message_get_bus(m
));
52 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
61 /* We unregister the name here and then wait for the
62 * NameOwnerChanged signal for this event to arrive before we
63 * quit. We do this in order to make sure that any queued
64 * requests are still processed before we really exit. */
66 r
= sd_bus_get_unique_name(bus
, &unique
);
71 "sender='org.freedesktop.DBus',"
73 "interface='org.freedesktop.DBus',"
74 "member='NameOwnerChanged',"
75 "path='/org/freedesktop/DBus',"
77 "arg1='", unique
, "',",
80 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
84 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
91 int bus_event_loop_with_idle(
96 check_idle_t check_idle
,
108 r
= sd_event_get_state(e
);
111 if (r
== SD_EVENT_FINISHED
)
115 idle
= check_idle(userdata
);
119 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
123 if (r
== 0 && !exiting
&& idle
) {
125 r
= sd_bus_try_close(bus
);
129 /* Fallback for dbus1 connections: we
130 * unregister the name and wait for the
131 * response to come through for it */
132 if (r
== -EOPNOTSUPP
) {
134 /* Inform the service manager that we
135 * are going down, so that it will
136 * queue all further start requests,
137 * instead of assuming we are already
139 sd_notify(false, "STOPPING=1");
141 r
= bus_async_unregister_and_exit(e
, bus
, name
);
157 r
= sd_event_get_exit_code(e
, &code
);
164 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
165 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
166 int r
, has_owner
= 0;
171 r
= sd_bus_call_method(c
,
172 "org.freedesktop.DBus",
173 "/org/freedesktop/dbus",
174 "org.freedesktop.DBus",
183 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
185 return sd_bus_error_set_errno(error
, r
);
190 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
191 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
197 if (good_user
== UID_INVALID
)
200 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
204 /* Don't trust augmented credentials for authorization */
205 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
207 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
211 return sender_uid
== good_user
;
215 sd_bus_message
*call
,
218 const char **details
,
228 /* Tests non-interactively! */
230 r
= check_good_user(call
, good_user
);
234 r
= sd_bus_query_sender_privilege(call
, capability
);
241 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
242 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
243 int authorized
= false, challenge
= false;
244 const char *sender
, **k
, **v
;
246 sender
= sd_bus_message_get_sender(call
);
250 r
= sd_bus_message_new_method_call(
253 "org.freedesktop.PolicyKit1",
254 "/org/freedesktop/PolicyKit1/Authority",
255 "org.freedesktop.PolicyKit1.Authority",
256 "CheckAuthorization");
260 r
= sd_bus_message_append(
263 "system-bus-name", 1, "name", "s", sender
,
268 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
272 STRV_FOREACH_PAIR(k
, v
, details
) {
273 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
278 r
= sd_bus_message_close_container(request
);
282 r
= sd_bus_message_append(request
, "us", 0, NULL
);
286 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
288 /* Treat no PK available as access denied */
289 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
290 sd_bus_error_free(e
);
297 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
301 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
309 *_challenge
= challenge
;
320 typedef struct AsyncPolkitQuery
{
321 sd_bus_message
*request
, *reply
;
322 sd_bus_message_handler_t callback
;
328 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
333 sd_bus_slot_unref(q
->slot
);
335 if (q
->registry
&& q
->request
)
336 hashmap_remove(q
->registry
, q
->request
);
338 sd_bus_message_unref(q
->request
);
339 sd_bus_message_unref(q
->reply
);
344 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
345 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
346 AsyncPolkitQuery
*q
= userdata
;
352 q
->slot
= sd_bus_slot_unref(q
->slot
);
353 q
->reply
= sd_bus_message_ref(reply
);
355 r
= sd_bus_message_rewind(q
->request
, true);
357 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
361 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
362 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
365 async_polkit_query_free(q
);
372 int bus_verify_polkit_async(
373 sd_bus_message
*call
,
376 const char **details
,
380 sd_bus_error
*error
) {
383 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
385 const char *sender
, **k
, **v
;
386 sd_bus_message_handler_t callback
;
396 r
= check_good_user(call
, good_user
);
401 q
= hashmap_get(*registry
, call
);
403 int authorized
, challenge
;
405 /* This is the second invocation of this function, and
406 * there's already a response from polkit, let's
410 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
411 const sd_bus_error
*e
;
413 /* Copy error from polkit reply */
414 e
= sd_bus_message_get_error(q
->reply
);
415 sd_bus_error_copy(error
, e
);
417 /* Treat no PK available as access denied */
418 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
))
421 return -sd_bus_error_get_errno(e
);
424 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
426 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
435 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
441 r
= sd_bus_query_sender_privilege(call
, capability
);
448 if (sd_bus_get_current_message(call
->bus
) != call
)
451 callback
= sd_bus_get_current_handler(call
->bus
);
455 userdata
= sd_bus_get_current_userdata(call
->bus
);
457 sender
= sd_bus_message_get_sender(call
);
461 c
= sd_bus_message_get_allow_interactive_authorization(call
);
467 r
= hashmap_ensure_allocated(registry
, NULL
);
471 r
= sd_bus_message_new_method_call(
474 "org.freedesktop.PolicyKit1",
475 "/org/freedesktop/PolicyKit1/Authority",
476 "org.freedesktop.PolicyKit1.Authority",
477 "CheckAuthorization");
481 r
= sd_bus_message_append(
484 "system-bus-name", 1, "name", "s", sender
,
489 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
493 STRV_FOREACH_PAIR(k
, v
, details
) {
494 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
499 r
= sd_bus_message_close_container(pk
);
503 r
= sd_bus_message_append(pk
, "us", interactive
, NULL
);
507 q
= new0(AsyncPolkitQuery
, 1);
511 q
->request
= sd_bus_message_ref(call
);
512 q
->callback
= callback
;
513 q
->userdata
= userdata
;
515 r
= hashmap_put(*registry
, call
, q
);
517 async_polkit_query_free(q
);
521 q
->registry
= *registry
;
523 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
525 async_polkit_query_free(q
);
535 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
537 hashmap_free_with_destructor(registry
, async_polkit_query_free
);
541 int bus_check_peercred(sd_bus
*c
) {
547 fd
= sd_bus_get_fd(c
);
551 r
= getpeercred(fd
, &ucred
);
555 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
561 int bus_connect_system_systemd(sd_bus
**_bus
) {
562 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
568 return sd_bus_default_system(_bus
);
570 /* If we are root then let's talk directly to the system
571 * instance, instead of going via the bus */
573 r
= sd_bus_new(&bus
);
577 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
581 r
= sd_bus_start(bus
);
583 return sd_bus_default_system(_bus
);
585 r
= bus_check_peercred(bus
);
589 *_bus
= TAKE_PTR(bus
);
594 int bus_connect_user_systemd(sd_bus
**_bus
) {
595 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
596 _cleanup_free_
char *ee
= NULL
;
602 e
= secure_getenv("XDG_RUNTIME_DIR");
604 return sd_bus_default_user(_bus
);
606 ee
= bus_address_escape(e
);
610 r
= sd_bus_new(&bus
);
614 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
618 r
= sd_bus_start(bus
);
620 return sd_bus_default_user(_bus
);
622 r
= bus_check_peercred(bus
);
626 *_bus
= TAKE_PTR(bus
);
631 int bus_print_property_value(const char *name
, const char *expected_value
, bool only_value
, const char *value
) {
634 if (expected_value
&& !streq_ptr(expected_value
, value
))
640 printf("%s=%s\n", name
, value
);
645 int bus_print_property_valuef(const char *name
, const char *expected_value
, bool only_value
, const char *fmt
, ...) {
652 if (expected_value
) {
653 _cleanup_free_
char *s
= NULL
;
656 r
= vasprintf(&s
, fmt
, ap
);
661 if (streq_ptr(expected_value
, s
)) {
665 printf("%s=%s\n", name
, s
);
681 static int bus_print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
683 const char *contents
;
689 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
695 case SD_BUS_TYPE_STRING
: {
698 r
= sd_bus_message_read_basic(m
, type
, &s
);
702 if (all
|| !isempty(s
)) {
705 /* This property has a single value, so we need to take
706 * care not to print a new line, everything else is OK. */
707 good
= !strchr(s
, '\n');
708 bus_print_property_value(name
, expected_value
, value
, good
? s
: "[unprintable]");
714 case SD_BUS_TYPE_BOOLEAN
: {
717 r
= sd_bus_message_read_basic(m
, type
, &b
);
721 if (expected_value
&& parse_boolean(expected_value
) != b
)
724 bus_print_property_value(name
, NULL
, value
, yes_no(b
));
728 case SD_BUS_TYPE_UINT64
: {
731 r
= sd_bus_message_read_basic(m
, type
, &u
);
735 /* Yes, heuristics! But we can change this check
736 * should it turn out to not be sufficient */
738 if (endswith(name
, "Timestamp") ||
739 STR_IN_SET(name
, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
740 char timestamp
[FORMAT_TIMESTAMP_MAX
];
743 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
745 bus_print_property_value(name
, expected_value
, value
, strempty(t
));
747 } else if (strstr(name
, "USec")) {
748 char timespan
[FORMAT_TIMESPAN_MAX
];
750 (void) format_timespan(timespan
, sizeof(timespan
), u
, 0);
751 bus_print_property_value(name
, expected_value
, value
, timespan
);
753 } else if (streq(name
, "RestrictNamespaces")) {
754 _cleanup_free_
char *s
= NULL
;
757 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
759 else if ((u
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
762 r
= namespace_flags_to_string(u
, &s
);
769 bus_print_property_value(name
, expected_value
, value
, result
);
771 } else if (streq(name
, "MountFlags")) {
774 result
= mount_propagation_flags_to_string(u
);
778 bus_print_property_value(name
, expected_value
, value
, result
);
780 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
781 _cleanup_free_
char *s
= NULL
;
783 r
= capability_set_to_string_alloc(u
, &s
);
787 bus_print_property_value(name
, expected_value
, value
, s
);
789 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
790 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
791 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
792 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
793 (endswith(name
, "NSec") && u
== (uint64_t) -1))
795 bus_print_property_value(name
, expected_value
, value
, "[not set]");
797 else if ((STR_IN_SET(name
, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
798 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
799 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
800 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
802 bus_print_property_value(name
, expected_value
, value
, "infinity");
803 else if (STR_IN_SET(name
, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u
== (uint64_t) -1)
804 bus_print_property_value(name
, expected_value
, value
, "[no data]");
806 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu64
, u
);
811 case SD_BUS_TYPE_INT64
: {
814 r
= sd_bus_message_read_basic(m
, type
, &i
);
818 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi64
, i
);
822 case SD_BUS_TYPE_UINT32
: {
825 r
= sd_bus_message_read_basic(m
, type
, &u
);
829 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
830 bus_print_property_valuef(name
, expected_value
, value
, "%04o", u
);
832 else if (streq(name
, "UID")) {
833 if (u
== UID_INVALID
)
834 bus_print_property_value(name
, expected_value
, value
, "[not set]");
836 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
837 } else if (streq(name
, "GID")) {
838 if (u
== GID_INVALID
)
839 bus_print_property_value(name
, expected_value
, value
, "[not set]");
841 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
843 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
848 case SD_BUS_TYPE_INT32
: {
851 r
= sd_bus_message_read_basic(m
, type
, &i
);
855 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi32
, i
);
859 case SD_BUS_TYPE_DOUBLE
: {
862 r
= sd_bus_message_read_basic(m
, type
, &d
);
866 bus_print_property_valuef(name
, expected_value
, value
, "%g", d
);
870 case SD_BUS_TYPE_ARRAY
:
871 if (streq(contents
, "s")) {
875 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, contents
);
879 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
885 /* This property has multiple space-separated values, so
886 * neither spaces nor newlines can be allowed in a value. */
887 good
= str
[strcspn(str
, " \n")] == '\0';
889 printf("%s%s", first
? "" : " ", good
? str
: "[unprintable]");
896 if (first
&& all
&& !value
)
901 r
= sd_bus_message_exit_container(m
);
907 } else if (streq(contents
, "y")) {
911 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
921 for (i
= 0; i
< n
; i
++)
922 printf("%02x", u
[i
]);
929 } else if (streq(contents
, "u")) {
933 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
943 for (i
= 0; i
< n
; i
++)
944 printf("%08x", u
[i
]);
958 int bus_message_print_all_properties(
960 bus_message_print_t func
,
964 Set
**found_properties
) {
970 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
974 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
975 _cleanup_free_
char *name_with_equal
= NULL
;
976 const char *name
, *contents
, *expected_value
= NULL
;
978 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &name
);
982 if (found_properties
) {
983 r
= set_ensure_allocated(found_properties
, &string_hash_ops
);
987 r
= set_put(*found_properties
, name
);
988 if (r
< 0 && r
!= -EEXIST
)
992 name_with_equal
= strappend(name
, "=");
993 if (!name_with_equal
)
996 if (!filter
|| strv_find(filter
, name
) ||
997 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
998 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1002 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1007 r
= func(name
, expected_value
, m
, value
, all
);
1008 if (!func
|| r
== 0)
1009 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
1013 if (all
&& !expected_value
)
1014 printf("%s=[unprintable]\n", name
);
1015 /* skip what we didn't read */
1016 r
= sd_bus_message_skip(m
, contents
);
1021 r
= sd_bus_message_exit_container(m
);
1025 r
= sd_bus_message_skip(m
, "v");
1030 r
= sd_bus_message_exit_container(m
);
1037 r
= sd_bus_message_exit_container(m
);
1044 int bus_print_all_properties(
1048 bus_message_print_t func
,
1052 Set
**found_properties
) {
1054 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1055 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1061 r
= sd_bus_call_method(bus
,
1064 "org.freedesktop.DBus.Properties",
1072 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
1075 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1076 sd_id128_t
*p
= userdata
;
1081 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1088 memcpy((*p
).bytes
, v
, n
);
1095 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
1099 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1105 case SD_BUS_TYPE_STRING
: {
1106 const char **p
= userdata
;
1109 r
= sd_bus_message_read_basic(m
, type
, &s
);
1116 if (flags
& BUS_MAP_STRDUP
)
1117 return free_and_strdup((char **) userdata
, s
);
1123 case SD_BUS_TYPE_ARRAY
: {
1124 _cleanup_strv_free_
char **l
= NULL
;
1125 char ***p
= userdata
;
1127 r
= bus_message_read_strv_extend(m
, &l
);
1131 return strv_free_and_replace(*p
, l
);
1134 case SD_BUS_TYPE_BOOLEAN
: {
1137 r
= sd_bus_message_read_basic(m
, type
, &b
);
1141 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
1142 *(bool*) userdata
= b
;
1144 *(int*) userdata
= b
;
1149 case SD_BUS_TYPE_INT32
:
1150 case SD_BUS_TYPE_UINT32
: {
1151 uint32_t u
, *p
= userdata
;
1153 r
= sd_bus_message_read_basic(m
, type
, &u
);
1161 case SD_BUS_TYPE_INT64
:
1162 case SD_BUS_TYPE_UINT64
: {
1163 uint64_t t
, *p
= userdata
;
1165 r
= sd_bus_message_read_basic(m
, type
, &t
);
1173 case SD_BUS_TYPE_DOUBLE
: {
1174 double d
, *p
= userdata
;
1176 r
= sd_bus_message_read_basic(m
, type
, &d
);
1187 int bus_message_map_all_properties(
1189 const struct bus_properties_map
*map
,
1191 sd_bus_error
*error
,
1199 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1203 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1204 const struct bus_properties_map
*prop
;
1206 const char *contents
;
1210 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1214 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1215 if (streq(map
[i
].member
, member
)) {
1221 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1225 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1229 v
= (uint8_t *)userdata
+ prop
->offset
;
1231 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
1233 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
1237 r
= sd_bus_message_exit_container(m
);
1241 r
= sd_bus_message_skip(m
, "v");
1246 r
= sd_bus_message_exit_container(m
);
1253 return sd_bus_message_exit_container(m
);
1256 int bus_map_all_properties(
1258 const char *destination
,
1260 const struct bus_properties_map
*map
,
1262 sd_bus_error
*error
,
1263 sd_bus_message
**reply
,
1266 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1270 assert(destination
);
1273 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
1275 r
= sd_bus_call_method(
1279 "org.freedesktop.DBus.Properties",
1287 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
1292 *reply
= sd_bus_message_ref(m
);
1297 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
1298 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1301 assert(transport
>= 0);
1302 assert(transport
< _BUS_TRANSPORT_MAX
);
1305 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1306 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1308 switch (transport
) {
1310 case BUS_TRANSPORT_LOCAL
:
1312 r
= sd_bus_default_user(&bus
);
1314 if (sd_booted() <= 0) {
1315 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1316 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
1320 r
= sd_bus_default_system(&bus
);
1324 case BUS_TRANSPORT_REMOTE
:
1325 r
= sd_bus_open_system_remote(&bus
, host
);
1328 case BUS_TRANSPORT_MACHINE
:
1329 r
= sd_bus_open_system_machine(&bus
, host
);
1333 assert_not_reached("Hmm, unknown transport type.");
1338 r
= sd_bus_set_exit_on_disconnect(bus
, true);
1342 *ret
= TAKE_PTR(bus
);
1347 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1350 assert(transport
>= 0);
1351 assert(transport
< _BUS_TRANSPORT_MAX
);
1354 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1355 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1357 switch (transport
) {
1359 case BUS_TRANSPORT_LOCAL
:
1361 r
= bus_connect_user_systemd(bus
);
1363 if (sd_booted() <= 0)
1364 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1365 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1366 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1367 r
= bus_connect_system_systemd(bus
);
1371 case BUS_TRANSPORT_REMOTE
:
1372 r
= sd_bus_open_system_remote(bus
, host
);
1375 case BUS_TRANSPORT_MACHINE
:
1376 r
= sd_bus_open_system_machine(bus
, host
);
1380 assert_not_reached("Hmm, unknown transport type.");
1386 int bus_property_get_bool(
1389 const char *interface
,
1390 const char *property
,
1391 sd_bus_message
*reply
,
1393 sd_bus_error
*error
) {
1395 int b
= *(bool*) userdata
;
1397 return sd_bus_message_append_basic(reply
, 'b', &b
);
1400 int bus_property_set_bool(
1403 const char *interface
,
1404 const char *property
,
1405 sd_bus_message
*value
,
1407 sd_bus_error
*error
) {
1411 r
= sd_bus_message_read(value
, "b", &b
);
1415 *(bool*) userdata
= b
;
1419 int bus_property_get_id128(
1422 const char *interface
,
1423 const char *property
,
1424 sd_bus_message
*reply
,
1426 sd_bus_error
*error
) {
1428 sd_id128_t
*id
= userdata
;
1430 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1431 return sd_bus_message_append(reply
, "ay", 0);
1433 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1436 #if __SIZEOF_SIZE_T__ != 8
1437 int bus_property_get_size(
1440 const char *interface
,
1441 const char *property
,
1442 sd_bus_message
*reply
,
1444 sd_bus_error
*error
) {
1446 uint64_t sz
= *(size_t*) userdata
;
1448 return sd_bus_message_append_basic(reply
, 't', &sz
);
1452 #if __SIZEOF_LONG__ != 8
1453 int bus_property_get_long(
1456 const char *interface
,
1457 const char *property
,
1458 sd_bus_message
*reply
,
1460 sd_bus_error
*error
) {
1462 int64_t l
= *(long*) userdata
;
1464 return sd_bus_message_append_basic(reply
, 'x', &l
);
1467 int bus_property_get_ulong(
1470 const char *interface
,
1471 const char *property
,
1472 sd_bus_message
*reply
,
1474 sd_bus_error
*error
) {
1476 uint64_t ul
= *(unsigned long*) userdata
;
1478 return sd_bus_message_append_basic(reply
, 't', &ul
);
1482 int bus_log_parse_error(int r
) {
1483 return log_error_errno(r
, "Failed to parse bus message: %m");
1486 int bus_log_create_error(int r
) {
1487 return log_error_errno(r
, "Failed to create bus message: %m");
1491 * bus_path_encode_unique() - encode unique object path
1492 * @b: bus connection or NULL
1493 * @prefix: object path prefix
1494 * @sender_id: unique-name of client, or NULL
1495 * @external_id: external ID to be chosen by client, or NULL
1496 * @ret_path: storage for encoded object path pointer
1498 * Whenever we provide a bus API that allows clients to create and manage
1499 * server-side objects, we need to provide a unique name for these objects. If
1500 * we let the server choose the name, we suffer from a race condition: If a
1501 * client creates an object asynchronously, it cannot destroy that object until
1502 * it received the method reply. It cannot know the name of the new object,
1503 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1505 * Therefore, many APIs allow the client to choose the unique name for newly
1506 * created objects. There're two problems to solve, though:
1507 * 1) Object names are usually defined via dbus object paths, which are
1508 * usually globally namespaced. Therefore, multiple clients must be able
1509 * to choose unique object names without interference.
1510 * 2) If multiple libraries share the same bus connection, they must be
1511 * able to choose unique object names without interference.
1512 * The first problem is solved easily by prefixing a name with the
1513 * unique-bus-name of a connection. The server side must enforce this and
1514 * reject any other name. The second problem is solved by providing unique
1515 * suffixes from within sd-bus.
1517 * This helper allows clients to create unique object-paths. It uses the
1518 * template '/prefix/sender_id/external_id' and returns the new path in
1519 * @ret_path (must be freed by the caller).
1520 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1521 * NULL, this function allocates a unique suffix via @b (by requesting a new
1522 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1525 * Returns: 0 on success, negative error code on failure.
1527 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1528 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1529 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1532 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1533 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1534 assert_return(ret_path
, -EINVAL
);
1537 r
= sd_bus_get_unique_name(b
, &sender_id
);
1543 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1544 external_id
= external_buf
;
1547 sender_label
= bus_label_escape(sender_id
);
1551 external_label
= bus_label_escape(external_id
);
1552 if (!external_label
)
1555 p
= strjoin(prefix
, "/", sender_label
, "/", external_label
);
1564 * bus_path_decode_unique() - decode unique object path
1565 * @path: object path to decode
1566 * @prefix: object path prefix
1567 * @ret_sender: output parameter for sender-id label
1568 * @ret_external: output parameter for external-id label
1570 * This does the reverse of bus_path_encode_unique() (see its description for
1571 * details). Both trailing labels, sender-id and external-id, are unescaped and
1572 * returned in the given output parameters (the caller must free them).
1574 * Note that this function returns 0 if the path does not match the template
1575 * (see bus_path_encode_unique()), 1 if it matched.
1577 * Returns: Negative error code on failure, 0 if the given object path does not
1578 * match the template (return parameters are set to NULL), 1 if it was
1579 * parsed successfully (return parameters contain allocated labels).
1581 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1583 char *sender
, *external
;
1585 assert(object_path_is_valid(path
));
1586 assert(object_path_is_valid(prefix
));
1588 assert(ret_external
);
1590 p
= object_path_startswith(path
, prefix
);
1593 *ret_external
= NULL
;
1600 *ret_external
= NULL
;
1604 sender
= bus_label_unescape_n(p
, q
- p
);
1605 external
= bus_label_unescape(q
+ 1);
1606 if (!sender
|| !external
) {
1612 *ret_sender
= sender
;
1613 *ret_external
= external
;
1617 int bus_property_get_rlimit(
1620 const char *interface
,
1621 const char *property
,
1622 sd_bus_message
*reply
,
1624 sd_bus_error
*error
) {
1626 const char *is_soft
;
1635 is_soft
= endswith(property
, "Soft");
1637 rl
= *(struct rlimit
**) userdata
;
1639 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1641 struct rlimit buf
= {};
1645 /* Chop off "Soft" suffix */
1646 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1648 /* Skip over any prefix, such as "Default" */
1649 assert_se(p
= strstr(s
, "Limit"));
1651 z
= rlimit_from_string(p
+ 5);
1654 (void) getrlimit(z
, &buf
);
1655 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1658 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1660 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1662 return sd_bus_message_append(reply
, "t", u
);
1665 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1671 /* Continues adding after failure, and returns the first failure. */
1673 STRV_FOREACH(i
, l
) {
1676 k
= sd_bus_track_add_name(t
, *i
);
1677 if (k
< 0 && r
>= 0)
1684 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1685 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1691 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */
1693 r
= sd_bus_new(&bus
);
1698 r
= sd_bus_set_description(bus
, description
);
1703 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1705 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1707 r
= sd_bus_set_address(bus
, e
);
1711 r
= sd_bus_set_bus_client(bus
, true);
1715 r
= sd_bus_set_trusted(bus
, true);
1719 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1723 r
= sd_bus_set_watch_bind(bus
, true);
1727 r
= sd_bus_set_connected_signal(bus
, true);
1731 r
= sd_bus_start(bus
);
1735 *ret
= TAKE_PTR(bus
);
1740 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1741 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1747 /* Reply to the specified message with a message containing a dictionary put together from the specified
1750 r
= sd_bus_message_new_method_return(m
, &reply
);
1754 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1758 STRV_FOREACH_PAIR(k
, v
, l
) {
1759 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1764 r
= sd_bus_message_close_container(reply
);
1768 return sd_bus_send(NULL
, reply
, NULL
);