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 int bus_print_property(const char *name
, sd_bus_message
*property
, bool all
) {
717 const char *contents
;
723 r
= sd_bus_message_peek_type(property
, &type
, &contents
);
729 case SD_BUS_TYPE_STRING
: {
732 r
= sd_bus_message_read_basic(property
, type
, &s
);
736 if (all
|| !isempty(s
)) {
737 _cleanup_free_
char *escaped
= NULL
;
739 escaped
= xescape(s
, "\n");
743 printf("%s=%s\n", name
, escaped
);
749 case SD_BUS_TYPE_BOOLEAN
: {
752 r
= sd_bus_message_read_basic(property
, type
, &b
);
756 printf("%s=%s\n", name
, yes_no(b
));
761 case SD_BUS_TYPE_UINT64
: {
764 r
= sd_bus_message_read_basic(property
, type
, &u
);
768 /* Yes, heuristics! But we can change this check
769 * should it turn out to not be sufficient */
771 if (endswith(name
, "Timestamp")) {
772 char timestamp
[FORMAT_TIMESTAMP_MAX
], *t
;
774 t
= format_timestamp(timestamp
, sizeof(timestamp
), u
);
776 printf("%s=%s\n", name
, strempty(t
));
778 } else if (strstr(name
, "USec")) {
779 char timespan
[FORMAT_TIMESPAN_MAX
];
781 printf("%s=%s\n", name
, format_timespan(timespan
, sizeof(timespan
), u
, 0));
783 printf("%s=%llu\n", name
, (unsigned long long) u
);
788 case SD_BUS_TYPE_INT64
: {
791 r
= sd_bus_message_read_basic(property
, type
, &i
);
795 printf("%s=%lld\n", name
, (long long) i
);
800 case SD_BUS_TYPE_UINT32
: {
803 r
= sd_bus_message_read_basic(property
, type
, &u
);
807 if (strstr(name
, "UMask") || strstr(name
, "Mode"))
808 printf("%s=%04o\n", name
, u
);
810 printf("%s=%u\n", name
, (unsigned) u
);
815 case SD_BUS_TYPE_INT32
: {
818 r
= sd_bus_message_read_basic(property
, type
, &i
);
822 printf("%s=%i\n", name
, (int) i
);
826 case SD_BUS_TYPE_DOUBLE
: {
829 r
= sd_bus_message_read_basic(property
, type
, &d
);
833 printf("%s=%g\n", name
, d
);
837 case SD_BUS_TYPE_ARRAY
:
838 if (streq(contents
, "s")) {
842 r
= sd_bus_message_enter_container(property
, SD_BUS_TYPE_ARRAY
, contents
);
846 while ((r
= sd_bus_message_read_basic(property
, SD_BUS_TYPE_STRING
, &str
)) > 0) {
847 _cleanup_free_
char *escaped
= NULL
;
852 escaped
= xescape(str
, "\n ");
856 printf("%s%s", first
? "" : " ", escaped
);
868 r
= sd_bus_message_exit_container(property
);
874 } else if (streq(contents
, "y")) {
878 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_BYTE
, (const void**) &u
, &n
);
887 for (i
= 0; i
< n
; i
++)
888 printf("%02x", u
[i
]);
895 } else if (streq(contents
, "u")) {
899 r
= sd_bus_message_read_array(property
, SD_BUS_TYPE_UINT32
, (const void**) &u
, &n
);
908 for (i
= 0; i
< n
; i
++)
909 printf("%08x", u
[i
]);
923 int bus_print_all_properties(sd_bus
*bus
, const char *dest
, const char *path
, char **filter
, bool all
) {
924 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*reply
= NULL
;
925 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
931 r
= sd_bus_call_method(bus
,
934 "org.freedesktop.DBus.Properties",
942 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_ARRAY
, "{sv}");
946 while ((r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
948 const char *contents
;
950 r
= sd_bus_message_read_basic(reply
, SD_BUS_TYPE_STRING
, &name
);
954 if (!filter
|| strv_find(filter
, name
)) {
955 r
= sd_bus_message_peek_type(reply
, NULL
, &contents
);
959 r
= sd_bus_message_enter_container(reply
, SD_BUS_TYPE_VARIANT
, contents
);
963 r
= bus_print_property(name
, reply
, all
);
968 printf("%s=[unprintable]\n", name
);
969 /* skip what we didn't read */
970 r
= sd_bus_message_skip(reply
, contents
);
975 r
= sd_bus_message_exit_container(reply
);
979 r
= sd_bus_message_skip(reply
, "v");
984 r
= sd_bus_message_exit_container(reply
);
991 r
= sd_bus_message_exit_container(reply
);
998 int bus_map_id128(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
999 sd_id128_t
*p
= userdata
;
1004 r
= sd_bus_message_read_array(m
, SD_BUS_TYPE_BYTE
, &v
, &n
);
1011 memcpy((*p
).bytes
, v
, n
);
1018 static int map_basic(sd_bus
*bus
, const char *member
, sd_bus_message
*m
, sd_bus_error
*error
, void *userdata
) {
1022 r
= sd_bus_message_peek_type(m
, &type
, NULL
);
1027 case SD_BUS_TYPE_STRING
: {
1029 char **p
= userdata
;
1031 r
= sd_bus_message_read_basic(m
, type
, &s
);
1038 r
= free_and_strdup(p
, s
);
1042 case SD_BUS_TYPE_ARRAY
: {
1043 _cleanup_strv_free_
char **l
= NULL
;
1044 char ***p
= userdata
;
1046 r
= bus_message_read_strv_extend(m
, &l
);
1057 case SD_BUS_TYPE_BOOLEAN
: {
1061 r
= sd_bus_message_read_basic(m
, type
, &b
);
1070 case SD_BUS_TYPE_UINT32
: {
1072 uint32_t *p
= userdata
;
1074 r
= sd_bus_message_read_basic(m
, type
, &u
);
1083 case SD_BUS_TYPE_UINT64
: {
1085 uint64_t *p
= userdata
;
1087 r
= sd_bus_message_read_basic(m
, type
, &t
);
1103 int bus_message_map_all_properties(
1105 const struct bus_properties_map
*map
,
1108 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1114 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "{sv}");
1118 while ((r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_DICT_ENTRY
, "sv")) > 0) {
1119 const struct bus_properties_map
*prop
;
1121 const char *contents
;
1125 r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
);
1129 for (i
= 0, prop
= NULL
; map
[i
].member
; i
++)
1130 if (streq(map
[i
].member
, member
)) {
1136 r
= sd_bus_message_peek_type(m
, NULL
, &contents
);
1140 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_VARIANT
, contents
);
1144 v
= (uint8_t *)userdata
+ prop
->offset
;
1146 r
= prop
->set(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1148 r
= map_basic(sd_bus_message_get_bus(m
), member
, m
, &error
, v
);
1152 r
= sd_bus_message_exit_container(m
);
1156 r
= sd_bus_message_skip(m
, "v");
1161 r
= sd_bus_message_exit_container(m
);
1168 return sd_bus_message_exit_container(m
);
1171 int bus_message_map_properties_changed(
1173 const struct bus_properties_map
*map
,
1177 int r
, invalidated
, i
;
1182 r
= bus_message_map_all_properties(m
, map
, userdata
);
1186 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "s");
1191 while ((r
= sd_bus_message_read_basic(m
, SD_BUS_TYPE_STRING
, &member
)) > 0)
1192 for (i
= 0; map
[i
].member
; i
++)
1193 if (streq(map
[i
].member
, member
)) {
1200 r
= sd_bus_message_exit_container(m
);
1207 int bus_map_all_properties(
1209 const char *destination
,
1211 const struct bus_properties_map
*map
,
1214 _cleanup_(sd_bus_message_unrefp
) sd_bus_message
*m
= NULL
;
1215 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1219 assert(destination
);
1223 r
= sd_bus_call_method(
1227 "org.freedesktop.DBus.Properties",
1235 return bus_message_map_all_properties(m
, map
, userdata
);
1238 int bus_connect_transport(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1241 assert(transport
>= 0);
1242 assert(transport
< _BUS_TRANSPORT_MAX
);
1245 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1246 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1248 switch (transport
) {
1250 case BUS_TRANSPORT_LOCAL
:
1252 r
= sd_bus_default_user(bus
);
1254 r
= sd_bus_default_system(bus
);
1258 case BUS_TRANSPORT_REMOTE
:
1259 r
= sd_bus_open_system_remote(bus
, host
);
1262 case BUS_TRANSPORT_MACHINE
:
1263 r
= sd_bus_open_system_machine(bus
, host
);
1267 assert_not_reached("Hmm, unknown transport type.");
1273 int bus_connect_transport_systemd(BusTransport transport
, const char *host
, bool user
, sd_bus
**bus
) {
1276 assert(transport
>= 0);
1277 assert(transport
< _BUS_TRANSPORT_MAX
);
1280 assert_return((transport
== BUS_TRANSPORT_LOCAL
) == !host
, -EINVAL
);
1281 assert_return(transport
== BUS_TRANSPORT_LOCAL
|| !user
, -EOPNOTSUPP
);
1283 switch (transport
) {
1285 case BUS_TRANSPORT_LOCAL
:
1287 r
= bus_connect_user_systemd(bus
);
1289 r
= bus_connect_system_systemd(bus
);
1293 case BUS_TRANSPORT_REMOTE
:
1294 r
= sd_bus_open_system_remote(bus
, host
);
1297 case BUS_TRANSPORT_MACHINE
:
1298 r
= sd_bus_open_system_machine(bus
, host
);
1302 assert_not_reached("Hmm, unknown transport type.");
1308 int bus_property_get_bool(
1311 const char *interface
,
1312 const char *property
,
1313 sd_bus_message
*reply
,
1315 sd_bus_error
*error
) {
1317 int b
= *(bool*) userdata
;
1319 return sd_bus_message_append_basic(reply
, 'b', &b
);
1322 #if __SIZEOF_SIZE_T__ != 8
1323 int bus_property_get_size(
1326 const char *interface
,
1327 const char *property
,
1328 sd_bus_message
*reply
,
1330 sd_bus_error
*error
) {
1332 uint64_t sz
= *(size_t*) userdata
;
1334 return sd_bus_message_append_basic(reply
, 't', &sz
);
1338 #if __SIZEOF_LONG__ != 8
1339 int bus_property_get_long(
1342 const char *interface
,
1343 const char *property
,
1344 sd_bus_message
*reply
,
1346 sd_bus_error
*error
) {
1348 int64_t l
= *(long*) userdata
;
1350 return sd_bus_message_append_basic(reply
, 'x', &l
);
1353 int bus_property_get_ulong(
1356 const char *interface
,
1357 const char *property
,
1358 sd_bus_message
*reply
,
1360 sd_bus_error
*error
) {
1362 uint64_t ul
= *(unsigned long*) userdata
;
1364 return sd_bus_message_append_basic(reply
, 't', &ul
);
1368 int bus_log_parse_error(int r
) {
1369 return log_error_errno(r
, "Failed to parse bus message: %m");
1372 int bus_log_create_error(int r
) {
1373 return log_error_errno(r
, "Failed to create bus message: %m");
1376 int bus_parse_unit_info(sd_bus_message
*message
, UnitInfo
*u
) {
1382 return sd_bus_message_read(
1397 int bus_append_unit_property_assignment(sd_bus_message
*m
, const char *assignment
) {
1398 const char *eq
, *field
;
1404 eq
= strchr(assignment
, '=');
1406 log_error("Not an assignment: %s", assignment
);
1410 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1412 return bus_log_create_error(r
);
1414 field
= strndupa(assignment
, eq
- assignment
);
1417 if (streq(field
, "CPUQuota")) {
1420 r
= sd_bus_message_append(m
, "sv", "CPUQuotaPerSecUSec", "t", USEC_INFINITY
);
1421 else if (endswith(eq
, "%")) {
1424 if (sscanf(eq
, "%lf%%", &percent
) != 1 || percent
<= 0) {
1425 log_error("CPU quota '%s' invalid.", eq
);
1429 r
= sd_bus_message_append(m
, "sv", "CPUQuotaPerSecUSec", "t", (usec_t
) percent
* USEC_PER_SEC
/ 100);
1431 log_error("CPU quota needs to be in percent.");
1437 } else if (streq(field
, "EnvironmentFile")) {
1439 r
= sd_bus_message_append(m
, "sv", "EnvironmentFiles", "a(sb)", 1,
1440 eq
[0] == '-' ? eq
+ 1 : eq
,
1444 } else if (STR_IN_SET(field
, "AccuracySec", "RandomizedDelaySec", "RuntimeMaxSec")) {
1448 r
= parse_sec(eq
, &t
);
1450 return log_error_errno(r
, "Failed to parse %s= parameter: %s", field
, eq
);
1453 n
= newa(char, l
+ 2);
1457 /* Change suffix Sec → USec */
1458 strcpy(mempcpy(n
, field
, l
- 3), "USec");
1459 r
= sd_bus_message_append(m
, "sv", n
, "t", t
);
1463 r
= sd_bus_message_append_basic(m
, SD_BUS_TYPE_STRING
, field
);
1465 return bus_log_create_error(r
);
1467 rl
= rlimit_from_string(field
);
1472 r
= rlimit_parse(rl
, eq
, &l
);
1474 return log_error_errno(r
, "Failed to parse resource limit: %s", eq
);
1476 r
= sd_bus_message_append(m
, "v", "t", l
.rlim_max
);
1478 return bus_log_create_error(r
);
1480 r
= sd_bus_message_close_container(m
);
1482 return bus_log_create_error(r
);
1484 r
= sd_bus_message_open_container(m
, SD_BUS_TYPE_STRUCT
, "sv");
1486 return bus_log_create_error(r
);
1488 sn
= strjoina(field
, "Soft");
1489 r
= sd_bus_message_append(m
, "sv", sn
, "t", l
.rlim_cur
);
1491 } else if (STR_IN_SET(field
,
1492 "CPUAccounting", "MemoryAccounting", "BlockIOAccounting", "TasksAccounting",
1493 "SendSIGHUP", "SendSIGKILL", "WakeSystem", "DefaultDependencies",
1494 "IgnoreSIGPIPE", "TTYVHangup", "TTYReset", "RemainAfterExit",
1495 "PrivateTmp", "PrivateDevices", "PrivateNetwork", "NoNewPrivileges",
1496 "SyslogLevelPrefix", "Delegate", "RemainAfterElapse")) {
1498 r
= parse_boolean(eq
);
1500 return log_error_errno(r
, "Failed to parse boolean assignment %s.", assignment
);
1502 r
= sd_bus_message_append(m
, "v", "b", r
);
1504 } else if (streq(field
, "MemoryLimit")) {
1507 if (isempty(eq
) || streq(eq
, "infinity"))
1508 bytes
= (uint64_t) -1;
1510 r
= parse_size(eq
, 1024, &bytes
);
1512 log_error("Failed to parse bytes specification %s", assignment
);
1517 r
= sd_bus_message_append(m
, "v", "t", bytes
);
1519 } else if (streq(field
, "TasksMax")) {
1522 if (isempty(eq
) || streq(eq
, "infinity"))
1525 r
= safe_atou64(eq
, &n
);
1527 log_error("Failed to parse maximum tasks specification %s", assignment
);
1532 r
= sd_bus_message_append(m
, "v", "t", n
);
1534 } else if (STR_IN_SET(field
, "CPUShares", "StartupCPUShares")) {
1537 r
= cg_cpu_shares_parse(eq
, &u
);
1539 log_error("Failed to parse %s value %s.", field
, eq
);
1543 r
= sd_bus_message_append(m
, "v", "t", u
);
1545 } else if (STR_IN_SET(field
, "BlockIOWeight", "StartupBlockIOWeight")) {
1548 r
= cg_cpu_shares_parse(eq
, &u
);
1550 log_error("Failed to parse %s value %s.", field
, eq
);
1554 r
= sd_bus_message_append(m
, "v", "t", u
);
1556 } else if (STR_IN_SET(field
,
1557 "User", "Group", "DevicePolicy", "KillMode",
1558 "UtmpIdentifier", "UtmpMode", "PAMName", "TTYPath",
1559 "StandardInput", "StandardOutput", "StandardError",
1560 "Description", "Slice", "Type", "WorkingDirectory",
1561 "RootDirectory", "SyslogIdentifier", "ProtectSystem",
1563 r
= sd_bus_message_append(m
, "v", "s", eq
);
1565 else if (streq(field
, "SyslogLevel")) {
1568 level
= log_level_from_string(eq
);
1570 log_error("Failed to parse %s value %s.", field
, eq
);
1574 r
= sd_bus_message_append(m
, "v", "i", level
);
1576 } else if (streq(field
, "SyslogFacility")) {
1579 facility
= log_facility_unshifted_from_string(eq
);
1581 log_error("Failed to parse %s value %s.", field
, eq
);
1585 r
= sd_bus_message_append(m
, "v", "i", facility
);
1587 } else if (streq(field
, "DeviceAllow")) {
1590 r
= sd_bus_message_append(m
, "v", "a(ss)", 0);
1592 const char *path
, *rwm
, *e
;
1594 e
= strchr(eq
, ' ');
1596 path
= strndupa(eq
, e
- eq
);
1603 if (!path_startswith(path
, "/dev")) {
1604 log_error("%s is not a device file in /dev.", path
);
1608 r
= sd_bus_message_append(m
, "v", "a(ss)", 1, path
, rwm
);
1611 } else if (STR_IN_SET(field
, "BlockIOReadBandwidth", "BlockIOWriteBandwidth")) {
1614 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
1616 const char *path
, *bandwidth
, *e
;
1619 e
= strchr(eq
, ' ');
1621 path
= strndupa(eq
, e
- eq
);
1624 log_error("Failed to parse %s value %s.", field
, eq
);
1628 if (!path_startswith(path
, "/dev")) {
1629 log_error("%s is not a device file in /dev.", path
);
1633 r
= parse_size(bandwidth
, 1000, &bytes
);
1635 log_error("Failed to parse byte value %s.", bandwidth
);
1639 r
= sd_bus_message_append(m
, "v", "a(st)", 1, path
, bytes
);
1642 } else if (streq(field
, "BlockIODeviceWeight")) {
1645 r
= sd_bus_message_append(m
, "v", "a(st)", 0);
1647 const char *path
, *weight
, *e
;
1650 e
= strchr(eq
, ' ');
1652 path
= strndupa(eq
, e
- eq
);
1655 log_error("Failed to parse %s value %s.", field
, eq
);
1659 if (!path_startswith(path
, "/dev")) {
1660 log_error("%s is not a device file in /dev.", path
);
1664 r
= safe_atou64(weight
, &u
);
1666 log_error("Failed to parse %s value %s.", field
, weight
);
1669 r
= sd_bus_message_append(m
, "v", "a(st)", path
, u
);
1672 } else if (streq(field
, "Nice")) {
1675 r
= safe_atoi32(eq
, &i
);
1677 log_error("Failed to parse %s value %s.", field
, eq
);
1681 r
= sd_bus_message_append(m
, "v", "i", i
);
1683 } else if (STR_IN_SET(field
, "Environment", "PassEnvironment")) {
1686 r
= sd_bus_message_open_container(m
, 'v', "as");
1688 return bus_log_create_error(r
);
1690 r
= sd_bus_message_open_container(m
, 'a', "s");
1692 return bus_log_create_error(r
);
1697 _cleanup_free_
char *word
= NULL
;
1699 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
|EXTRACT_CUNESCAPE
);
1701 log_error("Failed to parse Environment value %s", eq
);
1707 if (streq(field
, "Environment")) {
1708 if (!env_assignment_is_valid(word
)) {
1709 log_error("Invalid environment assignment: %s", word
);
1712 } else { /* PassEnvironment */
1713 if (!env_name_is_valid(word
)) {
1714 log_error("Invalid environment variable name: %s", word
);
1719 r
= sd_bus_message_append_basic(m
, 's', word
);
1721 return bus_log_create_error(r
);
1724 r
= sd_bus_message_close_container(m
);
1726 return bus_log_create_error(r
);
1728 r
= sd_bus_message_close_container(m
);
1730 } else if (streq(field
, "KillSignal")) {
1733 sig
= signal_from_string_try_harder(eq
);
1735 log_error("Failed to parse %s value %s.", field
, eq
);
1739 r
= sd_bus_message_append(m
, "v", "i", sig
);
1741 } else if (streq(field
, "TimerSlackNSec")) {
1744 r
= parse_nsec(eq
, &n
);
1746 log_error("Failed to parse %s value %s", field
, eq
);
1750 r
= sd_bus_message_append(m
, "v", "t", n
);
1751 } else if (streq(field
, "OOMScoreAdjust")) {
1754 r
= safe_atoi(eq
, &oa
);
1756 log_error("Failed to parse %s value %s", field
, eq
);
1760 if (!oom_score_adjust_is_valid(oa
)) {
1761 log_error("OOM score adjust value out of range");
1765 r
= sd_bus_message_append(m
, "v", "i", oa
);
1766 } else if (STR_IN_SET(field
, "ReadWriteDirectories", "ReadOnlyDirectories", "InaccessibleDirectories")) {
1769 r
= sd_bus_message_open_container(m
, 'v', "as");
1771 return bus_log_create_error(r
);
1773 r
= sd_bus_message_open_container(m
, 'a', "s");
1775 return bus_log_create_error(r
);
1780 _cleanup_free_
char *word
= NULL
;
1783 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1785 log_error("Failed to parse %s value %s", field
, eq
);
1791 if (!utf8_is_valid(word
)) {
1792 log_error("Failed to parse %s value %s", field
, eq
);
1796 offset
= word
[0] == '-';
1797 if (!path_is_absolute(word
+ offset
)) {
1798 log_error("Failed to parse %s value %s", field
, eq
);
1802 path_kill_slashes(word
+ offset
);
1804 r
= sd_bus_message_append_basic(m
, 's', word
);
1806 return bus_log_create_error(r
);
1809 r
= sd_bus_message_close_container(m
);
1811 return bus_log_create_error(r
);
1813 r
= sd_bus_message_close_container(m
);
1815 } else if (streq(field
, "RuntimeDirectory")) {
1818 r
= sd_bus_message_open_container(m
, 'v', "as");
1820 return bus_log_create_error(r
);
1822 r
= sd_bus_message_open_container(m
, 'a', "s");
1824 return bus_log_create_error(r
);
1829 _cleanup_free_
char *word
= NULL
;
1831 r
= extract_first_word(&p
, &word
, NULL
, EXTRACT_QUOTES
);
1833 return log_error_errno(r
, "Failed to parse %s value %s", field
, eq
);
1838 r
= sd_bus_message_append_basic(m
, 's', word
);
1840 return bus_log_create_error(r
);
1843 r
= sd_bus_message_close_container(m
);
1845 return bus_log_create_error(r
);
1847 r
= sd_bus_message_close_container(m
);
1850 log_error("Unknown assignment %s.", assignment
);
1856 return bus_log_create_error(r
);
1858 r
= sd_bus_message_close_container(m
);
1860 return bus_log_create_error(r
);
1865 typedef struct BusWaitForJobs
{
1872 sd_bus_slot
*slot_job_removed
;
1873 sd_bus_slot
*slot_disconnected
;
1876 static int match_disconnected(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1879 log_error("Warning! D-Bus connection terminated.");
1880 sd_bus_close(sd_bus_message_get_bus(m
));
1885 static int match_job_removed(sd_bus_message
*m
, void *userdata
, sd_bus_error
*error
) {
1886 const char *path
, *unit
, *result
;
1887 BusWaitForJobs
*d
= userdata
;
1895 r
= sd_bus_message_read(m
, "uoss", &id
, &path
, &unit
, &result
);
1897 bus_log_parse_error(r
);
1901 found
= set_remove(d
->jobs
, (char*) path
);
1907 if (!isempty(result
))
1908 d
->result
= strdup(result
);
1911 d
->name
= strdup(unit
);
1916 void bus_wait_for_jobs_free(BusWaitForJobs
*d
) {
1920 set_free_free(d
->jobs
);
1922 sd_bus_slot_unref(d
->slot_disconnected
);
1923 sd_bus_slot_unref(d
->slot_job_removed
);
1925 sd_bus_unref(d
->bus
);
1933 int bus_wait_for_jobs_new(sd_bus
*bus
, BusWaitForJobs
**ret
) {
1934 _cleanup_(bus_wait_for_jobs_freep
) BusWaitForJobs
*d
= NULL
;
1940 d
= new0(BusWaitForJobs
, 1);
1944 d
->bus
= sd_bus_ref(bus
);
1946 /* When we are a bus client we match by sender. Direct
1947 * connections OTOH have no initialized sender field, and
1948 * hence we ignore the sender then */
1949 r
= sd_bus_add_match(
1951 &d
->slot_job_removed
,
1954 "sender='org.freedesktop.systemd1',"
1955 "interface='org.freedesktop.systemd1.Manager',"
1956 "member='JobRemoved',"
1957 "path='/org/freedesktop/systemd1'" :
1959 "interface='org.freedesktop.systemd1.Manager',"
1960 "member='JobRemoved',"
1961 "path='/org/freedesktop/systemd1'",
1962 match_job_removed
, d
);
1966 r
= sd_bus_add_match(
1968 &d
->slot_disconnected
,
1970 "sender='org.freedesktop.DBus.Local',"
1971 "interface='org.freedesktop.DBus.Local',"
1972 "member='Disconnected'",
1973 match_disconnected
, d
);
1983 static int bus_process_wait(sd_bus
*bus
) {
1987 r
= sd_bus_process(bus
, NULL
);
1993 r
= sd_bus_wait(bus
, (uint64_t) -1);
1999 static int bus_job_get_service_result(BusWaitForJobs
*d
, char **result
) {
2000 _cleanup_free_
char *dbus_path
= NULL
;
2006 dbus_path
= unit_dbus_path_from_name(d
->name
);
2010 return sd_bus_get_property_string(d
->bus
,
2011 "org.freedesktop.systemd1",
2013 "org.freedesktop.systemd1.Service",
2019 static const struct {
2020 const char *result
, *explanation
;
2021 } explanations
[] = {
2022 { "resources", "a configured resource limit was exceeded" },
2023 { "timeout", "a timeout was exceeded" },
2024 { "exit-code", "the control process exited with error code" },
2025 { "signal", "a fatal signal was delivered to the control process" },
2026 { "core-dump", "a fatal signal was delivered causing the control process to dump core" },
2027 { "watchdog", "the service failed to send watchdog ping" },
2028 { "start-limit", "start of the service was attempted too often" }
2031 static void log_job_error_with_service_result(const char* service
, const char *result
, const char* const* extra_args
) {
2032 _cleanup_free_
char *service_shell_quoted
= NULL
;
2033 const char *systemctl
= "systemctl", *journalctl
= "journalctl";
2037 service_shell_quoted
= shell_maybe_quote(service
);
2039 if (extra_args
&& extra_args
[1]) {
2040 _cleanup_free_
char *t
;
2042 t
= strv_join((char**) extra_args
, " ");
2043 systemctl
= strjoina("systemctl ", t
?: "<args>", NULL
);
2044 journalctl
= strjoina("journalctl ", t
?: "<args>", NULL
);
2047 if (!isempty(result
)) {
2050 for (i
= 0; i
< ELEMENTSOF(explanations
); ++i
)
2051 if (streq(result
, explanations
[i
].result
))
2054 if (i
< ELEMENTSOF(explanations
)) {
2055 log_error("Job for %s failed because %s.\n"
2056 "See \"%s status %s\" and \"%s -xe\" for details.\n",
2058 explanations
[i
].explanation
,
2060 service_shell_quoted
?: "<service>",
2066 log_error("Job for %s failed.\n"
2067 "See \"%s status %s\" and \"%s -xe\" for details.\n",
2070 service_shell_quoted
?: "<service>",
2074 /* For some results maybe additional explanation is required */
2075 if (streq_ptr(result
, "start-limit"))
2076 log_info("To force a start use \"%1$s reset-failed %2$s\"\n"
2077 "followed by \"%1$s start %2$s\" again.",
2079 service_shell_quoted
?: "<service>");
2082 static int check_wait_response(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
2088 if (streq(d
->result
, "canceled"))
2089 log_error("Job for %s canceled.", strna(d
->name
));
2090 else if (streq(d
->result
, "timeout"))
2091 log_error("Job for %s timed out.", strna(d
->name
));
2092 else if (streq(d
->result
, "dependency"))
2093 log_error("A dependency job for %s failed. See 'journalctl -xe' for details.", strna(d
->name
));
2094 else if (streq(d
->result
, "invalid"))
2095 log_error("%s is not active, cannot reload.", strna(d
->name
));
2096 else if (streq(d
->result
, "assert"))
2097 log_error("Assertion failed on job for %s.", strna(d
->name
));
2098 else if (streq(d
->result
, "unsupported"))
2099 log_error("Operation on or unit type of %s not supported on this system.", strna(d
->name
));
2100 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped")) {
2103 _cleanup_free_
char *result
= NULL
;
2105 q
= bus_job_get_service_result(d
, &result
);
2107 log_debug_errno(q
, "Failed to get Result property of service %s: %m", d
->name
);
2109 log_job_error_with_service_result(d
->name
, result
, extra_args
);
2111 log_error("Job failed. See \"journalctl -xe\" for details.");
2115 if (streq(d
->result
, "canceled"))
2117 else if (streq(d
->result
, "timeout"))
2119 else if (streq(d
->result
, "dependency"))
2121 else if (streq(d
->result
, "invalid"))
2123 else if (streq(d
->result
, "assert"))
2125 else if (streq(d
->result
, "unsupported"))
2127 else if (!streq(d
->result
, "done") && !streq(d
->result
, "skipped"))
2133 int bus_wait_for_jobs(BusWaitForJobs
*d
, bool quiet
, const char* const* extra_args
) {
2138 while (!set_isempty(d
->jobs
)) {
2141 q
= bus_process_wait(d
->bus
);
2143 return log_error_errno(q
, "Failed to wait for response: %m");
2146 q
= check_wait_response(d
, quiet
, extra_args
);
2147 /* Return the first error as it is most likely to be
2149 if (q
< 0 && r
== 0)
2152 log_debug_errno(q
, "Got result %s/%m for job %s", strna(d
->result
), strna(d
->name
));
2155 d
->name
= mfree(d
->name
);
2156 d
->result
= mfree(d
->result
);
2162 int bus_wait_for_jobs_add(BusWaitForJobs
*d
, const char *path
) {
2167 r
= set_ensure_allocated(&d
->jobs
, &string_hash_ops
);
2171 return set_put_strdup(d
->jobs
, path
);
2174 int bus_wait_for_jobs_one(BusWaitForJobs
*d
, const char *path
, bool quiet
) {
2177 r
= bus_wait_for_jobs_add(d
, path
);
2181 return bus_wait_for_jobs(d
, quiet
, NULL
);
2184 int bus_deserialize_and_dump_unit_file_changes(sd_bus_message
*m
, bool quiet
, UnitFileChange
**changes
, unsigned *n_changes
) {
2185 const char *type
, *path
, *source
;
2188 r
= sd_bus_message_enter_container(m
, SD_BUS_TYPE_ARRAY
, "(sss)");
2190 return bus_log_parse_error(r
);
2192 while ((r
= sd_bus_message_read(m
, "(sss)", &type
, &path
, &source
)) > 0) {
2194 if (streq(type
, "symlink"))
2195 log_info("Created symlink from %s to %s.", path
, source
);
2197 log_info("Removed symlink %s.", path
);
2200 r
= unit_file_changes_add(changes
, n_changes
, streq(type
, "symlink") ? UNIT_FILE_SYMLINK
: UNIT_FILE_UNLINK
, path
, source
);
2205 return bus_log_parse_error(r
);
2207 r
= sd_bus_message_exit_container(m
);
2209 return bus_log_parse_error(r
);
2215 * bus_path_encode_unique() - encode unique object path
2216 * @b: bus connection or NULL
2217 * @prefix: object path prefix
2218 * @sender_id: unique-name of client, or NULL
2219 * @external_id: external ID to be chosen by client, or NULL
2220 * @ret_path: storage for encoded object path pointer
2222 * Whenever we provide a bus API that allows clients to create and manage
2223 * server-side objects, we need to provide a unique name for these objects. If
2224 * we let the server choose the name, we suffer from a race condition: If a
2225 * client creates an object asynchronously, it cannot destroy that object until
2226 * it received the method reply. It cannot know the name of the new object,
2227 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
2229 * Therefore, many APIs allow the client to choose the unique name for newly
2230 * created objects. There're two problems to solve, though:
2231 * 1) Object names are usually defined via dbus object paths, which are
2232 * usually globally namespaced. Therefore, multiple clients must be able
2233 * to choose unique object names without interference.
2234 * 2) If multiple libraries share the same bus connection, they must be
2235 * able to choose unique object names without interference.
2236 * The first problem is solved easily by prefixing a name with the
2237 * unique-bus-name of a connection. The server side must enforce this and
2238 * reject any other name. The second problem is solved by providing unique
2239 * suffixes from within sd-bus.
2241 * This helper allows clients to create unique object-paths. It uses the
2242 * template '/prefix/sender_id/external_id' and returns the new path in
2243 * @ret_path (must be freed by the caller).
2244 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
2245 * NULL, this function allocates a unique suffix via @b (by requesting a new
2246 * cookie). If both @sender_id and @external_id are given, @b can be passed as
2249 * Returns: 0 on success, negative error code on failure.
2251 int bus_path_encode_unique(sd_bus
*b
, const char *prefix
, const char *sender_id
, const char *external_id
, char **ret_path
) {
2252 _cleanup_free_
char *sender_label
= NULL
, *external_label
= NULL
;
2253 char external_buf
[DECIMAL_STR_MAX(uint64_t)], *p
;
2256 assert_return(b
|| (sender_id
&& external_id
), -EINVAL
);
2257 assert_return(object_path_is_valid(prefix
), -EINVAL
);
2258 assert_return(ret_path
, -EINVAL
);
2261 r
= sd_bus_get_unique_name(b
, &sender_id
);
2267 xsprintf(external_buf
, "%"PRIu64
, ++b
->cookie
);
2268 external_id
= external_buf
;
2271 sender_label
= bus_label_escape(sender_id
);
2275 external_label
= bus_label_escape(external_id
);
2276 if (!external_label
)
2279 p
= strjoin(prefix
, "/", sender_label
, "/", external_label
, NULL
);
2288 * bus_path_decode_unique() - decode unique object path
2289 * @path: object path to decode
2290 * @prefix: object path prefix
2291 * @ret_sender: output parameter for sender-id label
2292 * @ret_external: output parameter for external-id label
2294 * This does the reverse of bus_path_encode_unique() (see its description for
2295 * details). Both trailing labels, sender-id and external-id, are unescaped and
2296 * returned in the given output parameters (the caller must free them).
2298 * Note that this function returns 0 if the path does not match the template
2299 * (see bus_path_encode_unique()), 1 if it matched.
2301 * Returns: Negative error code on failure, 0 if the given object path does not
2302 * match the template (return parameters are set to NULL), 1 if it was
2303 * parsed successfully (return parameters contain allocated labels).
2305 int bus_path_decode_unique(const char *path
, const char *prefix
, char **ret_sender
, char **ret_external
) {
2307 char *sender
, *external
;
2309 assert(object_path_is_valid(path
));
2310 assert(object_path_is_valid(prefix
));
2312 assert(ret_external
);
2314 p
= object_path_startswith(path
, prefix
);
2317 *ret_external
= NULL
;
2324 *ret_external
= NULL
;
2328 sender
= bus_label_unescape_n(p
, q
- p
);
2329 external
= bus_label_unescape(q
+ 1);
2330 if (!sender
|| !external
) {
2336 *ret_sender
= sender
;
2337 *ret_external
= external
;
2341 bool is_kdbus_wanted(void) {
2342 _cleanup_free_
char *value
= NULL
;
2344 const bool configured
= true;
2346 const bool configured
= false;
2351 if (get_proc_cmdline_key("kdbus", NULL
) > 0)
2354 r
= get_proc_cmdline_key("kdbus=", &value
);
2358 return parse_boolean(value
) == 1;
2361 bool is_kdbus_available(void) {
2362 _cleanup_close_
int fd
= -1;
2363 struct kdbus_cmd cmd
= { .size
= sizeof(cmd
), .flags
= KDBUS_FLAG_NEGOTIATE
};
2365 if (!is_kdbus_wanted())
2368 fd
= open("/sys/fs/kdbus/control", O_RDWR
| O_CLOEXEC
| O_NONBLOCK
| O_NOCTTY
);
2372 return ioctl(fd
, KDBUS_CMD_BUS_MAKE
, &cmd
) >= 0;
2375 int bus_property_get_rlimit(
2378 const char *interface
,
2379 const char *property
,
2380 sd_bus_message
*reply
,
2382 sd_bus_error
*error
) {
2387 const char *is_soft
;
2393 is_soft
= endswith(property
, "Soft");
2394 rl
= *(struct rlimit
**) userdata
;
2396 x
= is_soft
? rl
->rlim_cur
: rl
->rlim_max
;
2398 struct rlimit buf
= {};
2402 s
= is_soft
? strndupa(property
, is_soft
- property
) : property
;
2404 z
= rlimit_from_string(strstr(s
, "Limit"));
2408 x
= is_soft
? buf
.rlim_cur
: buf
.rlim_max
;
2411 /* rlim_t might have different sizes, let's map
2412 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
2414 u
= x
== RLIM_INFINITY
? (uint64_t) -1 : (uint64_t) x
;
2416 return sd_bus_message_append(reply
, "t", u
);