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"
47 #include "mount-util.h"
49 #include "parse-util.h"
50 #include "proc-cmdline.h"
51 //#include "rlimit-util.h"
52 #include "stdio-util.h"
54 #include "user-util.h"
56 #if 0 /// UNNEEDED by elogind
57 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
58 sd_event *e = userdata;
63 sd_bus_close(sd_bus_message_get_bus(m));
69 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
70 _cleanup_free_ char *match = NULL;
78 /* We unregister the name here and then wait for the
79 * NameOwnerChanged signal for this event to arrive before we
80 * quit. We do this in order to make sure that any queued
81 * requests are still processed before we really exit. */
83 r = sd_bus_get_unique_name(bus, &unique);
88 "sender='org.freedesktop.DBus',"
90 "interface='org.freedesktop.DBus',"
91 "member='NameOwnerChanged',"
92 "path='/org/freedesktop/DBus',"
95 "arg2=''", name, unique);
99 r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
103 r = sd_bus_release_name(bus, name);
110 int bus_event_loop_with_idle(
115 check_idle_t check_idle,
117 bool exiting = false;
127 r = sd_event_get_state(e);
130 if (r == SD_EVENT_FINISHED)
134 idle = check_idle(userdata);
138 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
142 if (r == 0 && !exiting && idle) {
144 r = sd_bus_try_close(bus);
148 /* Fallback for dbus1 connections: we
149 * unregister the name and wait for the
150 * response to come through for it */
151 if (r == -EOPNOTSUPP) {
153 /* Inform the service manager that we
154 * are going down, so that it will
155 * queue all further start requests,
156 * instead of assuming we are already
158 sd_notify(false, "STOPPING=1");
160 r = bus_async_unregister_and_exit(e, bus, name);
176 r = sd_event_get_exit_code(e, &code);
184 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
185 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
186 int r, has_owner = 0;
191 r = sd_bus_call_method(c,
192 "org.freedesktop.DBus",
193 "/org/freedesktop/dbus",
194 "org.freedesktop.DBus",
203 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
205 return sd_bus_error_set_errno(error, r);
210 static int check_good_user(sd_bus_message *m, uid_t good_user) {
211 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
217 if (good_user == UID_INVALID)
220 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
224 /* Don't trust augmented credentials for authorization */
225 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
227 r = sd_bus_creds_get_euid(creds, &sender_uid);
231 return sender_uid == good_user;
235 sd_bus_message *call,
238 const char **details,
248 /* Tests non-interactively! */
250 r = check_good_user(call, good_user);
254 r = sd_bus_query_sender_privilege(call, capability);
261 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
262 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
263 int authorized = false, challenge = false;
264 const char *sender, **k, **v;
266 sender = sd_bus_message_get_sender(call);
270 r = sd_bus_message_new_method_call(
273 "org.freedesktop.PolicyKit1",
274 "/org/freedesktop/PolicyKit1/Authority",
275 "org.freedesktop.PolicyKit1.Authority",
276 "CheckAuthorization");
280 r = sd_bus_message_append(
283 "system-bus-name", 1, "name", "s", sender,
288 r = sd_bus_message_open_container(request, 'a', "{ss}");
292 STRV_FOREACH_PAIR(k, v, details) {
293 r = sd_bus_message_append(request, "{ss}", *k, *v);
298 r = sd_bus_message_close_container(request);
302 r = sd_bus_message_append(request, "us", 0, NULL);
306 r = sd_bus_call(call->bus, request, 0, e, &reply);
308 /* Treat no PK available as access denied */
309 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
310 sd_bus_error_free(e);
317 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
321 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
329 *_challenge = challenge;
340 typedef struct AsyncPolkitQuery {
341 sd_bus_message *request, *reply;
342 sd_bus_message_handler_t callback;
348 static void async_polkit_query_free(AsyncPolkitQuery *q) {
353 sd_bus_slot_unref(q->slot);
355 if (q->registry && q->request)
356 hashmap_remove(q->registry, q->request);
358 sd_bus_message_unref(q->request);
359 sd_bus_message_unref(q->reply);
364 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
365 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
366 AsyncPolkitQuery *q = userdata;
372 q->slot = sd_bus_slot_unref(q->slot);
373 q->reply = sd_bus_message_ref(reply);
375 r = sd_bus_message_rewind(q->request, true);
377 r = sd_bus_reply_method_errno(q->request, r, NULL);
381 r = q->callback(q->request, q->userdata, &error_buffer);
382 r = bus_maybe_reply_error(q->request, r, &error_buffer);
385 async_polkit_query_free(q);
392 int bus_verify_polkit_async(
393 sd_bus_message *call,
396 const char **details,
400 sd_bus_error *error) {
403 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
405 const char *sender, **k, **v;
406 sd_bus_message_handler_t callback;
416 r = check_good_user(call, good_user);
421 q = hashmap_get(*registry, call);
423 int authorized, challenge;
425 /* This is the second invocation of this function, and
426 * there's already a response from polkit, let's
430 if (sd_bus_message_is_method_error(q->reply, NULL)) {
431 const sd_bus_error *e;
433 /* Copy error from polkit reply */
434 e = sd_bus_message_get_error(q->reply);
435 sd_bus_error_copy(error, e);
437 /* Treat no PK available as access denied */
438 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
441 return -sd_bus_error_get_errno(e);
444 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
446 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
455 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
461 r = sd_bus_query_sender_privilege(call, capability);
468 if (sd_bus_get_current_message(call->bus) != call)
471 callback = sd_bus_get_current_handler(call->bus);
475 userdata = sd_bus_get_current_userdata(call->bus);
477 sender = sd_bus_message_get_sender(call);
481 c = sd_bus_message_get_allow_interactive_authorization(call);
487 r = hashmap_ensure_allocated(registry, NULL);
491 r = sd_bus_message_new_method_call(
494 "org.freedesktop.PolicyKit1",
495 "/org/freedesktop/PolicyKit1/Authority",
496 "org.freedesktop.PolicyKit1.Authority",
497 "CheckAuthorization");
501 r = sd_bus_message_append(
504 "system-bus-name", 1, "name", "s", sender,
509 r = sd_bus_message_open_container(pk, 'a', "{ss}");
513 STRV_FOREACH_PAIR(k, v, details) {
514 r = sd_bus_message_append(pk, "{ss}", *k, *v);
519 r = sd_bus_message_close_container(pk);
523 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
527 q = new0(AsyncPolkitQuery, 1);
531 q->request = sd_bus_message_ref(call);
532 q->callback = callback;
533 q->userdata = userdata;
535 r = hashmap_put(*registry, call, q);
537 async_polkit_query_free(q);
541 q->registry = *registry;
543 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
545 async_polkit_query_free(q);
555 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
559 while ((q = hashmap_steal_first(registry)))
560 async_polkit_query_free(q);
562 hashmap_free(registry);
566 #if 0 /// UNNEEDED by elogind
567 int bus_check_peercred(sd_bus *c) {
574 fd = sd_bus_get_fd(c);
578 l = sizeof(struct ucred);
579 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
582 if (l != sizeof(struct ucred))
585 if (ucred.uid != 0 && ucred.uid != geteuid())
591 int bus_connect_system_systemd(sd_bus **_bus) {
592 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
598 return sd_bus_default_system(_bus);
600 /* If we are root and kdbus is not available, then let's talk
601 * directly to the system instance, instead of going via the
604 r = sd_bus_new(&bus);
608 r = sd_bus_set_address(bus, KERNEL_SYSTEM_BUS_ADDRESS);
612 bus->bus_client = true;
614 r = sd_bus_start(bus);
621 bus = sd_bus_unref(bus);
623 r = sd_bus_new(&bus);
627 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
631 r = sd_bus_start(bus);
633 return sd_bus_default_system(_bus);
635 r = bus_check_peercred(bus);
645 int bus_connect_user_systemd(sd_bus **_bus) {
646 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
647 _cleanup_free_ char *ee = NULL;
651 /* Try via kdbus first, and then directly */
655 r = sd_bus_new(&bus);
659 if (asprintf(&bus->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid()) < 0)
662 bus->bus_client = true;
664 r = sd_bus_start(bus);
671 bus = sd_bus_unref(bus);
673 e = secure_getenv("XDG_RUNTIME_DIR");
675 return sd_bus_default_user(_bus);
677 ee = bus_address_escape(e);
681 r = sd_bus_new(&bus);
685 bus->address = strjoin("unix:path=", ee, "/systemd/private");
689 r = sd_bus_start(bus);
691 return sd_bus_default_user(_bus);
693 r = bus_check_peercred(bus);
704 #define print_property(name, fmt, ...) \
707 printf(fmt "\n", __VA_ARGS__); \
709 printf("%s=" fmt "\n", name, __VA_ARGS__); \
712 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
714 const char *contents;
720 r = sd_bus_message_peek_type(property, &type, &contents);
726 case SD_BUS_TYPE_STRING: {
729 r = sd_bus_message_read_basic(property, type, &s);
733 if (all || !isempty(s)) {
736 /* This property has a single value, so we need to take
737 * care not to print a new line, everything else is OK. */
738 good = !strchr(s, '\n');
739 print_property(name, "%s", good ? s : "[unprintable]");
745 case SD_BUS_TYPE_BOOLEAN: {
748 r = sd_bus_message_read_basic(property, type, &b);
752 print_property(name, "%s", yes_no(b));
757 case SD_BUS_TYPE_UINT64: {
760 r = sd_bus_message_read_basic(property, type, &u);
764 /* Yes, heuristics! But we can change this check
765 * should it turn out to not be sufficient */
767 if (endswith(name, "Timestamp")) {
768 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
770 t = format_timestamp(timestamp, sizeof(timestamp), u);
772 print_property(name, "%s", strempty(t));
774 } else if (strstr(name, "USec")) {
775 char timespan[FORMAT_TIMESPAN_MAX];
777 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
778 } else if (streq(name, "RestrictNamespaces")) {
779 _cleanup_free_ char *s = NULL;
780 const char *result = NULL;
782 if ((u & NAMESPACE_FLAGS_ALL) == 0)
784 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
787 r = namespace_flag_to_string_many(u, &s);
794 print_property(name, "%s", result);
796 } else if (streq(name, "MountFlags")) {
797 const char *result = NULL;
799 result = mount_propagation_flags_to_string(u);
803 print_property(name, "%s", result);
805 } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) ||
806 (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) ||
807 (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) ||
808 (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) ||
809 (endswith(name, "NSec") && u == (uint64_t) -1))
811 print_property(name, "%s", "[not set]");
813 else if ((STR_IN_SET(name, "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) ||
814 (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) ||
815 (startswith(name, "Limit") && u == (uint64_t) -1) ||
816 (startswith(name, "DefaultLimit") && u == (uint64_t) -1))
818 print_property(name, "%s", "infinity");
820 print_property(name, "%"PRIu64, u);
825 case SD_BUS_TYPE_INT64: {
828 r = sd_bus_message_read_basic(property, type, &i);
832 print_property(name, "%"PRIi64, i);
837 case SD_BUS_TYPE_UINT32: {
840 r = sd_bus_message_read_basic(property, type, &u);
844 if (strstr(name, "UMask") || strstr(name, "Mode"))
845 print_property(name, "%04o", u);
847 print_property(name, "%"PRIu32, u);
852 case SD_BUS_TYPE_INT32: {
855 r = sd_bus_message_read_basic(property, type, &i);
859 print_property(name, "%"PRIi32, i);
863 case SD_BUS_TYPE_DOUBLE: {
866 r = sd_bus_message_read_basic(property, type, &d);
870 print_property(name, "%g", d);
874 case SD_BUS_TYPE_ARRAY:
875 if (streq(contents, "s")) {
879 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
883 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
889 /* This property has multiple space-seperated values, so
890 * neither spaces not newlines can be allowed in a value. */
891 good = str[strcspn(str, " \n")] == '\0';
893 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
900 if (first && all && !value)
905 r = sd_bus_message_exit_container(property);
911 } else if (streq(contents, "y")) {
915 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
925 for (i = 0; i < n; i++)
926 printf("%02x", u[i]);
933 } else if (streq(contents, "u")) {
937 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
947 for (i = 0; i < n; i++)
948 printf("%08x", u[i]);
962 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
963 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
964 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
970 r = sd_bus_call_method(bus,
973 "org.freedesktop.DBus.Properties",
981 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
985 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
987 const char *contents;
989 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
993 if (!filter || strv_find(filter, name)) {
994 r = sd_bus_message_peek_type(reply, NULL, &contents);
998 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
1002 r = bus_print_property(name, reply, value, all);
1007 printf("%s=[unprintable]\n", name);
1008 /* skip what we didn't read */
1009 r = sd_bus_message_skip(reply, contents);
1014 r = sd_bus_message_exit_container(reply);
1018 r = sd_bus_message_skip(reply, "v");
1023 r = sd_bus_message_exit_container(reply);
1030 r = sd_bus_message_exit_container(reply);
1037 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1038 sd_id128_t *p = userdata;
1043 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1050 memcpy((*p).bytes, v, n);
1057 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1061 r = sd_bus_message_peek_type(m, &type, NULL);
1067 case SD_BUS_TYPE_STRING: {
1068 char **p = userdata;
1071 r = sd_bus_message_read_basic(m, type, &s);
1078 return free_and_strdup(p, s);
1081 case SD_BUS_TYPE_ARRAY: {
1082 _cleanup_strv_free_ char **l = NULL;
1083 char ***p = userdata;
1085 r = bus_message_read_strv_extend(m, &l);
1095 case SD_BUS_TYPE_BOOLEAN: {
1099 r = sd_bus_message_read_basic(m, type, &b);
1107 case SD_BUS_TYPE_INT32:
1108 case SD_BUS_TYPE_UINT32: {
1109 uint32_t u, *p = userdata;
1111 r = sd_bus_message_read_basic(m, type, &u);
1119 case SD_BUS_TYPE_INT64:
1120 case SD_BUS_TYPE_UINT64: {
1121 uint64_t t, *p = userdata;
1123 r = sd_bus_message_read_basic(m, type, &t);
1131 case SD_BUS_TYPE_DOUBLE: {
1132 double d, *p = userdata;
1134 r = sd_bus_message_read_basic(m, type, &d);
1145 int bus_message_map_all_properties(
1147 const struct bus_properties_map *map,
1148 sd_bus_error *error,
1156 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1160 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1161 const struct bus_properties_map *prop;
1163 const char *contents;
1167 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1171 for (i = 0, prop = NULL; map[i].member; i++)
1172 if (streq(map[i].member, member)) {
1178 r = sd_bus_message_peek_type(m, NULL, &contents);
1182 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1186 v = (uint8_t *)userdata + prop->offset;
1188 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1190 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1194 r = sd_bus_message_exit_container(m);
1198 r = sd_bus_message_skip(m, "v");
1203 r = sd_bus_message_exit_container(m);
1210 return sd_bus_message_exit_container(m);
1213 #if 0 /// UNNEEDED by elogind
1214 int bus_message_map_properties_changed(
1216 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, 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,
1257 sd_bus_error *error,
1260 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1264 assert(destination);
1268 r = sd_bus_call_method(
1272 "org.freedesktop.DBus.Properties",
1280 return bus_message_map_all_properties(m, map, error, userdata);
1283 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1284 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1287 assert(transport >= 0);
1288 assert(transport < _BUS_TRANSPORT_MAX);
1291 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1292 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1294 switch (transport) {
1296 case BUS_TRANSPORT_LOCAL:
1297 #if 0 /// elogind does not support a user bus
1299 r = sd_bus_default_user(&bus);
1302 r = sd_bus_default_system(&bus);
1306 case BUS_TRANSPORT_REMOTE:
1307 r = sd_bus_open_system_remote(&bus, host);
1310 case BUS_TRANSPORT_MACHINE:
1311 r = sd_bus_open_system_machine(&bus, host);
1315 assert_not_reached("Hmm, unknown transport type.");
1320 r = sd_bus_set_exit_on_disconnect(bus, true);
1330 #if 0 /// UNNEEDED by elogind
1331 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1334 assert(transport >= 0);
1335 assert(transport < _BUS_TRANSPORT_MAX);
1338 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1339 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1341 switch (transport) {
1343 case BUS_TRANSPORT_LOCAL:
1345 r = bus_connect_user_systemd(bus);
1347 r = bus_connect_system_systemd(bus);
1351 case BUS_TRANSPORT_REMOTE:
1352 r = sd_bus_open_system_remote(bus, host);
1355 case BUS_TRANSPORT_MACHINE:
1356 r = sd_bus_open_system_machine(bus, host);
1360 assert_not_reached("Hmm, unknown transport type.");
1367 int bus_property_get_bool(
1370 const char *interface,
1371 const char *property,
1372 sd_bus_message *reply,
1374 sd_bus_error *error) {
1376 int b = *(bool*) userdata;
1378 return sd_bus_message_append_basic(reply, 'b', &b);
1381 #if 0 /// UNNEEDED by elogind
1382 int bus_property_get_id128(
1385 const char *interface,
1386 const char *property,
1387 sd_bus_message *reply,
1389 sd_bus_error *error) {
1391 sd_id128_t *id = userdata;
1393 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1394 return sd_bus_message_append(reply, "ay", 0);
1396 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1400 #if __SIZEOF_SIZE_T__ != 8
1401 int bus_property_get_size(
1404 const char *interface,
1405 const char *property,
1406 sd_bus_message *reply,
1408 sd_bus_error *error) {
1410 uint64_t sz = *(size_t*) userdata;
1412 return sd_bus_message_append_basic(reply, 't', &sz);
1416 #if __SIZEOF_LONG__ != 8
1417 int bus_property_get_long(
1420 const char *interface,
1421 const char *property,
1422 sd_bus_message *reply,
1424 sd_bus_error *error) {
1426 int64_t l = *(long*) userdata;
1428 return sd_bus_message_append_basic(reply, 'x', &l);
1431 int bus_property_get_ulong(
1434 const char *interface,
1435 const char *property,
1436 sd_bus_message *reply,
1438 sd_bus_error *error) {
1440 uint64_t ul = *(unsigned long*) userdata;
1442 return sd_bus_message_append_basic(reply, 't', &ul);
1446 int bus_log_parse_error(int r) {
1447 return log_error_errno(r, "Failed to parse bus message: %m");
1450 #if 0 /// UNNEEDED by elogind
1451 int bus_log_create_error(int r) {
1452 return log_error_errno(r, "Failed to create bus message: %m");
1456 #if 0 /// UNNEEDED by elogind
1458 * bus_path_encode_unique() - encode unique object path
1459 * @b: bus connection or NULL
1460 * @prefix: object path prefix
1461 * @sender_id: unique-name of client, or NULL
1462 * @external_id: external ID to be chosen by client, or NULL
1463 * @ret_path: storage for encoded object path pointer
1465 * Whenever we provide a bus API that allows clients to create and manage
1466 * server-side objects, we need to provide a unique name for these objects. If
1467 * we let the server choose the name, we suffer from a race condition: If a
1468 * client creates an object asynchronously, it cannot destroy that object until
1469 * it received the method reply. It cannot know the name of the new object,
1470 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1472 * Therefore, many APIs allow the client to choose the unique name for newly
1473 * created objects. There're two problems to solve, though:
1474 * 1) Object names are usually defined via dbus object paths, which are
1475 * usually globally namespaced. Therefore, multiple clients must be able
1476 * to choose unique object names without interference.
1477 * 2) If multiple libraries share the same bus connection, they must be
1478 * able to choose unique object names without interference.
1479 * The first problem is solved easily by prefixing a name with the
1480 * unique-bus-name of a connection. The server side must enforce this and
1481 * reject any other name. The second problem is solved by providing unique
1482 * suffixes from within sd-bus.
1484 * This helper allows clients to create unique object-paths. It uses the
1485 * template '/prefix/sender_id/external_id' and returns the new path in
1486 * @ret_path (must be freed by the caller).
1487 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1488 * NULL, this function allocates a unique suffix via @b (by requesting a new
1489 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1492 * Returns: 0 on success, negative error code on failure.
1494 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1495 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1496 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1499 assert_return(b || (sender_id && external_id), -EINVAL);
1500 assert_return(object_path_is_valid(prefix), -EINVAL);
1501 assert_return(ret_path, -EINVAL);
1504 r = sd_bus_get_unique_name(b, &sender_id);
1510 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1511 external_id = external_buf;
1514 sender_label = bus_label_escape(sender_id);
1518 external_label = bus_label_escape(external_id);
1519 if (!external_label)
1522 p = strjoin(prefix, "/", sender_label, "/", external_label);
1531 * bus_path_decode_unique() - decode unique object path
1532 * @path: object path to decode
1533 * @prefix: object path prefix
1534 * @ret_sender: output parameter for sender-id label
1535 * @ret_external: output parameter for external-id label
1537 * This does the reverse of bus_path_encode_unique() (see its description for
1538 * details). Both trailing labels, sender-id and external-id, are unescaped and
1539 * returned in the given output parameters (the caller must free them).
1541 * Note that this function returns 0 if the path does not match the template
1542 * (see bus_path_encode_unique()), 1 if it matched.
1544 * Returns: Negative error code on failure, 0 if the given object path does not
1545 * match the template (return parameters are set to NULL), 1 if it was
1546 * parsed successfully (return parameters contain allocated labels).
1548 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1550 char *sender, *external;
1552 assert(object_path_is_valid(path));
1553 assert(object_path_is_valid(prefix));
1555 assert(ret_external);
1557 p = object_path_startswith(path, prefix);
1560 *ret_external = NULL;
1567 *ret_external = NULL;
1571 sender = bus_label_unescape_n(p, q - p);
1572 external = bus_label_unescape(q + 1);
1573 if (!sender || !external) {
1579 *ret_sender = sender;
1580 *ret_external = external;
1585 #if 0 /// UNNEEDED by elogind
1586 int bus_property_get_rlimit(
1589 const char *interface,
1590 const char *property,
1591 sd_bus_message *reply,
1593 sd_bus_error *error) {
1598 const char *is_soft;
1604 is_soft = endswith(property, "Soft");
1605 rl = *(struct rlimit**) userdata;
1607 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1609 struct rlimit buf = {};
1613 s = is_soft ? strndupa(property, is_soft - property) : property;
1615 z = rlimit_from_string(strstr(s, "Limit"));
1619 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1622 /* rlim_t might have different sizes, let's map
1623 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1625 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1627 return sd_bus_message_append(reply, "t", u);
1630 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1636 /* Continues adding after failure, and returns the first failure. */
1638 STRV_FOREACH(i, l) {
1641 k = sd_bus_track_add_name(t, *i);
1642 if (k < 0 && r >= 0)