1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include <sys/resource.h>
9 #include <sys/socket.h>
12 #include "sd-bus-protocol.h"
14 #include "sd-daemon.h"
18 #include "alloc-util.h"
19 #include "bus-internal.h"
20 #include "bus-label.h"
21 #include "bus-message.h"
24 #include "cgroup-util.h"
28 #include "mountpoint-util.h"
30 #include "parse-util.h"
31 #include "path-util.h"
32 #include "proc-cmdline.h"
33 #include "rlimit-util.h"
34 #include "stdio-util.h"
36 #include "user-util.h"
38 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
39 sd_event
*e
= userdata
;
44 sd_bus_close(sd_bus_message_get_bus(m
));
50 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
59 /* We unregister the name here and then wait for the
60 * NameOwnerChanged signal for this event to arrive before we
61 * quit. We do this in order to make sure that any queued
62 * requests are still processed before we really exit. */
64 r
= sd_bus_get_unique_name(bus
, &unique
);
69 "sender='org.freedesktop.DBus',"
71 "interface='org.freedesktop.DBus',"
72 "member='NameOwnerChanged',"
73 "path='/org/freedesktop/DBus',"
75 "arg1='", unique
, "',",
78 r
= sd_bus_add_match_async(bus
, NULL
, match
, name_owner_change_callback
, NULL
, e
);
82 r
= sd_bus_release_name_async(bus
, NULL
, name
, NULL
, NULL
);
89 int bus_event_loop_with_idle(
94 check_idle_t check_idle
,
106 r
= sd_event_get_state(e
);
109 if (r
== SD_EVENT_FINISHED
)
113 idle
= check_idle(userdata
);
117 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
121 if (r
== 0 && !exiting
&& idle
) {
123 r
= sd_bus_try_close(bus
);
127 /* Fallback for dbus1 connections: we
128 * unregister the name and wait for the
129 * response to come through for it */
130 if (r
== -EOPNOTSUPP
) {
132 /* Inform the service manager that we
133 * are going down, so that it will
134 * queue all further start requests,
135 * instead of assuming we are already
137 sd_notify(false, "STOPPING=1");
139 r
= bus_async_unregister_and_exit(e
, bus
, name
);
155 r
= sd_event_get_exit_code(e
, &code
);
162 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
163 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
164 int r
, has_owner
= 0;
169 r
= sd_bus_call_method(c
,
170 "org.freedesktop.DBus",
171 "/org/freedesktop/dbus",
172 "org.freedesktop.DBus",
181 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
183 return sd_bus_error_set_errno(error
, r
);
188 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
189 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
195 if (good_user
== UID_INVALID
)
198 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
202 /* Don't trust augmented credentials for authorization */
203 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
205 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
209 return sender_uid
== good_user
;
213 sd_bus_message
*call
,
216 const char **details
,
226 /* Tests non-interactively! */
228 r
= check_good_user(call
, good_user
);
232 r
= sd_bus_query_sender_privilege(call
, capability
);
239 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
240 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
241 int authorized
= false, challenge
= false;
242 const char *sender
, **k
, **v
;
244 sender
= sd_bus_message_get_sender(call
);
248 r
= sd_bus_message_new_method_call(
251 "org.freedesktop.PolicyKit1",
252 "/org/freedesktop/PolicyKit1/Authority",
253 "org.freedesktop.PolicyKit1.Authority",
254 "CheckAuthorization");
258 r
= sd_bus_message_append(
261 "system-bus-name", 1, "name", "s", sender
,
266 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
270 STRV_FOREACH_PAIR(k
, v
, details
) {
271 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
276 r
= sd_bus_message_close_container(request
);
280 r
= sd_bus_message_append(request
, "us", 0, NULL
);
284 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
286 /* Treat no PK available as access denied */
287 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
288 sd_bus_error_free(e
);
295 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
299 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
307 *_challenge
= challenge
;
318 typedef struct AsyncPolkitQuery
{
319 sd_bus_message
*request
, *reply
;
320 sd_bus_message_handler_t callback
;
326 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
331 sd_bus_slot_unref(q
->slot
);
333 if (q
->registry
&& q
->request
)
334 hashmap_remove(q
->registry
, q
->request
);
336 sd_bus_message_unref(q
->request
);
337 sd_bus_message_unref(q
->reply
);
342 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
343 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
344 AsyncPolkitQuery
*q
= userdata
;
350 q
->slot
= sd_bus_slot_unref(q
->slot
);
351 q
->reply
= sd_bus_message_ref(reply
);
353 r
= sd_bus_message_rewind(q
->request
, true);
355 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
359 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
360 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
363 async_polkit_query_free(q
);
370 int bus_verify_polkit_async(
371 sd_bus_message
*call
,
374 const char **details
,
378 sd_bus_error
*error
) {
381 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
383 const char *sender
, **k
, **v
;
384 sd_bus_message_handler_t callback
;
394 r
= check_good_user(call
, good_user
);
399 q
= hashmap_get(*registry
, call
);
401 int authorized
, challenge
;
403 /* This is the second invocation of this function, and
404 * there's already a response from polkit, let's
408 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
409 const sd_bus_error
*e
;
411 e
= sd_bus_message_get_error(q
->reply
);
413 /* Treat no PK available as access denied */
414 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
) ||
415 sd_bus_error_has_name(e
, SD_BUS_ERROR_NAME_HAS_NO_OWNER
))
418 /* Copy error from polkit reply */
419 sd_bus_error_copy(error
, e
);
420 return -sd_bus_error_get_errno(e
);
423 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
425 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
433 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
439 r
= sd_bus_query_sender_privilege(call
, capability
);
446 if (sd_bus_get_current_message(call
->bus
) != call
)
449 callback
= sd_bus_get_current_handler(call
->bus
);
453 userdata
= sd_bus_get_current_userdata(call
->bus
);
455 sender
= sd_bus_message_get_sender(call
);
459 c
= sd_bus_message_get_allow_interactive_authorization(call
);
465 r
= hashmap_ensure_allocated(registry
, NULL
);
469 r
= sd_bus_message_new_method_call(
472 "org.freedesktop.PolicyKit1",
473 "/org/freedesktop/PolicyKit1/Authority",
474 "org.freedesktop.PolicyKit1.Authority",
475 "CheckAuthorization");
479 r
= sd_bus_message_append(
482 "system-bus-name", 1, "name", "s", sender
,
487 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
491 STRV_FOREACH_PAIR(k
, v
, details
) {
492 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
497 r
= sd_bus_message_close_container(pk
);
501 r
= sd_bus_message_append(pk
, "us", interactive
, NULL
);
505 q
= new0(AsyncPolkitQuery
, 1);
509 q
->request
= sd_bus_message_ref(call
);
510 q
->callback
= callback
;
511 q
->userdata
= userdata
;
513 r
= hashmap_put(*registry
, call
, q
);
515 async_polkit_query_free(q
);
519 q
->registry
= *registry
;
521 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
523 async_polkit_query_free(q
);
533 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
535 hashmap_free_with_destructor(registry
, async_polkit_query_free
);
539 int bus_check_peercred(sd_bus
*c
) {
545 fd
= sd_bus_get_fd(c
);
549 r
= getpeercred(fd
, &ucred
);
553 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
559 int bus_connect_system_systemd(sd_bus
**_bus
) {
560 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
566 return sd_bus_default_system(_bus
);
568 /* If we are root then let's talk directly to the system
569 * instance, instead of going via the bus */
571 r
= sd_bus_new(&bus
);
575 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
579 r
= sd_bus_start(bus
);
581 return sd_bus_default_system(_bus
);
583 r
= bus_check_peercred(bus
);
587 *_bus
= TAKE_PTR(bus
);
592 int bus_connect_user_systemd(sd_bus
**_bus
) {
593 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
594 _cleanup_free_
char *ee
= NULL
;
600 e
= secure_getenv("XDG_RUNTIME_DIR");
602 return sd_bus_default_user(_bus
);
604 ee
= bus_address_escape(e
);
608 r
= sd_bus_new(&bus
);
612 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
616 r
= sd_bus_start(bus
);
618 return sd_bus_default_user(_bus
);
620 r
= bus_check_peercred(bus
);
624 *_bus
= TAKE_PTR(bus
);
629 int bus_print_property_value(const char *name
, const char *expected_value
, bool only_value
, const char *value
) {
632 if (expected_value
&& !streq_ptr(expected_value
, value
))
638 printf("%s=%s\n", name
, value
);
643 int bus_print_property_valuef(const char *name
, const char *expected_value
, bool only_value
, const char *fmt
, ...) {
650 if (expected_value
) {
651 _cleanup_free_
char *s
= NULL
;
654 r
= vasprintf(&s
, fmt
, ap
);
659 if (streq_ptr(expected_value
, s
)) {
663 printf("%s=%s\n", name
, s
);
679 static int bus_print_property(const char *name
, const char *expected_value
, sd_bus_message
*m
, bool value
, bool all
) {
681 const char *contents
;
687 r
= sd_bus_message_peek_type(m
, &type
, &contents
);
693 case SD_BUS_TYPE_STRING
: {
696 r
= sd_bus_message_read_basic(m
, type
, &s
);
700 if (all
|| !isempty(s
)) {
703 /* This property has a single value, so we need to take
704 * care not to print a new line, everything else is OK. */
705 good
= !strchr(s
, '\n');
706 bus_print_property_value(name
, expected_value
, value
, good
? s
: "[unprintable]");
712 case SD_BUS_TYPE_BOOLEAN
: {
715 r
= sd_bus_message_read_basic(m
, type
, &b
);
719 if (expected_value
&& parse_boolean(expected_value
) != b
)
722 bus_print_property_value(name
, NULL
, value
, yes_no(b
));
726 case SD_BUS_TYPE_UINT64
: {
729 r
= sd_bus_message_read_basic(m
, type
, &u
);
733 /* Yes, heuristics! But we can change this check
734 * should it turn out to not be sufficient */
736 if (endswith(name
, "Timestamp") ||
737 STR_IN_SET(name
, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) {
738 char timestamp
[FORMAT_TIMESTAMP_MAX
];
741 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
743 bus_print_property_value(name
, expected_value
, value
, strempty(t
));
745 } else if (strstr(name
, "USec")) {
746 char timespan
[FORMAT_TIMESPAN_MAX
];
748 (void) format_timespan(timespan
, sizeof(timespan
), u
, 0);
749 bus_print_property_value(name
, expected_value
, value
, timespan
);
751 } else if (streq(name
, "RestrictNamespaces")) {
752 _cleanup_free_
char *s
= NULL
;
755 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
757 else if (FLAGS_SET(u
, NAMESPACE_FLAGS_ALL
))
760 r
= namespace_flags_to_string(u
, &s
);
767 bus_print_property_value(name
, expected_value
, value
, result
);
769 } else if (streq(name
, "MountFlags")) {
772 result
= mount_propagation_flags_to_string(u
);
776 bus_print_property_value(name
, expected_value
, value
, result
);
778 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
779 _cleanup_free_
char *s
= NULL
;
781 r
= capability_set_to_string_alloc(u
, &s
);
785 bus_print_property_value(name
, expected_value
, value
, s
);
787 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
788 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
789 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
790 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
791 (endswith(name
, "NSec") && u
== (uint64_t) -1))
793 bus_print_property_value(name
, expected_value
, value
, "[not set]");
795 else if ((STR_IN_SET(name
, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
796 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
797 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
798 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
800 bus_print_property_value(name
, expected_value
, value
, "infinity");
801 else if (STR_IN_SET(name
, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u
== (uint64_t) -1)
802 bus_print_property_value(name
, expected_value
, value
, "[no data]");
804 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu64
, u
);
809 case SD_BUS_TYPE_INT64
: {
812 r
= sd_bus_message_read_basic(m
, type
, &i
);
816 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi64
, i
);
820 case SD_BUS_TYPE_UINT32
: {
823 r
= sd_bus_message_read_basic(m
, type
, &u
);
827 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
828 bus_print_property_valuef(name
, expected_value
, value
, "%04o", u
);
830 else if (streq(name
, "UID")) {
831 if (u
== UID_INVALID
)
832 bus_print_property_value(name
, expected_value
, value
, "[not set]");
834 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
835 } else if (streq(name
, "GID")) {
836 if (u
== GID_INVALID
)
837 bus_print_property_value(name
, expected_value
, value
, "[not set]");
839 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
841 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIu32
, u
);
846 case SD_BUS_TYPE_INT32
: {
849 r
= sd_bus_message_read_basic(m
, type
, &i
);
853 bus_print_property_valuef(name
, expected_value
, value
, "%"PRIi32
, i
);
857 case SD_BUS_TYPE_DOUBLE
: {
860 r
= sd_bus_message_read_basic(m
, type
, &d
);
864 bus_print_property_valuef(name
, expected_value
, value
, "%g", d
);
868 case SD_BUS_TYPE_ARRAY
:
869 if (streq(contents
, "s")) {
873 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, contents
);
877 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
883 /* This property has multiple space-separated values, so
884 * neither spaces nor newlines can be allowed in a value. */
885 good
= str
[strcspn(str
, " \n")] == '\0';
887 printf("%s%s", first
? "" : " ", good
? str
: "[unprintable]");
894 if (first
&& all
&& !value
)
899 r
= sd_bus_message_exit_container(m
);
905 } else if (streq(contents
, "y")) {
909 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
919 for (i
= 0; i
< n
; i
++)
920 printf("%02x", u
[i
]);
927 } else if (streq(contents
, "u")) {
931 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
941 for (i
= 0; i
< n
; i
++)
942 printf("%08x", u
[i
]);
956 int bus_message_print_all_properties(
958 bus_message_print_t func
,
962 Set
**found_properties
) {
968 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
972 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
973 _cleanup_free_
char *name_with_equal
= NULL
;
974 const char *name
, *contents
, *expected_value
= NULL
;
976 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &name
);
980 if (found_properties
) {
981 r
= set_ensure_allocated(found_properties
, &string_hash_ops
);
985 r
= set_put(*found_properties
, name
);
986 if (r
< 0 && r
!= -EEXIST
)
990 name_with_equal
= strjoin(name
, "=");
991 if (!name_with_equal
)
994 if (!filter
|| strv_find(filter
, name
) ||
995 (expected_value
= strv_find_startswith(filter
, name_with_equal
))) {
996 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1000 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1005 r
= func(name
, expected_value
, m
, value
, all
);
1006 if (!func
|| r
== 0)
1007 r
= bus_print_property(name
, expected_value
, m
, value
, all
);
1011 if (all
&& !expected_value
)
1012 printf("%s=[unprintable]\n", name
);
1013 /* skip what we didn't read */
1014 r
= sd_bus_message_skip(m
, contents
);
1019 r
= sd_bus_message_exit_container(m
);
1023 r
= sd_bus_message_skip(m
, "v");
1028 r
= sd_bus_message_exit_container(m
);
1035 r
= sd_bus_message_exit_container(m
);
1042 int bus_print_all_properties(
1046 bus_message_print_t func
,
1050 Set
**found_properties
) {
1052 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1053 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1059 r
= sd_bus_call_method(bus
,
1062 "org.freedesktop.DBus.Properties",
1070 return bus_message_print_all_properties(reply
, func
, filter
, value
, all
, found_properties
);
1073 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1074 sd_id128_t
*p
= userdata
;
1079 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1086 memcpy((*p
).bytes
, v
, n
);
1093 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, unsigned flags
, sd_bus_error
*error
, void *userdata
) {
1097 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1103 case SD_BUS_TYPE_STRING
:
1104 case SD_BUS_TYPE_OBJECT_PATH
: {
1105 const char **p
= userdata
;
1108 r
= sd_bus_message_read_basic(m
, type
, &s
);
1115 if (flags
& BUS_MAP_STRDUP
)
1116 return free_and_strdup((char **) userdata
, s
);
1122 case SD_BUS_TYPE_ARRAY
: {
1123 _cleanup_strv_free_
char **l
= NULL
;
1124 char ***p
= userdata
;
1126 r
= bus_message_read_strv_extend(m
, &l
);
1130 return strv_extend_strv(p
, l
, false);
1133 case SD_BUS_TYPE_BOOLEAN
: {
1136 r
= sd_bus_message_read_basic(m
, type
, &b
);
1140 if (flags
& BUS_MAP_BOOLEAN_AS_BOOL
)
1141 *(bool*) userdata
= b
;
1143 *(int*) userdata
= b
;
1148 case SD_BUS_TYPE_INT32
:
1149 case SD_BUS_TYPE_UINT32
: {
1150 uint32_t u
, *p
= userdata
;
1152 r
= sd_bus_message_read_basic(m
, type
, &u
);
1160 case SD_BUS_TYPE_INT64
:
1161 case SD_BUS_TYPE_UINT64
: {
1162 uint64_t t
, *p
= userdata
;
1164 r
= sd_bus_message_read_basic(m
, type
, &t
);
1172 case SD_BUS_TYPE_DOUBLE
: {
1173 double d
, *p
= userdata
;
1175 r
= sd_bus_message_read_basic(m
, type
, &d
);
1186 int bus_message_map_all_properties(
1188 const struct bus_properties_map
*map
,
1190 sd_bus_error
*error
,
1198 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1202 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1203 const struct bus_properties_map
*prop
;
1205 const char *contents
;
1209 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1213 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1214 if (streq(map
[i
].member
, member
)) {
1220 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1224 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1228 v
= (uint8_t *)userdata
+ prop
->offset
;
1230 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
1232 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, flags
, error
, v
);
1236 r
= sd_bus_message_exit_container(m
);
1240 r
= sd_bus_message_skip(m
, "v");
1245 r
= sd_bus_message_exit_container(m
);
1252 return sd_bus_message_exit_container(m
);
1255 int bus_map_all_properties(
1257 const char *destination
,
1259 const struct bus_properties_map
*map
,
1261 sd_bus_error
*error
,
1262 sd_bus_message
**reply
,
1265 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1269 assert(destination
);
1272 assert(reply
|| (flags
& BUS_MAP_STRDUP
));
1274 r
= sd_bus_call_method(
1278 "org.freedesktop.DBus.Properties",
1286 r
= bus_message_map_all_properties(m
, map
, flags
, error
, userdata
);
1291 *reply
= sd_bus_message_ref(m
);
1296 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
1297 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1300 assert(transport
>= 0);
1301 assert(transport
< _BUS_TRANSPORT_MAX
);
1304 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1305 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1307 switch (transport
) {
1309 case BUS_TRANSPORT_LOCAL
:
1311 r
= sd_bus_default_user(&bus
);
1313 if (sd_booted() <= 0) {
1314 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1315 log_error("System has not been booted with systemd as init system (PID 1). Can't operate.");
1319 r
= sd_bus_default_system(&bus
);
1323 case BUS_TRANSPORT_REMOTE
:
1324 r
= sd_bus_open_system_remote(&bus
, host
);
1327 case BUS_TRANSPORT_MACHINE
:
1328 r
= sd_bus_open_system_machine(&bus
, host
);
1332 assert_not_reached("Hmm, unknown transport type.");
1337 r
= sd_bus_set_exit_on_disconnect(bus
, true);
1341 *ret
= TAKE_PTR(bus
);
1346 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1349 assert(transport
>= 0);
1350 assert(transport
< _BUS_TRANSPORT_MAX
);
1353 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1354 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1356 switch (transport
) {
1358 case BUS_TRANSPORT_LOCAL
:
1360 r
= bus_connect_user_systemd(bus
);
1362 if (sd_booted() <= 0)
1363 /* Print a friendly message when the local system is actually not running systemd as PID 1. */
1364 return log_error_errno(SYNTHETIC_ERRNO(EHOSTDOWN
),
1365 "System has not been booted with systemd as init system (PID 1). Can't operate.");
1366 r
= bus_connect_system_systemd(bus
);
1370 case BUS_TRANSPORT_REMOTE
:
1371 r
= sd_bus_open_system_remote(bus
, host
);
1374 case BUS_TRANSPORT_MACHINE
:
1375 r
= sd_bus_open_system_machine(bus
, host
);
1379 assert_not_reached("Hmm, unknown transport type.");
1385 int bus_property_get_bool(
1388 const char *interface
,
1389 const char *property
,
1390 sd_bus_message
*reply
,
1392 sd_bus_error
*error
) {
1394 int b
= *(bool*) userdata
;
1396 return sd_bus_message_append_basic(reply
, 'b', &b
);
1399 int bus_property_set_bool(
1402 const char *interface
,
1403 const char *property
,
1404 sd_bus_message
*value
,
1406 sd_bus_error
*error
) {
1410 r
= sd_bus_message_read(value
, "b", &b
);
1414 *(bool*) userdata
= b
;
1418 int bus_property_get_id128(
1421 const char *interface
,
1422 const char *property
,
1423 sd_bus_message
*reply
,
1425 sd_bus_error
*error
) {
1427 sd_id128_t
*id
= userdata
;
1429 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1430 return sd_bus_message_append(reply
, "ay", 0);
1432 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1435 #if __SIZEOF_SIZE_T__ != 8
1436 int bus_property_get_size(
1439 const char *interface
,
1440 const char *property
,
1441 sd_bus_message
*reply
,
1443 sd_bus_error
*error
) {
1445 uint64_t sz
= *(size_t*) userdata
;
1447 return sd_bus_message_append_basic(reply
, 't', &sz
);
1451 #if __SIZEOF_LONG__ != 8
1452 int bus_property_get_long(
1455 const char *interface
,
1456 const char *property
,
1457 sd_bus_message
*reply
,
1459 sd_bus_error
*error
) {
1461 int64_t l
= *(long*) userdata
;
1463 return sd_bus_message_append_basic(reply
, 'x', &l
);
1466 int bus_property_get_ulong(
1469 const char *interface
,
1470 const char *property
,
1471 sd_bus_message
*reply
,
1473 sd_bus_error
*error
) {
1475 uint64_t ul
= *(unsigned long*) userdata
;
1477 return sd_bus_message_append_basic(reply
, 't', &ul
);
1482 * bus_path_encode_unique() - encode unique object path
1483 * @b: bus connection or NULL
1484 * @prefix: object path prefix
1485 * @sender_id: unique-name of client, or NULL
1486 * @external_id: external ID to be chosen by client, or NULL
1487 * @ret_path: storage for encoded object path pointer
1489 * Whenever we provide a bus API that allows clients to create and manage
1490 * server-side objects, we need to provide a unique name for these objects. If
1491 * we let the server choose the name, we suffer from a race condition: If a
1492 * client creates an object asynchronously, it cannot destroy that object until
1493 * it received the method reply. It cannot know the name of the new object,
1494 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1496 * Therefore, many APIs allow the client to choose the unique name for newly
1497 * created objects. There're two problems to solve, though:
1498 * 1) Object names are usually defined via dbus object paths, which are
1499 * usually globally namespaced. Therefore, multiple clients must be able
1500 * to choose unique object names without interference.
1501 * 2) If multiple libraries share the same bus connection, they must be
1502 * able to choose unique object names without interference.
1503 * The first problem is solved easily by prefixing a name with the
1504 * unique-bus-name of a connection. The server side must enforce this and
1505 * reject any other name. The second problem is solved by providing unique
1506 * suffixes from within sd-bus.
1508 * This helper allows clients to create unique object-paths. It uses the
1509 * template '/prefix/sender_id/external_id' and returns the new path in
1510 * @ret_path (must be freed by the caller).
1511 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1512 * NULL, this function allocates a unique suffix via @b (by requesting a new
1513 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1516 * Returns: 0 on success, negative error code on failure.
1518 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1519 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1520 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1523 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1524 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1525 assert_return(ret_path
, -EINVAL
);
1528 r
= sd_bus_get_unique_name(b
, &sender_id
);
1534 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1535 external_id
= external_buf
;
1538 sender_label
= bus_label_escape(sender_id
);
1542 external_label
= bus_label_escape(external_id
);
1543 if (!external_label
)
1546 p
= path_join(prefix
, sender_label
, external_label
);
1555 * bus_path_decode_unique() - decode unique object path
1556 * @path: object path to decode
1557 * @prefix: object path prefix
1558 * @ret_sender: output parameter for sender-id label
1559 * @ret_external: output parameter for external-id label
1561 * This does the reverse of bus_path_encode_unique() (see its description for
1562 * details). Both trailing labels, sender-id and external-id, are unescaped and
1563 * returned in the given output parameters (the caller must free them).
1565 * Note that this function returns 0 if the path does not match the template
1566 * (see bus_path_encode_unique()), 1 if it matched.
1568 * Returns: Negative error code on failure, 0 if the given object path does not
1569 * match the template (return parameters are set to NULL), 1 if it was
1570 * parsed successfully (return parameters contain allocated labels).
1572 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1574 char *sender
, *external
;
1576 assert(object_path_is_valid(path
));
1577 assert(object_path_is_valid(prefix
));
1579 assert(ret_external
);
1581 p
= object_path_startswith(path
, prefix
);
1584 *ret_external
= NULL
;
1591 *ret_external
= NULL
;
1595 sender
= bus_label_unescape_n(p
, q
- p
);
1596 external
= bus_label_unescape(q
+ 1);
1597 if (!sender
|| !external
) {
1603 *ret_sender
= sender
;
1604 *ret_external
= external
;
1608 int bus_property_get_rlimit(
1611 const char *interface
,
1612 const char *property
,
1613 sd_bus_message
*reply
,
1615 sd_bus_error
*error
) {
1617 const char *is_soft
;
1626 is_soft
= endswith(property
, "Soft");
1628 rl
= *(struct rlimit
**) userdata
;
1630 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1632 struct rlimit buf
= {};
1636 /* Chop off "Soft" suffix */
1637 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1639 /* Skip over any prefix, such as "Default" */
1640 assert_se(p
= strstr(s
, "Limit"));
1642 z
= rlimit_from_string(p
+ 5);
1645 (void) getrlimit(z
, &buf
);
1646 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1649 /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all
1651 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1653 return sd_bus_message_append(reply
, "t", u
);
1656 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1662 /* Continues adding after failure, and returns the first failure. */
1664 STRV_FOREACH(i
, l
) {
1667 k
= sd_bus_track_add_name(t
, *i
);
1668 if (k
< 0 && r
>= 0)
1675 int bus_open_system_watch_bind_with_description(sd_bus
**ret
, const char *description
) {
1676 _cleanup_(sd_bus_close_unrefp
) sd_bus
*bus
= NULL
;
1682 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal
1685 r
= sd_bus_new(&bus
);
1690 r
= sd_bus_set_description(bus
, description
);
1695 e
= secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1697 e
= DEFAULT_SYSTEM_BUS_ADDRESS
;
1699 r
= sd_bus_set_address(bus
, e
);
1703 r
= sd_bus_set_bus_client(bus
, true);
1707 r
= sd_bus_negotiate_creds(bus
, true, SD_BUS_CREDS_UID
|SD_BUS_CREDS_EUID
|SD_BUS_CREDS_EFFECTIVE_CAPS
);
1711 r
= sd_bus_set_watch_bind(bus
, true);
1715 r
= sd_bus_set_connected_signal(bus
, true);
1719 r
= sd_bus_start(bus
);
1723 *ret
= TAKE_PTR(bus
);
1728 int bus_reply_pair_array(sd_bus_message
*m
, char **l
) {
1729 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
1735 /* Reply to the specified message with a message containing a dictionary put together from the
1738 r
= sd_bus_message_new_method_return(m
, &reply
);
1742 r
= sd_bus_message_open_container(reply
, 'a', "{ss}");
1746 STRV_FOREACH_PAIR(k
, v
, l
) {
1747 r
= sd_bus_message_append(reply
, "{ss}", *k
, *v
);
1752 r
= sd_bus_message_close_container(reply
);
1756 return sd_bus_send(NULL
, reply
, NULL
);
1759 static void bus_message_unref_wrapper(void *m
) {
1760 sd_bus_message_unref(m
);
1763 const struct hash_ops bus_message_hash_ops
= {
1764 .hash
= trivial_hash_func
,
1765 .compare
= trivial_compare_func
,
1766 .free_value
= bus_message_unref_wrapper
,