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"
46 #include "parse-util.h"
47 #include "proc-cmdline.h"
48 //#include "rlimit-util.h"
49 #include "stdio-util.h"
51 #include "user-util.h"
53 #if 0 /// UNNEEDED by elogind
54 static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) {
55 sd_event *e = userdata;
60 sd_bus_close(sd_bus_message_get_bus(m));
66 int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name) {
67 _cleanup_free_ char *match = NULL;
75 /* We unregister the name here and then wait for the
76 * NameOwnerChanged signal for this event to arrive before we
77 * quit. We do this in order to make sure that any queued
78 * requests are still processed before we really exit. */
80 r = sd_bus_get_unique_name(bus, &unique);
85 "sender='org.freedesktop.DBus',"
87 "interface='org.freedesktop.DBus',"
88 "member='NameOwnerChanged',"
89 "path='/org/freedesktop/DBus',"
92 "arg2=''", name, unique);
96 r = sd_bus_add_match(bus, NULL, match, name_owner_change_callback, e);
100 r = sd_bus_release_name(bus, name);
107 int bus_event_loop_with_idle(
112 check_idle_t check_idle,
114 bool exiting = false;
124 r = sd_event_get_state(e);
127 if (r == SD_EVENT_FINISHED)
131 idle = check_idle(userdata);
135 r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
139 if (r == 0 && !exiting && idle) {
141 r = sd_bus_try_close(bus);
145 /* Fallback for dbus1 connections: we
146 * unregister the name and wait for the
147 * response to come through for it */
148 if (r == -EOPNOTSUPP) {
150 /* Inform the service manager that we
151 * are going down, so that it will
152 * queue all further start requests,
153 * instead of assuming we are already
155 sd_notify(false, "STOPPING=1");
157 r = bus_async_unregister_and_exit(e, bus, name);
173 r = sd_event_get_exit_code(e, &code);
181 int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error) {
182 _cleanup_(sd_bus_message_unrefp) sd_bus_message *rep = NULL;
183 int r, has_owner = 0;
188 r = sd_bus_call_method(c,
189 "org.freedesktop.DBus",
190 "/org/freedesktop/dbus",
191 "org.freedesktop.DBus",
200 r = sd_bus_message_read_basic(rep, 'b', &has_owner);
202 return sd_bus_error_set_errno(error, r);
207 static int check_good_user(sd_bus_message *m, uid_t good_user) {
208 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
214 if (good_user == UID_INVALID)
217 r = sd_bus_query_sender_creds(m, SD_BUS_CREDS_EUID, &creds);
221 /* Don't trust augmented credentials for authorization */
222 assert_return((sd_bus_creds_get_augmented_mask(creds) & SD_BUS_CREDS_EUID) == 0, -EPERM);
224 r = sd_bus_creds_get_euid(creds, &sender_uid);
228 return sender_uid == good_user;
232 sd_bus_message *call,
235 const char **details,
245 /* Tests non-interactively! */
247 r = check_good_user(call, good_user);
251 r = sd_bus_query_sender_privilege(call, capability);
258 _cleanup_(sd_bus_message_unrefp) sd_bus_message *request = NULL;
259 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
260 int authorized = false, challenge = false;
261 const char *sender, **k, **v;
263 sender = sd_bus_message_get_sender(call);
267 r = sd_bus_message_new_method_call(
270 "org.freedesktop.PolicyKit1",
271 "/org/freedesktop/PolicyKit1/Authority",
272 "org.freedesktop.PolicyKit1.Authority",
273 "CheckAuthorization");
277 r = sd_bus_message_append(
280 "system-bus-name", 1, "name", "s", sender,
285 r = sd_bus_message_open_container(request, 'a', "{ss}");
289 STRV_FOREACH_PAIR(k, v, details) {
290 r = sd_bus_message_append(request, "{ss}", *k, *v);
295 r = sd_bus_message_close_container(request);
299 r = sd_bus_message_append(request, "us", 0, NULL);
303 r = sd_bus_call(call->bus, request, 0, e, &reply);
305 /* Treat no PK available as access denied */
306 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN)) {
307 sd_bus_error_free(e);
314 r = sd_bus_message_enter_container(reply, 'r', "bba{ss}");
318 r = sd_bus_message_read(reply, "bb", &authorized, &challenge);
326 *_challenge = challenge;
337 typedef struct AsyncPolkitQuery {
338 sd_bus_message *request, *reply;
339 sd_bus_message_handler_t callback;
345 static void async_polkit_query_free(AsyncPolkitQuery *q) {
350 sd_bus_slot_unref(q->slot);
352 if (q->registry && q->request)
353 hashmap_remove(q->registry, q->request);
355 sd_bus_message_unref(q->request);
356 sd_bus_message_unref(q->reply);
361 static int async_polkit_callback(sd_bus_message *reply, void *userdata, sd_bus_error *error) {
362 _cleanup_(sd_bus_error_free) sd_bus_error error_buffer = SD_BUS_ERROR_NULL;
363 AsyncPolkitQuery *q = userdata;
369 q->slot = sd_bus_slot_unref(q->slot);
370 q->reply = sd_bus_message_ref(reply);
372 r = sd_bus_message_rewind(q->request, true);
374 r = sd_bus_reply_method_errno(q->request, r, NULL);
378 r = q->callback(q->request, q->userdata, &error_buffer);
379 r = bus_maybe_reply_error(q->request, r, &error_buffer);
382 async_polkit_query_free(q);
389 int bus_verify_polkit_async(
390 sd_bus_message *call,
393 const char **details,
397 sd_bus_error *error) {
400 _cleanup_(sd_bus_message_unrefp) sd_bus_message *pk = NULL;
402 const char *sender, **k, **v;
403 sd_bus_message_handler_t callback;
413 r = check_good_user(call, good_user);
418 q = hashmap_get(*registry, call);
420 int authorized, challenge;
422 /* This is the second invocation of this function, and
423 * there's already a response from polkit, let's
427 if (sd_bus_message_is_method_error(q->reply, NULL)) {
428 const sd_bus_error *e;
430 /* Copy error from polkit reply */
431 e = sd_bus_message_get_error(q->reply);
432 sd_bus_error_copy(error, e);
434 /* Treat no PK available as access denied */
435 if (sd_bus_error_has_name(e, SD_BUS_ERROR_SERVICE_UNKNOWN))
438 return -sd_bus_error_get_errno(e);
441 r = sd_bus_message_enter_container(q->reply, 'r', "bba{ss}");
443 r = sd_bus_message_read(q->reply, "bb", &authorized, &challenge);
452 return sd_bus_error_set(error, SD_BUS_ERROR_INTERACTIVE_AUTHORIZATION_REQUIRED, "Interactive authentication required.");
458 r = sd_bus_query_sender_privilege(call, capability);
465 if (sd_bus_get_current_message(call->bus) != call)
468 callback = sd_bus_get_current_handler(call->bus);
472 userdata = sd_bus_get_current_userdata(call->bus);
474 sender = sd_bus_message_get_sender(call);
478 c = sd_bus_message_get_allow_interactive_authorization(call);
484 r = hashmap_ensure_allocated(registry, NULL);
488 r = sd_bus_message_new_method_call(
491 "org.freedesktop.PolicyKit1",
492 "/org/freedesktop/PolicyKit1/Authority",
493 "org.freedesktop.PolicyKit1.Authority",
494 "CheckAuthorization");
498 r = sd_bus_message_append(
501 "system-bus-name", 1, "name", "s", sender,
506 r = sd_bus_message_open_container(pk, 'a', "{ss}");
510 STRV_FOREACH_PAIR(k, v, details) {
511 r = sd_bus_message_append(pk, "{ss}", *k, *v);
516 r = sd_bus_message_close_container(pk);
520 r = sd_bus_message_append(pk, "us", !!interactive, NULL);
524 q = new0(AsyncPolkitQuery, 1);
528 q->request = sd_bus_message_ref(call);
529 q->callback = callback;
530 q->userdata = userdata;
532 r = hashmap_put(*registry, call, q);
534 async_polkit_query_free(q);
538 q->registry = *registry;
540 r = sd_bus_call_async(call->bus, &q->slot, pk, async_polkit_callback, q, 0);
542 async_polkit_query_free(q);
552 void bus_verify_polkit_async_registry_free(Hashmap *registry) {
556 while ((q = hashmap_steal_first(registry)))
557 async_polkit_query_free(q);
559 hashmap_free(registry);
563 #if 0 /// UNNEEDED by elogind
564 int bus_check_peercred(sd_bus *c) {
571 fd = sd_bus_get_fd(c);
575 l = sizeof(struct ucred);
576 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &l) < 0)
579 if (l != sizeof(struct ucred))
582 if (ucred.uid != 0 && ucred.uid != geteuid())
588 int bus_connect_system_systemd(sd_bus **_bus) {
589 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
595 return sd_bus_default_system(_bus);
597 /* If we are root and kdbus is not available, then let's talk
598 * directly to the system instance, instead of going via the
601 r = sd_bus_new(&bus);
605 r = sd_bus_set_address(bus, KERNEL_SYSTEM_BUS_ADDRESS);
609 bus->bus_client = true;
611 r = sd_bus_start(bus);
618 bus = sd_bus_unref(bus);
620 r = sd_bus_new(&bus);
624 r = sd_bus_set_address(bus, "unix:path=/run/systemd/private");
628 r = sd_bus_start(bus);
630 return sd_bus_default_system(_bus);
632 r = bus_check_peercred(bus);
642 int bus_connect_user_systemd(sd_bus **_bus) {
643 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
644 _cleanup_free_ char *ee = NULL;
648 /* Try via kdbus first, and then directly */
652 r = sd_bus_new(&bus);
656 if (asprintf(&bus->address, KERNEL_USER_BUS_ADDRESS_FMT, getuid()) < 0)
659 bus->bus_client = true;
661 r = sd_bus_start(bus);
668 bus = sd_bus_unref(bus);
670 e = secure_getenv("XDG_RUNTIME_DIR");
672 return sd_bus_default_user(_bus);
674 ee = bus_address_escape(e);
678 r = sd_bus_new(&bus);
682 bus->address = strjoin("unix:path=", ee, "/systemd/private");
686 r = sd_bus_start(bus);
688 return sd_bus_default_user(_bus);
690 r = bus_check_peercred(bus);
701 #define print_property(name, fmt, ...) \
704 printf(fmt "\n", __VA_ARGS__); \
706 printf("%s=" fmt "\n", name, __VA_ARGS__); \
709 int bus_print_property(const char *name, sd_bus_message *property, bool value, bool all) {
711 const char *contents;
717 r = sd_bus_message_peek_type(property, &type, &contents);
723 case SD_BUS_TYPE_STRING: {
726 r = sd_bus_message_read_basic(property, type, &s);
730 if (all || !isempty(s)) {
731 _cleanup_free_ char *escaped = NULL;
733 escaped = xescape(s, "\n");
737 print_property(name, "%s", escaped);
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) {
858 _cleanup_free_ char *escaped = NULL;
863 escaped = xescape(str, "\n ");
867 printf("%s%s", first ? "" : " ", escaped);
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,
1124 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
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,
1194 int r, invalidated, i;
1199 r = bus_message_map_all_properties(m, map, userdata);
1203 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1208 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1209 for (i = 0; map[i].member; i++)
1210 if (streq(map[i].member, member)) {
1217 r = sd_bus_message_exit_container(m);
1225 int bus_map_all_properties(
1227 const char *destination,
1229 const struct bus_properties_map *map,
1232 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1233 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1237 assert(destination);
1241 r = sd_bus_call_method(
1245 "org.freedesktop.DBus.Properties",
1253 return bus_message_map_all_properties(m, map, userdata);
1256 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1257 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1260 assert(transport >= 0);
1261 assert(transport < _BUS_TRANSPORT_MAX);
1264 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1265 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1267 switch (transport) {
1269 case BUS_TRANSPORT_LOCAL:
1270 #if 0 /// elogind does not support a user bus
1272 r = sd_bus_default_user(&bus);
1275 r = sd_bus_default_system(&bus);
1279 case BUS_TRANSPORT_REMOTE:
1280 r = sd_bus_open_system_remote(&bus, host);
1283 case BUS_TRANSPORT_MACHINE:
1284 r = sd_bus_open_system_machine(&bus, host);
1288 assert_not_reached("Hmm, unknown transport type.");
1293 r = sd_bus_set_exit_on_disconnect(bus, true);
1303 #if 0 /// UNNEEDED by elogind
1304 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1307 assert(transport >= 0);
1308 assert(transport < _BUS_TRANSPORT_MAX);
1311 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1312 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1314 switch (transport) {
1316 case BUS_TRANSPORT_LOCAL:
1318 r = bus_connect_user_systemd(bus);
1320 r = bus_connect_system_systemd(bus);
1324 case BUS_TRANSPORT_REMOTE:
1325 r = sd_bus_open_system_remote(bus, host);
1328 case BUS_TRANSPORT_MACHINE:
1329 r = sd_bus_open_system_machine(bus, host);
1333 assert_not_reached("Hmm, unknown transport type.");
1340 int bus_property_get_bool(
1343 const char *interface,
1344 const char *property,
1345 sd_bus_message *reply,
1347 sd_bus_error *error) {
1349 int b = *(bool*) userdata;
1351 return sd_bus_message_append_basic(reply, 'b', &b);
1354 #if 0 /// UNNEEDED by elogind
1355 int bus_property_get_id128(
1358 const char *interface,
1359 const char *property,
1360 sd_bus_message *reply,
1362 sd_bus_error *error) {
1364 sd_id128_t *id = userdata;
1366 if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */
1367 return sd_bus_message_append(reply, "ay", 0);
1369 return sd_bus_message_append_array(reply, 'y', id->bytes, 16);
1373 #if __SIZEOF_SIZE_T__ != 8
1374 int bus_property_get_size(
1377 const char *interface,
1378 const char *property,
1379 sd_bus_message *reply,
1381 sd_bus_error *error) {
1383 uint64_t sz = *(size_t*) userdata;
1385 return sd_bus_message_append_basic(reply, 't', &sz);
1389 #if __SIZEOF_LONG__ != 8
1390 int bus_property_get_long(
1393 const char *interface,
1394 const char *property,
1395 sd_bus_message *reply,
1397 sd_bus_error *error) {
1399 int64_t l = *(long*) userdata;
1401 return sd_bus_message_append_basic(reply, 'x', &l);
1404 int bus_property_get_ulong(
1407 const char *interface,
1408 const char *property,
1409 sd_bus_message *reply,
1411 sd_bus_error *error) {
1413 uint64_t ul = *(unsigned long*) userdata;
1415 return sd_bus_message_append_basic(reply, 't', &ul);
1419 int bus_log_parse_error(int r) {
1420 return log_error_errno(r, "Failed to parse bus message: %m");
1423 #if 0 /// UNNEEDED by elogind
1424 int bus_log_create_error(int r) {
1425 return log_error_errno(r, "Failed to create bus message: %m");
1429 #if 0 /// UNNEEDED by elogind
1431 * bus_path_encode_unique() - encode unique object path
1432 * @b: bus connection or NULL
1433 * @prefix: object path prefix
1434 * @sender_id: unique-name of client, or NULL
1435 * @external_id: external ID to be chosen by client, or NULL
1436 * @ret_path: storage for encoded object path pointer
1438 * Whenever we provide a bus API that allows clients to create and manage
1439 * server-side objects, we need to provide a unique name for these objects. If
1440 * we let the server choose the name, we suffer from a race condition: If a
1441 * client creates an object asynchronously, it cannot destroy that object until
1442 * it received the method reply. It cannot know the name of the new object,
1443 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1445 * Therefore, many APIs allow the client to choose the unique name for newly
1446 * created objects. There're two problems to solve, though:
1447 * 1) Object names are usually defined via dbus object paths, which are
1448 * usually globally namespaced. Therefore, multiple clients must be able
1449 * to choose unique object names without interference.
1450 * 2) If multiple libraries share the same bus connection, they must be
1451 * able to choose unique object names without interference.
1452 * The first problem is solved easily by prefixing a name with the
1453 * unique-bus-name of a connection. The server side must enforce this and
1454 * reject any other name. The second problem is solved by providing unique
1455 * suffixes from within sd-bus.
1457 * This helper allows clients to create unique object-paths. It uses the
1458 * template '/prefix/sender_id/external_id' and returns the new path in
1459 * @ret_path (must be freed by the caller).
1460 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1461 * NULL, this function allocates a unique suffix via @b (by requesting a new
1462 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1465 * Returns: 0 on success, negative error code on failure.
1467 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1468 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1469 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1472 assert_return(b || (sender_id && external_id), -EINVAL);
1473 assert_return(object_path_is_valid(prefix), -EINVAL);
1474 assert_return(ret_path, -EINVAL);
1477 r = sd_bus_get_unique_name(b, &sender_id);
1483 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1484 external_id = external_buf;
1487 sender_label = bus_label_escape(sender_id);
1491 external_label = bus_label_escape(external_id);
1492 if (!external_label)
1495 p = strjoin(prefix, "/", sender_label, "/", external_label);
1504 * bus_path_decode_unique() - decode unique object path
1505 * @path: object path to decode
1506 * @prefix: object path prefix
1507 * @ret_sender: output parameter for sender-id label
1508 * @ret_external: output parameter for external-id label
1510 * This does the reverse of bus_path_encode_unique() (see its description for
1511 * details). Both trailing labels, sender-id and external-id, are unescaped and
1512 * returned in the given output parameters (the caller must free them).
1514 * Note that this function returns 0 if the path does not match the template
1515 * (see bus_path_encode_unique()), 1 if it matched.
1517 * Returns: Negative error code on failure, 0 if the given object path does not
1518 * match the template (return parameters are set to NULL), 1 if it was
1519 * parsed successfully (return parameters contain allocated labels).
1521 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1523 char *sender, *external;
1525 assert(object_path_is_valid(path));
1526 assert(object_path_is_valid(prefix));
1528 assert(ret_external);
1530 p = object_path_startswith(path, prefix);
1533 *ret_external = NULL;
1540 *ret_external = NULL;
1544 sender = bus_label_unescape_n(p, q - p);
1545 external = bus_label_unescape(q + 1);
1546 if (!sender || !external) {
1552 *ret_sender = sender;
1553 *ret_external = external;
1558 #if 0 /// UNNEEDED by elogind
1559 int bus_property_get_rlimit(
1562 const char *interface,
1563 const char *property,
1564 sd_bus_message *reply,
1566 sd_bus_error *error) {
1571 const char *is_soft;
1577 is_soft = endswith(property, "Soft");
1578 rl = *(struct rlimit**) userdata;
1580 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1582 struct rlimit buf = {};
1586 s = is_soft ? strndupa(property, is_soft - property) : property;
1588 z = rlimit_from_string(strstr(s, "Limit"));
1592 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1595 /* rlim_t might have different sizes, let's map
1596 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1598 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1600 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)