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"
47 #include "parse-util.h"
48 #include "proc-cmdline.h"
49 //#include "rlimit-util.h"
50 #include "stdio-util.h"
52 #include "user-util.h"
54 #if 0 /// UNNEEDED by elogind
55 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
56 sd_event *e = userdata;
61 sd_bus_close(sd_bus_message_get_bus(m));
67 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
68 _cleanup_free_ char *match = NULL;
76 /* We unregister the name here and then wait for the
77 * NameOwnerChanged signal for this event to arrive before we
78 * quit. We do this in order to make sure that any queued
79 * requests are still processed before we really exit. */
81 r = sd_bus_get_unique_name(bus, &unique);
86 "sender='org.freedesktop.DBus',"
88 "interface='org.freedesktop.DBus',"
89 "member='NameOwnerChanged',"
90 "path='/org/freedesktop/DBus',"
93 "arg2=''", name, unique);
97 r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
101 r = sd_bus_release_name(bus, name);
108 int bus_event_loop_with_idle(
113 check_idle_t check_idle,
115 bool exiting = false;
125 r = sd_event_get_state(e);
128 if (r == SD_EVENT_FINISHED)
132 idle = check_idle(userdata);
136 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
140 if (r == 0 && !exiting && idle) {
142 r = sd_bus_try_close(bus);
146 /* Fallback for dbus1 connections: we
147 * unregister the name and wait for the
148 * response to come through for it */
149 if (r == -EOPNOTSUPP) {
151 /* Inform the service manager that we
152 * are going down, so that it will
153 * queue all further start requests,
154 * instead of assuming we are already
156 sd_notify(false, "STOPPING=1");
158 r = bus_async_unregister_and_exit(e, bus, name);
174 r = sd_event_get_exit_code(e, &code);
182 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
183 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
184 int r, has_owner = 0;
189 r = sd_bus_call_method(c,
190 "org.freedesktop.DBus",
191 "/org/freedesktop/dbus",
192 "org.freedesktop.DBus",
201 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
203 return sd_bus_error_set_errno(error, r);
208 static int check_good_user(sd_bus_message *m, uid_t good_user) {
209 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
215 if (good_user == UID_INVALID)
218 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
222 /* Don't trust augmented credentials for authorization */
223 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
225 r = sd_bus_creds_get_euid(creds, &sender_uid);
229 return sender_uid == good_user;
233 sd_bus_message *call,
236 const char **details,
246 /* Tests non-interactively! */
248 r = check_good_user(call, good_user);
252 r = sd_bus_query_sender_privilege(call, capability);
259 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
260 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
261 int authorized = false, challenge = false;
262 const char *sender, **k, **v;
264 sender = sd_bus_message_get_sender(call);
268 r = sd_bus_message_new_method_call(
271 "org.freedesktop.PolicyKit1",
272 "/org/freedesktop/PolicyKit1/Authority",
273 "org.freedesktop.PolicyKit1.Authority",
274 "CheckAuthorization");
278 r = sd_bus_message_append(
281 "system-bus-name", 1, "name", "s", sender,
286 r = sd_bus_message_open_container(request, 'a', "{ss}");
290 STRV_FOREACH_PAIR(k, v, details) {
291 r = sd_bus_message_append(request, "{ss}", *k, *v);
296 r = sd_bus_message_close_container(request);
300 r = sd_bus_message_append(request, "us", 0, NULL);
304 r = sd_bus_call(call->bus, request, 0, e, &reply);
306 /* Treat no PK available as access denied */
307 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
308 sd_bus_error_free(e);
315 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
319 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
327 *_challenge = challenge;
338 typedef struct AsyncPolkitQuery {
339 sd_bus_message *request, *reply;
340 sd_bus_message_handler_t callback;
346 static void async_polkit_query_free(AsyncPolkitQuery *q) {
351 sd_bus_slot_unref(q->slot);
353 if (q->registry && q->request)
354 hashmap_remove(q->registry, q->request);
356 sd_bus_message_unref(q->request);
357 sd_bus_message_unref(q->reply);
362 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
363 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
364 AsyncPolkitQuery *q = userdata;
370 q->slot = sd_bus_slot_unref(q->slot);
371 q->reply = sd_bus_message_ref(reply);
373 r = sd_bus_message_rewind(q->request, true);
375 r = sd_bus_reply_method_errno(q->request, r, NULL);
379 r = q->callback(q->request, q->userdata, &error_buffer);
380 r = bus_maybe_reply_error(q->request, r, &error_buffer);
383 async_polkit_query_free(q);
390 int bus_verify_polkit_async(
391 sd_bus_message *call,
394 const char **details,
398 sd_bus_error *error) {
401 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
403 const char *sender, **k, **v;
404 sd_bus_message_handler_t callback;
414 r = check_good_user(call, good_user);
419 q = hashmap_get(*registry, call);
421 int authorized, challenge;
423 /* This is the second invocation of this function, and
424 * there's already a response from polkit, let's
428 if (sd_bus_message_is_method_error(q->reply, NULL)) {
429 const sd_bus_error *e;
431 /* Copy error from polkit reply */
432 e = sd_bus_message_get_error(q->reply);
433 sd_bus_error_copy(error, e);
435 /* Treat no PK available as access denied */
436 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
439 return -sd_bus_error_get_errno(e);
442 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
444 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
453 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
459 r = sd_bus_query_sender_privilege(call, capability);
466 if (sd_bus_get_current_message(call->bus) != call)
469 callback = sd_bus_get_current_handler(call->bus);
473 userdata = sd_bus_get_current_userdata(call->bus);
475 sender = sd_bus_message_get_sender(call);
479 c = sd_bus_message_get_allow_interactive_authorization(call);
485 r = hashmap_ensure_allocated(registry, NULL);
489 r = sd_bus_message_new_method_call(
492 "org.freedesktop.PolicyKit1",
493 "/org/freedesktop/PolicyKit1/Authority",
494 "org.freedesktop.PolicyKit1.Authority",
495 "CheckAuthorization");
499 r = sd_bus_message_append(
502 "system-bus-name", 1, "name", "s", sender,
507 r = sd_bus_message_open_container(pk, 'a', "{ss}");
511 STRV_FOREACH_PAIR(k, v, details) {
512 r = sd_bus_message_append(pk, "{ss}", *k, *v);
517 r = sd_bus_message_close_container(pk);
521 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
525 q = new0(AsyncPolkitQuery, 1);
529 q->request = sd_bus_message_ref(call);
530 q->callback = callback;
531 q->userdata = userdata;
533 r = hashmap_put(*registry, call, q);
535 async_polkit_query_free(q);
539 q->registry = *registry;
541 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
543 async_polkit_query_free(q);
553 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
557 while ((q = hashmap_steal_first(registry)))
558 async_polkit_query_free(q);
560 hashmap_free(registry);
564 #if 0 /// UNNEEDED by elogind
565 int bus_check_peercred(sd_bus *c) {
572 fd = sd_bus_get_fd(c);
576 l = sizeof(struct ucred);
577 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
580 if (l != sizeof(struct ucred))
583 if (ucred.uid != 0 && ucred.uid != geteuid())
589 int bus_connect_system_systemd(sd_bus **_bus) {
590 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
596 return sd_bus_default_system(_bus);
598 /* If we are root and kdbus is not available, then let's talk
599 * directly to the system instance, instead of going via the
602 r = sd_bus_new(&bus);
606 r = sd_bus_set_address(bus, KERNEL_SYSTEM_BUS_ADDRESS);
610 bus->bus_client = true;
612 r = sd_bus_start(bus);
619 bus = sd_bus_unref(bus);
621 r = sd_bus_new(&bus);
625 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
629 r = sd_bus_start(bus);
631 return sd_bus_default_system(_bus);
633 r = bus_check_peercred(bus);
643 int bus_connect_user_systemd(sd_bus **_bus) {
644 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
645 _cleanup_free_ char *ee = NULL;
649 /* Try via kdbus first, and then directly */
653 r = sd_bus_new(&bus);
657 if (asprintf(&bus->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid()) < 0)
660 bus->bus_client = true;
662 r = sd_bus_start(bus);
669 bus = sd_bus_unref(bus);
671 e = secure_getenv("XDG_RUNTIME_DIR");
673 return sd_bus_default_user(_bus);
675 ee = bus_address_escape(e);
679 r = sd_bus_new(&bus);
683 bus->address = strjoin("unix:path=", ee, "/systemd/private");
687 r = sd_bus_start(bus);
689 return sd_bus_default_user(_bus);
691 r = bus_check_peercred(bus);
702 #define print_property(name, fmt, ...) \
705 printf(fmt "\n", __VA_ARGS__); \
707 printf("%s=" fmt "\n", name, __VA_ARGS__); \
710 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
712 const char *contents;
718 r = sd_bus_message_peek_type(property, &type, &contents);
724 case SD_BUS_TYPE_STRING: {
727 r = sd_bus_message_read_basic(property, type, &s);
731 if (all || !isempty(s)) {
734 /* This property has a single value, so we need to take
735 * care not to print a new line, everything else is OK. */
736 good = !strchr(s, '\n');
737 print_property(name, "%s", good ? s : "[unprintable]");
743 case SD_BUS_TYPE_BOOLEAN: {
746 r = sd_bus_message_read_basic(property, type, &b);
750 print_property(name, "%s", yes_no(b));
755 case SD_BUS_TYPE_UINT64: {
758 r = sd_bus_message_read_basic(property, type, &u);
762 /* Yes, heuristics! But we can change this check
763 * should it turn out to not be sufficient */
765 if (endswith(name, "Timestamp")) {
766 char timestamp[FORMAT_TIMESTAMP_MAX], *t;
768 t = format_timestamp(timestamp, sizeof(timestamp), u);
770 print_property(name, "%s", strempty(t));
772 } else if (strstr(name, "USec")) {
773 char timespan[FORMAT_TIMESPAN_MAX];
775 print_property(name, "%s", format_timespan(timespan, sizeof(timespan), u, 0));
776 } else if (streq(name, "RestrictNamespaces")) {
777 _cleanup_free_ char *s = NULL;
778 const char *result = NULL;
780 if ((u & NAMESPACE_FLAGS_ALL) == 0)
782 else if ((u & NAMESPACE_FLAGS_ALL) == NAMESPACE_FLAGS_ALL)
785 r = namespace_flag_to_string_many(u, &s);
792 print_property(name, "%s", result);
794 print_property(name, "%"PRIu64, u);
799 case SD_BUS_TYPE_INT64: {
802 r = sd_bus_message_read_basic(property, type, &i);
806 print_property(name, "%"PRIi64, i);
811 case SD_BUS_TYPE_UINT32: {
814 r = sd_bus_message_read_basic(property, type, &u);
818 if (strstr(name, "UMask") || strstr(name, "Mode"))
819 print_property(name, "%04o", u);
821 print_property(name, "%"PRIu32, u);
826 case SD_BUS_TYPE_INT32: {
829 r = sd_bus_message_read_basic(property, type, &i);
833 print_property(name, "%"PRIi32, i);
837 case SD_BUS_TYPE_DOUBLE: {
840 r = sd_bus_message_read_basic(property, type, &d);
844 print_property(name, "%g", d);
848 case SD_BUS_TYPE_ARRAY:
849 if (streq(contents, "s")) {
853 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
857 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
863 /* This property has multiple space-seperated values, so
864 * neither spaces not newlines can be allowed in a value. */
865 good = str[strcspn(str, " \n")] == '\0';
867 printf("%s%s", first ? "" : " ", good ? str : "[unprintable]");
874 if (first && all && !value)
879 r = sd_bus_message_exit_container(property);
885 } else if (streq(contents, "y")) {
889 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
899 for (i = 0; i < n; i++)
900 printf("%02x", u[i]);
907 } else if (streq(contents, "u")) {
911 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
921 for (i = 0; i < n; i++)
922 printf("%08x", u[i]);
936 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
937 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
938 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
944 r = sd_bus_call_method(bus,
947 "org.freedesktop.DBus.Properties",
955 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
959 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
961 const char *contents;
963 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
967 if (!filter || strv_find(filter, name)) {
968 r = sd_bus_message_peek_type(reply, NULL, &contents);
972 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
976 r = bus_print_property(name, reply, value, all);
981 printf("%s=[unprintable]\n", name);
982 /* skip what we didn't read */
983 r = sd_bus_message_skip(reply, contents);
988 r = sd_bus_message_exit_container(reply);
992 r = sd_bus_message_skip(reply, "v");
997 r = sd_bus_message_exit_container(reply);
1004 r = sd_bus_message_exit_container(reply);
1011 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1012 sd_id128_t *p = userdata;
1017 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1024 memcpy((*p).bytes, v, n);
1031 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1035 r = sd_bus_message_peek_type(m, &type, NULL);
1041 case SD_BUS_TYPE_STRING: {
1042 char **p = userdata;
1045 r = sd_bus_message_read_basic(m, type, &s);
1052 return free_and_strdup(p, s);
1055 case SD_BUS_TYPE_ARRAY: {
1056 _cleanup_strv_free_ char **l = NULL;
1057 char ***p = userdata;
1059 r = bus_message_read_strv_extend(m, &l);
1069 case SD_BUS_TYPE_BOOLEAN: {
1073 r = sd_bus_message_read_basic(m, type, &b);
1081 case SD_BUS_TYPE_INT32:
1082 case SD_BUS_TYPE_UINT32: {
1083 uint32_t u, *p = userdata;
1085 r = sd_bus_message_read_basic(m, type, &u);
1093 case SD_BUS_TYPE_INT64:
1094 case SD_BUS_TYPE_UINT64: {
1095 uint64_t t, *p = userdata;
1097 r = sd_bus_message_read_basic(m, type, &t);
1105 case SD_BUS_TYPE_DOUBLE: {
1106 double d, *p = userdata;
1108 r = sd_bus_message_read_basic(m, type, &d);
1119 int bus_message_map_all_properties(
1121 const struct bus_properties_map *map,
1122 sd_bus_error *error,
1130 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1134 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1135 const struct bus_properties_map *prop;
1137 const char *contents;
1141 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1145 for (i = 0, prop = NULL; map[i].member; i++)
1146 if (streq(map[i].member, member)) {
1152 r = sd_bus_message_peek_type(m, NULL, &contents);
1156 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1160 v = (uint8_t *)userdata + prop->offset;
1162 r = prop->set(sd_bus_message_get_bus(m), member, m, error, v);
1164 r = map_basic(sd_bus_message_get_bus(m), member, m, error, v);
1168 r = sd_bus_message_exit_container(m);
1172 r = sd_bus_message_skip(m, "v");
1177 r = sd_bus_message_exit_container(m);
1184 return sd_bus_message_exit_container(m);
1187 #if 0 /// UNNEEDED by elogind
1188 int bus_message_map_properties_changed(
1190 const struct bus_properties_map *map,
1191 sd_bus_error *error,
1195 int r, invalidated, i;
1200 r = bus_message_map_all_properties(m, map, error, userdata);
1204 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1209 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1210 for (i = 0; map[i].member; i++)
1211 if (streq(map[i].member, member)) {
1218 r = sd_bus_message_exit_container(m);
1226 int bus_map_all_properties(
1228 const char *destination,
1230 const struct bus_properties_map *map,
1231 sd_bus_error *error,
1234 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1238 assert(destination);
1242 r = sd_bus_call_method(
1246 "org.freedesktop.DBus.Properties",
1254 return bus_message_map_all_properties(m, map, error, userdata);
1257 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1258 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1261 assert(transport >= 0);
1262 assert(transport < _BUS_TRANSPORT_MAX);
1265 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1266 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1268 switch (transport) {
1270 case BUS_TRANSPORT_LOCAL:
1271 #if 0 /// elogind does not support a user bus
1273 r = sd_bus_default_user(&bus);
1276 r = sd_bus_default_system(&bus);
1280 case BUS_TRANSPORT_REMOTE:
1281 r = sd_bus_open_system_remote(&bus, host);
1284 case BUS_TRANSPORT_MACHINE:
1285 r = sd_bus_open_system_machine(&bus, host);
1289 assert_not_reached("Hmm, unknown transport type.");
1294 r = sd_bus_set_exit_on_disconnect(bus, true);
1304 #if 0 /// UNNEEDED by elogind
1305 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1308 assert(transport >= 0);
1309 assert(transport < _BUS_TRANSPORT_MAX);
1312 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1313 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1315 switch (transport) {
1317 case BUS_TRANSPORT_LOCAL:
1319 r = bus_connect_user_systemd(bus);
1321 r = bus_connect_system_systemd(bus);
1325 case BUS_TRANSPORT_REMOTE:
1326 r = sd_bus_open_system_remote(bus, host);
1329 case BUS_TRANSPORT_MACHINE:
1330 r = sd_bus_open_system_machine(bus, host);
1334 assert_not_reached("Hmm, unknown transport type.");
1341 int bus_property_get_bool(
1344 const char *interface,
1345 const char *property,
1346 sd_bus_message *reply,
1348 sd_bus_error *error) {
1350 int b = *(bool*) userdata;
1352 return sd_bus_message_append_basic(reply, 'b', &b);
1355 #if 0 /// UNNEEDED by elogind
1356 int bus_property_get_id128(
1359 const char *interface,
1360 const char *property,
1361 sd_bus_message *reply,
1363 sd_bus_error *error) {
1365 sd_id128_t *id = userdata;
1367 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1368 return sd_bus_message_append(reply, "ay", 0);
1370 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1374 #if __SIZEOF_SIZE_T__ != 8
1375 int bus_property_get_size(
1378 const char *interface,
1379 const char *property,
1380 sd_bus_message *reply,
1382 sd_bus_error *error) {
1384 uint64_t sz = *(size_t*) userdata;
1386 return sd_bus_message_append_basic(reply, 't', &sz);
1390 #if __SIZEOF_LONG__ != 8
1391 int bus_property_get_long(
1394 const char *interface,
1395 const char *property,
1396 sd_bus_message *reply,
1398 sd_bus_error *error) {
1400 int64_t l = *(long*) userdata;
1402 return sd_bus_message_append_basic(reply, 'x', &l);
1405 int bus_property_get_ulong(
1408 const char *interface,
1409 const char *property,
1410 sd_bus_message *reply,
1412 sd_bus_error *error) {
1414 uint64_t ul = *(unsigned long*) userdata;
1416 return sd_bus_message_append_basic(reply, 't', &ul);
1420 int bus_log_parse_error(int r) {
1421 return log_error_errno(r, "Failed to parse bus message: %m");
1424 #if 0 /// UNNEEDED by elogind
1425 int bus_log_create_error(int r) {
1426 return log_error_errno(r, "Failed to create bus message: %m");
1430 #if 0 /// UNNEEDED by elogind
1432 * bus_path_encode_unique() - encode unique object path
1433 * @b: bus connection or NULL
1434 * @prefix: object path prefix
1435 * @sender_id: unique-name of client, or NULL
1436 * @external_id: external ID to be chosen by client, or NULL
1437 * @ret_path: storage for encoded object path pointer
1439 * Whenever we provide a bus API that allows clients to create and manage
1440 * server-side objects, we need to provide a unique name for these objects. If
1441 * we let the server choose the name, we suffer from a race condition: If a
1442 * client creates an object asynchronously, it cannot destroy that object until
1443 * it received the method reply. It cannot know the name of the new object,
1444 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1446 * Therefore, many APIs allow the client to choose the unique name for newly
1447 * created objects. There're two problems to solve, though:
1448 * 1) Object names are usually defined via dbus object paths, which are
1449 * usually globally namespaced. Therefore, multiple clients must be able
1450 * to choose unique object names without interference.
1451 * 2) If multiple libraries share the same bus connection, they must be
1452 * able to choose unique object names without interference.
1453 * The first problem is solved easily by prefixing a name with the
1454 * unique-bus-name of a connection. The server side must enforce this and
1455 * reject any other name. The second problem is solved by providing unique
1456 * suffixes from within sd-bus.
1458 * This helper allows clients to create unique object-paths. It uses the
1459 * template '/prefix/sender_id/external_id' and returns the new path in
1460 * @ret_path (must be freed by the caller).
1461 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1462 * NULL, this function allocates a unique suffix via @b (by requesting a new
1463 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1466 * Returns: 0 on success, negative error code on failure.
1468 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1469 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1470 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1473 assert_return(b || (sender_id && external_id), -EINVAL);
1474 assert_return(object_path_is_valid(prefix), -EINVAL);
1475 assert_return(ret_path, -EINVAL);
1478 r = sd_bus_get_unique_name(b, &sender_id);
1484 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1485 external_id = external_buf;
1488 sender_label = bus_label_escape(sender_id);
1492 external_label = bus_label_escape(external_id);
1493 if (!external_label)
1496 p = strjoin(prefix, "/", sender_label, "/", external_label);
1505 * bus_path_decode_unique() - decode unique object path
1506 * @path: object path to decode
1507 * @prefix: object path prefix
1508 * @ret_sender: output parameter for sender-id label
1509 * @ret_external: output parameter for external-id label
1511 * This does the reverse of bus_path_encode_unique() (see its description for
1512 * details). Both trailing labels, sender-id and external-id, are unescaped and
1513 * returned in the given output parameters (the caller must free them).
1515 * Note that this function returns 0 if the path does not match the template
1516 * (see bus_path_encode_unique()), 1 if it matched.
1518 * Returns: Negative error code on failure, 0 if the given object path does not
1519 * match the template (return parameters are set to NULL), 1 if it was
1520 * parsed successfully (return parameters contain allocated labels).
1522 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1524 char *sender, *external;
1526 assert(object_path_is_valid(path));
1527 assert(object_path_is_valid(prefix));
1529 assert(ret_external);
1531 p = object_path_startswith(path, prefix);
1534 *ret_external = NULL;
1541 *ret_external = NULL;
1545 sender = bus_label_unescape_n(p, q - p);
1546 external = bus_label_unescape(q + 1);
1547 if (!sender || !external) {
1553 *ret_sender = sender;
1554 *ret_external = external;
1559 #if 0 /// UNNEEDED by elogind
1560 int bus_property_get_rlimit(
1563 const char *interface,
1564 const char *property,
1565 sd_bus_message *reply,
1567 sd_bus_error *error) {
1572 const char *is_soft;
1578 is_soft = endswith(property, "Soft");
1579 rl = *(struct rlimit**) userdata;
1581 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1583 struct rlimit buf = {};
1587 s = is_soft ? strndupa(property, is_soft - property) : property;
1589 z = rlimit_from_string(strstr(s, "Limit"));
1593 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1596 /* rlim_t might have different sizes, let's map
1597 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1599 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1601 return sd_bus_message_append(reply, "t", u);
1604 int bus_track_add_name_many(sd_bus_track *t, char **l) {
1610 /* Continues adding after failure, and returns the first failure. */
1612 STRV_FOREACH(i, l) {
1615 k = sd_bus_track_add_name(t, *i);
1616 if (k < 0 && r >= 0)