diff --git a/Makefile-man.am b/Makefile-man.am index 73afdd8c9bb..346bc632483 100644 --- a/Makefile-man.am +++ b/Makefile-man.am @@ -691,7 +691,6 @@ MANPAGES += \ man/sd_bus_creds_get_pid.3 \ man/sd_bus_creds_new_from_pid.3 \ man/sd_bus_error.3 \ - man/sd_bus_label_escape.3 \ man/sd_bus_message_append.3 \ man/sd_bus_message_append_array.3 \ man/sd_bus_message_append_basic.3 \ @@ -701,6 +700,7 @@ MANPAGES += \ man/sd_bus_message_get_monotonic_usec.3 \ man/sd_bus_new.3 \ man/sd_bus_open_user.3 \ + man/sd_bus_path_encode.3 \ man/sd_bus_request_name.3 \ man/systemd-bus-proxyd.8 \ man/systemd-bus-proxyd@.service.8 @@ -742,7 +742,6 @@ MANPAGES_ALIAS += \ man/sd_bus_error_set_const.3 \ man/sd_bus_error_set_errno.3 \ man/sd_bus_error_set_errnof.3 \ - man/sd_bus_label_unescape.3 \ man/sd_bus_message_append_array_iovec.3 \ man/sd_bus_message_append_array_memfd.3 \ man/sd_bus_message_append_array_space.3 \ @@ -754,6 +753,7 @@ MANPAGES_ALIAS += \ man/sd_bus_open_system.3 \ man/sd_bus_open_system_container.3 \ man/sd_bus_open_system_remote.3 \ + man/sd_bus_path_decode.3 \ man/sd_bus_ref.3 \ man/sd_bus_release_name.3 \ man/sd_bus_unref.3 \ @@ -795,7 +795,6 @@ man/sd_bus_error_set.3: man/sd_bus_error.3 man/sd_bus_error_set_const.3: man/sd_bus_error.3 man/sd_bus_error_set_errno.3: man/sd_bus_error.3 man/sd_bus_error_set_errnof.3: man/sd_bus_error.3 -man/sd_bus_label_unescape.3: man/sd_bus_label_escape.3 man/sd_bus_message_append_array_iovec.3: man/sd_bus_message_append_array.3 man/sd_bus_message_append_array_memfd.3: man/sd_bus_message_append_array.3 man/sd_bus_message_append_array_space.3: man/sd_bus_message_append_array.3 @@ -807,6 +806,7 @@ man/sd_bus_message_get_seqnum.3: man/sd_bus_message_get_monotonic_usec.3 man/sd_bus_open_system.3: man/sd_bus_open_user.3 man/sd_bus_open_system_container.3: man/sd_bus_open_user.3 man/sd_bus_open_system_remote.3: man/sd_bus_open_user.3 +man/sd_bus_path_decode.3: man/sd_bus_path_encode.3 man/sd_bus_ref.3: man/sd_bus_new.3 man/sd_bus_release_name.3: man/sd_bus_request_name.3 man/sd_bus_unref.3: man/sd_bus_new.3 @@ -922,9 +922,6 @@ man/sd_bus_error_set_errno.html: man/sd_bus_error.html man/sd_bus_error_set_errnof.html: man/sd_bus_error.html $(html-alias) -man/sd_bus_label_unescape.html: man/sd_bus_label_escape.html - $(html-alias) - man/sd_bus_message_append_array_iovec.html: man/sd_bus_message_append_array.html $(html-alias) @@ -958,6 +955,9 @@ man/sd_bus_open_system_container.html: man/sd_bus_open_user.html man/sd_bus_open_system_remote.html: man/sd_bus_open_user.html $(html-alias) +man/sd_bus_path_decode.html: man/sd_bus_path_encode.html + $(html-alias) + man/sd_bus_ref.html: man/sd_bus_new.html $(html-alias) @@ -1392,7 +1392,6 @@ EXTRA_DIST += \ man/sd_bus_creds_get_pid.xml \ man/sd_bus_creds_new_from_pid.xml \ man/sd_bus_error.xml \ - man/sd_bus_label_escape.xml \ man/sd_bus_message_append.xml \ man/sd_bus_message_append_array.xml \ man/sd_bus_message_append_basic.xml \ @@ -1402,6 +1401,7 @@ EXTRA_DIST += \ man/sd_bus_message_get_monotonic_usec.xml \ man/sd_bus_new.xml \ man/sd_bus_open_user.xml \ + man/sd_bus_path_encode.xml \ man/sd_bus_request_name.xml \ man/sd_get_seats.xml \ man/sd_id128_get_machine.xml \ diff --git a/man/sd_bus_label_escape.xml b/man/sd_bus_label_escape.xml deleted file mode 100644 index 0b5564f15dc..00000000000 --- a/man/sd_bus_label_escape.xml +++ /dev/null @@ -1,111 +0,0 @@ - - - - - - - - - sd_bus_label_escape - systemd - - - - A monkey with a typewriter - Zbigniew - Jędrzejewski-Szmek - zbyszek@in.waw.pl - - - - - - sd_bus_label_escape - 3 - - - - sd_bus_label_escape - sd_bus_label_unescape - - Escape D-Bus object path special characters - - - - - #include <systemd/sd-bus.h> - - - char *sd_bus_label_escape - const char *s - - - - char *sd_bus_label_unescape - const char *f - - - - - - Description - - sd_bus_label_escape() takes a - NUL-terminated string as a argument. It will - replace all characters which are invalid in a D-Bus object path by - _ and a hexadecimal number. As a special case, - the empty string will be replaced by a lone _. - sd_bus_label_unescape() can be used to - reverse this process. - - - - Return Value - - On success, a new NUL-terminated string - will be returned. It must be - free3d - by the caller. If a memory allocation failure occurs, - NULL will be returned. - - - - - Notes - - sd_bus_label_escape() and - sd_bus_label_unescape() are available as a - shared library, which can be compiled and linked to with the - libsystemd pkg-config1 - file. - - - - See Also - - - systemd1, - sd-bus3, - free3 - - - - diff --git a/man/sd_bus_path_encode.xml b/man/sd_bus_path_encode.xml new file mode 100644 index 00000000000..5c166d072be --- /dev/null +++ b/man/sd_bus_path_encode.xml @@ -0,0 +1,150 @@ + + + + + + + + + sd_bus_path_encode + systemd + + + + A monkey with a typewriter + Zbigniew + Jędrzejewski-Szmek + zbyszek@in.waw.pl + + + + + + sd_bus_path_encode + 3 + + + + sd_bus_path_encode + sd_bus_path_decode + + Convert an external identifier into an object path and back + + + + + #include <systemd/sd-bus.h> + + + int sd_bus_path_encode + const char *prefix + const char *external_id + char **ret_path + + + + int sd_bus_path_decode + const char *prefix + const char *path + char **ret_external_id + + + + + + Description + + sd_bus_path_encode() and + sd_bus_path_decode() convert external + identifier strings into object paths and back. These functions are + useful to map application-specific string identifiers of any kind + into bus object paths in a simple, reversible and safe way. + + sd_bus_path_encode() takes a bus path + prefix and an external identifier string as arguments, plus a + place to store the returned bus path string. The bus path prefix + must be a valid bus path, starting with a slash + /, but not ending in one. The external + identifier string may be in any format, may be the empty string + and no restrictions on the charset are made - however it must + always be NUL-terminated. The returned string + will be the concatenation of the bus path prefix plus an escaped + version of the external identifier string. This operation may be + reversed with sd_bus_decode(). It is + recommended to only use external identifiers here that generally + require little escaping to be turned into valid bus path + identifiers (for example by sticking to a 7bit ASCII character + set), in order to ensure the resulting bus path is still short and + easily processed. + + sd_bus_path_decode() reverses the + operation of sd_bus_path_encode() and thus + regenerates an external identifier string from a bus path. It + takes a bus path and a prefix string, plus a place to store the + returned external identifier string. If the bus path does not + start with the specified prefix, 0 is returned and the returned + string is set to NULL. Otherwise the the + string following the prefix is unescaped and returned in the + external identifier string. + + The escaping used will will replace all characters which are + invalid in a bus object path by _ followed by a + hexadecimal value. As a special case, the empty string will be + replaced by a lone _. + + + + Return Value + + On success, sd_bus_path_encode() + returns positive or 0, and a valid bus path in the return + argument. On success, sd_bus_path_decode() + returns a positive value if the prefixed matched, or 0 if it + didn't. If the prefix matched the external identifier is returned + in the return parameter. If it did not match NULL is returned in + the return parameter. On failure, a negative errno-style error + number is returned by either function. The returned strings must + be + free3'd + by the caller. + + + + Notes + + sd_bus_path_encode() and + sd_bus_path_decode() are available as a + shared library, which can be compiled and linked to with the + libsystemd pkg-config1 + file. + + + + See Also + + + systemd1, + sd-bus3, + free3 + + + + diff --git a/src/libsystemd/libsystemd.sym.m4 b/src/libsystemd/libsystemd.sym.m4 index c708c7f38fb..9c29169e7cb 100644 --- a/src/libsystemd/libsystemd.sym.m4 +++ b/src/libsystemd/libsystemd.sym.m4 @@ -311,8 +311,8 @@ m4_ifdef(`ENABLE_KDBUS', sd_bus_error_copy; sd_bus_error_is_set; sd_bus_error_has_name; - sd_bus_label_escape; - sd_bus_label_unescape; + sd_bus_path_encode; + sd_bus_path_decode; sd_bus_track_new; sd_bus_track_ref; sd_bus_track_unref; diff --git a/src/libsystemd/sd-bus/bus-creds.c b/src/libsystemd/sd-bus/bus-creds.c index 2d56f3c461d..3da77cf1d70 100644 --- a/src/libsystemd/sd-bus/bus-creds.c +++ b/src/libsystemd/sd-bus/bus-creds.c @@ -30,6 +30,7 @@ #include "time-util.h" #include "strv.h" #include "bus-creds.h" +#include "bus-label.h" enum { CAP_OFFSET_INHERITABLE = 0, @@ -474,7 +475,7 @@ _public_ int sd_bus_creds_get_connection_name(sd_bus_creds *c, const char **ret) assert(c->conn_name); if (!c->unescaped_conn_name) { - c->unescaped_conn_name = sd_bus_label_unescape(c->conn_name); + c->unescaped_conn_name = bus_label_unescape(c->conn_name); if (!c->unescaped_conn_name) return -ENOMEM; } diff --git a/src/libsystemd/sd-bus/bus-kernel.c b/src/libsystemd/sd-bus/bus-kernel.c index 2a1b0b424a9..8a2ca029967 100644 --- a/src/libsystemd/sd-bus/bus-kernel.c +++ b/src/libsystemd/sd-bus/bus-kernel.c @@ -36,6 +36,7 @@ #include "bus-kernel.h" #include "bus-bloom.h" #include "bus-util.h" +#include "bus-label.h" #include "cgroup-util.h" #define UNIQUE_NAME_MAX (3+DECIMAL_STR_MAX(uint64_t)) @@ -658,7 +659,7 @@ int bus_kernel_take_fd(sd_bus *b) { b->use_memfd = 1; if (b->connection_name) { - g = sd_bus_label_escape(b->connection_name); + g = bus_label_escape(b->connection_name); if (!g) return -ENOMEM; @@ -678,7 +679,7 @@ int bus_kernel_take_fd(sd_bus *b) { } else { _cleanup_free_ char *e = NULL; - e = sd_bus_label_escape(pr); + e = bus_label_escape(pr); if (!e) return -ENOMEM; @@ -691,7 +692,7 @@ int bus_kernel_take_fd(sd_bus *b) { name = g; } - b->connection_name = sd_bus_label_unescape(name); + b->connection_name = bus_label_unescape(name); if (!b->connection_name) return -ENOMEM; } @@ -1119,7 +1120,7 @@ int bus_kernel_pop_memfd(sd_bus *bus, void **address, size_t *mapped, size_t *al assert(bus->connection_name); - g = sd_bus_label_escape(bus->connection_name); + g = bus_label_escape(bus->connection_name); if (!g) return -ENOMEM; diff --git a/src/libsystemd/sd-bus/sd-bus.c b/src/libsystemd/sd-bus/sd-bus.c index fbf1a5919f1..ffa3369feb3 100644 --- a/src/libsystemd/sd-bus/sd-bus.c +++ b/src/libsystemd/sd-bus/sd-bus.c @@ -3063,12 +3063,46 @@ _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { return -ENXIO; } -_public_ char *sd_bus_label_escape(const char *s) { - return bus_label_escape(s); +_public_ int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path) { + _cleanup_free_ char *e = NULL; + char *ret; + + assert_return(object_path_is_valid(prefix), -EINVAL); + assert_return(external_id, -EINVAL); + assert_return(ret_path, -EINVAL); + + e = bus_label_escape(external_id); + if (!e) + return -ENOMEM; + + ret = strjoin(prefix, "/", e, NULL); + if (!ret) + return -ENOMEM; + + *ret_path = ret; + return 0; } -_public_ char *sd_bus_label_unescape(const char *f) { - return bus_label_unescape(f); +_public_ int sd_bus_path_decode(const char *path, const char *prefix, char **external_id) { + const char *e; + char *ret; + + assert_return(object_path_is_valid(path), -EINVAL); + assert_return(object_path_is_valid(prefix), -EINVAL); + assert_return(external_id, -EINVAL); + + e = object_path_startswith(path, prefix); + if (!e) { + *external_id = NULL; + return 0; + } + + ret = bus_label_unescape(e); + if (!ret) + return -ENOMEM; + + *external_id = ret; + return 1; } _public_ int sd_bus_get_peer_creds(sd_bus *bus, uint64_t mask, sd_bus_creds **ret) { diff --git a/src/libsystemd/sd-bus/sd-memfd.c b/src/libsystemd/sd-bus/sd-memfd.c index 7c71476eeef..8f9e2363925 100644 --- a/src/libsystemd/sd-bus/sd-memfd.c +++ b/src/libsystemd/sd-bus/sd-memfd.c @@ -27,6 +27,7 @@ #include "util.h" #include "kdbus.h" +#include "bus-label.h" #include "sd-memfd.h" #include "sd-bus.h" @@ -56,7 +57,7 @@ _public_ int sd_memfd_new(sd_memfd **m, const char *name) { * set here, let's do the usual bus escaping to deal * with that. */ - g = sd_bus_label_escape(name); + g = bus_label_escape(name); if (!g) return -ENOMEM; @@ -76,7 +77,7 @@ _public_ int sd_memfd_new(sd_memfd **m, const char *name) { else { _cleanup_free_ char *e = NULL; - e = sd_bus_label_escape(pr); + e = bus_label_escape(pr); if (!e) return -ENOMEM; @@ -311,7 +312,7 @@ _public_ int sd_memfd_get_name(sd_memfd *m, char **name) { if (!n) return -ENOMEM; - e = sd_bus_label_unescape(n); + e = bus_label_unescape(n); if (!e) return -ENOMEM; diff --git a/src/libsystemd/sd-bus/test-bus-marshal.c b/src/libsystemd/sd-bus/test-bus-marshal.c index d438a231a63..2321873da53 100644 --- a/src/libsystemd/sd-bus/test-bus-marshal.c +++ b/src/libsystemd/sd-bus/test-bus-marshal.c @@ -38,17 +38,35 @@ #include "bus-message.h" #include "bus-util.h" #include "bus-dump.h" +#include "bus-label.h" + +static void test_bus_path_encode(void) { + _cleanup_free_ char *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *f = NULL; + + assert_se(sd_bus_path_encode("/foo/bar", "waldo", &a) >= 0 && streq(a, "/foo/bar/waldo")); + assert_se(sd_bus_path_decode(a, "/waldo", &b) == 0 && b == NULL); + assert_se(sd_bus_path_decode(a, "/foo/bar", &b) > 0 && streq(b, "waldo")); + + assert_se(sd_bus_path_encode("xxxx", "waldo", &c) < 0); + assert_se(sd_bus_path_encode("/foo/", "waldo", &c) < 0); + + assert_se(sd_bus_path_encode("/foo/bar", "", &c) >= 0 && streq(c, "/foo/bar/_")); + assert_se(sd_bus_path_decode(c, "/foo/bar", &d) > 0 && streq(d, "")); + + assert_se(sd_bus_path_encode("/foo/bar", "foo.bar", &e) >= 0 && streq(e, "/foo/bar/foo_2ebar")); + assert_se(sd_bus_path_decode(e, "/foo/bar", &f) > 0 && streq(f, "foo.bar")); +} static void test_bus_label_escape_one(const char *a, const char *b) { _cleanup_free_ char *t = NULL, *x = NULL, *y = NULL; - assert_se(t = sd_bus_label_escape(a)); + assert_se(t = bus_label_escape(a)); assert_se(streq(t, b)); - assert_se(x = sd_bus_label_unescape(t)); + assert_se(x = bus_label_unescape(t)); assert_se(streq(a, x)); - assert_se(y = sd_bus_label_unescape(b)); + assert_se(y = bus_label_unescape(b)); assert_se(streq(a, y)); } @@ -322,6 +340,7 @@ int main(int argc, char *argv[]) { assert_se(streq(d, "3")); test_bus_label_escape(); + test_bus_path_encode(); return 0; } diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c index 315e6baf6be..82323d6b877 100644 --- a/src/login/logind-seat-dbus.c +++ b/src/login/logind-seat-dbus.c @@ -27,6 +27,7 @@ #include "bus-util.h" #include "strv.h" #include "bus-errors.h" +#include "bus-label.h" #include "logind.h" #include "logind-seat.h" @@ -356,7 +357,7 @@ int seat_object_find(sd_bus *bus, const char *path, const char *interface, void if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -374,7 +375,7 @@ char *seat_bus_path(Seat *s) { assert(s); - t = sd_bus_label_escape(s->id); + t = bus_label_escape(s->id); if (!t) return NULL; diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c index c3d4d60158f..ffdc5582dca 100644 --- a/src/login/logind-session-dbus.c +++ b/src/login/logind-session-dbus.c @@ -27,6 +27,7 @@ #include "strv.h" #include "bus-util.h" #include "bus-errors.h" +#include "bus-label.h" #include "logind.h" #include "logind-session.h" @@ -515,7 +516,7 @@ int session_object_find(sd_bus *bus, const char *path, const char *interface, vo if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -533,7 +534,7 @@ char *session_bus_path(Session *s) { assert(s); - t = sd_bus_label_escape(s->id); + t = bus_label_escape(s->id); if (!t) return NULL; diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index c9e3bb7c758..920c9956aee 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -24,6 +24,7 @@ #include #include "bus-util.h" +#include "bus-label.h" #include "strv.h" #include "machine.h" @@ -182,7 +183,7 @@ int machine_object_find(sd_bus *bus, const char *path, const char *interface, vo if (!p) return 0; - e = sd_bus_label_unescape(p); + e = bus_label_unescape(p); if (!e) return -ENOMEM; @@ -200,7 +201,7 @@ char *machine_bus_path(Machine *m) { assert(m); - e = sd_bus_label_escape(m->name); + e = bus_label_escape(m->name); if (!e) return NULL; diff --git a/src/systemd/sd-bus.h b/src/systemd/sd-bus.h index e26ca6b7042..8817380f1a0 100644 --- a/src/systemd/sd-bus.h +++ b/src/systemd/sd-bus.h @@ -349,8 +349,8 @@ int sd_bus_error_has_name(const sd_bus_error *e, const char *name); /* Label escaping */ -char *sd_bus_label_escape(const char *s); -char *sd_bus_label_unescape(const char *f); +int sd_bus_path_encode(const char *prefix, const char *external_id, char **ret_path); +int sd_bus_path_decode(const char *path, const char *prefix, char **ret_external_id); /* Tracking peers */