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"
43 #include "cgroup-util.h"
48 #include "mount-util.h"
50 #include "parse-util.h"
51 #include "proc-cmdline.h"
52 #include "rlimit-util.h"
53 #include "stdio-util.h"
55 #include "user-util.h"
57 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
58 sd_event
*e
= userdata
;
63 sd_bus_close(sd_bus_message_get_bus(m
));
69 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
70 _cleanup_free_
char *match
= NULL
;
78 /* We unregister the name here and then wait for the
79 * NameOwnerChanged signal for this event to arrive before we
80 * quit. We do this in order to make sure that any queued
81 * requests are still processed before we really exit. */
83 r
= sd_bus_get_unique_name(bus
, &unique
);
88 "sender='org.freedesktop.DBus',"
90 "interface='org.freedesktop.DBus',"
91 "member='NameOwnerChanged',"
92 "path='/org/freedesktop/DBus',"
95 "arg2=''", name
, unique
);
99 r
= sd_bus_add_match(bus
, NULL
, match
, name_owner_change_callback
, e
);
103 r
= sd_bus_release_name(bus
, name
);
110 int bus_event_loop_with_idle(
115 check_idle_t check_idle
,
117 bool exiting
= false;
127 r
= sd_event_get_state(e
);
130 if (r
== SD_EVENT_FINISHED
)
134 idle
= check_idle(userdata
);
138 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
142 if (r
== 0 && !exiting
&& idle
) {
144 r
= sd_bus_try_close(bus
);
148 /* Fallback for dbus1 connections: we
149 * unregister the name and wait for the
150 * response to come through for it */
151 if (r
== -EOPNOTSUPP
) {
153 /* Inform the service manager that we
154 * are going down, so that it will
155 * queue all further start requests,
156 * instead of assuming we are already
158 sd_notify(false, "STOPPING=1");
160 r
= bus_async_unregister_and_exit(e
, bus
, name
);
176 r
= sd_event_get_exit_code(e
, &code
);
183 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
184 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
185 int r
, has_owner
= 0;
190 r
= sd_bus_call_method(c
,
191 "org.freedesktop.DBus",
192 "/org/freedesktop/dbus",
193 "org.freedesktop.DBus",
202 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
204 return sd_bus_error_set_errno(error
, r
);
209 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
210 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
216 if (good_user
== UID_INVALID
)
219 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
223 /* Don't trust augmented credentials for authorization */
224 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
226 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
230 return sender_uid
== good_user
;
234 sd_bus_message
*call
,
237 const char **details
,
247 /* Tests non-interactively! */
249 r
= check_good_user(call
, good_user
);
253 r
= sd_bus_query_sender_privilege(call
, capability
);
260 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
261 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
262 int authorized
= false, challenge
= false;
263 const char *sender
, **k
, **v
;
265 sender
= sd_bus_message_get_sender(call
);
269 r
= sd_bus_message_new_method_call(
272 "org.freedesktop.PolicyKit1",
273 "/org/freedesktop/PolicyKit1/Authority",
274 "org.freedesktop.PolicyKit1.Authority",
275 "CheckAuthorization");
279 r
= sd_bus_message_append(
282 "system-bus-name", 1, "name", "s", sender
,
287 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
291 STRV_FOREACH_PAIR(k
, v
, details
) {
292 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
297 r
= sd_bus_message_close_container(request
);
301 r
= sd_bus_message_append(request
, "us", 0, NULL
);
305 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
307 /* Treat no PK available as access denied */
308 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
309 sd_bus_error_free(e
);
316 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
320 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
328 *_challenge
= challenge
;
339 typedef struct AsyncPolkitQuery
{
340 sd_bus_message
*request
, *reply
;
341 sd_bus_message_handler_t callback
;
347 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
352 sd_bus_slot_unref(q
->slot
);
354 if (q
->registry
&& q
->request
)
355 hashmap_remove(q
->registry
, q
->request
);
357 sd_bus_message_unref(q
->request
);
358 sd_bus_message_unref(q
->reply
);
363 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
364 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
365 AsyncPolkitQuery
*q
= userdata
;
371 q
->slot
= sd_bus_slot_unref(q
->slot
);
372 q
->reply
= sd_bus_message_ref(reply
);
374 r
= sd_bus_message_rewind(q
->request
, true);
376 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
380 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
381 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
384 async_polkit_query_free(q
);
391 int bus_verify_polkit_async(
392 sd_bus_message
*call
,
395 const char **details
,
399 sd_bus_error
*error
) {
402 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
404 const char *sender
, **k
, **v
;
405 sd_bus_message_handler_t callback
;
415 r
= check_good_user(call
, good_user
);
420 q
= hashmap_get(*registry
, call
);
422 int authorized
, challenge
;
424 /* This is the second invocation of this function, and
425 * there's already a response from polkit, let's
429 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
430 const sd_bus_error
*e
;
432 /* Copy error from polkit reply */
433 e
= sd_bus_message_get_error(q
->reply
);
434 sd_bus_error_copy(error
, e
);
436 /* Treat no PK available as access denied */
437 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
))
440 return -sd_bus_error_get_errno(e
);
443 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
445 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
454 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
460 r
= sd_bus_query_sender_privilege(call
, capability
);
467 if (sd_bus_get_current_message(call
->bus
) != call
)
470 callback
= sd_bus_get_current_handler(call
->bus
);
474 userdata
= sd_bus_get_current_userdata(call
->bus
);
476 sender
= sd_bus_message_get_sender(call
);
480 c
= sd_bus_message_get_allow_interactive_authorization(call
);
486 r
= hashmap_ensure_allocated(registry
, NULL
);
490 r
= sd_bus_message_new_method_call(
493 "org.freedesktop.PolicyKit1",
494 "/org/freedesktop/PolicyKit1/Authority",
495 "org.freedesktop.PolicyKit1.Authority",
496 "CheckAuthorization");
500 r
= sd_bus_message_append(
503 "system-bus-name", 1, "name", "s", sender
,
508 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
512 STRV_FOREACH_PAIR(k
, v
, details
) {
513 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
518 r
= sd_bus_message_close_container(pk
);
522 r
= sd_bus_message_append(pk
, "us", !!interactive
, NULL
);
526 q
= new0(AsyncPolkitQuery
, 1);
530 q
->request
= sd_bus_message_ref(call
);
531 q
->callback
= callback
;
532 q
->userdata
= userdata
;
534 r
= hashmap_put(*registry
, call
, q
);
536 async_polkit_query_free(q
);
540 q
->registry
= *registry
;
542 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
544 async_polkit_query_free(q
);
554 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
558 while ((q
= hashmap_steal_first(registry
)))
559 async_polkit_query_free(q
);
561 hashmap_free(registry
);
565 int bus_check_peercred(sd_bus
*c
) {
572 fd
= sd_bus_get_fd(c
);
576 l
= sizeof(struct ucred
);
577 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
580 if (l
!= sizeof(struct ucred
))
583 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
589 int bus_connect_system_systemd(sd_bus
**_bus
) {
590 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
596 return sd_bus_default_system(_bus
);
598 /* If we are root then let's talk directly to the system
599 * instance, instead of going via the bus */
601 r
= sd_bus_new(&bus
);
605 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
609 r
= sd_bus_start(bus
);
611 return sd_bus_default_system(_bus
);
613 r
= bus_check_peercred(bus
);
623 int bus_connect_user_systemd(sd_bus
**_bus
) {
624 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
625 _cleanup_free_
char *ee
= NULL
;
631 e
= secure_getenv("XDG_RUNTIME_DIR");
633 return sd_bus_default_user(_bus
);
635 ee
= bus_address_escape(e
);
639 r
= sd_bus_new(&bus
);
643 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private");
647 r
= sd_bus_start(bus
);
649 return sd_bus_default_user(_bus
);
651 r
= bus_check_peercred(bus
);
661 #define print_property(name, fmt, ...) \
664 printf(fmt "\n", __VA_ARGS__); \
666 printf("%s=" fmt "\n", name, __VA_ARGS__); \
669 int bus_print_property(const char *name
, sd_bus_message
*property
, bool value
, bool all
) {
671 const char *contents
;
677 r
= sd_bus_message_peek_type(property
, &type
, &contents
);
683 case SD_BUS_TYPE_STRING
: {
686 r
= sd_bus_message_read_basic(property
, type
, &s
);
690 if (all
|| !isempty(s
)) {
693 /* This property has a single value, so we need to take
694 * care not to print a new line, everything else is OK. */
695 good
= !strchr(s
, '\n');
696 print_property(name
, "%s", good
? s
: "[unprintable]");
702 case SD_BUS_TYPE_BOOLEAN
: {
705 r
= sd_bus_message_read_basic(property
, type
, &b
);
709 print_property(name
, "%s", yes_no(b
));
714 case SD_BUS_TYPE_UINT64
: {
717 r
= sd_bus_message_read_basic(property
, type
, &u
);
721 /* Yes, heuristics! But we can change this check
722 * should it turn out to not be sufficient */
724 if (endswith(name
, "Timestamp")) {
725 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
727 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
729 print_property(name
, "%s", strempty(t
));
731 } else if (strstr(name
, "USec")) {
732 char timespan
[FORMAT_TIMESPAN_MAX
];
734 print_property(name
, "%s", format_timespan(timespan
, sizeof(timespan
), u
, 0));
735 } else if (streq(name
, "RestrictNamespaces")) {
736 _cleanup_free_
char *s
= NULL
;
739 if ((u
& NAMESPACE_FLAGS_ALL
) == 0)
741 else if ((u
& NAMESPACE_FLAGS_ALL
) == NAMESPACE_FLAGS_ALL
)
744 r
= namespace_flag_to_string_many(u
, &s
);
751 print_property(name
, "%s", result
);
753 } else if (streq(name
, "MountFlags")) {
756 result
= mount_propagation_flags_to_string(u
);
760 print_property(name
, "%s", result
);
762 } else if (STR_IN_SET(name
, "CapabilityBoundingSet", "AmbientCapabilities")) {
763 _cleanup_free_
char *s
= NULL
;
765 r
= capability_set_to_string_alloc(u
, &s
);
769 print_property(name
, "%s", s
);
771 } else if ((STR_IN_SET(name
, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u
== CGROUP_WEIGHT_INVALID
) ||
772 (STR_IN_SET(name
, "CPUShares", "StartupCPUShares") && u
== CGROUP_CPU_SHARES_INVALID
) ||
773 (STR_IN_SET(name
, "BlockIOWeight", "StartupBlockIOWeight") && u
== CGROUP_BLKIO_WEIGHT_INVALID
) ||
774 (STR_IN_SET(name
, "MemoryCurrent", "TasksCurrent") && u
== (uint64_t) -1) ||
775 (endswith(name
, "NSec") && u
== (uint64_t) -1))
777 print_property(name
, "%s", "[not set]");
779 else if ((STR_IN_SET(name
, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u
== CGROUP_LIMIT_MAX
) ||
780 (STR_IN_SET(name
, "TasksMax", "DefaultTasksMax") && u
== (uint64_t) -1) ||
781 (startswith(name
, "Limit") && u
== (uint64_t) -1) ||
782 (startswith(name
, "DefaultLimit") && u
== (uint64_t) -1))
784 print_property(name
, "%s", "infinity");
786 print_property(name
, "%"PRIu64
, u
);
791 case SD_BUS_TYPE_INT64
: {
794 r
= sd_bus_message_read_basic(property
, type
, &i
);
798 print_property(name
, "%"PRIi64
, i
);
803 case SD_BUS_TYPE_UINT32
: {
806 r
= sd_bus_message_read_basic(property
, type
, &u
);
810 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
811 print_property(name
, "%04o", u
);
812 else if (streq(name
, "UID")) {
813 if (u
== UID_INVALID
)
814 print_property(name
, "%s", "[not set]");
816 print_property(name
, "%"PRIu32
, u
);
817 } else if (streq(name
, "GID")) {
818 if (u
== GID_INVALID
)
819 print_property(name
, "%s", "[not set]");
821 print_property(name
, "%"PRIu32
, u
);
823 print_property(name
, "%"PRIu32
, u
);
828 case SD_BUS_TYPE_INT32
: {
831 r
= sd_bus_message_read_basic(property
, type
, &i
);
835 print_property(name
, "%"PRIi32
, i
);
839 case SD_BUS_TYPE_DOUBLE
: {
842 r
= sd_bus_message_read_basic(property
, type
, &d
);
846 print_property(name
, "%g", d
);
850 case SD_BUS_TYPE_ARRAY
:
851 if (streq(contents
, "s")) {
855 r
= sd_bus_message_enter_container(property
, SD_BUS_TYPE_ARRAY
, contents
);
859 while ((r
= sd_bus_message_read_basic(property
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
865 /* This property has multiple space-separated values, so
866 * neither spaces not newlines can be allowed in a value. */
867 good
= str
[strcspn(str
, " \n")] == '\0';
869 printf("%s%s", first
? "" : " ", good
? str
: "[unprintable]");
876 if (first
&& all
&& !value
)
881 r
= sd_bus_message_exit_container(property
);
887 } else if (streq(contents
, "y")) {
891 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
901 for (i
= 0; i
< n
; i
++)
902 printf("%02x", u
[i
]);
909 } else if (streq(contents
, "u")) {
913 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
923 for (i
= 0; i
< n
; i
++)
924 printf("%08x", u
[i
]);
938 int bus_print_all_properties(sd_bus
*bus
, const char *dest
, const char *path
, char **filter
, bool value
, bool all
) {
939 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
940 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
946 r
= sd_bus_call_method(bus
,
949 "org.freedesktop.DBus.Properties",
957 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
961 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
963 const char *contents
;
965 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &name
);
969 if (!filter
|| strv_find(filter
, name
)) {
970 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
974 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
978 r
= bus_print_property(name
, reply
, value
, all
);
983 printf("%s=[unprintable]\n", name
);
984 /* skip what we didn't read */
985 r
= sd_bus_message_skip(reply
, contents
);
990 r
= sd_bus_message_exit_container(reply
);
994 r
= sd_bus_message_skip(reply
, "v");
999 r
= sd_bus_message_exit_container(reply
);
1006 r
= sd_bus_message_exit_container(reply
);
1013 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1014 sd_id128_t
*p
= userdata
;
1019 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1026 memcpy((*p
).bytes
, v
, n
);
1033 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1037 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1043 case SD_BUS_TYPE_STRING
: {
1044 char **p
= userdata
;
1047 r
= sd_bus_message_read_basic(m
, type
, &s
);
1054 return free_and_strdup(p
, s
);
1057 case SD_BUS_TYPE_ARRAY
: {
1058 _cleanup_strv_free_
char **l
= NULL
;
1059 char ***p
= userdata
;
1061 r
= bus_message_read_strv_extend(m
, &l
);
1071 case SD_BUS_TYPE_BOOLEAN
: {
1075 r
= sd_bus_message_read_basic(m
, type
, &b
);
1083 case SD_BUS_TYPE_INT32
:
1084 case SD_BUS_TYPE_UINT32
: {
1085 uint32_t u
, *p
= userdata
;
1087 r
= sd_bus_message_read_basic(m
, type
, &u
);
1095 case SD_BUS_TYPE_INT64
:
1096 case SD_BUS_TYPE_UINT64
: {
1097 uint64_t t
, *p
= userdata
;
1099 r
= sd_bus_message_read_basic(m
, type
, &t
);
1107 case SD_BUS_TYPE_DOUBLE
: {
1108 double d
, *p
= userdata
;
1110 r
= sd_bus_message_read_basic(m
, type
, &d
);
1121 int bus_message_map_all_properties(
1123 const struct bus_properties_map
*map
,
1124 sd_bus_error
*error
,
1132 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1136 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1137 const struct bus_properties_map
*prop
;
1139 const char *contents
;
1143 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1147 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1148 if (streq(map
[i
].member
, member
)) {
1154 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1158 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1162 v
= (uint8_t *)userdata
+ prop
->offset
;
1164 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
1166 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, error
, v
);
1170 r
= sd_bus_message_exit_container(m
);
1174 r
= sd_bus_message_skip(m
, "v");
1179 r
= sd_bus_message_exit_container(m
);
1186 return sd_bus_message_exit_container(m
);
1189 int bus_message_map_properties_changed(
1191 const struct bus_properties_map
*map
,
1192 sd_bus_error
*error
,
1196 int r
, invalidated
, i
;
1201 r
= bus_message_map_all_properties(m
, map
, error
, userdata
);
1205 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "s");
1210 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
)) > 0)
1211 for (i
= 0; map
[i
].member
; i
++)
1212 if (streq(map
[i
].member
, member
)) {
1219 r
= sd_bus_message_exit_container(m
);
1226 int bus_map_all_properties(
1228 const char *destination
,
1230 const struct bus_properties_map
*map
,
1231 sd_bus_error
*error
,
1234 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1238 assert(destination
);
1242 r
= sd_bus_call_method(
1246 "org.freedesktop.DBus.Properties",
1254 return bus_message_map_all_properties(m
, map
, error
, userdata
);
1257 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**ret
) {
1258 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
1261 assert(transport
>= 0);
1262 assert(transport
< _BUS_TRANSPORT_MAX
);
1265 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1266 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1268 switch (transport
) {
1270 case BUS_TRANSPORT_LOCAL
:
1272 r
= sd_bus_default_user(&bus
);
1274 r
= sd_bus_default_system(&bus
);
1278 case BUS_TRANSPORT_REMOTE
:
1279 r
= sd_bus_open_system_remote(&bus
, host
);
1282 case BUS_TRANSPORT_MACHINE
:
1283 r
= sd_bus_open_system_machine(&bus
, host
);
1287 assert_not_reached("Hmm, unknown transport type.");
1292 r
= sd_bus_set_exit_on_disconnect(bus
, true);
1302 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1305 assert(transport
>= 0);
1306 assert(transport
< _BUS_TRANSPORT_MAX
);
1309 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1310 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1312 switch (transport
) {
1314 case BUS_TRANSPORT_LOCAL
:
1316 r
= bus_connect_user_systemd(bus
);
1318 r
= bus_connect_system_systemd(bus
);
1322 case BUS_TRANSPORT_REMOTE
:
1323 r
= sd_bus_open_system_remote(bus
, host
);
1326 case BUS_TRANSPORT_MACHINE
:
1327 r
= sd_bus_open_system_machine(bus
, host
);
1331 assert_not_reached("Hmm, unknown transport type.");
1337 int bus_property_get_bool(
1340 const char *interface
,
1341 const char *property
,
1342 sd_bus_message
*reply
,
1344 sd_bus_error
*error
) {
1346 int b
= *(bool*) userdata
;
1348 return sd_bus_message_append_basic(reply
, 'b', &b
);
1351 int bus_property_get_id128(
1354 const char *interface
,
1355 const char *property
,
1356 sd_bus_message
*reply
,
1358 sd_bus_error
*error
) {
1360 sd_id128_t
*id
= userdata
;
1362 if (sd_id128_is_null(*id
)) /* Add an empty array if the ID is zero */
1363 return sd_bus_message_append(reply
, "ay", 0);
1365 return sd_bus_message_append_array(reply
, 'y', id
->bytes
, 16);
1368 #if __SIZEOF_SIZE_T__ != 8
1369 int bus_property_get_size(
1372 const char *interface
,
1373 const char *property
,
1374 sd_bus_message
*reply
,
1376 sd_bus_error
*error
) {
1378 uint64_t sz
= *(size_t*) userdata
;
1380 return sd_bus_message_append_basic(reply
, 't', &sz
);
1384 #if __SIZEOF_LONG__ != 8
1385 int bus_property_get_long(
1388 const char *interface
,
1389 const char *property
,
1390 sd_bus_message
*reply
,
1392 sd_bus_error
*error
) {
1394 int64_t l
= *(long*) userdata
;
1396 return sd_bus_message_append_basic(reply
, 'x', &l
);
1399 int bus_property_get_ulong(
1402 const char *interface
,
1403 const char *property
,
1404 sd_bus_message
*reply
,
1406 sd_bus_error
*error
) {
1408 uint64_t ul
= *(unsigned long*) userdata
;
1410 return sd_bus_message_append_basic(reply
, 't', &ul
);
1414 int bus_log_parse_error(int r
) {
1415 return log_error_errno(r
, "Failed to parse bus message: %m");
1418 int bus_log_create_error(int r
) {
1419 return log_error_errno(r
, "Failed to create bus message: %m");
1423 * bus_path_encode_unique() - encode unique object path
1424 * @b: bus connection or NULL
1425 * @prefix: object path prefix
1426 * @sender_id: unique-name of client, or NULL
1427 * @external_id: external ID to be chosen by client, or NULL
1428 * @ret_path: storage for encoded object path pointer
1430 * Whenever we provide a bus API that allows clients to create and manage
1431 * server-side objects, we need to provide a unique name for these objects. If
1432 * we let the server choose the name, we suffer from a race condition: If a
1433 * client creates an object asynchronously, it cannot destroy that object until
1434 * it received the method reply. It cannot know the name of the new object,
1435 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1437 * Therefore, many APIs allow the client to choose the unique name for newly
1438 * created objects. There're two problems to solve, though:
1439 * 1) Object names are usually defined via dbus object paths, which are
1440 * usually globally namespaced. Therefore, multiple clients must be able
1441 * to choose unique object names without interference.
1442 * 2) If multiple libraries share the same bus connection, they must be
1443 * able to choose unique object names without interference.
1444 * The first problem is solved easily by prefixing a name with the
1445 * unique-bus-name of a connection. The server side must enforce this and
1446 * reject any other name. The second problem is solved by providing unique
1447 * suffixes from within sd-bus.
1449 * This helper allows clients to create unique object-paths. It uses the
1450 * template '/prefix/sender_id/external_id' and returns the new path in
1451 * @ret_path (must be freed by the caller).
1452 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1453 * NULL, this function allocates a unique suffix via @b (by requesting a new
1454 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1457 * Returns: 0 on success, negative error code on failure.
1459 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
1460 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
1461 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
1464 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
1465 assert_return(object_path_is_valid(prefix
), -EINVAL
);
1466 assert_return(ret_path
, -EINVAL
);
1469 r
= sd_bus_get_unique_name(b
, &sender_id
);
1475 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
1476 external_id
= external_buf
;
1479 sender_label
= bus_label_escape(sender_id
);
1483 external_label
= bus_label_escape(external_id
);
1484 if (!external_label
)
1487 p
= strjoin(prefix
, "/", sender_label
, "/", external_label
);
1496 * bus_path_decode_unique() - decode unique object path
1497 * @path: object path to decode
1498 * @prefix: object path prefix
1499 * @ret_sender: output parameter for sender-id label
1500 * @ret_external: output parameter for external-id label
1502 * This does the reverse of bus_path_encode_unique() (see its description for
1503 * details). Both trailing labels, sender-id and external-id, are unescaped and
1504 * returned in the given output parameters (the caller must free them).
1506 * Note that this function returns 0 if the path does not match the template
1507 * (see bus_path_encode_unique()), 1 if it matched.
1509 * Returns: Negative error code on failure, 0 if the given object path does not
1510 * match the template (return parameters are set to NULL), 1 if it was
1511 * parsed successfully (return parameters contain allocated labels).
1513 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
1515 char *sender
, *external
;
1517 assert(object_path_is_valid(path
));
1518 assert(object_path_is_valid(prefix
));
1520 assert(ret_external
);
1522 p
= object_path_startswith(path
, prefix
);
1525 *ret_external
= NULL
;
1532 *ret_external
= NULL
;
1536 sender
= bus_label_unescape_n(p
, q
- p
);
1537 external
= bus_label_unescape(q
+ 1);
1538 if (!sender
|| !external
) {
1544 *ret_sender
= sender
;
1545 *ret_external
= external
;
1549 int bus_property_get_rlimit(
1552 const char *interface
,
1553 const char *property
,
1554 sd_bus_message
*reply
,
1556 sd_bus_error
*error
) {
1561 const char *is_soft
;
1567 is_soft
= endswith(property
, "Soft");
1568 rl
= *(struct rlimit
**) userdata
;
1570 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
1572 struct rlimit buf
= {};
1576 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
1578 z
= rlimit_from_string(strstr(s
, "Limit"));
1582 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
1585 /* rlim_t might have different sizes, let's map
1586 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1588 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
1590 return sd_bus_message_append(reply
, "t", u
);
1593 int bus_track_add_name_many(sd_bus_track
*t
, char **l
) {
1599 /* Continues adding after failure, and returns the first failure. */
1601 STRV_FOREACH(i
, l
) {
1604 k
= sd_bus_track_add_name(t
, *i
);
1605 if (k
< 0 && r
>= 0)