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"
42 #include "cgroup-util.h"
46 #include "extract-word.h"
54 #include "parse-util.h"
55 #include "path-util.h"
56 #include "proc-cmdline.h"
57 #include "process-util.h"
58 #include "rlimit-util.h"
60 #include "signal-util.h"
61 #include "stdio-util.h"
62 #include "string-util.h"
64 #include "syslog-util.h"
65 #include "time-util.h"
66 #include "unit-name.h"
67 #include "user-util.h"
71 static int name_owner_change_callback(sd_bus_message
*m
, void *userdata
, sd_bus_error
*ret_error
) {
72 sd_event
*e
= userdata
;
77 sd_bus_close(sd_bus_message_get_bus(m
));
83 int bus_async_unregister_and_exit(sd_event
*e
, sd_bus
*bus
, const char *name
) {
84 _cleanup_free_
char *match
= NULL
;
92 /* We unregister the name here and then wait for the
93 * NameOwnerChanged signal for this event to arrive before we
94 * quit. We do this in order to make sure that any queued
95 * requests are still processed before we really exit. */
97 r
= sd_bus_get_unique_name(bus
, &unique
);
102 "sender='org.freedesktop.DBus',"
104 "interface='org.freedesktop.DBus',"
105 "member='NameOwnerChanged',"
106 "path='/org/freedesktop/DBus',"
109 "arg2=''", name
, unique
);
113 r
= sd_bus_add_match(bus
, NULL
, match
, name_owner_change_callback
, e
);
117 r
= sd_bus_release_name(bus
, name
);
124 int bus_event_loop_with_idle(
129 check_idle_t check_idle
,
131 bool exiting
= false;
141 r
= sd_event_get_state(e
);
144 if (r
== SD_EVENT_FINISHED
)
148 idle
= check_idle(userdata
);
152 r
= sd_event_run(e
, exiting
|| !idle
? (uint64_t) -1 : timeout
);
156 if (r
== 0 && !exiting
&& idle
) {
158 r
= sd_bus_try_close(bus
);
162 /* Fallback for dbus1 connections: we
163 * unregister the name and wait for the
164 * response to come through for it */
165 if (r
== -EOPNOTSUPP
) {
167 /* Inform the service manager that we
168 * are going down, so that it will
169 * queue all further start requests,
170 * instead of assuming we are already
172 sd_notify(false, "STOPPING=1");
174 r
= bus_async_unregister_and_exit(e
, bus
, name
);
190 r
= sd_event_get_exit_code(e
, &code
);
197 int bus_name_has_owner(sd_bus
*c
, const char *name
, sd_bus_error
*error
) {
198 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*rep
= NULL
;
199 int r
, has_owner
= 0;
204 r
= sd_bus_call_method(c
,
205 "org.freedesktop.DBus",
206 "/org/freedesktop/dbus",
207 "org.freedesktop.DBus",
216 r
= sd_bus_message_read_basic(rep
, 'b', &has_owner
);
218 return sd_bus_error_set_errno(error
, r
);
223 static int check_good_user(sd_bus_message
*m
, uid_t good_user
) {
224 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
230 if (good_user
== UID_INVALID
)
233 r
= sd_bus_query_sender_creds(m
, SD_BUS_CREDS_EUID
, &creds
);
237 /* Don't trust augmented credentials for authorization */
238 assert_return((sd_bus_creds_get_augmented_mask(creds
) & SD_BUS_CREDS_EUID
) == 0, -EPERM
);
240 r
= sd_bus_creds_get_euid(creds
, &sender_uid
);
244 return sender_uid
== good_user
;
248 sd_bus_message
*call
,
251 const char **details
,
261 /* Tests non-interactively! */
263 r
= check_good_user(call
, good_user
);
267 r
= sd_bus_query_sender_privilege(call
, capability
);
274 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*request
= NULL
;
275 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
276 int authorized
= false, challenge
= false;
277 const char *sender
, **k
, **v
;
279 sender
= sd_bus_message_get_sender(call
);
283 r
= sd_bus_message_new_method_call(
286 "org.freedesktop.PolicyKit1",
287 "/org/freedesktop/PolicyKit1/Authority",
288 "org.freedesktop.PolicyKit1.Authority",
289 "CheckAuthorization");
293 r
= sd_bus_message_append(
296 "system-bus-name", 1, "name", "s", sender
,
301 r
= sd_bus_message_open_container(request
, 'a', "{ss}");
305 STRV_FOREACH_PAIR(k
, v
, details
) {
306 r
= sd_bus_message_append(request
, "{ss}", *k
, *v
);
311 r
= sd_bus_message_close_container(request
);
315 r
= sd_bus_message_append(request
, "us", 0, NULL
);
319 r
= sd_bus_call(call
->bus
, request
, 0, e
, &reply
);
321 /* Treat no PK available as access denied */
322 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
)) {
323 sd_bus_error_free(e
);
330 r
= sd_bus_message_enter_container(reply
, 'r', "bba{ss}");
334 r
= sd_bus_message_read(reply
, "bb", &authorized
, &challenge
);
342 *_challenge
= challenge
;
353 typedef struct AsyncPolkitQuery
{
354 sd_bus_message
*request
, *reply
;
355 sd_bus_message_handler_t callback
;
361 static void async_polkit_query_free(AsyncPolkitQuery
*q
) {
366 sd_bus_slot_unref(q
->slot
);
368 if (q
->registry
&& q
->request
)
369 hashmap_remove(q
->registry
, q
->request
);
371 sd_bus_message_unref(q
->request
);
372 sd_bus_message_unref(q
->reply
);
377 static int async_polkit_callback(sd_bus_message
*reply
, void *userdata
, sd_bus_error
*error
) {
378 _cleanup_(sd_bus_error_free
) sd_bus_error error_buffer
= SD_BUS_ERROR_NULL
;
379 AsyncPolkitQuery
*q
= userdata
;
385 q
->slot
= sd_bus_slot_unref(q
->slot
);
386 q
->reply
= sd_bus_message_ref(reply
);
388 r
= sd_bus_message_rewind(q
->request
, true);
390 r
= sd_bus_reply_method_errno(q
->request
, r
, NULL
);
394 r
= q
->callback(q
->request
, q
->userdata
, &error_buffer
);
395 r
= bus_maybe_reply_error(q
->request
, r
, &error_buffer
);
398 async_polkit_query_free(q
);
405 int bus_verify_polkit_async(
406 sd_bus_message
*call
,
409 const char **details
,
413 sd_bus_error
*error
) {
416 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*pk
= NULL
;
418 const char *sender
, **k
, **v
;
419 sd_bus_message_handler_t callback
;
429 r
= check_good_user(call
, good_user
);
434 q
= hashmap_get(*registry
, call
);
436 int authorized
, challenge
;
438 /* This is the second invocation of this function, and
439 * there's already a response from polkit, let's
443 if (sd_bus_message_is_method_error(q
->reply
, NULL
)) {
444 const sd_bus_error
*e
;
446 /* Copy error from polkit reply */
447 e
= sd_bus_message_get_error(q
->reply
);
448 sd_bus_error_copy(error
, e
);
450 /* Treat no PK available as access denied */
451 if (sd_bus_error_has_name(e
, SD_BUS_ERROR_SERVICE_UNKNOWN
))
454 return -sd_bus_error_get_errno(e
);
457 r
= sd_bus_message_enter_container(q
->reply
, 'r', "bba{ss}");
459 r
= sd_bus_message_read(q
->reply
, "bb", &authorized
, &challenge
);
468 return sd_bus_error_set(error
, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED
, "Interactive authentication required.");
474 r
= sd_bus_query_sender_privilege(call
, capability
);
481 if (sd_bus_get_current_message(call
->bus
) != call
)
484 callback
= sd_bus_get_current_handler(call
->bus
);
488 userdata
= sd_bus_get_current_userdata(call
->bus
);
490 sender
= sd_bus_message_get_sender(call
);
494 c
= sd_bus_message_get_allow_interactive_authorization(call
);
500 r
= hashmap_ensure_allocated(registry
, NULL
);
504 r
= sd_bus_message_new_method_call(
507 "org.freedesktop.PolicyKit1",
508 "/org/freedesktop/PolicyKit1/Authority",
509 "org.freedesktop.PolicyKit1.Authority",
510 "CheckAuthorization");
514 r
= sd_bus_message_append(
517 "system-bus-name", 1, "name", "s", sender
,
522 r
= sd_bus_message_open_container(pk
, 'a', "{ss}");
526 STRV_FOREACH_PAIR(k
, v
, details
) {
527 r
= sd_bus_message_append(pk
, "{ss}", *k
, *v
);
532 r
= sd_bus_message_close_container(pk
);
536 r
= sd_bus_message_append(pk
, "us", !!interactive
, NULL
);
540 q
= new0(AsyncPolkitQuery
, 1);
544 q
->request
= sd_bus_message_ref(call
);
545 q
->callback
= callback
;
546 q
->userdata
= userdata
;
548 r
= hashmap_put(*registry
, call
, q
);
550 async_polkit_query_free(q
);
554 q
->registry
= *registry
;
556 r
= sd_bus_call_async(call
->bus
, &q
->slot
, pk
, async_polkit_callback
, q
, 0);
558 async_polkit_query_free(q
);
568 void bus_verify_polkit_async_registry_free(Hashmap
*registry
) {
572 while ((q
= hashmap_steal_first(registry
)))
573 async_polkit_query_free(q
);
575 hashmap_free(registry
);
579 int bus_check_peercred(sd_bus
*c
) {
586 fd
= sd_bus_get_fd(c
);
590 l
= sizeof(struct ucred
);
591 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &ucred
, &l
) < 0)
594 if (l
!= sizeof(struct ucred
))
597 if (ucred
.uid
!= 0 && ucred
.uid
!= geteuid())
603 int bus_connect_system_systemd(sd_bus
**_bus
) {
604 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
610 return sd_bus_default_system(_bus
);
612 /* If we are root and kdbus is not available, then let's talk
613 * directly to the system instance, instead of going via the
616 r
= sd_bus_new(&bus
);
620 r
= sd_bus_set_address(bus
, KERNEL_SYSTEM_BUS_ADDRESS
);
624 bus
->bus_client
= true;
626 r
= sd_bus_start(bus
);
633 bus
= sd_bus_unref(bus
);
635 r
= sd_bus_new(&bus
);
639 r
= sd_bus_set_address(bus
, "unix:path=/run/systemd/private");
643 r
= sd_bus_start(bus
);
645 return sd_bus_default_system(_bus
);
647 r
= bus_check_peercred(bus
);
657 int bus_connect_user_systemd(sd_bus
**_bus
) {
658 _cleanup_(sd_bus_unrefp
) sd_bus
*bus
= NULL
;
659 _cleanup_free_
char *ee
= NULL
;
663 /* Try via kdbus first, and then directly */
667 r
= sd_bus_new(&bus
);
671 if (asprintf(&bus
->address
, KERNEL_USER_BUS_ADDRESS_FMT
, getuid()) < 0)
674 bus
->bus_client
= true;
676 r
= sd_bus_start(bus
);
683 bus
= sd_bus_unref(bus
);
685 e
= secure_getenv("XDG_RUNTIME_DIR");
687 return sd_bus_default_user(_bus
);
689 ee
= bus_address_escape(e
);
693 r
= sd_bus_new(&bus
);
697 bus
->address
= strjoin("unix:path=", ee
, "/systemd/private", NULL
);
701 r
= sd_bus_start(bus
);
703 return sd_bus_default_user(_bus
);
705 r
= bus_check_peercred(bus
);
715 #define print_property(name, fmt, ...) \
718 printf(fmt "\n", __VA_ARGS__); \
720 printf("%s=" fmt "\n", name, __VA_ARGS__); \
723 int bus_print_property(const char *name
, sd_bus_message
*property
, bool value
, bool all
) {
725 const char *contents
;
731 r
= sd_bus_message_peek_type(property
, &type
, &contents
);
737 case SD_BUS_TYPE_STRING
: {
740 r
= sd_bus_message_read_basic(property
, type
, &s
);
744 if (all
|| !isempty(s
)) {
745 _cleanup_free_
char *escaped
= NULL
;
747 escaped
= xescape(s
, "\n");
751 print_property(name
, "%s", escaped
);
757 case SD_BUS_TYPE_BOOLEAN
: {
760 r
= sd_bus_message_read_basic(property
, type
, &b
);
764 print_property(name
, "%s", yes_no(b
));
769 case SD_BUS_TYPE_UINT64
: {
772 r
= sd_bus_message_read_basic(property
, type
, &u
);
776 /* Yes, heuristics! But we can change this check
777 * should it turn out to not be sufficient */
779 if (endswith(name
, "Timestamp")) {
780 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
782 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
784 print_property(name
, "%s", strempty(t
));
786 } else if (strstr(name
, "USec")) {
787 char timespan
[FORMAT_TIMESPAN_MAX
];
789 print_property(name
, "%s", format_timespan(timespan
, sizeof(timespan
), u
, 0));
791 print_property(name
, "%"PRIu64
, u
);
796 case SD_BUS_TYPE_INT64
: {
799 r
= sd_bus_message_read_basic(property
, type
, &i
);
803 print_property(name
, "%"PRIi64
, i
);
808 case SD_BUS_TYPE_UINT32
: {
811 r
= sd_bus_message_read_basic(property
, type
, &u
);
815 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
816 print_property(name
, "%04o", u
);
818 print_property(name
, "%"PRIu32
, u
);
823 case SD_BUS_TYPE_INT32
: {
826 r
= sd_bus_message_read_basic(property
, type
, &i
);
830 print_property(name
, "%"PRIi32
, i
);
834 case SD_BUS_TYPE_DOUBLE
: {
837 r
= sd_bus_message_read_basic(property
, type
, &d
);
841 print_property(name
, "%g", d
);
845 case SD_BUS_TYPE_ARRAY
:
846 if (streq(contents
, "s")) {
850 r
= sd_bus_message_enter_container(property
, SD_BUS_TYPE_ARRAY
, contents
);
854 while ((r
= sd_bus_message_read_basic(property
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
855 _cleanup_free_
char *escaped
= NULL
;
860 escaped
= xescape(str
, "\n ");
864 printf("%s%s", first
? "" : " ", escaped
);
871 if (first
&& all
&& !value
)
876 r
= sd_bus_message_exit_container(property
);
882 } else if (streq(contents
, "y")) {
886 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
896 for (i
= 0; i
< n
; i
++)
897 printf("%02x", u
[i
]);
904 } else if (streq(contents
, "u")) {
908 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
918 for (i
= 0; i
< n
; i
++)
919 printf("%08x", u
[i
]);
933 int bus_print_all_properties(sd_bus
*bus
, const char *dest
, const char *path
, char **filter
, bool value
, bool all
) {
934 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
935 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
941 r
= sd_bus_call_method(bus
,
944 "org.freedesktop.DBus.Properties",
952 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
956 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
958 const char *contents
;
960 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &name
);
964 if (!filter
|| strv_find(filter
, name
)) {
965 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
969 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
973 r
= bus_print_property(name
, reply
, value
, all
);
978 printf("%s=[unprintable]\n", name
);
979 /* skip what we didn't read */
980 r
= sd_bus_message_skip(reply
, contents
);
985 r
= sd_bus_message_exit_container(reply
);
989 r
= sd_bus_message_skip(reply
, "v");
994 r
= sd_bus_message_exit_container(reply
);
1001 r
= sd_bus_message_exit_container(reply
);
1008 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1009 sd_id128_t
*p
= userdata
;
1014 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1021 memcpy((*p
).bytes
, v
, n
);
1028 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1032 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1037 case SD_BUS_TYPE_STRING
: {
1039 char **p
= userdata
;
1041 r
= sd_bus_message_read_basic(m
, type
, &s
);
1048 r
= free_and_strdup(p
, s
);
1052 case SD_BUS_TYPE_ARRAY
: {
1053 _cleanup_strv_free_
char **l
= NULL
;
1054 char ***p
= userdata
;
1056 r
= bus_message_read_strv_extend(m
, &l
);
1067 case SD_BUS_TYPE_BOOLEAN
: {
1071 r
= sd_bus_message_read_basic(m
, type
, &b
);
1080 case SD_BUS_TYPE_UINT32
: {
1082 uint32_t *p
= userdata
;
1084 r
= sd_bus_message_read_basic(m
, type
, &u
);
1093 case SD_BUS_TYPE_UINT64
: {
1095 uint64_t *p
= userdata
;
1097 r
= sd_bus_message_read_basic(m
, type
, &t
);
1113 int bus_message_map_all_properties(
1115 const struct bus_properties_map
*map
,
1118 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1124 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1128 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1129 const struct bus_properties_map
*prop
;
1131 const char *contents
;
1135 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1139 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1140 if (streq(map
[i
].member
, member
)) {
1146 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1150 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1154 v
= (uint8_t *)userdata
+ prop
->offset
;
1156 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1158 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1162 r
= sd_bus_message_exit_container(m
);
1166 r
= sd_bus_message_skip(m
, "v");
1171 r
= sd_bus_message_exit_container(m
);
1178 return sd_bus_message_exit_container(m
);
1181 int bus_message_map_properties_changed(
1183 const struct bus_properties_map
*map
,
1187 int r
, invalidated
, i
;
1192 r
= bus_message_map_all_properties(m
, map
, userdata
);
1196 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "s");
1201 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
)) > 0)
1202 for (i
= 0; map
[i
].member
; i
++)
1203 if (streq(map
[i
].member
, member
)) {
1210 r
= sd_bus_message_exit_container(m
);
1217 int bus_map_all_properties(
1219 const char *destination
,
1221 const struct bus_properties_map
*map
,
1224 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1225 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1229 assert(destination
);
1233 r
= sd_bus_call_method(
1237 "org.freedesktop.DBus.Properties",
1245 return bus_message_map_all_properties(m
, map
, userdata
);
1248 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1251 assert(transport
>= 0);
1252 assert(transport
< _BUS_TRANSPORT_MAX
);
1255 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1256 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1258 switch (transport
) {
1260 case BUS_TRANSPORT_LOCAL
:
1262 r
= sd_bus_default_user(bus
);
1264 r
= sd_bus_default_system(bus
);
1268 case BUS_TRANSPORT_REMOTE
:
1269 r
= sd_bus_open_system_remote(bus
, host
);
1272 case BUS_TRANSPORT_MACHINE
:
1273 r
= sd_bus_open_system_machine(bus
, host
);
1277 assert_not_reached("Hmm, unknown transport type.");
1283 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1286 assert(transport
>= 0);
1287 assert(transport
< _BUS_TRANSPORT_MAX
);
1290 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1291 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1293 switch (transport
) {
1295 case BUS_TRANSPORT_LOCAL
:
1297 r
= bus_connect_user_systemd(bus
);
1299 r
= bus_connect_system_systemd(bus
);
1303 case BUS_TRANSPORT_REMOTE
:
1304 r
= sd_bus_open_system_remote(bus
, host
);
1307 case BUS_TRANSPORT_MACHINE
:
1308 r
= sd_bus_open_system_machine(bus
, host
);
1312 assert_not_reached("Hmm, unknown transport type.");
1318 int bus_property_get_bool(
1321 const char *interface
,
1322 const char *property
,
1323 sd_bus_message
*reply
,
1325 sd_bus_error
*error
) {
1327 int b
= *(bool*) userdata
;
1329 return sd_bus_message_append_basic(reply
, 'b', &b
);
1332 #if __SIZEOF_SIZE_T__ != 8
1333 int bus_property_get_size(
1336 const char *interface
,
1337 const char *property
,
1338 sd_bus_message
*reply
,
1340 sd_bus_error
*error
) {
1342 uint64_t sz
= *(size_t*) userdata
;
1344 return sd_bus_message_append_basic(reply
, 't', &sz
);
1348 #if __SIZEOF_LONG__ != 8
1349 int bus_property_get_long(
1352 const char *interface
,
1353 const char *property
,
1354 sd_bus_message
*reply
,
1356 sd_bus_error
*error
) {
1358 int64_t l
= *(long*) userdata
;
1360 return sd_bus_message_append_basic(reply
, 'x', &l
);
1363 int bus_property_get_ulong(
1366 const char *interface
,
1367 const char *property
,
1368 sd_bus_message
*reply
,
1370 sd_bus_error
*error
) {
1372 uint64_t ul
= *(unsigned long*) userdata
;
1374 return sd_bus_message_append_basic(reply
, 't', &ul
);
1378 int bus_log_parse_error(int r
) {
1379 return log_error_errno(r
, "Failed to parse bus message: %m");
1382 int bus_log_create_error(int r
) {
1383 return log_error_errno(r
, "Failed to create bus message: %m");
1386 int bus_parse_unit_info(sd_bus_message
*message
, UnitInfo
*u
) {
1392 return sd_bus_message_read(
1407 int bus_append_unit_property_assignment(sd_bus_message
*m
, const char *assignment
) {
1408 const char *eq
, *field
;
1414 eq
= strchr(assignment
, '=');
1416 log_error("Not an assignment: %s", assignment
);
1420 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1422 return bus_log_create_error(r
);
1424 field
= strndupa(assignment
, eq
- assignment
);
1427 if (streq(field
, "CPUQuota")) {
1430 r
= sd_bus_message_append(m
, "sv", "CPUQuotaPerSecUSec", "t", USEC_INFINITY
);
1431 else if (endswith(eq
, "%")) {
1434 if (sscanf(eq
, "%lf%%", &percent
) != 1 || percent
<= 0) {
1435 log_error("CPU quota '%s' invalid.", eq
);
1439 r
= sd_bus_message_append(m
, "sv", "CPUQuotaPerSecUSec", "t", (usec_t
) percent
* USEC_PER_SEC
/ 100);
1441 log_error("CPU quota needs to be in percent.");
1447 } else if (streq(field
, "EnvironmentFile")) {
1449 r
= sd_bus_message_append(m
, "sv", "EnvironmentFiles", "a(sb)", 1,
1450 eq
[0] == '-' ? eq
+ 1 : eq
,
1454 } else if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec", "RuntimeMaxSec")) {
1458 r
= parse_sec(eq
, &t
);
1460 return log_error_errno(r
, "Failed to parse %s= parameter: %s", field
, eq
);
1463 n
= newa(char, l
+ 2);
1467 /* Change suffix Sec → USec */
1468 strcpy(mempcpy(n
, field
, l
- 3), "USec");
1469 r
= sd_bus_message_append(m
, "sv", n
, "t", t
);
1473 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1475 return bus_log_create_error(r
);
1477 rl
= rlimit_from_string(field
);
1482 r
= rlimit_parse(rl
, eq
, &l
);
1484 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
1486 r
= sd_bus_message_append(m
, "v", "t", l
.rlim_max
);
1488 return bus_log_create_error(r
);
1490 r
= sd_bus_message_close_container(m
);
1492 return bus_log_create_error(r
);
1494 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1496 return bus_log_create_error(r
);
1498 sn
= strjoina(field
, "Soft");
1499 r
= sd_bus_message_append(m
, "sv", sn
, "t", l
.rlim_cur
);
1501 } else if (STR_IN_SET(field
,
1502 "CPUAccounting", "MemoryAccounting", "BlockIOAccounting", "TasksAccounting",
1503 "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies",
1504 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "RemainAfterExit",
1505 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "NoNewPrivileges",
1506 "SyslogLevelPrefix", "Delegate", "RemainAfterElapse")) {
1508 r
= parse_boolean(eq
);
1510 return log_error_errno(r
, "Failed to parse boolean assignment %s.", assignment
);
1512 r
= sd_bus_message_append(m
, "v", "b", r
);
1514 } else if (streq(field
, "MemoryLimit")) {
1517 if (isempty(eq
) || streq(eq
, "infinity"))
1518 bytes
= (uint64_t) -1;
1520 r
= parse_size(eq
, 1024, &bytes
);
1522 log_error("Failed to parse bytes specification %s", assignment
);
1527 r
= sd_bus_message_append(m
, "v", "t", bytes
);
1529 } else if (streq(field
, "TasksMax")) {
1532 if (isempty(eq
) || streq(eq
, "infinity"))
1535 r
= safe_atou64(eq
, &n
);
1537 log_error("Failed to parse maximum tasks specification %s", assignment
);
1542 r
= sd_bus_message_append(m
, "v", "t", n
);
1544 } else if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares")) {
1547 r
= cg_cpu_shares_parse(eq
, &u
);
1549 log_error("Failed to parse %s value %s.", field
, eq
);
1553 r
= sd_bus_message_append(m
, "v", "t", u
);
1555 } else if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight")) {
1558 r
= cg_cpu_shares_parse(eq
, &u
);
1560 log_error("Failed to parse %s value %s.", field
, eq
);
1564 r
= sd_bus_message_append(m
, "v", "t", u
);
1566 } else if (STR_IN_SET(field
,
1567 "User", "Group", "DevicePolicy", "KillMode",
1568 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
1569 "StandardInput", "StandardOutput", "StandardError",
1570 "Description", "Slice", "Type", "WorkingDirectory",
1571 "RootDirectory", "SyslogIdentifier", "ProtectSystem",
1573 r
= sd_bus_message_append(m
, "v", "s", eq
);
1575 else if (streq(field
, "SyslogLevel")) {
1578 level
= log_level_from_string(eq
);
1580 log_error("Failed to parse %s value %s.", field
, eq
);
1584 r
= sd_bus_message_append(m
, "v", "i", level
);
1586 } else if (streq(field
, "SyslogFacility")) {
1589 facility
= log_facility_unshifted_from_string(eq
);
1591 log_error("Failed to parse %s value %s.", field
, eq
);
1595 r
= sd_bus_message_append(m
, "v", "i", facility
);
1597 } else if (streq(field
, "DeviceAllow")) {
1600 r
= sd_bus_message_append(m
, "v", "a(ss)", 0);
1602 const char *path
, *rwm
, *e
;
1604 e
= strchr(eq
, ' ');
1606 path
= strndupa(eq
, e
- eq
);
1613 if (!path_startswith(path
, "/dev")) {
1614 log_error("%s is not a device file in /dev.", path
);
1618 r
= sd_bus_message_append(m
, "v", "a(ss)", 1, path
, rwm
);
1621 } else if (STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1624 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
1626 const char *path
, *bandwidth
, *e
;
1629 e
= strchr(eq
, ' ');
1631 path
= strndupa(eq
, e
- eq
);
1634 log_error("Failed to parse %s value %s.", field
, eq
);
1638 if (!path_startswith(path
, "/dev")) {
1639 log_error("%s is not a device file in /dev.", path
);
1643 r
= parse_size(bandwidth
, 1000, &bytes
);
1645 log_error("Failed to parse byte value %s.", bandwidth
);
1649 r
= sd_bus_message_append(m
, "v", "a(st)", 1, path
, bytes
);
1652 } else if (streq(field
, "BlockIODeviceWeight")) {
1655 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
1657 const char *path
, *weight
, *e
;
1660 e
= strchr(eq
, ' ');
1662 path
= strndupa(eq
, e
- eq
);
1665 log_error("Failed to parse %s value %s.", field
, eq
);
1669 if (!path_startswith(path
, "/dev")) {
1670 log_error("%s is not a device file in /dev.", path
);
1674 r
= safe_atou64(weight
, &u
);
1676 log_error("Failed to parse %s value %s.", field
, weight
);
1679 r
= sd_bus_message_append(m
, "v", "a(st)", path
, u
);
1682 } else if (streq(field
, "Nice")) {
1685 r
= safe_atoi32(eq
, &i
);
1687 log_error("Failed to parse %s value %s.", field
, eq
);
1691 r
= sd_bus_message_append(m
, "v", "i", i
);
1693 } else if (STR_IN_SET(field
, "Environment", "PassEnvironment")) {
1696 r
= sd_bus_message_open_container(m
, 'v', "as");
1698 return bus_log_create_error(r
);
1700 r
= sd_bus_message_open_container(m
, 'a', "s");
1702 return bus_log_create_error(r
);
1707 _cleanup_free_
char *word
= NULL
;
1709 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
1711 log_error("Failed to parse Environment value %s", eq
);
1717 if (streq(field
, "Environment")) {
1718 if (!env_assignment_is_valid(word
)) {
1719 log_error("Invalid environment assignment: %s", word
);
1722 } else { /* PassEnvironment */
1723 if (!env_name_is_valid(word
)) {
1724 log_error("Invalid environment variable name: %s", word
);
1729 r
= sd_bus_message_append_basic(m
, 's', word
);
1731 return bus_log_create_error(r
);
1734 r
= sd_bus_message_close_container(m
);
1736 return bus_log_create_error(r
);
1738 r
= sd_bus_message_close_container(m
);
1740 } else if (streq(field
, "KillSignal")) {
1743 sig
= signal_from_string_try_harder(eq
);
1745 log_error("Failed to parse %s value %s.", field
, eq
);
1749 r
= sd_bus_message_append(m
, "v", "i", sig
);
1751 } else if (streq(field
, "TimerSlackNSec")) {
1754 r
= parse_nsec(eq
, &n
);
1756 log_error("Failed to parse %s value %s", field
, eq
);
1760 r
= sd_bus_message_append(m
, "v", "t", n
);
1761 } else if (streq(field
, "OOMScoreAdjust")) {
1764 r
= safe_atoi(eq
, &oa
);
1766 log_error("Failed to parse %s value %s", field
, eq
);
1770 if (!oom_score_adjust_is_valid(oa
)) {
1771 log_error("OOM score adjust value out of range");
1775 r
= sd_bus_message_append(m
, "v", "i", oa
);
1776 } else if (STR_IN_SET(field
, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
1779 r
= sd_bus_message_open_container(m
, 'v', "as");
1781 return bus_log_create_error(r
);
1783 r
= sd_bus_message_open_container(m
, 'a', "s");
1785 return bus_log_create_error(r
);
1790 _cleanup_free_
char *word
= NULL
;
1793 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1795 log_error("Failed to parse %s value %s", field
, eq
);
1801 if (!utf8_is_valid(word
)) {
1802 log_error("Failed to parse %s value %s", field
, eq
);
1806 offset
= word
[0] == '-';
1807 if (!path_is_absolute(word
+ offset
)) {
1808 log_error("Failed to parse %s value %s", field
, eq
);
1812 path_kill_slashes(word
+ offset
);
1814 r
= sd_bus_message_append_basic(m
, 's', word
);
1816 return bus_log_create_error(r
);
1819 r
= sd_bus_message_close_container(m
);
1821 return bus_log_create_error(r
);
1823 r
= sd_bus_message_close_container(m
);
1825 } else if (streq(field
, "RuntimeDirectory")) {
1828 r
= sd_bus_message_open_container(m
, 'v', "as");
1830 return bus_log_create_error(r
);
1832 r
= sd_bus_message_open_container(m
, 'a', "s");
1834 return bus_log_create_error(r
);
1839 _cleanup_free_
char *word
= NULL
;
1841 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1843 return log_error_errno(r
, "Failed to parse %s value %s", field
, eq
);
1848 r
= sd_bus_message_append_basic(m
, 's', word
);
1850 return bus_log_create_error(r
);
1853 r
= sd_bus_message_close_container(m
);
1855 return bus_log_create_error(r
);
1857 r
= sd_bus_message_close_container(m
);
1860 log_error("Unknown assignment %s.", assignment
);
1866 return bus_log_create_error(r
);
1868 r
= sd_bus_message_close_container(m
);
1870 return bus_log_create_error(r
);
1875 typedef struct BusWaitForJobs
{
1882 sd_bus_slot
*slot_job_removed
;
1883 sd_bus_slot
*slot_disconnected
;
1886 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1889 log_error("Warning! D-Bus connection terminated.");
1890 sd_bus_close(sd_bus_message_get_bus(m
));
1895 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1896 const char *path
, *unit
, *result
;
1897 BusWaitForJobs
*d
= userdata
;
1905 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1907 bus_log_parse_error(r
);
1911 found
= set_remove(d
->jobs
, (char*) path
);
1917 if (!isempty(result
))
1918 d
->result
= strdup(result
);
1921 d
->name
= strdup(unit
);
1926 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1930 set_free_free(d
->jobs
);
1932 sd_bus_slot_unref(d
->slot_disconnected
);
1933 sd_bus_slot_unref(d
->slot_job_removed
);
1935 sd_bus_unref(d
->bus
);
1943 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1944 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1950 d
= new0(BusWaitForJobs
, 1);
1954 d
->bus
= sd_bus_ref(bus
);
1956 /* When we are a bus client we match by sender. Direct
1957 * connections OTOH have no initialized sender field, and
1958 * hence we ignore the sender then */
1959 r
= sd_bus_add_match(
1961 &d
->slot_job_removed
,
1964 "sender='org.freedesktop.systemd1',"
1965 "interface='org.freedesktop.systemd1.Manager',"
1966 "member='JobRemoved',"
1967 "path='/org/freedesktop/systemd1'" :
1969 "interface='org.freedesktop.systemd1.Manager',"
1970 "member='JobRemoved',"
1971 "path='/org/freedesktop/systemd1'",
1972 match_job_removed
, d
);
1976 r
= sd_bus_add_match(
1978 &d
->slot_disconnected
,
1980 "sender='org.freedesktop.DBus.Local',"
1981 "interface='org.freedesktop.DBus.Local',"
1982 "member='Disconnected'",
1983 match_disconnected
, d
);
1993 static int bus_process_wait(sd_bus
*bus
) {
1997 r
= sd_bus_process(bus
, NULL
);
2003 r
= sd_bus_wait(bus
, (uint64_t) -1);
2009 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
2010 _cleanup_free_
char *dbus_path
= NULL
;
2016 dbus_path
= unit_dbus_path_from_name(d
->name
);
2020 return sd_bus_get_property_string(d
->bus
,
2021 "org.freedesktop.systemd1",
2023 "org.freedesktop.systemd1.Service",
2029 static const struct {
2030 const char *result
, *explanation
;
2031 } explanations
[] = {
2032 { "resources", "a configured resource limit was exceeded" },
2033 { "timeout", "a timeout was exceeded" },
2034 { "exit-code", "the control process exited with error code" },
2035 { "signal", "a fatal signal was delivered to the control process" },
2036 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
2037 { "watchdog", "the service failed to send watchdog ping" },
2038 { "start-limit", "start of the service was attempted too often" }
2041 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
2042 _cleanup_free_
char *service_shell_quoted
= NULL
;
2043 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
2047 service_shell_quoted
= shell_maybe_quote(service
);
2049 if (extra_args
&& extra_args
[1]) {
2050 _cleanup_free_
char *t
;
2052 t
= strv_join((char**) extra_args
, " ");
2053 systemctl
= strjoina("systemctl ", t
? : "<args>");
2054 journalctl
= strjoina("journalctl ", t
? : "<args>");
2057 if (!isempty(result
)) {
2060 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
2061 if (streq(result
, explanations
[i
].result
))
2064 if (i
< ELEMENTSOF(explanations
)) {
2065 log_error("Job for %s failed because %s.\n"
2066 "See \"%s status %s\" and \"%s -xe\" for details.\n",
2068 explanations
[i
].explanation
,
2070 service_shell_quoted
?: "<service>",
2076 log_error("Job for %s failed.\n"
2077 "See \"%s status %s\" and \"%s -xe\" for details.\n",
2080 service_shell_quoted
?: "<service>",
2084 /* For some results maybe additional explanation is required */
2085 if (streq_ptr(result
, "start-limit"))
2086 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
2087 "followed by \"%1$s start %2$s\" again.",
2089 service_shell_quoted
?: "<service>");
2092 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
2098 if (streq(d
->result
, "canceled"))
2099 log_error("Job for %s canceled.", strna(d
->name
));
2100 else if (streq(d
->result
, "timeout"))
2101 log_error("Job for %s timed out.", strna(d
->name
));
2102 else if (streq(d
->result
, "dependency"))
2103 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
2104 else if (streq(d
->result
, "invalid"))
2105 log_error("%s is not active, cannot reload.", strna(d
->name
));
2106 else if (streq(d
->result
, "assert"))
2107 log_error("Assertion failed on job for %s.", strna(d
->name
));
2108 else if (streq(d
->result
, "unsupported"))
2109 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
2110 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped")) {
2113 _cleanup_free_
char *result
= NULL
;
2115 q
= bus_job_get_service_result(d
, &result
);
2117 log_debug_errno(q
, "Failed to get Result property of service %s: %m", d
->name
);
2119 log_job_error_with_service_result(d
->name
, result
, extra_args
);
2121 log_error("Job failed. See \"journalctl -xe\" for details.");
2125 if (streq(d
->result
, "canceled"))
2127 else if (streq(d
->result
, "timeout"))
2129 else if (streq(d
->result
, "dependency"))
2131 else if (streq(d
->result
, "invalid"))
2133 else if (streq(d
->result
, "assert"))
2135 else if (streq(d
->result
, "unsupported"))
2137 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped"))
2143 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
2148 while (!set_isempty(d
->jobs
)) {
2151 q
= bus_process_wait(d
->bus
);
2153 return log_error_errno(q
, "Failed to wait for response: %m");
2156 q
= check_wait_response(d
, quiet
, extra_args
);
2157 /* Return the first error as it is most likely to be
2159 if (q
< 0 && r
== 0)
2162 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
2165 d
->name
= mfree(d
->name
);
2166 d
->result
= mfree(d
->result
);
2172 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
2177 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
2181 return set_put_strdup(d
->jobs
, path
);
2184 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2187 r
= bus_wait_for_jobs_add(d
, path
);
2191 return bus_wait_for_jobs(d
, quiet
, NULL
);
2194 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, unsigned *n_changes
) {
2195 const char *type
, *path
, *source
;
2198 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2200 return bus_log_parse_error(r
);
2202 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2204 if (streq(type
, "symlink"))
2205 log_info("Created symlink from %s to %s.", path
, source
);
2207 log_info("Removed symlink %s.", path
);
2210 r
= unit_file_changes_add(changes
, n_changes
, streq(type
, "symlink") ? UNIT_FILE_SYMLINK
: UNIT_FILE_UNLINK
, path
, source
);
2215 return bus_log_parse_error(r
);
2217 r
= sd_bus_message_exit_container(m
);
2219 return bus_log_parse_error(r
);
2225 * bus_path_encode_unique() - encode unique object path
2226 * @b: bus connection or NULL
2227 * @prefix: object path prefix
2228 * @sender_id: unique-name of client, or NULL
2229 * @external_id: external ID to be chosen by client, or NULL
2230 * @ret_path: storage for encoded object path pointer
2232 * Whenever we provide a bus API that allows clients to create and manage
2233 * server-side objects, we need to provide a unique name for these objects. If
2234 * we let the server choose the name, we suffer from a race condition: If a
2235 * client creates an object asynchronously, it cannot destroy that object until
2236 * it received the method reply. It cannot know the name of the new object,
2237 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
2239 * Therefore, many APIs allow the client to choose the unique name for newly
2240 * created objects. There're two problems to solve, though:
2241 * 1) Object names are usually defined via dbus object paths, which are
2242 * usually globally namespaced. Therefore, multiple clients must be able
2243 * to choose unique object names without interference.
2244 * 2) If multiple libraries share the same bus connection, they must be
2245 * able to choose unique object names without interference.
2246 * The first problem is solved easily by prefixing a name with the
2247 * unique-bus-name of a connection. The server side must enforce this and
2248 * reject any other name. The second problem is solved by providing unique
2249 * suffixes from within sd-bus.
2251 * This helper allows clients to create unique object-paths. It uses the
2252 * template '/prefix/sender_id/external_id' and returns the new path in
2253 * @ret_path (must be freed by the caller).
2254 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
2255 * NULL, this function allocates a unique suffix via @b (by requesting a new
2256 * cookie). If both @sender_id and @external_id are given, @b can be passed as
2259 * Returns: 0 on success, negative error code on failure.
2261 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
2262 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
2263 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
2266 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
2267 assert_return(object_path_is_valid(prefix
), -EINVAL
);
2268 assert_return(ret_path
, -EINVAL
);
2271 r
= sd_bus_get_unique_name(b
, &sender_id
);
2277 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
2278 external_id
= external_buf
;
2281 sender_label
= bus_label_escape(sender_id
);
2285 external_label
= bus_label_escape(external_id
);
2286 if (!external_label
)
2289 p
= strjoin(prefix
, "/", sender_label
, "/", external_label
, NULL
);
2298 * bus_path_decode_unique() - decode unique object path
2299 * @path: object path to decode
2300 * @prefix: object path prefix
2301 * @ret_sender: output parameter for sender-id label
2302 * @ret_external: output parameter for external-id label
2304 * This does the reverse of bus_path_encode_unique() (see its description for
2305 * details). Both trailing labels, sender-id and external-id, are unescaped and
2306 * returned in the given output parameters (the caller must free them).
2308 * Note that this function returns 0 if the path does not match the template
2309 * (see bus_path_encode_unique()), 1 if it matched.
2311 * Returns: Negative error code on failure, 0 if the given object path does not
2312 * match the template (return parameters are set to NULL), 1 if it was
2313 * parsed successfully (return parameters contain allocated labels).
2315 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
2317 char *sender
, *external
;
2319 assert(object_path_is_valid(path
));
2320 assert(object_path_is_valid(prefix
));
2322 assert(ret_external
);
2324 p
= object_path_startswith(path
, prefix
);
2327 *ret_external
= NULL
;
2334 *ret_external
= NULL
;
2338 sender
= bus_label_unescape_n(p
, q
- p
);
2339 external
= bus_label_unescape(q
+ 1);
2340 if (!sender
|| !external
) {
2346 *ret_sender
= sender
;
2347 *ret_external
= external
;
2351 bool is_kdbus_wanted(void) {
2352 _cleanup_free_
char *value
= NULL
;
2354 const bool configured
= true;
2356 const bool configured
= false;
2361 if (get_proc_cmdline_key("kdbus", NULL
) > 0)
2364 r
= get_proc_cmdline_key("kdbus=", &value
);
2368 return parse_boolean(value
) == 1;
2371 bool is_kdbus_available(void) {
2372 _cleanup_close_
int fd
= -1;
2373 struct kdbus_cmd cmd
= { .size
= sizeof(cmd
), .flags
= KDBUS_FLAG_NEGOTIATE
};
2375 if (!is_kdbus_wanted())
2378 fd
= open("/sys/fs/kdbus/control", O_RDWR
| O_CLOEXEC
| O_NONBLOCK
| O_NOCTTY
);
2382 return ioctl(fd
, KDBUS_CMD_BUS_MAKE
, &cmd
) >= 0;
2385 int bus_property_get_rlimit(
2388 const char *interface
,
2389 const char *property
,
2390 sd_bus_message
*reply
,
2392 sd_bus_error
*error
) {
2397 const char *is_soft
;
2403 is_soft
= endswith(property
, "Soft");
2404 rl
= *(struct rlimit
**) userdata
;
2406 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
2408 struct rlimit buf
= {};
2412 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
2414 z
= rlimit_from_string(strstr(s
, "Limit"));
2418 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
2421 /* rlim_t might have different sizes, let's map
2422 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
2424 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
2426 return sd_bus_message_append(reply
, "t", u
);