2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/ioctl.h>
27 #include <sys/resource.h>
28 #include <sys/socket.h>
31 #include "sd-bus-protocol.h"
33 #include "sd-daemon.h"
37 #include "alloc-util.h"
38 #include "bus-internal.h"
39 #include "bus-label.h"
40 #include "bus-message.h"
46 #include "parse-util.h"
47 #include "proc-cmdline.h"
48 #include "rlimit-util.h"
49 #include "stdio-util.h"
51 #include "user-util.h"
53 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
54 sd_event
*e
= userdata
;
59 sd_bus_close(sd_bus_message_get_bus(m
));
65 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
66 _cleanup_free_
char *match
= NULL
;
74 /* We unregister the name here and then wait for the
75 * NameOwnerChanged signal for this event to arrive before we
76 * quit. We do this in order to make sure that any queued
77 * requests are still processed before we really exit. */
79 r
= sd_bus_get_unique_name(bus
, &unique
);
84 "sender='org.freedesktop.DBus',"
86 "interface='org.freedesktop.DBus',"
87 "member='NameOwnerChanged',"
88 "path='/org/freedesktop/DBus',"
91 "arg2=''", name
, unique
);
95 r
= sd_bus_add_match(bus
, NULL
, match
, name_owner_change_callback
, e
);
99 r
= sd_bus_release_name(bus
, name
);
106 int bus_event_loop_with_idle(
111 check_idle_t check_idle
,
113 bool exiting
= false;
123 r
= sd_event_get_state(e
);
126 if (r
== SD_EVENT_FINISHED
)
130 idle
= check_idle(userdata
);
134 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
138 if (r
== 0 && !exiting
&& idle
) {
140 r
= sd_bus_try_close(bus
);
144 /* Fallback for dbus1 connections: we
145 * unregister the name and wait for the
146 * response to come through for it */
147 if (r
== -EOPNOTSUPP
) {
149 /* Inform the service manager that we
150 * are going down, so that it will
151 * queue all further start requests,
152 * instead of assuming we are already
154 sd_notify(false, "STOPPING=1");
156 r
= bus_async_unregister_and_exit(e
, bus
, name
);
172 r
= sd_event_get_exit_code(e
, &code
);
179 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
180 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
181 int r
, has_owner
= 0;
186 r
= sd_bus_call_method(c
,
187 "org.freedesktop.DBus",
188 "/org/freedesktop/dbus",
189 "org.freedesktop.DBus",
198 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
200 return sd_bus_error_set_errno(error
, r
);
205 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
206 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
212 if (good_user
== UID_INVALID
)
215 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
219 /* Don't trust augmented credentials for authorization */
220 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
222 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
226 return sender_uid
== good_user
;
230 sd_bus_message
*call
,
233 const char **details
,
243 /* Tests non-interactively! */
245 r
= check_good_user(call
, good_user
);
249 r
= sd_bus_query_sender_privilege(call
, capability
);
256 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
257 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
258 int authorized
= false, challenge
= false;
259 const char *sender
, **k
, **v
;
261 sender
= sd_bus_message_get_sender(call
);
265 r
= sd_bus_message_new_method_call(
268 "org.freedesktop.PolicyKit1",
269 "/org/freedesktop/PolicyKit1/Authority",
270 "org.freedesktop.PolicyKit1.Authority",
271 "CheckAuthorization");
275 r
= sd_bus_message_append(
278 "system-bus-name", 1, "name", "s", sender
,
283 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
287 STRV_FOREACH_PAIR(k
, v
, details
) {
288 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
293 r
= sd_bus_message_close_container(request
);
297 r
= sd_bus_message_append(request
, "us", 0, NULL
);
301 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
303 /* Treat no PK available as access denied */
304 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
305 sd_bus_error_free(e
);
312 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
316 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
324 *_challenge
= challenge
;
335 typedef struct AsyncPolkitQuery
{
336 sd_bus_message
*request
, *reply
;
337 sd_bus_message_handler_t callback
;
343 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
348 sd_bus_slot_unref(q
->slot
);
350 if (q
->registry
&& q
->request
)
351 hashmap_remove(q
->registry
, q
->request
);
353 sd_bus_message_unref(q
->request
);
354 sd_bus_message_unref(q
->reply
);
359 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
360 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
361 AsyncPolkitQuery
*q
= userdata
;
367 q
->slot
= sd_bus_slot_unref(q
->slot
);
368 q
->reply
= sd_bus_message_ref(reply
);
370 r
= sd_bus_message_rewind(q
->request
, true);
372 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
376 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
377 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
380 async_polkit_query_free(q
);
387 int bus_verify_polkit_async(
388 sd_bus_message
*call
,
391 const char **details
,
395 sd_bus_error
*error
) {
398 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
400 const char *sender
, **k
, **v
;
401 sd_bus_message_handler_t callback
;
411 r
= check_good_user(call
, good_user
);
416 q
= hashmap_get(*registry
, call
);
418 int authorized
, challenge
;
420 /* This is the second invocation of this function, and
421 * there's already a response from polkit, let's
425 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
426 const sd_bus_error
*e
;
428 /* Copy error from polkit reply */
429 e
= sd_bus_message_get_error(q
->reply
);
430 sd_bus_error_copy(error
, e
);
432 /* Treat no PK available as access denied */
433 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
))
436 return -sd_bus_error_get_errno(e
);
439 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
441 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
450 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
456 r
= sd_bus_query_sender_privilege(call
, capability
);
463 if (sd_bus_get_current_message(call
->bus
) != call
)
466 callback
= sd_bus_get_current_handler(call
->bus
);
470 userdata
= sd_bus_get_current_userdata(call
->bus
);
472 sender
= sd_bus_message_get_sender(call
);
476 c
= sd_bus_message_get_allow_interactive_authorization(call
);
482 r
= hashmap_ensure_allocated(registry
, NULL
);
486 r
= sd_bus_message_new_method_call(
489 "org.freedesktop.PolicyKit1",
490 "/org/freedesktop/PolicyKit1/Authority",
491 "org.freedesktop.PolicyKit1.Authority",
492 "CheckAuthorization");
496 r
= sd_bus_message_append(
499 "system-bus-name", 1, "name", "s", sender
,
504 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
508 STRV_FOREACH_PAIR(k
, v
, details
) {
509 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
514 r
= sd_bus_message_close_container(pk
);
518 r
= sd_bus_message_append(pk
, "us", !!interactive
, NULL
);
522 q
= new0(AsyncPolkitQuery
, 1);
526 q
->request
= sd_bus_message_ref(call
);
527 q
->callback
= callback
;
528 q
->userdata
= userdata
;
530 r
= hashmap_put(*registry
, call
, q
);
532 async_polkit_query_free(q
);
536 q
->registry
= *registry
;
538 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
540 async_polkit_query_free(q
);
550 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
554 while ((q
= hashmap_steal_first(registry
)))
555 async_polkit_query_free(q
);
557 hashmap_free(registry
);
561 int bus_check_peercred(sd_bus
*c
) {
568 fd
= sd_bus_get_fd(c
);
572 l
= sizeof(struct ucred
);
573 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
576 if (l
!= sizeof(struct ucred
))
579 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
585 int bus_connect_system_systemd(sd_bus
**_bus
) {
586 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
592 return sd_bus_default_system(_bus
);
594 /* If we are root and kdbus is not available, then let's talk
595 * directly to the system instance, instead of going via the
598 r
= sd_bus_new(&bus
);
602 r
= sd_bus_set_address(bus
, KERNEL_SYSTEM_BUS_ADDRESS
);
606 bus
->bus_client
= true;
608 r
= sd_bus_start(bus
);
615 bus
= sd_bus_unref(bus
);
617 r
= sd_bus_new(&bus
);
621 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
625 r
= sd_bus_start(bus
);
627 return sd_bus_default_system(_bus
);
629 r
= bus_check_peercred(bus
);
639 int bus_connect_user_systemd(sd_bus
**_bus
) {
640 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
641 _cleanup_free_
char *ee
= NULL
;
645 /* Try via kdbus first, and then directly */
649 r
= sd_bus_new(&bus
);
653 if (asprintf(&bus
->address
, KERNEL_USER_BUS_ADDRESS_FMT
, getuid()) < 0)
656 bus
->bus_client
= true;
658 r
= sd_bus_start(bus
);
665 bus
= sd_bus_unref(bus
);
667 e
= secure_getenv("XDG_RUNTIME_DIR");
669 return sd_bus_default_user(_bus
);
671 ee
= bus_address_escape(e
);
675 r
= sd_bus_new(&bus
);
679 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
683 r
= sd_bus_start(bus
);
685 return sd_bus_default_user(_bus
);
687 r
= bus_check_peercred(bus
);
697 #define print_property(name, fmt, ...) \
700 printf(fmt "\n", __VA_ARGS__); \
702 printf("%s=" fmt "\n", name, __VA_ARGS__); \
705 int bus_print_property(const char *name
, sd_bus_message
*property
, bool value
, bool all
) {
707 const char *contents
;
713 r
= sd_bus_message_peek_type(property
, &type
, &contents
);
719 case SD_BUS_TYPE_STRING
: {
722 r
= sd_bus_message_read_basic(property
, type
, &s
);
726 if (all
|| !isempty(s
)) {
727 _cleanup_free_
char *escaped
= NULL
;
729 escaped
= xescape(s
, "\n");
733 print_property(name
, "%s", escaped
);
739 case SD_BUS_TYPE_BOOLEAN
: {
742 r
= sd_bus_message_read_basic(property
, type
, &b
);
746 print_property(name
, "%s", yes_no(b
));
751 case SD_BUS_TYPE_UINT64
: {
754 r
= sd_bus_message_read_basic(property
, type
, &u
);
758 /* Yes, heuristics! But we can change this check
759 * should it turn out to not be sufficient */
761 if (endswith(name
, "Timestamp")) {
762 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
764 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
766 print_property(name
, "%s", strempty(t
));
768 } else if (strstr(name
, "USec")) {
769 char timespan
[FORMAT_TIMESPAN_MAX
];
771 print_property(name
, "%s", format_timespan(timespan
, sizeof(timespan
), u
, 0));
773 print_property(name
, "%"PRIu64
, u
);
778 case SD_BUS_TYPE_INT64
: {
781 r
= sd_bus_message_read_basic(property
, type
, &i
);
785 print_property(name
, "%"PRIi64
, i
);
790 case SD_BUS_TYPE_UINT32
: {
793 r
= sd_bus_message_read_basic(property
, type
, &u
);
797 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
798 print_property(name
, "%04o", u
);
800 print_property(name
, "%"PRIu32
, u
);
805 case SD_BUS_TYPE_INT32
: {
808 r
= sd_bus_message_read_basic(property
, type
, &i
);
812 print_property(name
, "%"PRIi32
, i
);
816 case SD_BUS_TYPE_DOUBLE
: {
819 r
= sd_bus_message_read_basic(property
, type
, &d
);
823 print_property(name
, "%g", d
);
827 case SD_BUS_TYPE_ARRAY
:
828 if (streq(contents
, "s")) {
832 r
= sd_bus_message_enter_container(property
, SD_BUS_TYPE_ARRAY
, contents
);
836 while ((r
= sd_bus_message_read_basic(property
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
837 _cleanup_free_
char *escaped
= NULL
;
842 escaped
= xescape(str
, "\n ");
846 printf("%s%s", first
? "" : " ", escaped
);
853 if (first
&& all
&& !value
)
858 r
= sd_bus_message_exit_container(property
);
864 } else if (streq(contents
, "y")) {
868 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
878 for (i
= 0; i
< n
; i
++)
879 printf("%02x", u
[i
]);
886 } else if (streq(contents
, "u")) {
890 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
900 for (i
= 0; i
< n
; i
++)
901 printf("%08x", u
[i
]);
915 int bus_print_all_properties(sd_bus
*bus
, const char *dest
, const char *path
, char **filter
, bool value
, bool all
) {
916 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
917 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
923 r
= sd_bus_call_method(bus
,
926 "org.freedesktop.DBus.Properties",
934 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
938 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
940 const char *contents
;
942 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &name
);
946 if (!filter
|| strv_find(filter
, name
)) {
947 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
951 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
955 r
= bus_print_property(name
, reply
, value
, all
);
960 printf("%s=[unprintable]\n", name
);
961 /* skip what we didn't read */
962 r
= sd_bus_message_skip(reply
, contents
);
967 r
= sd_bus_message_exit_container(reply
);
971 r
= sd_bus_message_skip(reply
, "v");
976 r
= sd_bus_message_exit_container(reply
);
983 r
= sd_bus_message_exit_container(reply
);
990 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
991 sd_id128_t
*p
= userdata
;
996 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1003 memcpy((*p
).bytes
, v
, n
);
1010 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1014 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1020 case SD_BUS_TYPE_STRING
: {
1021 char **p
= userdata
;
1024 r
= sd_bus_message_read_basic(m
, type
, &s
);
1031 return free_and_strdup(p
, s
);
1034 case SD_BUS_TYPE_ARRAY
: {
1035 _cleanup_strv_free_
char **l
= NULL
;
1036 char ***p
= userdata
;
1038 r
= bus_message_read_strv_extend(m
, &l
);
1048 case SD_BUS_TYPE_BOOLEAN
: {
1052 r
= sd_bus_message_read_basic(m
, type
, &b
);
1060 case SD_BUS_TYPE_INT32
:
1061 case SD_BUS_TYPE_UINT32
: {
1062 uint32_t u
, *p
= userdata
;
1064 r
= sd_bus_message_read_basic(m
, type
, &u
);
1072 case SD_BUS_TYPE_INT64
:
1073 case SD_BUS_TYPE_UINT64
: {
1074 uint64_t t
, *p
= userdata
;
1076 r
= sd_bus_message_read_basic(m
, type
, &t
);
1084 case SD_BUS_TYPE_DOUBLE
: {
1085 double d
, *p
= userdata
;
1087 r
= sd_bus_message_read_basic(m
, type
, &d
);
1098 int bus_message_map_all_properties(
1100 const struct bus_properties_map
*map
,
1103 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1109 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1113 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1114 const struct bus_properties_map
*prop
;
1116 const char *contents
;
1120 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1124 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1125 if (streq(map
[i
].member
, member
)) {
1131 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1135 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1139 v
= (uint8_t *)userdata
+ prop
->offset
;
1141 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1143 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1147 r
= sd_bus_message_exit_container(m
);
1151 r
= sd_bus_message_skip(m
, "v");
1156 r
= sd_bus_message_exit_container(m
);
1163 return sd_bus_message_exit_container(m
);
1166 int bus_message_map_properties_changed(
1168 const struct bus_properties_map
*map
,
1172 int r
, invalidated
, i
;
1177 r
= bus_message_map_all_properties(m
, map
, userdata
);
1181 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "s");
1186 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
)) > 0)
1187 for (i
= 0; map
[i
].member
; i
++)
1188 if (streq(map
[i
].member
, member
)) {
1195 r
= sd_bus_message_exit_container(m
);
1202 int bus_map_all_properties(
1204 const char *destination
,
1206 const struct bus_properties_map
*map
,
1209 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1210 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1214 assert(destination
);
1218 r
= sd_bus_call_method(
1222 "org.freedesktop.DBus.Properties",
1230 return bus_message_map_all_properties(m
, map
, userdata
);
1233 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
1234 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
1237 assert(transport
>= 0);
1238 assert(transport
< _BUS_TRANSPORT_MAX
);
1241 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1242 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1244 switch (transport
) {
1246 case BUS_TRANSPORT_LOCAL
:
1248 r
= sd_bus_default_user(&bus
);
1250 r
= sd_bus_default_system(&bus
);
1254 case BUS_TRANSPORT_REMOTE
:
1255 r
= sd_bus_open_system_remote(&bus
, host
);
1258 case BUS_TRANSPORT_MACHINE
:
1259 r
= sd_bus_open_system_machine(&bus
, host
);
1263 assert_not_reached("Hmm, unknown transport type.");
1268 r
= sd_bus_set_exit_on_disconnect(bus
, true);
1278 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1281 assert(transport
>= 0);
1282 assert(transport
< _BUS_TRANSPORT_MAX
);
1285 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1286 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1288 switch (transport
) {
1290 case BUS_TRANSPORT_LOCAL
:
1292 r
= bus_connect_user_systemd(bus
);
1294 r
= bus_connect_system_systemd(bus
);
1298 case BUS_TRANSPORT_REMOTE
:
1299 r
= sd_bus_open_system_remote(bus
, host
);
1302 case BUS_TRANSPORT_MACHINE
:
1303 r
= sd_bus_open_system_machine(bus
, host
);
1307 assert_not_reached("Hmm, unknown transport type.");
1313 int bus_property_get_bool(
1316 const char *interface
,
1317 const char *property
,
1318 sd_bus_message
*reply
,
1320 sd_bus_error
*error
) {
1322 int b
= *(bool*) userdata
;
1324 return sd_bus_message_append_basic(reply
, 'b', &b
);
1327 int bus_property_get_id128(
1330 const char *interface
,
1331 const char *property
,
1332 sd_bus_message
*reply
,
1334 sd_bus_error
*error
) {
1336 sd_id128_t
*id
= userdata
;
1338 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1339 return sd_bus_message_append(reply
, "ay", 0);
1341 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1344 #if __SIZEOF_SIZE_T__ != 8
1345 int bus_property_get_size(
1348 const char *interface
,
1349 const char *property
,
1350 sd_bus_message
*reply
,
1352 sd_bus_error
*error
) {
1354 uint64_t sz
= *(size_t*) userdata
;
1356 return sd_bus_message_append_basic(reply
, 't', &sz
);
1360 #if __SIZEOF_LONG__ != 8
1361 int bus_property_get_long(
1364 const char *interface
,
1365 const char *property
,
1366 sd_bus_message
*reply
,
1368 sd_bus_error
*error
) {
1370 int64_t l
= *(long*) userdata
;
1372 return sd_bus_message_append_basic(reply
, 'x', &l
);
1375 int bus_property_get_ulong(
1378 const char *interface
,
1379 const char *property
,
1380 sd_bus_message
*reply
,
1382 sd_bus_error
*error
) {
1384 uint64_t ul
= *(unsigned long*) userdata
;
1386 return sd_bus_message_append_basic(reply
, 't', &ul
);
1390 int bus_log_parse_error(int r
) {
1391 return log_error_errno(r
, "Failed to parse bus message: %m");
1394 int bus_log_create_error(int r
) {
1395 return log_error_errno(r
, "Failed to create bus message: %m");
1399 * bus_path_encode_unique() - encode unique object path
1400 * @b: bus connection or NULL
1401 * @prefix: object path prefix
1402 * @sender_id: unique-name of client, or NULL
1403 * @external_id: external ID to be chosen by client, or NULL
1404 * @ret_path: storage for encoded object path pointer
1406 * Whenever we provide a bus API that allows clients to create and manage
1407 * server-side objects, we need to provide a unique name for these objects. If
1408 * we let the server choose the name, we suffer from a race condition: If a
1409 * client creates an object asynchronously, it cannot destroy that object until
1410 * it received the method reply. It cannot know the name of the new object,
1411 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1413 * Therefore, many APIs allow the client to choose the unique name for newly
1414 * created objects. There're two problems to solve, though:
1415 * 1) Object names are usually defined via dbus object paths, which are
1416 * usually globally namespaced. Therefore, multiple clients must be able
1417 * to choose unique object names without interference.
1418 * 2) If multiple libraries share the same bus connection, they must be
1419 * able to choose unique object names without interference.
1420 * The first problem is solved easily by prefixing a name with the
1421 * unique-bus-name of a connection. The server side must enforce this and
1422 * reject any other name. The second problem is solved by providing unique
1423 * suffixes from within sd-bus.
1425 * This helper allows clients to create unique object-paths. It uses the
1426 * template '/prefix/sender_id/external_id' and returns the new path in
1427 * @ret_path (must be freed by the caller).
1428 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1429 * NULL, this function allocates a unique suffix via @b (by requesting a new
1430 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1433 * Returns: 0 on success, negative error code on failure.
1435 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1436 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1437 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1440 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1441 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1442 assert_return(ret_path
, -EINVAL
);
1445 r
= sd_bus_get_unique_name(b
, &sender_id
);
1451 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1452 external_id
= external_buf
;
1455 sender_label
= bus_label_escape(sender_id
);
1459 external_label
= bus_label_escape(external_id
);
1460 if (!external_label
)
1463 p
= strjoin(prefix
, "/", sender_label
, "/", external_label
);
1472 * bus_path_decode_unique() - decode unique object path
1473 * @path: object path to decode
1474 * @prefix: object path prefix
1475 * @ret_sender: output parameter for sender-id label
1476 * @ret_external: output parameter for external-id label
1478 * This does the reverse of bus_path_encode_unique() (see its description for
1479 * details). Both trailing labels, sender-id and external-id, are unescaped and
1480 * returned in the given output parameters (the caller must free them).
1482 * Note that this function returns 0 if the path does not match the template
1483 * (see bus_path_encode_unique()), 1 if it matched.
1485 * Returns: Negative error code on failure, 0 if the given object path does not
1486 * match the template (return parameters are set to NULL), 1 if it was
1487 * parsed successfully (return parameters contain allocated labels).
1489 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1491 char *sender
, *external
;
1493 assert(object_path_is_valid(path
));
1494 assert(object_path_is_valid(prefix
));
1496 assert(ret_external
);
1498 p
= object_path_startswith(path
, prefix
);
1501 *ret_external
= NULL
;
1508 *ret_external
= NULL
;
1512 sender
= bus_label_unescape_n(p
, q
- p
);
1513 external
= bus_label_unescape(q
+ 1);
1514 if (!sender
|| !external
) {
1520 *ret_sender
= sender
;
1521 *ret_external
= external
;
1525 int bus_property_get_rlimit(
1528 const char *interface
,
1529 const char *property
,
1530 sd_bus_message
*reply
,
1532 sd_bus_error
*error
) {
1537 const char *is_soft
;
1543 is_soft
= endswith(property
, "Soft");
1544 rl
= *(struct rlimit
**) userdata
;
1546 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1548 struct rlimit buf
= {};
1552 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1554 z
= rlimit_from_string(strstr(s
, "Limit"));
1558 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1561 /* rlim_t might have different sizes, let's map
1562 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1564 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1566 return sd_bus_message_append(reply
, "t", u
);