1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
14 #include <sys/ioctl.h>
15 #include <sys/resource.h>
16 #include <sys/socket.h>
19 #include "sd-bus-protocol.h"
21 #include "sd-daemon.h"
25 #include "alloc-util.h"
26 #include "bus-internal.h"
27 #include "bus-label.h"
28 #include "bus-message.h"
31 #include "cgroup-util.h"
36 #include "mount-util.h"
38 #include "parse-util.h"
39 #include "proc-cmdline.h"
40 //#include "rlimit-util.h"
41 #include "stdio-util.h"
43 #include "user-util.h"
45 #if 0 /// UNNEEDED by elogind
46 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
47 sd_event *e = userdata;
52 sd_bus_close(sd_bus_message_get_bus(m));
58 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
67 /* We unregister the name here and then wait for the
68 * NameOwnerChanged signal for this event to arrive before we
69 * quit. We do this in order to make sure that any queued
70 * requests are still processed before we really exit. */
72 r = sd_bus_get_unique_name(bus, &unique);
77 "sender='org.freedesktop.DBus',"
79 "interface='org.freedesktop.DBus',"
80 "member='NameOwnerChanged',"
81 "path='/org/freedesktop/DBus',"
83 "arg1='", unique, "',",
86 r = sd_bus_add_match_async(bus, NULL, match, name_owner_change_callback, NULL, e);
90 r = sd_bus_release_name_async(bus, NULL, name, NULL, NULL);
97 int bus_event_loop_with_idle(
102 check_idle_t check_idle,
104 bool exiting = false;
114 r = sd_event_get_state(e);
117 if (r == SD_EVENT_FINISHED)
121 idle = check_idle(userdata);
125 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
129 if (r == 0 && !exiting && idle) {
131 r = sd_bus_try_close(bus);
135 /* Fallback for dbus1 connections: we
136 * unregister the name and wait for the
137 * response to come through for it */
138 if (r == -EOPNOTSUPP) {
140 /* Inform the service manager that we
141 * are going down, so that it will
142 * queue all further start requests,
143 * instead of assuming we are already
145 sd_notify(false, "STOPPING=1");
147 r = bus_async_unregister_and_exit(e, bus, name);
163 r = sd_event_get_exit_code(e, &code);
171 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
172 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
173 int r, has_owner = 0;
178 r = sd_bus_call_method(c,
179 "org.freedesktop.DBus",
180 "/org/freedesktop/dbus",
181 "org.freedesktop.DBus",
190 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
192 return sd_bus_error_set_errno(error, r);
197 static int check_good_user(sd_bus_message *m, uid_t good_user) {
198 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
204 if (good_user == UID_INVALID)
207 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
211 /* Don't trust augmented credentials for authorization */
212 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
214 r = sd_bus_creds_get_euid(creds, &sender_uid);
218 return sender_uid == good_user;
222 sd_bus_message *call,
225 const char **details,
235 /* Tests non-interactively! */
237 r = check_good_user(call, good_user);
241 r = sd_bus_query_sender_privilege(call, capability);
248 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
249 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
250 int authorized = false, challenge = false;
251 const char *sender, **k, **v;
253 sender = sd_bus_message_get_sender(call);
257 r = sd_bus_message_new_method_call(
260 "org.freedesktop.PolicyKit1",
261 "/org/freedesktop/PolicyKit1/Authority",
262 "org.freedesktop.PolicyKit1.Authority",
263 "CheckAuthorization");
267 r = sd_bus_message_append(
270 "system-bus-name", 1, "name", "s", sender,
275 r = sd_bus_message_open_container(request, 'a', "{ss}");
279 STRV_FOREACH_PAIR(k, v, details) {
280 r = sd_bus_message_append(request, "{ss}", *k, *v);
285 r = sd_bus_message_close_container(request);
289 r = sd_bus_message_append(request, "us", 0, NULL);
293 r = sd_bus_call(call->bus, request, 0, e, &reply);
295 /* Treat no PK available as access denied */
296 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
297 sd_bus_error_free(e);
304 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
308 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
316 *_challenge = challenge;
327 typedef struct AsyncPolkitQuery {
328 sd_bus_message *request, *reply;
329 sd_bus_message_handler_t callback;
335 static void async_polkit_query_free(AsyncPolkitQuery *q) {
340 sd_bus_slot_unref(q->slot);
342 if (q->registry && q->request)
343 hashmap_remove(q->registry, q->request);
345 sd_bus_message_unref(q->request);
346 sd_bus_message_unref(q->reply);
351 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
352 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
353 AsyncPolkitQuery *q = userdata;
359 q->slot = sd_bus_slot_unref(q->slot);
360 q->reply = sd_bus_message_ref(reply);
362 r = sd_bus_message_rewind(q->request, true);
364 r = sd_bus_reply_method_errno(q->request, r, NULL);
368 r = q->callback(q->request, q->userdata, &error_buffer);
369 r = bus_maybe_reply_error(q->request, r, &error_buffer);
372 async_polkit_query_free(q);
379 int bus_verify_polkit_async(
380 sd_bus_message *call,
383 const char **details,
387 sd_bus_error *error) {
390 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
392 const char *sender, **k, **v;
393 sd_bus_message_handler_t callback;
403 r = check_good_user(call, good_user);
408 q = hashmap_get(*registry, call);
410 int authorized, challenge;
412 /* This is the second invocation of this function, and
413 * there's already a response from polkit, let's
417 if (sd_bus_message_is_method_error(q->reply, NULL)) {
418 const sd_bus_error *e;
420 /* Copy error from polkit reply */
421 e = sd_bus_message_get_error(q->reply);
422 sd_bus_error_copy(error, e);
424 /* Treat no PK available as access denied */
425 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
428 return -sd_bus_error_get_errno(e);
431 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
433 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
442 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
448 r = sd_bus_query_sender_privilege(call, capability);
455 if (sd_bus_get_current_message(call->bus) != call)
458 callback = sd_bus_get_current_handler(call->bus);
462 userdata = sd_bus_get_current_userdata(call->bus);
464 sender = sd_bus_message_get_sender(call);
468 c = sd_bus_message_get_allow_interactive_authorization(call);
474 r = hashmap_ensure_allocated(registry, NULL);
478 r = sd_bus_message_new_method_call(
481 "org.freedesktop.PolicyKit1",
482 "/org/freedesktop/PolicyKit1/Authority",
483 "org.freedesktop.PolicyKit1.Authority",
484 "CheckAuthorization");
488 r = sd_bus_message_append(
491 "system-bus-name", 1, "name", "s", sender,
496 r = sd_bus_message_open_container(pk, 'a', "{ss}");
500 STRV_FOREACH_PAIR(k, v, details) {
501 r = sd_bus_message_append(pk, "{ss}", *k, *v);
506 r = sd_bus_message_close_container(pk);
510 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
514 q = new0(AsyncPolkitQuery, 1);
518 q->request = sd_bus_message_ref(call);
519 q->callback = callback;
520 q->userdata = userdata;
522 r = hashmap_put(*registry, call, q);
524 async_polkit_query_free(q);
528 q->registry = *registry;
530 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
532 async_polkit_query_free(q);
542 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
544 hashmap_free_with_destructor(registry, async_polkit_query_free);
548 #if 0 /// UNNEEDED by elogind
549 int bus_check_peercred(sd_bus *c) {
555 fd = sd_bus_get_fd(c);
559 r = getpeercred(fd, &ucred);
563 if (ucred.uid != 0 && ucred.uid != geteuid())
569 int bus_connect_system_systemd(sd_bus **_bus) {
570 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
576 return sd_bus_default_system(_bus);
578 /* If we are root then let's talk directly to the system
579 * instance, instead of going via the bus */
581 r = sd_bus_new(&bus);
585 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
589 r = sd_bus_start(bus);
591 return sd_bus_default_system(_bus);
593 r = bus_check_peercred(bus);
597 *_bus = TAKE_PTR(bus);
602 int bus_connect_user_systemd(sd_bus **_bus) {
603 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
604 _cleanup_free_ char *ee = NULL;
610 e = secure_getenv("XDG_RUNTIME_DIR");
612 return sd_bus_default_user(_bus);
614 ee = bus_address_escape(e);
618 r = sd_bus_new(&bus);
622 bus->address = strjoin("unix:path=", ee, "/systemd/private");
626 r = sd_bus_start(bus);
628 return sd_bus_default_user(_bus);
630 r = bus_check_peercred(bus);
634 *_bus = TAKE_PTR(bus);
640 #define print_property(name, fmt, ...) \
643 printf(fmt "\n", __VA_ARGS__); \
645 printf("%s=" fmt "\n", name, __VA_ARGS__); \
648 int bus_print_property(const char *name, sd_bus_message *m, bool value, bool all) {
650 const char *contents;
656 r = sd_bus_message_peek_type(m, &type, &contents);
662 case SD_BUS_TYPE_STRING: {
665 r = sd_bus_message_read_basic(m, type, &s);
669 if (all || !isempty(s)) {
672 /* This property has a single value, so we need to take
673 * care not to print a new line, everything else is OK. */
674 good = !strchr(s, '\n');
675 print_property(name, "%s", good ? s : "[unprintable]");
681 case SD_BUS_TYPE_BOOLEAN: {
684 r = sd_bus_message_read_basic(m, type, &b);
688 print_property(name, "%s", yes_no(b));
693 case SD_BUS_TYPE_UINT64: {
696 r = sd_bus_message_read_basic(m, type, &u);
700 /* Yes, heuristics! But we can change this check
701 * should it turn out to not be sufficient */
703 if (endswith(name, "Timestamp") || STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec")) {
704 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
706 t = format_timestamp(timestamp, sizeof(timestamp), u);
708 print_property(name, "%s", strempty(t));
710 } else if (strstr(name, "USec")) {
711 char timespan[FORMAT_TIMESPAN_MAX];
713 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
714 } else if (streq(name, "RestrictNamespaces")) {
715 _cleanup_free_ char *s = NULL;
718 if ((u & NAMESPACE_FLAGS_ALL) == 0)
720 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
723 r = namespace_flags_to_string(u, &s);
730 print_property(name, "%s", result);
732 } else if (streq(name, "MountFlags")) {
735 result = mount_propagation_flags_to_string(u);
739 print_property(name, "%s", result);
741 } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) {
742 _cleanup_free_ char *s = NULL;
744 r = capability_set_to_string_alloc(u, &s);
748 print_property(name, "%s", s);
750 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
751 (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
752 (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
753 (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
754 (endswith(name, "NSec") && u == (uint64_t) -1))
756 print_property(name, "%s", "[not set]");
758 else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
759 (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
760 (startswith(name, "Limit") && u == (uint64_t) -1) ||
761 (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
763 print_property(name, "%s", "infinity");
765 print_property(name, "%"PRIu64, u);
770 case SD_BUS_TYPE_INT64: {
773 r = sd_bus_message_read_basic(m, type, &i);
777 print_property(name, "%"PRIi64, i);
782 case SD_BUS_TYPE_UINT32: {
785 r = sd_bus_message_read_basic(m, type, &u);
789 if (strstr(name, "UMask") || strstr(name, "Mode"))
790 print_property(name, "%04o", u);
791 else if (streq(name, "UID")) {
792 if (u == UID_INVALID)
793 print_property(name, "%s", "[not set]");
795 print_property(name, "%"PRIu32, u);
796 } else if (streq(name, "GID")) {
797 if (u == GID_INVALID)
798 print_property(name, "%s", "[not set]");
800 print_property(name, "%"PRIu32, u);
802 print_property(name, "%"PRIu32, u);
807 case SD_BUS_TYPE_INT32: {
810 r = sd_bus_message_read_basic(m, type, &i);
814 print_property(name, "%"PRIi32, i);
818 case SD_BUS_TYPE_DOUBLE: {
821 r = sd_bus_message_read_basic(m, type, &d);
825 print_property(name, "%g", d);
829 case SD_BUS_TYPE_ARRAY:
830 if (streq(contents, "s")) {
834 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents);
838 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) {
844 /* This property has multiple space-separated values, so
845 * neither spaces not newlines can be allowed in a value. */
846 good = str[strcspn(str, " \n")] == '\0';
848 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
855 if (first && all && !value)
860 r = sd_bus_message_exit_container(m);
866 } else if (streq(contents, "y")) {
870 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
880 for (i = 0; i < n; i++)
881 printf("%02x", u[i]);
888 } else if (streq(contents, "u")) {
892 r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
902 for (i = 0; i < n; i++)
903 printf("%08x", u[i]);
917 int bus_message_print_all_properties(
919 bus_message_print_t func,
923 Set **found_properties) {
929 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
933 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
935 const char *contents;
937 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name);
941 if (found_properties) {
942 r = set_ensure_allocated(found_properties, &string_hash_ops);
946 r = set_put(*found_properties, name);
947 if (r < 0 && r != EEXIST)
951 if (!filter || strv_find(filter, name)) {
952 r = sd_bus_message_peek_type(m, NULL, &contents);
956 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
961 r = func(name, m, value, all);
963 r = bus_print_property(name, m, value, all);
968 printf("%s=[unprintable]\n", name);
969 /* skip what we didn't read */
970 r = sd_bus_message_skip(m, contents);
975 r = sd_bus_message_exit_container(m);
979 r = sd_bus_message_skip(m, "v");
984 r = sd_bus_message_exit_container(m);
991 r = sd_bus_message_exit_container(m);
998 int bus_print_all_properties(
1002 bus_message_print_t func,
1006 Set **found_properties) {
1008 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
1009 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1015 r = sd_bus_call_method(bus,
1018 "org.freedesktop.DBus.Properties",
1026 return bus_message_print_all_properties(reply, func, filter, value, all, found_properties);
1029 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1030 sd_id128_t *p = userdata;
1035 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1042 memcpy((*p).bytes, v, n);
1049 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) {
1053 r = sd_bus_message_peek_type(m, &type, NULL);
1059 case SD_BUS_TYPE_STRING: {
1060 const char **p = userdata;
1063 r = sd_bus_message_read_basic(m, type, &s);
1070 if (flags & BUS_MAP_STRDUP)
1071 return free_and_strdup((char **) userdata, s);
1077 case SD_BUS_TYPE_ARRAY: {
1078 _cleanup_strv_free_ char **l = NULL;
1079 char ***p = userdata;
1081 r = bus_message_read_strv_extend(m, &l);
1090 case SD_BUS_TYPE_BOOLEAN: {
1093 r = sd_bus_message_read_basic(m, type, &b);
1097 if (flags & BUS_MAP_BOOLEAN_AS_BOOL)
1098 * (bool*) userdata = !!b;
1100 * (int*) userdata = b;
1105 case SD_BUS_TYPE_INT32:
1106 case SD_BUS_TYPE_UINT32: {
1107 uint32_t u, *p = userdata;
1109 r = sd_bus_message_read_basic(m, type, &u);
1117 case SD_BUS_TYPE_INT64:
1118 case SD_BUS_TYPE_UINT64: {
1119 uint64_t t, *p = userdata;
1121 r = sd_bus_message_read_basic(m, type, &t);
1129 case SD_BUS_TYPE_DOUBLE: {
1130 double d, *p = userdata;
1132 r = sd_bus_message_read_basic(m, type, &d);
1143 int bus_message_map_all_properties(
1145 const struct bus_properties_map *map,
1147 sd_bus_error *error,
1155 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1159 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1160 const struct bus_properties_map *prop;
1162 const char *contents;
1166 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1170 for (i = 0, prop = NULL; map[i].member; i++)
1171 if (streq(map[i].member, member)) {
1177 r = sd_bus_message_peek_type(m, NULL, &contents);
1181 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1185 v = (uint8_t *)userdata + prop->offset;
1187 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1189 r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v);
1193 r = sd_bus_message_exit_container(m);
1197 r = sd_bus_message_skip(m, "v");
1202 r = sd_bus_message_exit_container(m);
1209 return sd_bus_message_exit_container(m);
1212 #if 0 /// UNNEEDED by elogind
1213 int bus_message_map_properties_changed(
1215 const struct bus_properties_map *map,
1217 sd_bus_error *error,
1221 int r, invalidated, i;
1226 r = bus_message_map_all_properties(m, map, flags, error, userdata);
1230 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1235 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1236 for (i = 0; map[i].member; i++)
1237 if (streq(map[i].member, member)) {
1244 r = sd_bus_message_exit_container(m);
1252 int bus_map_all_properties(
1254 const char *destination,
1256 const struct bus_properties_map *map,
1258 sd_bus_error *error,
1259 sd_bus_message **reply,
1262 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1266 assert(destination);
1269 assert(reply || (flags & BUS_MAP_STRDUP));
1271 r = sd_bus_call_method(
1275 "org.freedesktop.DBus.Properties",
1283 r = bus_message_map_all_properties(m, map, flags, error, userdata);
1288 *reply = sd_bus_message_ref(m);
1293 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1294 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1297 assert(transport >= 0);
1298 assert(transport < _BUS_TRANSPORT_MAX);
1301 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1302 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1304 switch (transport) {
1306 case BUS_TRANSPORT_LOCAL:
1307 #if 0 /// elogind does not support a user bus
1309 r = sd_bus_default_user(&bus);
1312 r = sd_bus_default_system(&bus);
1316 case BUS_TRANSPORT_REMOTE:
1317 r = sd_bus_open_system_remote(&bus, host);
1320 case BUS_TRANSPORT_MACHINE:
1321 r = sd_bus_open_system_machine(&bus, host);
1325 assert_not_reached("Hmm, unknown transport type.");
1330 r = sd_bus_set_exit_on_disconnect(bus, true);
1334 *ret = TAKE_PTR(bus);
1339 #if 0 /// UNNEEDED by elogind
1340 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1343 assert(transport >= 0);
1344 assert(transport < _BUS_TRANSPORT_MAX);
1347 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1348 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1350 switch (transport) {
1352 case BUS_TRANSPORT_LOCAL:
1354 r = bus_connect_user_systemd(bus);
1356 r = bus_connect_system_systemd(bus);
1360 case BUS_TRANSPORT_REMOTE:
1361 r = sd_bus_open_system_remote(bus, host);
1364 case BUS_TRANSPORT_MACHINE:
1365 r = sd_bus_open_system_machine(bus, host);
1369 assert_not_reached("Hmm, unknown transport type.");
1376 int bus_property_get_bool(
1379 const char *interface,
1380 const char *property,
1381 sd_bus_message *reply,
1383 sd_bus_error *error) {
1385 int b = *(bool*) userdata;
1387 return sd_bus_message_append_basic(reply, 'b', &b);
1390 int bus_property_set_bool(
1393 const char *interface,
1394 const char *property,
1395 sd_bus_message *value,
1397 sd_bus_error *error) {
1401 r = sd_bus_message_read(value, "b", &b);
1405 *(bool *) userdata = !!b;
1409 #if 0 /// UNNEEDED by elogind
1410 int bus_property_get_id128(
1413 const char *interface,
1414 const char *property,
1415 sd_bus_message *reply,
1417 sd_bus_error *error) {
1419 sd_id128_t *id = userdata;
1421 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1422 return sd_bus_message_append(reply, "ay", 0);
1424 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1428 #if __SIZEOF_SIZE_T__ != 8
1429 int bus_property_get_size(
1432 const char *interface,
1433 const char *property,
1434 sd_bus_message *reply,
1436 sd_bus_error *error) {
1438 uint64_t sz = *(size_t*) userdata;
1440 return sd_bus_message_append_basic(reply, 't', &sz);
1444 #if __SIZEOF_LONG__ != 8
1445 int bus_property_get_long(
1448 const char *interface,
1449 const char *property,
1450 sd_bus_message *reply,
1452 sd_bus_error *error) {
1454 int64_t l = *(long*) userdata;
1456 return sd_bus_message_append_basic(reply, 'x', &l);
1459 int bus_property_get_ulong(
1462 const char *interface,
1463 const char *property,
1464 sd_bus_message *reply,
1466 sd_bus_error *error) {
1468 uint64_t ul = *(unsigned long*) userdata;
1470 return sd_bus_message_append_basic(reply, 't', &ul);
1474 int bus_log_parse_error(int r) {
1475 return log_error_errno(r, "Failed to parse bus message: %m");
1478 #if 0 /// UNNEEDED by elogind
1479 int bus_log_create_error(int r) {
1480 return log_error_errno(r, "Failed to create bus message: %m");
1484 #if 0 /// UNNEEDED by elogind
1486 * bus_path_encode_unique() - encode unique object path
1487 * @b: bus connection or NULL
1488 * @prefix: object path prefix
1489 * @sender_id: unique-name of client, or NULL
1490 * @external_id: external ID to be chosen by client, or NULL
1491 * @ret_path: storage for encoded object path pointer
1493 * Whenever we provide a bus API that allows clients to create and manage
1494 * server-side objects, we need to provide a unique name for these objects. If
1495 * we let the server choose the name, we suffer from a race condition: If a
1496 * client creates an object asynchronously, it cannot destroy that object until
1497 * it received the method reply. It cannot know the name of the new object,
1498 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1500 * Therefore, many APIs allow the client to choose the unique name for newly
1501 * created objects. There're two problems to solve, though:
1502 * 1) Object names are usually defined via dbus object paths, which are
1503 * usually globally namespaced. Therefore, multiple clients must be able
1504 * to choose unique object names without interference.
1505 * 2) If multiple libraries share the same bus connection, they must be
1506 * able to choose unique object names without interference.
1507 * The first problem is solved easily by prefixing a name with the
1508 * unique-bus-name of a connection. The server side must enforce this and
1509 * reject any other name. The second problem is solved by providing unique
1510 * suffixes from within sd-bus.
1512 * This helper allows clients to create unique object-paths. It uses the
1513 * template '/prefix/sender_id/external_id' and returns the new path in
1514 * @ret_path (must be freed by the caller).
1515 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1516 * NULL, this function allocates a unique suffix via @b (by requesting a new
1517 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1520 * Returns: 0 on success, negative error code on failure.
1522 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1523 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1524 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1527 assert_return(b || (sender_id && external_id), -EINVAL);
1528 assert_return(object_path_is_valid(prefix), -EINVAL);
1529 assert_return(ret_path, -EINVAL);
1532 r = sd_bus_get_unique_name(b, &sender_id);
1538 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1539 external_id = external_buf;
1542 sender_label = bus_label_escape(sender_id);
1546 external_label = bus_label_escape(external_id);
1547 if (!external_label)
1550 p = strjoin(prefix, "/", sender_label, "/", external_label);
1559 * bus_path_decode_unique() - decode unique object path
1560 * @path: object path to decode
1561 * @prefix: object path prefix
1562 * @ret_sender: output parameter for sender-id label
1563 * @ret_external: output parameter for external-id label
1565 * This does the reverse of bus_path_encode_unique() (see its description for
1566 * details). Both trailing labels, sender-id and external-id, are unescaped and
1567 * returned in the given output parameters (the caller must free them).
1569 * Note that this function returns 0 if the path does not match the template
1570 * (see bus_path_encode_unique()), 1 if it matched.
1572 * Returns: Negative error code on failure, 0 if the given object path does not
1573 * match the template (return parameters are set to NULL), 1 if it was
1574 * parsed successfully (return parameters contain allocated labels).
1576 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1578 char *sender, *external;
1580 assert(object_path_is_valid(path));
1581 assert(object_path_is_valid(prefix));
1583 assert(ret_external);
1585 p = object_path_startswith(path, prefix);
1588 *ret_external = NULL;
1595 *ret_external = NULL;
1599 sender = bus_label_unescape_n(p, q - p);
1600 external = bus_label_unescape(q + 1);
1601 if (!sender || !external) {
1607 *ret_sender = sender;
1608 *ret_external = external;
1613 #if 0 /// UNNEEDED by elogind
1614 int bus_property_get_rlimit(
1617 const char *interface,
1618 const char *property,
1619 sd_bus_message *reply,
1621 sd_bus_error *error) {
1626 const char *is_soft;
1632 is_soft = endswith(property, "Soft");
1633 rl = *(struct rlimit**) userdata;
1635 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1637 struct rlimit buf = {};
1641 s = is_soft ? strndupa(property, is_soft - property) : property;
1643 z = rlimit_from_string(strstr(s, "Limit"));
1647 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1650 /* rlim_t might have different sizes, let's map
1651 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1653 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1655 return sd_bus_message_append(reply, "t", u);
1658 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1664 /* Continues adding after failure, and returns the first failure. */
1666 STRV_FOREACH(i, l) {
1669 k = sd_bus_track_add_name(t, *i);
1670 if (k < 0 && r >= 0)
1678 int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) {
1679 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1685 /* Match like sd_bus_open_system(), but with the "watch_bind" feature and the Connected() signal turned on. */
1687 r = sd_bus_new(&bus);
1692 r = sd_bus_set_description(bus, description);
1697 e = secure_getenv("DBUS_SYSTEM_BUS_ADDRESS");
1699 e = DEFAULT_SYSTEM_BUS_ADDRESS;
1701 r = sd_bus_set_address(bus, e);
1705 r = sd_bus_set_bus_client(bus, true);
1709 r = sd_bus_set_trusted(bus, true);
1713 r = sd_bus_negotiate_creds(bus, true, SD_BUS_CREDS_UID|SD_BUS_CREDS_EUID|SD_BUS_CREDS_EFFECTIVE_CAPS);
1717 r = sd_bus_set_watch_bind(bus, true);
1721 r = sd_bus_set_connected_signal(bus, true);
1725 r = sd_bus_start(bus);
1729 *ret = TAKE_PTR(bus);