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", NULL);
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));
777 print_property(name, "%"PRIu64, u);
782 case SD_BUS_TYPE_INT64: {
785 r = sd_bus_message_read_basic(property, type, &i);
789 print_property(name, "%"PRIi64, i);
794 case SD_BUS_TYPE_UINT32: {
797 r = sd_bus_message_read_basic(property, type, &u);
801 if (strstr(name, "UMask") || strstr(name, "Mode"))
802 print_property(name, "%04o", u);
804 print_property(name, "%"PRIu32, u);
809 case SD_BUS_TYPE_INT32: {
812 r = sd_bus_message_read_basic(property, type, &i);
816 print_property(name, "%"PRIi32, i);
820 case SD_BUS_TYPE_DOUBLE: {
823 r = sd_bus_message_read_basic(property, type, &d);
827 print_property(name, "%g", d);
831 case SD_BUS_TYPE_ARRAY:
832 if (streq(contents, "s")) {
836 r = sd_bus_message_enter_container(property, SD_BUS_TYPE_ARRAY, contents);
840 while ((r = sd_bus_message_read_basic(property, SD_BUS_TYPE_STRING, &str)) > 0) {
841 _cleanup_free_ char *escaped = NULL;
846 escaped = xescape(str, "\n ");
850 printf("%s%s", first ? "" : " ", escaped);
857 if (first && all && !value)
862 r = sd_bus_message_exit_container(property);
868 } else if (streq(contents, "y")) {
872 r = sd_bus_message_read_array(property, SD_BUS_TYPE_BYTE, (const void**) &u, &n);
882 for (i = 0; i < n; i++)
883 printf("%02x", u[i]);
890 } else if (streq(contents, "u")) {
894 r = sd_bus_message_read_array(property, SD_BUS_TYPE_UINT32, (const void**) &u, &n);
904 for (i = 0; i < n; i++)
905 printf("%08x", u[i]);
919 int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, char **filter, bool value, bool all) {
920 _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL;
921 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
927 r = sd_bus_call_method(bus,
930 "org.freedesktop.DBus.Properties",
938 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_ARRAY, "{sv}");
942 while ((r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
944 const char *contents;
946 r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_STRING, &name);
950 if (!filter || strv_find(filter, name)) {
951 r = sd_bus_message_peek_type(reply, NULL, &contents);
955 r = sd_bus_message_enter_container(reply, SD_BUS_TYPE_VARIANT, contents);
959 r = bus_print_property(name, reply, value, all);
964 printf("%s=[unprintable]\n", name);
965 /* skip what we didn't read */
966 r = sd_bus_message_skip(reply, contents);
971 r = sd_bus_message_exit_container(reply);
975 r = sd_bus_message_skip(reply, "v");
980 r = sd_bus_message_exit_container(reply);
987 r = sd_bus_message_exit_container(reply);
994 int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
995 sd_id128_t *p = userdata;
1000 r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n);
1007 memcpy((*p).bytes, v, n);
1014 static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
1018 r = sd_bus_message_peek_type(m, &type, NULL);
1024 case SD_BUS_TYPE_STRING: {
1025 char **p = userdata;
1028 r = sd_bus_message_read_basic(m, type, &s);
1035 return free_and_strdup(p, s);
1038 case SD_BUS_TYPE_ARRAY: {
1039 _cleanup_strv_free_ char **l = NULL;
1040 char ***p = userdata;
1042 r = bus_message_read_strv_extend(m, &l);
1052 case SD_BUS_TYPE_BOOLEAN: {
1056 r = sd_bus_message_read_basic(m, type, &b);
1064 case SD_BUS_TYPE_INT32:
1065 case SD_BUS_TYPE_UINT32: {
1066 uint32_t u, *p = userdata;
1068 r = sd_bus_message_read_basic(m, type, &u);
1076 case SD_BUS_TYPE_INT64:
1077 case SD_BUS_TYPE_UINT64: {
1078 uint64_t t, *p = userdata;
1080 r = sd_bus_message_read_basic(m, type, &t);
1088 case SD_BUS_TYPE_DOUBLE: {
1089 double d, *p = userdata;
1091 r = sd_bus_message_read_basic(m, type, &d);
1102 int bus_message_map_all_properties(
1104 const struct bus_properties_map *map,
1107 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1113 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}");
1117 while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) {
1118 const struct bus_properties_map *prop;
1120 const char *contents;
1124 r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member);
1128 for (i = 0, prop = NULL; map[i].member; i++)
1129 if (streq(map[i].member, member)) {
1135 r = sd_bus_message_peek_type(m, NULL, &contents);
1139 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents);
1143 v = (uint8_t *)userdata + prop->offset;
1145 r = prop->set(sd_bus_message_get_bus(m), member, m, &error, v);
1147 r = map_basic(sd_bus_message_get_bus(m), member, m, &error, v);
1151 r = sd_bus_message_exit_container(m);
1155 r = sd_bus_message_skip(m, "v");
1160 r = sd_bus_message_exit_container(m);
1167 return sd_bus_message_exit_container(m);
1170 #if 0 /// UNNEEDED by elogind
1171 int bus_message_map_properties_changed(
1173 const struct bus_properties_map *map,
1177 int r, invalidated, i;
1182 r = bus_message_map_all_properties(m, map, userdata);
1186 r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "s");
1191 while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member)) > 0)
1192 for (i = 0; map[i].member; i++)
1193 if (streq(map[i].member, member)) {
1200 r = sd_bus_message_exit_container(m);
1208 int bus_map_all_properties(
1210 const char *destination,
1212 const struct bus_properties_map *map,
1215 _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
1216 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1220 assert(destination);
1224 r = sd_bus_call_method(
1228 "org.freedesktop.DBus.Properties",
1236 return bus_message_map_all_properties(m, map, userdata);
1239 int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) {
1240 _cleanup_(sd_bus_unrefp) sd_bus *bus = NULL;
1243 assert(transport >= 0);
1244 assert(transport < _BUS_TRANSPORT_MAX);
1247 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1248 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1250 switch (transport) {
1252 case BUS_TRANSPORT_LOCAL:
1253 #if 0 /// elogind does not support a user bus
1255 r = sd_bus_default_user(&bus);
1258 r = sd_bus_default_system(&bus);
1262 case BUS_TRANSPORT_REMOTE:
1263 r = sd_bus_open_system_remote(&bus, host);
1266 case BUS_TRANSPORT_MACHINE:
1267 r = sd_bus_open_system_machine(&bus, host);
1271 assert_not_reached("Hmm, unknown transport type.");
1276 r = sd_bus_set_exit_on_disconnect(bus, true);
1286 #if 0 /// UNNEEDED by elogind
1287 int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus) {
1290 assert(transport >= 0);
1291 assert(transport < _BUS_TRANSPORT_MAX);
1294 assert_return((transport == BUS_TRANSPORT_LOCAL) == !host, -EINVAL);
1295 assert_return(transport == BUS_TRANSPORT_LOCAL || !user, -EOPNOTSUPP);
1297 switch (transport) {
1299 case BUS_TRANSPORT_LOCAL:
1301 r = bus_connect_user_systemd(bus);
1303 r = bus_connect_system_systemd(bus);
1307 case BUS_TRANSPORT_REMOTE:
1308 r = sd_bus_open_system_remote(bus, host);
1311 case BUS_TRANSPORT_MACHINE:
1312 r = sd_bus_open_system_machine(bus, host);
1316 assert_not_reached("Hmm, unknown transport type.");
1323 int bus_property_get_bool(
1326 const char *interface,
1327 const char *property,
1328 sd_bus_message *reply,
1330 sd_bus_error *error) {
1332 int b = *(bool*) userdata;
1334 return sd_bus_message_append_basic(reply, 'b', &b);
1337 #if __SIZEOF_SIZE_T__ != 8
1338 int bus_property_get_size(
1341 const char *interface,
1342 const char *property,
1343 sd_bus_message *reply,
1345 sd_bus_error *error) {
1347 uint64_t sz = *(size_t*) userdata;
1349 return sd_bus_message_append_basic(reply, 't', &sz);
1353 #if __SIZEOF_LONG__ != 8
1354 int bus_property_get_long(
1357 const char *interface,
1358 const char *property,
1359 sd_bus_message *reply,
1361 sd_bus_error *error) {
1363 int64_t l = *(long*) userdata;
1365 return sd_bus_message_append_basic(reply, 'x', &l);
1368 int bus_property_get_ulong(
1371 const char *interface,
1372 const char *property,
1373 sd_bus_message *reply,
1375 sd_bus_error *error) {
1377 uint64_t ul = *(unsigned long*) userdata;
1379 return sd_bus_message_append_basic(reply, 't', &ul);
1383 int bus_log_parse_error(int r) {
1384 return log_error_errno(r, "Failed to parse bus message: %m");
1387 #if 0 /// UNNEEDED by elogind
1388 int bus_log_create_error(int r) {
1389 return log_error_errno(r, "Failed to create bus message: %m");
1393 #if 0 /// UNNEEDED by elogind
1395 * bus_path_encode_unique() - encode unique object path
1396 * @b: bus connection or NULL
1397 * @prefix: object path prefix
1398 * @sender_id: unique-name of client, or NULL
1399 * @external_id: external ID to be chosen by client, or NULL
1400 * @ret_path: storage for encoded object path pointer
1402 * Whenever we provide a bus API that allows clients to create and manage
1403 * server-side objects, we need to provide a unique name for these objects. If
1404 * we let the server choose the name, we suffer from a race condition: If a
1405 * client creates an object asynchronously, it cannot destroy that object until
1406 * it received the method reply. It cannot know the name of the new object,
1407 * thus, it cannot destroy it. Furthermore, it enforces a round-trip.
1409 * Therefore, many APIs allow the client to choose the unique name for newly
1410 * created objects. There're two problems to solve, though:
1411 * 1) Object names are usually defined via dbus object paths, which are
1412 * usually globally namespaced. Therefore, multiple clients must be able
1413 * to choose unique object names without interference.
1414 * 2) If multiple libraries share the same bus connection, they must be
1415 * able to choose unique object names without interference.
1416 * The first problem is solved easily by prefixing a name with the
1417 * unique-bus-name of a connection. The server side must enforce this and
1418 * reject any other name. The second problem is solved by providing unique
1419 * suffixes from within sd-bus.
1421 * This helper allows clients to create unique object-paths. It uses the
1422 * template '/prefix/sender_id/external_id' and returns the new path in
1423 * @ret_path (must be freed by the caller).
1424 * If @sender_id is NULL, the unique-name of @b is used. If @external_id is
1425 * NULL, this function allocates a unique suffix via @b (by requesting a new
1426 * cookie). If both @sender_id and @external_id are given, @b can be passed as
1429 * Returns: 0 on success, negative error code on failure.
1431 int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path) {
1432 _cleanup_free_ char *sender_label = NULL, *external_label = NULL;
1433 char external_buf[DECIMAL_STR_MAX(uint64_t)], *p;
1436 assert_return(b || (sender_id && external_id), -EINVAL);
1437 assert_return(object_path_is_valid(prefix), -EINVAL);
1438 assert_return(ret_path, -EINVAL);
1441 r = sd_bus_get_unique_name(b, &sender_id);
1447 xsprintf(external_buf, "%"PRIu64, ++b->cookie);
1448 external_id = external_buf;
1451 sender_label = bus_label_escape(sender_id);
1455 external_label = bus_label_escape(external_id);
1456 if (!external_label)
1459 p = strjoin(prefix, "/", sender_label, "/", external_label, NULL);
1468 * bus_path_decode_unique() - decode unique object path
1469 * @path: object path to decode
1470 * @prefix: object path prefix
1471 * @ret_sender: output parameter for sender-id label
1472 * @ret_external: output parameter for external-id label
1474 * This does the reverse of bus_path_encode_unique() (see its description for
1475 * details). Both trailing labels, sender-id and external-id, are unescaped and
1476 * returned in the given output parameters (the caller must free them).
1478 * Note that this function returns 0 if the path does not match the template
1479 * (see bus_path_encode_unique()), 1 if it matched.
1481 * Returns: Negative error code on failure, 0 if the given object path does not
1482 * match the template (return parameters are set to NULL), 1 if it was
1483 * parsed successfully (return parameters contain allocated labels).
1485 int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external) {
1487 char *sender, *external;
1489 assert(object_path_is_valid(path));
1490 assert(object_path_is_valid(prefix));
1492 assert(ret_external);
1494 p = object_path_startswith(path, prefix);
1497 *ret_external = NULL;
1504 *ret_external = NULL;
1508 sender = bus_label_unescape_n(p, q - p);
1509 external = bus_label_unescape(q + 1);
1510 if (!sender || !external) {
1516 *ret_sender = sender;
1517 *ret_external = external;
1522 #if 0 /// UNNEEDED by elogind
1523 int bus_property_get_rlimit(
1526 const char *interface,
1527 const char *property,
1528 sd_bus_message *reply,
1530 sd_bus_error *error) {
1535 const char *is_soft;
1541 is_soft = endswith(property, "Soft");
1542 rl = *(struct rlimit**) userdata;
1544 x = is_soft ? rl->rlim_cur : rl->rlim_max;
1546 struct rlimit buf = {};
1550 s = is_soft ? strndupa(property, is_soft - property) : property;
1552 z = rlimit_from_string(strstr(s, "Limit"));
1556 x = is_soft ? buf.rlim_cur : buf.rlim_max;
1559 /* rlim_t might have different sizes, let's map
1560 * RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on
1562 u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x;
1564 return sd_bus_message_append(reply, "t", u);