From c664cf56071a256121015eda330d471ff166682f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Jun 2020 15:37:47 +0200 Subject: [PATCH 1/5] shared: split out BusObjectImplementor APIs Just some refactoring, no code changes --- src/core/dbus-job.h | 2 +- src/home/homed-home-bus.h | 2 +- src/libsystemd/sd-bus/bus-track.c | 1 + src/login/logind-dbus.h | 4 +- src/login/logind-seat-dbus.h | 2 +- src/login/logind-session-dbus.h | 1 + src/machine/image-dbus.h | 2 +- src/resolve/resolved-bus.h | 2 +- src/resolve/resolved-dnssd-bus.h | 2 +- src/shared/bus-log-control-api.h | 2 +- src/shared/bus-object.c | 177 ++++++++++++++++++++++++++++++ src/shared/bus-object.h | 34 ++++++ src/shared/bus-util.c | 172 +---------------------------- src/shared/bus-util.h | 26 ----- src/shared/meson.build | 2 + src/shared/service-util.h | 2 +- 16 files changed, 226 insertions(+), 207 deletions(-) create mode 100644 src/shared/bus-object.c create mode 100644 src/shared/bus-object.h diff --git a/src/core/dbus-job.h b/src/core/dbus-job.h index a840c6dfe8..96c5b66309 100644 --- a/src/core/dbus-job.h +++ b/src/core/dbus-job.h @@ -4,7 +4,7 @@ #include "sd-bus.h" #include "unit.h" -#include "bus-util.h" +#include "bus-object.h" extern const sd_bus_vtable bus_job_vtable[]; extern const BusObjectImplementation job_object; diff --git a/src/home/homed-home-bus.h b/src/home/homed-home-bus.h index 59b2a970b8..8ac7ff9995 100644 --- a/src/home/homed-home-bus.h +++ b/src/home/homed-home-bus.h @@ -3,7 +3,7 @@ #include "sd-bus.h" -#include "bus-util.h" +#include "bus-object.h" #include "homed-home.h" int bus_home_client_is_trusted(Home *h, sd_bus_message *message); diff --git a/src/libsystemd/sd-bus/bus-track.c b/src/libsystemd/sd-bus/bus-track.c index f609854925..13fd52ffd5 100644 --- a/src/libsystemd/sd-bus/bus-track.c +++ b/src/libsystemd/sd-bus/bus-track.c @@ -6,6 +6,7 @@ #include "bus-internal.h" #include "bus-track.h" #include "bus-util.h" +#include "string-util.h" struct track_item { unsigned n_ref; diff --git a/src/login/logind-dbus.h b/src/login/logind-dbus.h index 6a3ee82f1c..7707587545 100644 --- a/src/login/logind-dbus.h +++ b/src/login/logind-dbus.h @@ -3,10 +3,10 @@ #include "sd-bus.h" -#include "bus-util.h" -#include "logind.h" +#include "bus-object.h" #include "logind-session.h" #include "logind-user.h" +#include "logind.h" int manager_get_session_from_creds(Manager *m, sd_bus_message *message, const char *name, sd_bus_error *error, Session **ret); int manager_get_user_from_creds(Manager *m, sd_bus_message *message, uid_t uid, sd_bus_error *error, User **ret); diff --git a/src/login/logind-seat-dbus.h b/src/login/logind-seat-dbus.h index 5fc857584e..6169cfe1ed 100644 --- a/src/login/logind-seat-dbus.h +++ b/src/login/logind-seat-dbus.h @@ -3,8 +3,8 @@ #include "sd-bus.h" +#include "bus-object.h" #include "logind-seat.h" -#include "bus-util.h" extern const BusObjectImplementation seat_object; diff --git a/src/login/logind-session-dbus.h b/src/login/logind-session-dbus.h index 6df96e06b2..97f7c413cb 100644 --- a/src/login/logind-session-dbus.h +++ b/src/login/logind-session-dbus.h @@ -3,6 +3,7 @@ #include "sd-bus.h" +#include "bus-object.h" #include "logind-session.h" extern const BusObjectImplementation session_object; diff --git a/src/machine/image-dbus.h b/src/machine/image-dbus.h index 43f5f34d20..d785c10817 100644 --- a/src/machine/image-dbus.h +++ b/src/machine/image-dbus.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -#include "bus-util.h" +#include "bus-object.h" #include "machined.h" extern const BusObjectImplementation image_object; diff --git a/src/resolve/resolved-bus.h b/src/resolve/resolved-bus.h index e69171ca70..6d6b095323 100644 --- a/src/resolve/resolved-bus.h +++ b/src/resolve/resolved-bus.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -#include "bus-util.h" +#include "bus-object.h" #include "resolved-manager.h" extern const BusObjectImplementation manager_object; diff --git a/src/resolve/resolved-dnssd-bus.h b/src/resolve/resolved-dnssd-bus.h index 8cd75642a7..403455e89f 100644 --- a/src/resolve/resolved-dnssd-bus.h +++ b/src/resolve/resolved-dnssd-bus.h @@ -2,7 +2,7 @@ #include "sd-bus.h" -#include "bus-util.h" +#include "bus-object.h" extern const BusObjectImplementation dnssd_object; diff --git a/src/shared/bus-log-control-api.h b/src/shared/bus-log-control-api.h index 3e654d7d81..64eaa540f4 100644 --- a/src/shared/bus-log-control-api.h +++ b/src/shared/bus-log-control-api.h @@ -3,7 +3,7 @@ #include "sd-bus.h" -#include "bus-util.h" +#include "bus-object.h" extern const BusObjectImplementation log_control_object; static inline int bus_log_control_api_register(sd_bus *bus) { diff --git a/src/shared/bus-object.c b/src/shared/bus-object.c new file mode 100644 index 0000000000..217d43d333 --- /dev/null +++ b/src/shared/bus-object.c @@ -0,0 +1,177 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "bus-introspect.h" +#include "bus-object.h" +#include "macro.h" +#include "string-util.h" +#include "strv.h" + +int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata) { + int r; + + log_debug("Registering bus object implementation for path=%s iface=%s", impl->path, impl->interface); + + for (const sd_bus_vtable **p = impl->vtables; p && *p; p++) { + r = sd_bus_add_object_vtable(bus, NULL, + impl->path, + impl->interface, + *p, + userdata); + if (r < 0) + return log_error_errno(r, "Failed to register bus path %s with interface %s: %m", + impl->path, + impl->interface); + } + + for (const BusObjectVtablePair *p = impl->fallback_vtables; p && p->vtable; p++) { + r = sd_bus_add_fallback_vtable(bus, NULL, + impl->path, + impl->interface, + p->vtable, + p->object_find, + userdata); + if (r < 0) + return log_error_errno(r, "Failed to register bus path %s with interface %s: %m", + impl->path, + impl->interface); + } + + if (impl->node_enumerator) { + r = sd_bus_add_node_enumerator(bus, NULL, + impl->path, + impl->node_enumerator, + userdata); + if (r < 0) + return log_error_errno(r, "Failed to add node enumerator for %s: %m", + impl->path); + } + + if (impl->manager) { + r = sd_bus_add_object_manager(bus, NULL, impl->path); + if (r < 0) + return log_error_errno(r, "Failed to add object manager for %s: %m", impl->path); + } + + for (size_t i = 0; impl->children && impl->children[i]; i++) { + r = bus_add_implementation(bus, impl->children[i], userdata); + if (r < 0) + return r; + } + + return 0; +} + +static const BusObjectImplementation* find_implementation( + const char *pattern, + const BusObjectImplementation* const* bus_objects) { + + for (size_t i = 0; bus_objects && bus_objects[i]; i++) { + const BusObjectImplementation *impl = bus_objects[i]; + + if (STR_IN_SET(pattern, impl->path, impl->interface)) + return impl; + + impl = find_implementation(pattern, impl->children); + if (impl) + return impl; + } + + return NULL; +} + +static int bus_introspect_implementation( + struct introspect *intro, + const BusObjectImplementation *impl) { + int r; + + for (const sd_bus_vtable **p = impl->vtables; p && *p; p++) { + r = introspect_write_interface(intro, impl->interface, *p); + if (r < 0) + return log_error_errno(r, "Failed to write introspection data: %m"); + } + + for (const BusObjectVtablePair *p = impl->fallback_vtables; p && p->vtable; p++) { + r = introspect_write_interface(intro, impl->interface, p->vtable); + if (r < 0) + return log_error_errno(r, "Failed to write introspection data: %m"); + } + + return 0; +} + +static void list_paths( + FILE *out, + const BusObjectImplementation* const* bus_objects) { + + for (size_t i = 0; bus_objects[i]; i++) { + fprintf(out, "%s\t%s\n", bus_objects[i]->path, bus_objects[i]->interface); + if (bus_objects[i]->children) + list_paths(out, bus_objects[i]->children); + } +} + +int bus_introspect_implementations( + FILE *out, + const char *pattern, + const BusObjectImplementation* const* bus_objects) { + + const BusObjectImplementation *impl, *main_impl = NULL; + _cleanup_free_ char *s = NULL; + int r; + + if (streq(pattern, "list")) { + list_paths(out, bus_objects); + return 0; + } + + struct introspect intro = {}; + bool is_interface = sd_bus_interface_name_is_valid(pattern); + + impl = find_implementation(pattern, bus_objects); + if (!impl) + return log_error_errno(SYNTHETIC_ERRNO(ENOENT), + "%s %s not found", + is_interface ? "Interface" : "Object path", + pattern); + + /* We use trusted=false here to get all the @org.freedesktop.systemd1.Privileged annotations. */ + r = introspect_begin(&intro, false); + if (r < 0) + return log_error_errno(r, "Failed to write introspection data: %m"); + + r = introspect_write_default_interfaces(&intro, impl->manager); + if (r < 0) + return log_error_errno(r, "Failed to write introspection data: %m"); + + /* Check if there is a non-fallback path that applies to the given interface, also + * print it. This is useful in the case of units: o.fd.systemd1.Service is declared + * as a fallback vtable for o/fd/systemd1/unit, and we also want to print + * o.fd.systemd1.Unit, which is the non-fallback implementation. */ + if (impl->fallback_vtables && is_interface) + main_impl = find_implementation(impl->path, bus_objects); + + if (main_impl) + bus_introspect_implementation(&intro, main_impl); + + if (impl != main_impl) + bus_introspect_implementation(&intro, impl); + + _cleanup_set_free_ Set *nodes = NULL; + + for (size_t i = 0; impl->children && impl->children[i]; i++) { + r = set_put_strdup(&nodes, impl->children[i]->path); + if (r < 0) + return log_oom(); + } + + r = introspect_write_child_nodes(&intro, nodes, impl->path); + if (r < 0) + return r; + + r = introspect_finish(&intro, &s); + if (r < 0) + return log_error_errno(r, "Failed to write introspection data: %m"); + + fputs(s, out); + return 0; +} diff --git a/src/shared/bus-object.h b/src/shared/bus-object.h new file mode 100644 index 0000000000..8add854e59 --- /dev/null +++ b/src/shared/bus-object.h @@ -0,0 +1,34 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include +#include + +#include "sd-bus.h" + +typedef struct BusObjectImplementation BusObjectImplementation; + +typedef struct BusObjectVtablePair { + const sd_bus_vtable *vtable; + sd_bus_object_find_t object_find; +} BusObjectVtablePair; + +struct BusObjectImplementation { + const char *path; + const char *interface; + const sd_bus_vtable **vtables; + const BusObjectVtablePair *fallback_vtables; + sd_bus_node_enumerator_t node_enumerator; + bool manager; + const BusObjectImplementation **children; +}; + +#define BUS_VTABLES(...) ((const sd_bus_vtable* []){ __VA_ARGS__, NULL }) +#define BUS_FALLBACK_VTABLES(...) ((const BusObjectVtablePair[]) { __VA_ARGS__, {} }) +#define BUS_IMPLEMENTATIONS(...) ((const BusObjectImplementation* []) { __VA_ARGS__, NULL }) + +int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata); +int bus_introspect_implementations( + FILE *out, + const char *pattern, + const BusObjectImplementation* const* bus_objects); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index f64e200731..a43fe66888 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -16,7 +16,6 @@ #include "alloc-util.h" #include "bus-internal.h" -#include "bus-introspect.h" #include "bus-label.h" #include "bus-message.h" #include "bus-util.h" @@ -30,6 +29,7 @@ #include "rlimit-util.h" #include "socket-util.h" #include "stdio-util.h" +#include "string-util.h" #include "strv.h" #include "user-util.h" @@ -1546,173 +1546,3 @@ int bus_message_new_method_call( return sd_bus_message_new_method_call(bus, m, locator->destination, locator->path, locator->interface, member); } - -int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata) { - int r; - - log_debug("Registering bus object implementation for path=%s iface=%s", impl->path, impl->interface); - - for (const sd_bus_vtable **p = impl->vtables; p && *p; p++) { - r = sd_bus_add_object_vtable(bus, NULL, - impl->path, - impl->interface, - *p, - userdata); - if (r < 0) - return log_error_errno(r, "Failed to register bus path %s with interface %s: %m", - impl->path, - impl->interface); - } - - for (const BusObjectVtablePair *p = impl->fallback_vtables; p && p->vtable; p++) { - r = sd_bus_add_fallback_vtable(bus, NULL, - impl->path, - impl->interface, - p->vtable, - p->object_find, - userdata); - if (r < 0) - return log_error_errno(r, "Failed to register bus path %s with interface %s: %m", - impl->path, - impl->interface); - } - - if (impl->node_enumerator) { - r = sd_bus_add_node_enumerator(bus, NULL, - impl->path, - impl->node_enumerator, - userdata); - if (r < 0) - return log_error_errno(r, "Failed to add node enumerator for %s: %m", - impl->path); - } - - if (impl->manager) { - r = sd_bus_add_object_manager(bus, NULL, impl->path); - if (r < 0) - return log_error_errno(r, "Failed to add object manager for %s: %m", impl->path); - } - - for (size_t i = 0; impl->children && impl->children[i]; i++) { - r = bus_add_implementation(bus, impl->children[i], userdata); - if (r < 0) - return r; - } - - return 0; -} - -static const BusObjectImplementation* find_implementation( - const char *pattern, - const BusObjectImplementation* const* bus_objects) { - - for (size_t i = 0; bus_objects && bus_objects[i]; i++) { - const BusObjectImplementation *impl = bus_objects[i]; - - if (STR_IN_SET(pattern, impl->path, impl->interface)) - return impl; - - impl = find_implementation(pattern, impl->children); - if (impl) - return impl; - } - - return NULL; -} - -static int bus_introspect_implementation( - struct introspect *intro, - const BusObjectImplementation *impl) { - int r; - - for (const sd_bus_vtable **p = impl->vtables; p && *p; p++) { - r = introspect_write_interface(intro, impl->interface, *p); - if (r < 0) - return log_error_errno(r, "Failed to write introspection data: %m"); - } - - for (const BusObjectVtablePair *p = impl->fallback_vtables; p && p->vtable; p++) { - r = introspect_write_interface(intro, impl->interface, p->vtable); - if (r < 0) - return log_error_errno(r, "Failed to write introspection data: %m"); - } - - return 0; -} - -static void list_paths( - FILE *out, - const BusObjectImplementation* const* bus_objects) { - - for (size_t i = 0; bus_objects[i]; i++) { - fprintf(out, "%s\t%s\n", bus_objects[i]->path, bus_objects[i]->interface); - if (bus_objects[i]->children) - list_paths(out, bus_objects[i]->children); - } -} - -int bus_introspect_implementations( - FILE *out, - const char *pattern, - const BusObjectImplementation* const* bus_objects) { - - const BusObjectImplementation *impl, *main_impl = NULL; - _cleanup_free_ char *s = NULL; - int r; - - if (streq(pattern, "list")) { - list_paths(out, bus_objects); - return 0; - } - - struct introspect intro = {}; - bool is_interface = sd_bus_interface_name_is_valid(pattern); - - impl = find_implementation(pattern, bus_objects); - if (!impl) - return log_error_errno(SYNTHETIC_ERRNO(ENOENT), - "%s %s not found", - is_interface ? "Interface" : "Object path", - pattern); - - /* We use trusted=false here to get all the @org.freedesktop.systemd1.Privileged annotations. */ - r = introspect_begin(&intro, false); - if (r < 0) - return log_error_errno(r, "Failed to write introspection data: %m"); - - r = introspect_write_default_interfaces(&intro, impl->manager); - if (r < 0) - return log_error_errno(r, "Failed to write introspection data: %m"); - - /* Check if there is a non-fallback path that applies to the given interface, also - * print it. This is useful in the case of units: o.fd.systemd1.Service is declared - * as a fallback vtable for o/fd/systemd1/unit, and we also want to print - * o.fd.systemd1.Unit, which is the non-fallback implementation. */ - if (impl->fallback_vtables && is_interface) - main_impl = find_implementation(impl->path, bus_objects); - - if (main_impl) - bus_introspect_implementation(&intro, main_impl); - - if (impl != main_impl) - bus_introspect_implementation(&intro, impl); - - _cleanup_set_free_ Set *nodes = NULL; - - for (size_t i = 0; impl->children && impl->children[i]; i++) { - r = set_put_strdup(&nodes, impl->children[i]->path); - if (r < 0) - return log_oom(); - } - - r = introspect_write_child_nodes(&intro, nodes, impl->path); - if (r < 0) - return r; - - r = introspect_finish(&intro, &s); - if (r < 0) - return log_error_errno(r, "Failed to write introspection data: %m"); - - fputs(s, out); - return 0; -} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index ef14217e93..1d44cd7676 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -23,26 +23,6 @@ typedef enum BusTransport { _BUS_TRANSPORT_INVALID = -1 } BusTransport; -typedef struct BusObjectImplementation BusObjectImplementation; - -typedef struct BusObjectVtablePair { - const sd_bus_vtable *vtable; - sd_bus_object_find_t object_find; -} BusObjectVtablePair; - -struct BusObjectImplementation { - const char *path; - const char *interface; - const sd_bus_vtable **vtables; - const BusObjectVtablePair *fallback_vtables; - sd_bus_node_enumerator_t node_enumerator; - bool manager; - const BusObjectImplementation **children; -}; - -#define BUS_VTABLES(...) ((const sd_bus_vtable* []){ __VA_ARGS__, NULL }) -#define BUS_FALLBACK_VTABLES(...) ((const BusObjectVtablePair[]) { __VA_ARGS__, {} }) -#define BUS_IMPLEMENTATIONS(...) ((const BusObjectImplementation* []) { __VA_ARGS__, NULL }) typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); @@ -215,9 +195,3 @@ int bus_set_property(sd_bus *bus, const BusLocator *locator, const char *member, int bus_match_signal(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata); int bus_match_signal_async(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata); int bus_message_new_method_call(sd_bus *bus, sd_bus_message **m, const BusLocator *locator, const char *member); - -int bus_add_implementation(sd_bus *bus, const BusObjectImplementation *impl, void *userdata); -int bus_introspect_implementations( - FILE *out, - const char *pattern, - const BusObjectImplementation* const* bus_objects); diff --git a/src/shared/meson.build b/src/shared/meson.build index e3aee57ac8..30ece1f193 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -31,6 +31,8 @@ shared_sources = files(''' bus-locator.h bus-log-control-api.c bus-log-control-api.h + bus-object.c + bus-object.h bus-polkit.c bus-polkit.h bus-unit-procs.c diff --git a/src/shared/service-util.h b/src/shared/service-util.h index 76bff61d58..928c5961e7 100644 --- a/src/shared/service-util.h +++ b/src/shared/service-util.h @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once -#include "bus-util.h" +#include "bus-object.h" int service_parse_argv( const char *service, From 9b71e4ab90b4e1cb048f4cd13e0fde6f999cf6e8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Jun 2020 15:59:37 +0200 Subject: [PATCH 2/5] shared: actually move all BusLocator related calls to bus-locator.c --- src/analyze/analyze.c | 2 +- src/home/homectl.c | 2 +- src/home/pam_systemd_home.c | 2 +- src/locale/localectl.c | 2 +- src/login/loginctl.c | 2 +- src/login/logind-dbus.c | 1 + src/login/logind.c | 2 +- src/login/pam_systemd.c | 2 +- src/machine/machine-dbus.c | 2 +- src/machine/machinectl.c | 2 +- src/machine/machined-dbus.c | 2 +- src/machine/machined.c | 2 +- src/mount/mount-tool.c | 2 +- src/network/networkctl.c | 4 +- src/nspawn/nspawn-register.c | 1 + src/nss-mymachines/nss-mymachines.c | 2 +- src/nss-resolve/nss-resolve.c | 2 +- src/portable/portablectl.c | 2 +- src/resolve/resolvectl.c | 2 +- src/run/run.c | 2 +- src/shared/bus-locator.c | 150 ++++++++++++++++++++++++++++ src/shared/bus-locator.h | 21 +++- src/shared/bus-util.c | 150 ---------------------------- src/shared/bus-util.h | 16 --- src/systemctl/systemctl.c | 2 +- src/timedate/timedatectl.c | 4 +- src/timedate/timedated.c | 2 +- 27 files changed, 193 insertions(+), 192 deletions(-) diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index a8bd993cbe..8911d95c07 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -17,8 +17,8 @@ #include "analyze-verify.h" #include "build.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "calendarspec.h" #include "conf-files.h" #include "copy.h" diff --git a/src/home/homectl.c b/src/home/homectl.c index 118bf63224..47a506f5e0 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -8,7 +8,7 @@ #include "ask-password-api.h" #include "bus-common-errors.h" #include "bus-error.h" -#include "bus-util.h" +#include "bus-locator.h" #include "cgroup-util.h" #include "dns-domain.h" #include "env-util.h" diff --git a/src/home/pam_systemd_home.c b/src/home/pam_systemd_home.c index baa3586bf7..34dfd134fc 100644 --- a/src/home/pam_systemd_home.c +++ b/src/home/pam_systemd_home.c @@ -6,7 +6,7 @@ #include "sd-bus.h" #include "bus-common-errors.h" -#include "bus-util.h" +#include "bus-locator.h" #include "errno-util.h" #include "fd-util.h" #include "home-util.h" diff --git a/src/locale/localectl.c b/src/locale/localectl.c index 96fc09a570..f00f15f3fe 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -8,7 +8,7 @@ #include "sd-bus.h" #include "bus-error.h" -#include "bus-util.h" +#include "bus-locator.h" #include "fd-util.h" #include "fileio.h" #include "kbd-util.h" diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 9216224bfc..85b6e162f1 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -9,8 +9,8 @@ #include "alloc-util.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-procs.h" -#include "bus-util.h" #include "cgroup-show.h" #include "cgroup-util.h" #include "format-table.h" diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 079338fbb5..98e0a6cff7 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -12,6 +12,7 @@ #include "bootspec.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-polkit.h" #include "bus-unit-util.h" #include "bus-util.h" diff --git a/src/login/logind.c b/src/login/logind.c index 18caae3487..c50a083b03 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -10,9 +10,9 @@ #include "alloc-util.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-log-control-api.h" #include "bus-polkit.h" -#include "bus-util.h" #include "cgroup-util.h" #include "def.h" #include "device-util.h" diff --git a/src/login/pam_systemd.c b/src/login/pam_systemd.c index 7d48e896de..525b2a0393 100644 --- a/src/login/pam_systemd.c +++ b/src/login/pam_systemd.c @@ -20,7 +20,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-internal.h" -#include "bus-util.h" +#include "bus-locator.h" #include "cgroup-setup.h" #include "errno-util.h" #include "fd-util.h" diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index 903dd8f161..a8f9d28a87 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -14,8 +14,8 @@ #include "bus-common-errors.h" #include "bus-internal.h" #include "bus-label.h" +#include "bus-locator.h" #include "bus-polkit.h" -#include "bus-util.h" #include "copy.h" #include "env-file.h" #include "env-util.h" diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index d8f4526644..bdb384fe58 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -16,9 +16,9 @@ #include "alloc-util.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "cgroup-show.h" #include "cgroup-util.h" diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index cbc549cb76..9fb8433592 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -8,8 +8,8 @@ #include "alloc-util.h" #include "btrfs-util.h" #include "bus-common-errors.h" +#include "bus-locator.h" #include "bus-polkit.h" -#include "bus-util.h" #include "cgroup-util.h" #include "errno-util.h" #include "fd-util.h" diff --git a/src/machine/machined.c b/src/machine/machined.c index 015622761c..4645e4ce41 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -10,9 +10,9 @@ #include "alloc-util.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-log-control-api.h" #include "bus-polkit.h" -#include "bus-util.h" #include "cgroup-util.h" #include "dirent-util.h" #include "fd-util.h" diff --git a/src/mount/mount-tool.c b/src/mount/mount-tool.c index ea86a6e06a..13f8a37ddf 100644 --- a/src/mount/mount-tool.c +++ b/src/mount/mount-tool.c @@ -6,8 +6,8 @@ #include "sd-device.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "device-util.h" #include "dirent-util.h" diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 8b7cf292fa..3b9da74e43 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -21,10 +21,10 @@ #include "alloc-util.h" #include "bond-util.h" +#include "bridge-util.h" #include "bus-common-errors.h" #include "bus-error.h" -#include "bus-util.h" -#include "bridge-util.h" +#include "bus-locator.h" #include "device-util.h" #include "escape.h" #include "ether-addr-util.h" diff --git a/src/nspawn/nspawn-register.c b/src/nspawn/nspawn-register.c index 64773a3d9c..50867f3843 100644 --- a/src/nspawn/nspawn-register.c +++ b/src/nspawn/nspawn-register.c @@ -3,6 +3,7 @@ #include "sd-bus.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-util.h" #include "bus-util.h" #include "bus-wait-for-jobs.h" diff --git a/src/nss-mymachines/nss-mymachines.c b/src/nss-mymachines/nss-mymachines.c index ce51fa8938..9269e7fd7b 100644 --- a/src/nss-mymachines/nss-mymachines.c +++ b/src/nss-mymachines/nss-mymachines.c @@ -8,7 +8,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" -#include "bus-util.h" +#include "bus-locator.h" #include "env-util.h" #include "errno-util.h" #include "format-util.h" diff --git a/src/nss-resolve/nss-resolve.c b/src/nss-resolve/nss-resolve.c index 0ee3ddd843..43ab521635 100644 --- a/src/nss-resolve/nss-resolve.c +++ b/src/nss-resolve/nss-resolve.c @@ -10,7 +10,7 @@ #include "sd-bus.h" #include "bus-common-errors.h" -#include "bus-util.h" +#include "bus-locator.h" #include "errno-util.h" #include "in-addr-util.h" #include "macro.h" diff --git a/src/portable/portablectl.c b/src/portable/portablectl.c index 01614b6684..46bb0e5437 100644 --- a/src/portable/portablectl.c +++ b/src/portable/portablectl.c @@ -7,8 +7,8 @@ #include "alloc-util.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "def.h" #include "dirent-util.h" diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index a80b450bc0..838da9aae9 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -11,7 +11,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" #include "bus-error.h" -#include "bus-util.h" +#include "bus-locator.h" #include "dns-domain.h" #include "escape.h" #include "format-table.h" diff --git a/src/run/run.c b/src/run/run.c index 70a27924b5..80ddd0f521 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -11,8 +11,8 @@ #include "alloc-util.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "calendarspec.h" #include "env-util.h" diff --git a/src/shared/bus-locator.c b/src/shared/bus-locator.c index a79699d62d..2a5aa74677 100644 --- a/src/shared/bus-locator.c +++ b/src/shared/bus-locator.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "bus-locator.h" +#include "macro.h" const BusLocator* const bus_home_mgr = &(BusLocator){ .destination = "org.freedesktop.home1", @@ -61,3 +62,152 @@ const BusLocator* const bus_timedate = &(BusLocator){ .path = "/org/freedesktop/timedate1", .interface = "org.freedesktop.timedate1" }; + +/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface strings encapsulated + * within a single struct. */ +int bus_call_method_async( + sd_bus *bus, + sd_bus_slot **slot, + const BusLocator *locator, + const char *member, + sd_bus_message_handler_t callback, + void *userdata, + const char *types, ...) { + + va_list ap; + int r; + + assert(locator); + + va_start(ap, types); + r = sd_bus_call_method_asyncv(bus, slot, locator->destination, locator->path, locator->interface, member, callback, userdata, types, ap); + va_end(ap); + + return r; +} + +int bus_call_method( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + sd_bus_message **reply, + const char *types, ...) { + + va_list ap; + int r; + + assert(locator); + + va_start(ap, types); + r = sd_bus_call_methodv(bus, locator->destination, locator->path, locator->interface, member, error, reply, types, ap); + va_end(ap); + + return r; +} + +int bus_get_property( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + sd_bus_message **reply, + const char *type) { + + assert(locator); + + return sd_bus_get_property(bus, locator->destination, locator->path, locator->interface, member, error, reply, type); +} + +int bus_get_property_trivial( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + char type, void *ptr) { + + assert(locator); + + return sd_bus_get_property_trivial(bus, locator->destination, locator->path, locator->interface, member, error, type, ptr); +} + +int bus_get_property_string( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + char **ret) { + + assert(locator); + + return sd_bus_get_property_string(bus, locator->destination, locator->path, locator->interface, member, error, ret); +} + +int bus_get_property_strv( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + char ***ret) { + + assert(locator); + + return sd_bus_get_property_strv(bus, locator->destination, locator->path, locator->interface, member, error, ret); +} + +int bus_set_property( + sd_bus *bus, + const BusLocator *locator, + const char *member, + sd_bus_error *error, + const char *type, ...) { + + va_list ap; + int r; + + assert(locator); + + va_start(ap, type); + r = sd_bus_set_propertyv(bus, locator->destination, locator->path, locator->interface, member, error, type, ap); + va_end(ap); + + return r; +} + +int bus_match_signal( + sd_bus *bus, + sd_bus_slot **ret, + const BusLocator *locator, + const char *member, + sd_bus_message_handler_t callback, + void *userdata) { + + assert(locator); + + return sd_bus_match_signal(bus, ret, locator->destination, locator->path, locator->interface, member, callback, userdata); +} + +int bus_match_signal_async( + sd_bus *bus, + sd_bus_slot **ret, + const BusLocator *locator, + const char *member, + sd_bus_message_handler_t callback, + sd_bus_message_handler_t install_callback, + void *userdata) { + + assert(locator); + + return sd_bus_match_signal_async(bus, ret, locator->destination, locator->path, locator->interface, member, callback, install_callback, userdata); +} + +int bus_message_new_method_call( + sd_bus *bus, + sd_bus_message **m, + const BusLocator *locator, + const char *member) { + + assert(locator); + + return sd_bus_message_new_method_call(bus, m, locator->destination, locator->path, locator->interface, member); +} diff --git a/src/shared/bus-locator.h b/src/shared/bus-locator.h index 91a9aaf63a..2b892360b8 100644 --- a/src/shared/bus-locator.h +++ b/src/shared/bus-locator.h @@ -1,10 +1,12 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #pragma once +#include "sd-bus.h" + typedef struct BusLocator { - const char *destination; - const char *path; - const char *interface; + const char *destination; + const char *path; + const char *interface; } BusLocator; extern const BusLocator* const bus_home_mgr; @@ -17,3 +19,16 @@ extern const BusLocator* const bus_portable_mgr; extern const BusLocator* const bus_resolve_mgr; extern const BusLocator* const bus_systemd_mgr; extern const BusLocator* const bus_timedate; + +/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface strings encapsulated + * within a single struct. */ +int bus_call_method_async(sd_bus *bus, sd_bus_slot **slot, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata, const char *types, ...); +int bus_call_method(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *types, ...); +int bus_get_property(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *type); +int bus_get_property_trivial(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char type, void *ptr); +int bus_get_property_string(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char **ret); +int bus_get_property_strv(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char ***ret); +int bus_set_property(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, const char *type, ...); +int bus_match_signal(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata); +int bus_match_signal_async(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata); +int bus_message_new_method_call(sd_bus *bus, sd_bus_message **m, const BusLocator *locator, const char *member); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index a43fe66888..b018a9e527 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -1396,153 +1396,3 @@ const struct hash_ops bus_message_hash_ops = { .compare = trivial_compare_func, .free_value = bus_message_unref_wrapper, }; - -/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface - * strings encapsulated within a single struct. - */ -int bus_call_method_async( - sd_bus *bus, - sd_bus_slot **slot, - const BusLocator *locator, - const char *member, - sd_bus_message_handler_t callback, - void *userdata, - const char *types, ...) { - - va_list ap; - int r; - - assert(locator); - - va_start(ap, types); - r = sd_bus_call_method_asyncv(bus, slot, locator->destination, locator->path, locator->interface, member, callback, userdata, types, ap); - va_end(ap); - - return r; -} - -int bus_call_method( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - sd_bus_message **reply, - const char *types, ...) { - - va_list ap; - int r; - - assert(locator); - - va_start(ap, types); - r = sd_bus_call_methodv(bus, locator->destination, locator->path, locator->interface, member, error, reply, types, ap); - va_end(ap); - - return r; -} - -int bus_get_property( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - sd_bus_message **reply, - const char *type) { - - assert(locator); - - return sd_bus_get_property(bus, locator->destination, locator->path, locator->interface, member, error, reply, type); -} - -int bus_get_property_trivial( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - char type, void *ptr) { - - assert(locator); - - return sd_bus_get_property_trivial(bus, locator->destination, locator->path, locator->interface, member, error, type, ptr); -} - -int bus_get_property_string( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - char **ret) { - - assert(locator); - - return sd_bus_get_property_string(bus, locator->destination, locator->path, locator->interface, member, error, ret); -} - -int bus_get_property_strv( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - char ***ret) { - - assert(locator); - - return sd_bus_get_property_strv(bus, locator->destination, locator->path, locator->interface, member, error, ret); -} - -int bus_set_property( - sd_bus *bus, - const BusLocator *locator, - const char *member, - sd_bus_error *error, - const char *type, ...) { - - va_list ap; - int r; - - assert(locator); - - va_start(ap, type); - r = sd_bus_set_propertyv(bus, locator->destination, locator->path, locator->interface, member, error, type, ap); - va_end(ap); - - return r; -} - -int bus_match_signal( - sd_bus *bus, - sd_bus_slot **ret, - const BusLocator *locator, - const char *member, - sd_bus_message_handler_t callback, - void *userdata) { - - assert(locator); - - return sd_bus_match_signal(bus, ret, locator->destination, locator->path, locator->interface, member, callback, userdata); -} - -int bus_match_signal_async( - sd_bus *bus, - sd_bus_slot **ret, - const BusLocator *locator, - const char *member, - sd_bus_message_handler_t callback, - sd_bus_message_handler_t install_callback, - void *userdata) { - - assert(locator); - - return sd_bus_match_signal_async(bus, ret, locator->destination, locator->path, locator->interface, member, callback, install_callback, userdata); -} - -int bus_message_new_method_call( - sd_bus *bus, - sd_bus_message **m, - const BusLocator *locator, - const char *member) { - - assert(locator); - - return sd_bus_message_new_method_call(bus, m, locator->destination, locator->path, locator->interface, member); -} diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 1d44cd7676..db245a791e 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -9,7 +9,6 @@ #include "sd-bus.h" #include "sd-event.h" -#include "bus-locator.h" #include "macro.h" #include "set.h" #include "string-util.h" @@ -23,7 +22,6 @@ typedef enum BusTransport { _BUS_TRANSPORT_INVALID = -1 } BusTransport; - typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); struct bus_properties_map { @@ -181,17 +179,3 @@ static inline int bus_open_system_watch_bind(sd_bus **ret) { int bus_reply_pair_array(sd_bus_message *m, char **l); extern const struct hash_ops bus_message_hash_ops; - -/* Shorthand flavors of the sd-bus convenience helpers with destination,path,interface - * strings encapsulated within a single struct. - */ -int bus_call_method_async(sd_bus *bus, sd_bus_slot **slot, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata, const char *types, ...); -int bus_call_method(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *types, ...); -int bus_get_property(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, sd_bus_message **reply, const char *type); -int bus_get_property_trivial(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char type, void *ptr); -int bus_get_property_string(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char **ret); -int bus_get_property_strv(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, char ***ret); -int bus_set_property(sd_bus *bus, const BusLocator *locator, const char *member, sd_bus_error *error, const char *type, ...); -int bus_match_signal(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, void *userdata); -int bus_match_signal_async(sd_bus *bus, sd_bus_slot **ret, const BusLocator *locator, const char *member, sd_bus_message_handler_t callback, sd_bus_message_handler_t install_callback, void *userdata); -int bus_message_new_method_call(sd_bus *bus, sd_bus_message **m, const BusLocator *locator, const char *member); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 678982c61c..c937b1e298 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -20,10 +20,10 @@ #include "bootspec.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-message.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" -#include "bus-util.h" #include "bus-wait-for-jobs.h" #include "bus-wait-for-units.h" #include "cgroup-show.h" diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 285eba802b..97768808b7 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -9,15 +9,15 @@ #include "sd-bus.h" #include "bus-error.h" -#include "bus-util.h" +#include "bus-locator.h" #include "format-table.h" #include "in-addr-util.h" #include "main-func.h" #include "pager.h" #include "parse-util.h" #include "pretty-print.h" -#include "spawn-polkit-agent.h" #include "sparse-endian.h" +#include "spawn-polkit-agent.h" #include "string-table.h" #include "strv.h" #include "terminal-util.h" diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index afe593ff64..8ecd9d5dd3 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -12,9 +12,9 @@ #include "alloc-util.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-locator.h" #include "bus-log-control-api.h" #include "bus-polkit.h" -#include "bus-util.h" #include "clock-util.h" #include "conf-files.h" #include "def.h" From 807542beceb75b1461ad39538c6a0504e3cfef8d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Jun 2020 16:08:37 +0200 Subject: [PATCH 3/5] shared: split out code that maps properties to local structs Just some refactoring, no code changes. --- src/analyze/analyze-security.c | 1 + src/analyze/analyze.c | 1 + src/hostname/hostnamectl.c | 2 +- src/locale/localectl.c | 1 + src/login/loginctl.c | 1 + src/machine/machinectl.c | 1 + src/resolve/resolvectl.c | 1 + src/run/run.c | 1 + src/shared/bus-map-properties.c | 229 ++++++++++++++++++++++++++++++++ src/shared/bus-map-properties.h | 24 ++++ src/shared/bus-util.c | 223 ------------------------------- src/shared/bus-util.h | 20 --- src/shared/bus-wait-for-units.c | 2 +- src/shared/meson.build | 2 + src/systemctl/systemctl.c | 1 + src/timedate/timedatectl.c | 1 + src/timedate/timedated.c | 1 + 17 files changed, 267 insertions(+), 245 deletions(-) create mode 100644 src/shared/bus-map-properties.c create mode 100644 src/shared/bus-map-properties.h diff --git a/src/analyze/analyze-security.c b/src/analyze/analyze-security.c index 7b32fcb7b7..441fb0075c 100644 --- a/src/analyze/analyze-security.c +++ b/src/analyze/analyze-security.c @@ -4,6 +4,7 @@ #include "analyze-security.h" #include "bus-error.h" +#include "bus-map-properties.h" #include "bus-unit-util.h" #include "bus-util.h" #include "env-util.h" diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 8911d95c07..e42f7ff935 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -18,6 +18,7 @@ #include "build.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "bus-unit-util.h" #include "calendarspec.h" #include "conf-files.h" diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index e08ab15acd..348d2b0429 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -12,7 +12,7 @@ #include "alloc-util.h" #include "architecture.h" #include "bus-error.h" -#include "bus-util.h" +#include "bus-map-properties.h" #include "hostname-util.h" #include "main-func.h" #include "pretty-print.h" diff --git a/src/locale/localectl.c b/src/locale/localectl.c index f00f15f3fe..251901c391 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -9,6 +9,7 @@ #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "fd-util.h" #include "fileio.h" #include "kbd-util.h" diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 85b6e162f1..6302589a48 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -10,6 +10,7 @@ #include "alloc-util.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "bus-unit-procs.h" #include "cgroup-show.h" #include "cgroup-util.h" diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index bdb384fe58..6d124b418c 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -17,6 +17,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" #include "bus-wait-for-jobs.h" diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 838da9aae9..b40f159c76 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -12,6 +12,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "dns-domain.h" #include "escape.h" #include "format-table.h" diff --git a/src/run/run.c b/src/run/run.c index 80ddd0f521..6b996015e3 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -12,6 +12,7 @@ #include "alloc-util.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "bus-unit-util.h" #include "bus-wait-for-jobs.h" #include "calendarspec.h" diff --git a/src/shared/bus-map-properties.c b/src/shared/bus-map-properties.c new file mode 100644 index 0000000000..ab393c3952 --- /dev/null +++ b/src/shared/bus-map-properties.c @@ -0,0 +1,229 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "bus-map-properties.h" +#include "alloc-util.h" +#include "strv.h" +#include "bus-message.h" + +int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { + sd_id128_t *p = userdata; + const void *v; + size_t n; + int r; + + r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n); + if (r < 0) + return r; + + if (n == 0) + *p = SD_ID128_NULL; + else if (n == 16) + memcpy((*p).bytes, v, n); + else + return -EINVAL; + + return 0; +} + +static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) { + char type; + int r; + + r = sd_bus_message_peek_type(m, &type, NULL); + if (r < 0) + return r; + + switch (type) { + + case SD_BUS_TYPE_STRING: + case SD_BUS_TYPE_OBJECT_PATH: { + const char **p = userdata; + const char *s; + + r = sd_bus_message_read_basic(m, type, &s); + if (r < 0) + return r; + + if (isempty(s)) + s = NULL; + + if (flags & BUS_MAP_STRDUP) + return free_and_strdup((char **) userdata, s); + + *p = s; + return 0; + } + + case SD_BUS_TYPE_ARRAY: { + _cleanup_strv_free_ char **l = NULL; + char ***p = userdata; + + r = bus_message_read_strv_extend(m, &l); + if (r < 0) + return r; + + return strv_extend_strv(p, l, false); + } + + case SD_BUS_TYPE_BOOLEAN: { + int b; + + r = sd_bus_message_read_basic(m, type, &b); + if (r < 0) + return r; + + if (flags & BUS_MAP_BOOLEAN_AS_BOOL) + *(bool*) userdata = b; + else + *(int*) userdata = b; + + return 0; + } + + case SD_BUS_TYPE_INT32: + case SD_BUS_TYPE_UINT32: { + uint32_t u, *p = userdata; + + r = sd_bus_message_read_basic(m, type, &u); + if (r < 0) + return r; + + *p = u; + return 0; + } + + case SD_BUS_TYPE_INT64: + case SD_BUS_TYPE_UINT64: { + uint64_t t, *p = userdata; + + r = sd_bus_message_read_basic(m, type, &t); + if (r < 0) + return r; + + *p = t; + return 0; + } + + case SD_BUS_TYPE_DOUBLE: { + double d, *p = userdata; + + r = sd_bus_message_read_basic(m, type, &d); + if (r < 0) + return r; + + *p = d; + return 0; + }} + + return -EOPNOTSUPP; +} + +int bus_message_map_all_properties( + sd_bus_message *m, + const struct bus_properties_map *map, + unsigned flags, + sd_bus_error *error, + void *userdata) { + + int r; + + assert(m); + assert(map); + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); + if (r < 0) + return r; + + while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { + const struct bus_properties_map *prop; + const char *member; + const char *contents; + void *v; + unsigned i; + + r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member); + if (r < 0) + return r; + + for (i = 0, prop = NULL; map[i].member; i++) + if (streq(map[i].member, member)) { + prop = &map[i]; + break; + } + + if (prop) { + r = sd_bus_message_peek_type(m, NULL, &contents); + if (r < 0) + return r; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); + if (r < 0) + return r; + + v = (uint8_t *)userdata + prop->offset; + if (map[i].set) + r = prop->set(sd_bus_message_get_bus(m), member, m, error, v); + else + r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v); + if (r < 0) + return r; + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + } else { + r = sd_bus_message_skip(m, "v"); + if (r < 0) + return r; + } + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + } + if (r < 0) + return r; + + return sd_bus_message_exit_container(m); +} + +int bus_map_all_properties( + sd_bus *bus, + const char *destination, + const char *path, + const struct bus_properties_map *map, + unsigned flags, + sd_bus_error *error, + sd_bus_message **reply, + void *userdata) { + + _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; + int r; + + assert(bus); + assert(destination); + assert(path); + assert(map); + assert(reply || (flags & BUS_MAP_STRDUP)); + + r = sd_bus_call_method( + bus, + destination, + path, + "org.freedesktop.DBus.Properties", + "GetAll", + error, + &m, + "s", ""); + if (r < 0) + return r; + + r = bus_message_map_all_properties(m, map, flags, error, userdata); + if (r < 0) + return r; + + if (reply) + *reply = sd_bus_message_ref(m); + + return r; +} diff --git a/src/shared/bus-map-properties.h b/src/shared/bus-map-properties.h new file mode 100644 index 0000000000..11b899227b --- /dev/null +++ b/src/shared/bus-map-properties.h @@ -0,0 +1,24 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include "sd-bus.h" + +typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); + +struct bus_properties_map { + const char *member; + const char *signature; + bus_property_set_t set; + size_t offset; +}; + +enum { + BUS_MAP_STRDUP = 1 << 0, /* If set, each "s" message is duplicated. Thus, each pointer needs to be freed. */ + BUS_MAP_BOOLEAN_AS_BOOL = 1 << 1, /* If set, each "b" message is written to a bool pointer. If not set, "b" is written to a int pointer. */ +}; + +int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); + +int bus_message_map_all_properties(sd_bus_message *m, const struct bus_properties_map *map, unsigned flags, sd_bus_error *error, void *userdata); +int bus_map_all_properties(sd_bus *bus, const char *destination, const char *path, const struct bus_properties_map *map, + unsigned flags, sd_bus_error *error, sd_bus_message **reply, void *userdata); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index b018a9e527..fc5ac7a057 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -701,229 +701,6 @@ int bus_print_all_properties( return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); } -int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) { - sd_id128_t *p = userdata; - const void *v; - size_t n; - int r; - - r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, &v, &n); - if (r < 0) - return r; - - if (n == 0) - *p = SD_ID128_NULL; - else if (n == 16) - memcpy((*p).bytes, v, n); - else - return -EINVAL; - - return 0; -} - -static int map_basic(sd_bus *bus, const char *member, sd_bus_message *m, unsigned flags, sd_bus_error *error, void *userdata) { - char type; - int r; - - r = sd_bus_message_peek_type(m, &type, NULL); - if (r < 0) - return r; - - switch (type) { - - case SD_BUS_TYPE_STRING: - case SD_BUS_TYPE_OBJECT_PATH: { - const char **p = userdata; - const char *s; - - r = sd_bus_message_read_basic(m, type, &s); - if (r < 0) - return r; - - if (isempty(s)) - s = NULL; - - if (flags & BUS_MAP_STRDUP) - return free_and_strdup((char **) userdata, s); - - *p = s; - return 0; - } - - case SD_BUS_TYPE_ARRAY: { - _cleanup_strv_free_ char **l = NULL; - char ***p = userdata; - - r = bus_message_read_strv_extend(m, &l); - if (r < 0) - return r; - - return strv_extend_strv(p, l, false); - } - - case SD_BUS_TYPE_BOOLEAN: { - int b; - - r = sd_bus_message_read_basic(m, type, &b); - if (r < 0) - return r; - - if (flags & BUS_MAP_BOOLEAN_AS_BOOL) - *(bool*) userdata = b; - else - *(int*) userdata = b; - - return 0; - } - - case SD_BUS_TYPE_INT32: - case SD_BUS_TYPE_UINT32: { - uint32_t u, *p = userdata; - - r = sd_bus_message_read_basic(m, type, &u); - if (r < 0) - return r; - - *p = u; - return 0; - } - - case SD_BUS_TYPE_INT64: - case SD_BUS_TYPE_UINT64: { - uint64_t t, *p = userdata; - - r = sd_bus_message_read_basic(m, type, &t); - if (r < 0) - return r; - - *p = t; - return 0; - } - - case SD_BUS_TYPE_DOUBLE: { - double d, *p = userdata; - - r = sd_bus_message_read_basic(m, type, &d); - if (r < 0) - return r; - - *p = d; - return 0; - }} - - return -EOPNOTSUPP; -} - -int bus_message_map_all_properties( - sd_bus_message *m, - const struct bus_properties_map *map, - unsigned flags, - sd_bus_error *error, - void *userdata) { - - int r; - - assert(m); - assert(map); - - r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); - if (r < 0) - return r; - - while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { - const struct bus_properties_map *prop; - const char *member; - const char *contents; - void *v; - unsigned i; - - r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &member); - if (r < 0) - return r; - - for (i = 0, prop = NULL; map[i].member; i++) - if (streq(map[i].member, member)) { - prop = &map[i]; - break; - } - - if (prop) { - r = sd_bus_message_peek_type(m, NULL, &contents); - if (r < 0) - return r; - - r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); - if (r < 0) - return r; - - v = (uint8_t *)userdata + prop->offset; - if (map[i].set) - r = prop->set(sd_bus_message_get_bus(m), member, m, error, v); - else - r = map_basic(sd_bus_message_get_bus(m), member, m, flags, error, v); - if (r < 0) - return r; - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - } else { - r = sd_bus_message_skip(m, "v"); - if (r < 0) - return r; - } - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - } - if (r < 0) - return r; - - return sd_bus_message_exit_container(m); -} - -int bus_map_all_properties( - sd_bus *bus, - const char *destination, - const char *path, - const struct bus_properties_map *map, - unsigned flags, - sd_bus_error *error, - sd_bus_message **reply, - void *userdata) { - - _cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL; - int r; - - assert(bus); - assert(destination); - assert(path); - assert(map); - assert(reply || (flags & BUS_MAP_STRDUP)); - - r = sd_bus_call_method( - bus, - destination, - path, - "org.freedesktop.DBus.Properties", - "GetAll", - error, - &m, - "s", ""); - if (r < 0) - return r; - - r = bus_message_map_all_properties(m, map, flags, error, userdata); - if (r < 0) - return r; - - if (reply) - *reply = sd_bus_message_ref(m); - - return r; -} - int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) { _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; int r; diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index db245a791e..8f700c3cfd 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -22,26 +22,6 @@ typedef enum BusTransport { _BUS_TRANSPORT_INVALID = -1 } BusTransport; -typedef int (*bus_property_set_t) (sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); - -struct bus_properties_map { - const char *member; - const char *signature; - bus_property_set_t set; - size_t offset; -}; - -enum { - BUS_MAP_STRDUP = 1 << 0, /* If set, each "s" message is duplicated. Thus, each pointer needs to be freed. */ - BUS_MAP_BOOLEAN_AS_BOOL = 1 << 1, /* If set, each "b" message is written to a bool pointer. If not set, "b" is written to a int pointer. */ -}; - -int bus_map_id128(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata); - -int bus_message_map_all_properties(sd_bus_message *m, const struct bus_properties_map *map, unsigned flags, sd_bus_error *error, void *userdata); -int bus_map_all_properties(sd_bus *bus, const char *destination, const char *path, const struct bus_properties_map *map, - unsigned flags, sd_bus_error *error, sd_bus_message **reply, void *userdata); - int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name); typedef bool (*check_idle_t)(void *userdata); diff --git a/src/shared/bus-wait-for-units.c b/src/shared/bus-wait-for-units.c index 3ee3c0ccba..7592dcf18d 100644 --- a/src/shared/bus-wait-for-units.c +++ b/src/shared/bus-wait-for-units.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include "bus-util.h" +#include "bus-map-properties.h" #include "bus-wait-for-units.h" #include "hashmap.h" #include "string-util.h" diff --git a/src/shared/meson.build b/src/shared/meson.build index 30ece1f193..881e30835e 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -31,6 +31,8 @@ shared_sources = files(''' bus-locator.h bus-log-control-api.c bus-log-control-api.h + bus-map-properties.c + bus-map-properties.h bus-object.c bus-object.h bus-polkit.c diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index c937b1e298..3bc32065a5 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -21,6 +21,7 @@ #include "bus-common-errors.h" #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "bus-message.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 97768808b7..9ee995802c 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -10,6 +10,7 @@ #include "bus-error.h" #include "bus-locator.h" +#include "bus-map-properties.h" #include "format-table.h" #include "in-addr-util.h" #include "main-func.h" diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 8ecd9d5dd3..d7d9305711 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -14,6 +14,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-log-control-api.h" +#include "bus-map-properties.h" #include "bus-polkit.h" #include "clock-util.h" #include "conf-files.h" From 9176326ba2f2167076269acf7573a4a99da82a2d Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Jun 2020 16:17:46 +0200 Subject: [PATCH 4/5] shared: split out code for printing properties No code changes, just some refactoring. --- src/login/loginctl.c | 1 + src/machine/machinectl.c | 1 + src/shared/bus-print-properties.c | 462 ++++++++++++++++++++++++++++++ src/shared/bus-print-properties.h | 16 ++ src/shared/bus-util.c | 457 ----------------------------- src/shared/bus-util.h | 8 - src/shared/meson.build | 2 + src/systemctl/systemctl.c | 1 + src/timedate/timedatectl.c | 1 + 9 files changed, 484 insertions(+), 465 deletions(-) create mode 100644 src/shared/bus-print-properties.c create mode 100644 src/shared/bus-print-properties.h diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 6302589a48..ee617929ff 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -11,6 +11,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-map-properties.h" +#include "bus-print-properties.h" #include "bus-unit-procs.h" #include "cgroup-show.h" #include "cgroup-util.h" diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 6d124b418c..f41cf464b1 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -18,6 +18,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-map-properties.h" +#include "bus-print-properties.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" #include "bus-wait-for-jobs.h" diff --git a/src/shared/bus-print-properties.c b/src/shared/bus-print-properties.c new file mode 100644 index 0000000000..bf7d050fa3 --- /dev/null +++ b/src/shared/bus-print-properties.c @@ -0,0 +1,462 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "bus-print-properties.h" +#include "cap-list.h" +#include "cgroup-util.h" +#include "escape.h" +#include "mountpoint-util.h" +#include "nsflags.h" +#include "parse-util.h" +#include "stdio-util.h" +#include "string-util.h" +#include "strv.h" +#include "time-util.h" +#include "user-util.h" + +int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) { + assert(name); + + if (expected_value && !streq_ptr(expected_value, value)) + return 0; + + if (only_value) + puts(value); + else + printf("%s=%s\n", name, value); + + return 0; +} + +int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) { + va_list ap; + int r; + + assert(name); + assert(fmt); + + if (expected_value) { + _cleanup_free_ char *s = NULL; + + va_start(ap, fmt); + r = vasprintf(&s, fmt, ap); + va_end(ap); + if (r < 0) + return -ENOMEM; + + if (streq_ptr(expected_value, s)) { + if (only_value) + puts(s); + else + printf("%s=%s\n", name, s); + } + + return 0; + } + + if (!only_value) + printf("%s=", name); + va_start(ap, fmt); + vprintf(fmt, ap); + va_end(ap); + puts(""); + + return 0; +} + +static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { + char type; + const char *contents; + int r; + + assert(name); + assert(m); + + r = sd_bus_message_peek_type(m, &type, &contents); + if (r < 0) + return r; + + switch (type) { + + case SD_BUS_TYPE_STRING: { + const char *s; + + r = sd_bus_message_read_basic(m, type, &s); + if (r < 0) + return r; + + if (all || !isempty(s)) { + bool good; + + /* This property has a single value, so we need to take + * care not to print a new line, everything else is OK. */ + good = !strchr(s, '\n'); + bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]"); + } + + return 1; + } + + case SD_BUS_TYPE_BOOLEAN: { + int b; + + r = sd_bus_message_read_basic(m, type, &b); + if (r < 0) + return r; + + if (expected_value && parse_boolean(expected_value) != b) + return 1; + + bus_print_property_value(name, NULL, value, yes_no(b)); + return 1; + } + + case SD_BUS_TYPE_UINT64: { + uint64_t u; + + r = sd_bus_message_read_basic(m, type, &u); + if (r < 0) + return r; + + /* Yes, heuristics! But we can change this check + * should it turn out to not be sufficient */ + + if (endswith(name, "Timestamp") || + STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) { + char timestamp[FORMAT_TIMESTAMP_MAX]; + const char *t; + + t = format_timestamp(timestamp, sizeof(timestamp), u); + if (t || all) + bus_print_property_value(name, expected_value, value, strempty(t)); + + } else if (strstr(name, "USec")) { + char timespan[FORMAT_TIMESPAN_MAX]; + + (void) format_timespan(timespan, sizeof(timespan), u, 0); + bus_print_property_value(name, expected_value, value, timespan); + + } else if (streq(name, "CoredumpFilter")) { + char buf[STRLEN("0xFFFFFFFF")]; + + xsprintf(buf, "0x%"PRIx64, u); + bus_print_property_value(name, expected_value, value, buf); + + } else if (streq(name, "RestrictNamespaces")) { + _cleanup_free_ char *s = NULL; + const char *result; + + if ((u & NAMESPACE_FLAGS_ALL) == 0) + result = "yes"; + else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL)) + result = "no"; + else { + r = namespace_flags_to_string(u, &s); + if (r < 0) + return r; + + result = strempty(s); + } + + bus_print_property_value(name, expected_value, value, result); + + } else if (streq(name, "MountFlags")) { + const char *result; + + result = mount_propagation_flags_to_string(u); + if (!result) + return -EINVAL; + + bus_print_property_value(name, expected_value, value, result); + + } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) { + _cleanup_free_ char *s = NULL; + + r = capability_set_to_string_alloc(u, &s); + if (r < 0) + return r; + + bus_print_property_value(name, expected_value, value, s); + + } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) || + (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) || + (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) || + (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) || + (endswith(name, "NSec") && u == (uint64_t) -1)) + + bus_print_property_value(name, expected_value, value, "[not set]"); + + else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) || + (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) || + (startswith(name, "Limit") && u == (uint64_t) -1) || + (startswith(name, "DefaultLimit") && u == (uint64_t) -1)) + + bus_print_property_value(name, expected_value, value, "infinity"); + else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == (uint64_t) -1) + bus_print_property_value(name, expected_value, value, "[no data]"); + else + bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u); + + return 1; + } + + case SD_BUS_TYPE_INT64: { + int64_t i; + + r = sd_bus_message_read_basic(m, type, &i); + if (r < 0) + return r; + + bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i); + return 1; + } + + case SD_BUS_TYPE_UINT32: { + uint32_t u; + + r = sd_bus_message_read_basic(m, type, &u); + if (r < 0) + return r; + + if (strstr(name, "UMask") || strstr(name, "Mode")) + bus_print_property_valuef(name, expected_value, value, "%04o", u); + + else if (streq(name, "UID")) { + if (u == UID_INVALID) + bus_print_property_value(name, expected_value, value, "[not set]"); + else + bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); + } else if (streq(name, "GID")) { + if (u == GID_INVALID) + bus_print_property_value(name, expected_value, value, "[not set]"); + else + bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); + } else + bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); + + return 1; + } + + case SD_BUS_TYPE_INT32: { + int32_t i; + + r = sd_bus_message_read_basic(m, type, &i); + if (r < 0) + return r; + + bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); + return 1; + } + + case SD_BUS_TYPE_DOUBLE: { + double d; + + r = sd_bus_message_read_basic(m, type, &d); + if (r < 0) + return r; + + bus_print_property_valuef(name, expected_value, value, "%g", d); + return 1; + } + + case SD_BUS_TYPE_ARRAY: + if (streq(contents, "s")) { + bool first = true; + const char *str; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); + if (r < 0) + return r; + + while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { + _cleanup_free_ char *e = NULL; + + e = shell_maybe_quote(str, ESCAPE_BACKSLASH_ONELINE); + if (!e) + return -ENOMEM; + + if (first) { + if (!value) + printf("%s=", name); + first = false; + } else + fputs(" ", stdout); + + fputs(e, stdout); + } + if (r < 0) + return r; + + if (first && all && !value) + printf("%s=", name); + if (!first || all) + puts(""); + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + + return 1; + + } else if (streq(contents, "y")) { + const uint8_t *u; + size_t n; + + r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); + if (r < 0) + return r; + + if (all || n > 0) { + unsigned i; + + if (!value) + printf("%s=", name); + + for (i = 0; i < n; i++) + printf("%02x", u[i]); + + puts(""); + } + + return 1; + + } else if (streq(contents, "u")) { + uint32_t *u; + size_t n; + + r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); + if (r < 0) + return r; + + if (all || n > 0) { + unsigned i; + + if (!value) + printf("%s=", name); + + for (i = 0; i < n; i++) + printf("%08x", u[i]); + + puts(""); + } + + return 1; + } + + break; + } + + return 0; +} + +int bus_message_print_all_properties( + sd_bus_message *m, + bus_message_print_t func, + char **filter, + bool value, + bool all, + Set **found_properties) { + + int r; + + assert(m); + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); + if (r < 0) + return r; + + while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { + _cleanup_free_ char *name_with_equal = NULL; + const char *name, *contents, *expected_value = NULL; + + r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); + if (r < 0) + return r; + + if (found_properties) { + r = set_ensure_put(found_properties, &string_hash_ops, name); + if (r < 0) + return log_oom(); + } + + name_with_equal = strjoin(name, "="); + if (!name_with_equal) + return log_oom(); + + if (!filter || strv_find(filter, name) || + (expected_value = strv_find_startswith(filter, name_with_equal))) { + r = sd_bus_message_peek_type(m, NULL, &contents); + if (r < 0) + return r; + + r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); + if (r < 0) + return r; + + if (func) + r = func(name, expected_value, m, value, all); + if (!func || r == 0) + r = bus_print_property(name, expected_value, m, value, all); + if (r < 0) + return r; + if (r == 0) { + if (all && !expected_value) + printf("%s=[unprintable]\n", name); + /* skip what we didn't read */ + r = sd_bus_message_skip(m, contents); + if (r < 0) + return r; + } + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + } else { + r = sd_bus_message_skip(m, "v"); + if (r < 0) + return r; + } + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + } + if (r < 0) + return r; + + r = sd_bus_message_exit_container(m); + if (r < 0) + return r; + + return 0; +} + +int bus_print_all_properties( + sd_bus *bus, + const char *dest, + const char *path, + bus_message_print_t func, + char **filter, + bool value, + bool all, + Set **found_properties) { + + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; + _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; + int r; + + assert(bus); + assert(path); + + r = sd_bus_call_method(bus, + dest, + path, + "org.freedesktop.DBus.Properties", + "GetAll", + &error, + &reply, + "s", ""); + if (r < 0) + return r; + + return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); +} diff --git a/src/shared/bus-print-properties.h b/src/shared/bus-print-properties.h new file mode 100644 index 0000000000..1c21dff2c8 --- /dev/null +++ b/src/shared/bus-print-properties.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include + +#include "sd-bus.h" + +#include "macro.h" +#include "set.h" + +typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all); + +int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value); +int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) _printf_(4,5); +int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); +int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index fc5ac7a057..85b5bedbc0 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -17,21 +17,12 @@ #include "alloc-util.h" #include "bus-internal.h" #include "bus-label.h" -#include "bus-message.h" #include "bus-util.h" -#include "cap-list.h" -#include "cgroup-util.h" -#include "escape.h" -#include "mountpoint-util.h" -#include "nsflags.h" -#include "parse-util.h" #include "path-util.h" #include "rlimit-util.h" #include "socket-util.h" #include "stdio-util.h" #include "string-util.h" -#include "strv.h" -#include "user-util.h" static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { sd_event *e = userdata; @@ -253,454 +244,6 @@ int bus_connect_user_systemd(sd_bus **_bus) { return 0; } -int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value) { - assert(name); - - if (expected_value && !streq_ptr(expected_value, value)) - return 0; - - if (only_value) - puts(value); - else - printf("%s=%s\n", name, value); - - return 0; -} - -int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) { - va_list ap; - int r; - - assert(name); - assert(fmt); - - if (expected_value) { - _cleanup_free_ char *s = NULL; - - va_start(ap, fmt); - r = vasprintf(&s, fmt, ap); - va_end(ap); - if (r < 0) - return -ENOMEM; - - if (streq_ptr(expected_value, s)) { - if (only_value) - puts(s); - else - printf("%s=%s\n", name, s); - } - - return 0; - } - - if (!only_value) - printf("%s=", name); - va_start(ap, fmt); - vprintf(fmt, ap); - va_end(ap); - puts(""); - - return 0; -} - -static int bus_print_property(const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all) { - char type; - const char *contents; - int r; - - assert(name); - assert(m); - - r = sd_bus_message_peek_type(m, &type, &contents); - if (r < 0) - return r; - - switch (type) { - - case SD_BUS_TYPE_STRING: { - const char *s; - - r = sd_bus_message_read_basic(m, type, &s); - if (r < 0) - return r; - - if (all || !isempty(s)) { - bool good; - - /* This property has a single value, so we need to take - * care not to print a new line, everything else is OK. */ - good = !strchr(s, '\n'); - bus_print_property_value(name, expected_value, value, good ? s : "[unprintable]"); - } - - return 1; - } - - case SD_BUS_TYPE_BOOLEAN: { - int b; - - r = sd_bus_message_read_basic(m, type, &b); - if (r < 0) - return r; - - if (expected_value && parse_boolean(expected_value) != b) - return 1; - - bus_print_property_value(name, NULL, value, yes_no(b)); - return 1; - } - - case SD_BUS_TYPE_UINT64: { - uint64_t u; - - r = sd_bus_message_read_basic(m, type, &u); - if (r < 0) - return r; - - /* Yes, heuristics! But we can change this check - * should it turn out to not be sufficient */ - - if (endswith(name, "Timestamp") || - STR_IN_SET(name, "NextElapseUSecRealtime", "LastTriggerUSec", "TimeUSec", "RTCTimeUSec")) { - char timestamp[FORMAT_TIMESTAMP_MAX]; - const char *t; - - t = format_timestamp(timestamp, sizeof(timestamp), u); - if (t || all) - bus_print_property_value(name, expected_value, value, strempty(t)); - - } else if (strstr(name, "USec")) { - char timespan[FORMAT_TIMESPAN_MAX]; - - (void) format_timespan(timespan, sizeof(timespan), u, 0); - bus_print_property_value(name, expected_value, value, timespan); - - } else if (streq(name, "CoredumpFilter")) { - char buf[STRLEN("0xFFFFFFFF")]; - - xsprintf(buf, "0x%"PRIx64, u); - bus_print_property_value(name, expected_value, value, buf); - - } else if (streq(name, "RestrictNamespaces")) { - _cleanup_free_ char *s = NULL; - const char *result; - - if ((u & NAMESPACE_FLAGS_ALL) == 0) - result = "yes"; - else if (FLAGS_SET(u, NAMESPACE_FLAGS_ALL)) - result = "no"; - else { - r = namespace_flags_to_string(u, &s); - if (r < 0) - return r; - - result = strempty(s); - } - - bus_print_property_value(name, expected_value, value, result); - - } else if (streq(name, "MountFlags")) { - const char *result; - - result = mount_propagation_flags_to_string(u); - if (!result) - return -EINVAL; - - bus_print_property_value(name, expected_value, value, result); - - } else if (STR_IN_SET(name, "CapabilityBoundingSet", "AmbientCapabilities")) { - _cleanup_free_ char *s = NULL; - - r = capability_set_to_string_alloc(u, &s); - if (r < 0) - return r; - - bus_print_property_value(name, expected_value, value, s); - - } else if ((STR_IN_SET(name, "CPUWeight", "StartupCPUWeight", "IOWeight", "StartupIOWeight") && u == CGROUP_WEIGHT_INVALID) || - (STR_IN_SET(name, "CPUShares", "StartupCPUShares") && u == CGROUP_CPU_SHARES_INVALID) || - (STR_IN_SET(name, "BlockIOWeight", "StartupBlockIOWeight") && u == CGROUP_BLKIO_WEIGHT_INVALID) || - (STR_IN_SET(name, "MemoryCurrent", "TasksCurrent") && u == (uint64_t) -1) || - (endswith(name, "NSec") && u == (uint64_t) -1)) - - bus_print_property_value(name, expected_value, value, "[not set]"); - - else if ((STR_IN_SET(name, "DefaultMemoryLow", "DefaultMemoryMin", "MemoryLow", "MemoryHigh", "MemoryMax", "MemorySwapMax", "MemoryLimit") && u == CGROUP_LIMIT_MAX) || - (STR_IN_SET(name, "TasksMax", "DefaultTasksMax") && u == (uint64_t) -1) || - (startswith(name, "Limit") && u == (uint64_t) -1) || - (startswith(name, "DefaultLimit") && u == (uint64_t) -1)) - - bus_print_property_value(name, expected_value, value, "infinity"); - else if (STR_IN_SET(name, "IPIngressBytes", "IPIngressPackets", "IPEgressBytes", "IPEgressPackets") && u == (uint64_t) -1) - bus_print_property_value(name, expected_value, value, "[no data]"); - else - bus_print_property_valuef(name, expected_value, value, "%"PRIu64, u); - - return 1; - } - - case SD_BUS_TYPE_INT64: { - int64_t i; - - r = sd_bus_message_read_basic(m, type, &i); - if (r < 0) - return r; - - bus_print_property_valuef(name, expected_value, value, "%"PRIi64, i); - return 1; - } - - case SD_BUS_TYPE_UINT32: { - uint32_t u; - - r = sd_bus_message_read_basic(m, type, &u); - if (r < 0) - return r; - - if (strstr(name, "UMask") || strstr(name, "Mode")) - bus_print_property_valuef(name, expected_value, value, "%04o", u); - - else if (streq(name, "UID")) { - if (u == UID_INVALID) - bus_print_property_value(name, expected_value, value, "[not set]"); - else - bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); - } else if (streq(name, "GID")) { - if (u == GID_INVALID) - bus_print_property_value(name, expected_value, value, "[not set]"); - else - bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); - } else - bus_print_property_valuef(name, expected_value, value, "%"PRIu32, u); - - return 1; - } - - case SD_BUS_TYPE_INT32: { - int32_t i; - - r = sd_bus_message_read_basic(m, type, &i); - if (r < 0) - return r; - - bus_print_property_valuef(name, expected_value, value, "%"PRIi32, i); - return 1; - } - - case SD_BUS_TYPE_DOUBLE: { - double d; - - r = sd_bus_message_read_basic(m, type, &d); - if (r < 0) - return r; - - bus_print_property_valuef(name, expected_value, value, "%g", d); - return 1; - } - - case SD_BUS_TYPE_ARRAY: - if (streq(contents, "s")) { - bool first = true; - const char *str; - - r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, contents); - if (r < 0) - return r; - - while ((r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &str)) > 0) { - _cleanup_free_ char *e = NULL; - - e = shell_maybe_quote(str, ESCAPE_BACKSLASH_ONELINE); - if (!e) - return -ENOMEM; - - if (first) { - if (!value) - printf("%s=", name); - first = false; - } else - fputs(" ", stdout); - - fputs(e, stdout); - } - if (r < 0) - return r; - - if (first && all && !value) - printf("%s=", name); - if (!first || all) - puts(""); - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - - return 1; - - } else if (streq(contents, "y")) { - const uint8_t *u; - size_t n; - - r = sd_bus_message_read_array(m, SD_BUS_TYPE_BYTE, (const void**) &u, &n); - if (r < 0) - return r; - - if (all || n > 0) { - unsigned i; - - if (!value) - printf("%s=", name); - - for (i = 0; i < n; i++) - printf("%02x", u[i]); - - puts(""); - } - - return 1; - - } else if (streq(contents, "u")) { - uint32_t *u; - size_t n; - - r = sd_bus_message_read_array(m, SD_BUS_TYPE_UINT32, (const void**) &u, &n); - if (r < 0) - return r; - - if (all || n > 0) { - unsigned i; - - if (!value) - printf("%s=", name); - - for (i = 0; i < n; i++) - printf("%08x", u[i]); - - puts(""); - } - - return 1; - } - - break; - } - - return 0; -} - -int bus_message_print_all_properties( - sd_bus_message *m, - bus_message_print_t func, - char **filter, - bool value, - bool all, - Set **found_properties) { - - int r; - - assert(m); - - r = sd_bus_message_enter_container(m, SD_BUS_TYPE_ARRAY, "{sv}"); - if (r < 0) - return r; - - while ((r = sd_bus_message_enter_container(m, SD_BUS_TYPE_DICT_ENTRY, "sv")) > 0) { - _cleanup_free_ char *name_with_equal = NULL; - const char *name, *contents, *expected_value = NULL; - - r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &name); - if (r < 0) - return r; - - if (found_properties) { - r = set_ensure_put(found_properties, &string_hash_ops, name); - if (r < 0) - return log_oom(); - } - - name_with_equal = strjoin(name, "="); - if (!name_with_equal) - return log_oom(); - - if (!filter || strv_find(filter, name) || - (expected_value = strv_find_startswith(filter, name_with_equal))) { - r = sd_bus_message_peek_type(m, NULL, &contents); - if (r < 0) - return r; - - r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, contents); - if (r < 0) - return r; - - if (func) - r = func(name, expected_value, m, value, all); - if (!func || r == 0) - r = bus_print_property(name, expected_value, m, value, all); - if (r < 0) - return r; - if (r == 0) { - if (all && !expected_value) - printf("%s=[unprintable]\n", name); - /* skip what we didn't read */ - r = sd_bus_message_skip(m, contents); - if (r < 0) - return r; - } - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - } else { - r = sd_bus_message_skip(m, "v"); - if (r < 0) - return r; - } - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - } - if (r < 0) - return r; - - r = sd_bus_message_exit_container(m); - if (r < 0) - return r; - - return 0; -} - -int bus_print_all_properties( - sd_bus *bus, - const char *dest, - const char *path, - bus_message_print_t func, - char **filter, - bool value, - bool all, - Set **found_properties) { - - _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; - _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; - int r; - - assert(bus); - assert(path); - - r = sd_bus_call_method(bus, - dest, - path, - "org.freedesktop.DBus.Properties", - "GetAll", - &error, - &reply, - "s", ""); - if (r < 0) - return r; - - return bus_message_print_all_properties(reply, func, filter, value, all, found_properties); -} - int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **ret) { _cleanup_(sd_bus_close_unrefp) sd_bus *bus = NULL; int r; diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 8f700c3cfd..4dbf74fdf7 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -10,7 +10,6 @@ #include "sd-event.h" #include "macro.h" -#include "set.h" #include "string-util.h" #include "time-util.h" @@ -38,13 +37,6 @@ int bus_connect_user_systemd(sd_bus **_bus); int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus); int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus); -typedef int (*bus_message_print_t) (const char *name, const char *expected_value, sd_bus_message *m, bool value, bool all); - -int bus_print_property_value(const char *name, const char *expected_value, bool only_value, const char *value); -int bus_print_property_valuef(const char *name, const char *expected_value, bool only_value, const char *fmt, ...) _printf_(4,5); -int bus_message_print_all_properties(sd_bus_message *m, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); -int bus_print_all_properties(sd_bus *bus, const char *dest, const char *path, bus_message_print_t func, char **filter, bool value, bool all, Set **found_properties); - int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); int bus_property_set_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error); int bus_property_get_id128(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); diff --git a/src/shared/meson.build b/src/shared/meson.build index 881e30835e..3bd680295c 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -37,6 +37,8 @@ shared_sources = files(''' bus-object.h bus-polkit.c bus-polkit.h + bus-print-properties.c + bus-print-properties.h bus-unit-procs.c bus-unit-procs.h bus-unit-util.c diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index 3bc32065a5..954e5080ea 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -23,6 +23,7 @@ #include "bus-locator.h" #include "bus-map-properties.h" #include "bus-message.h" +#include "bus-print-properties.h" #include "bus-unit-procs.h" #include "bus-unit-util.h" #include "bus-wait-for-jobs.h" diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 9ee995802c..96be78746d 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -11,6 +11,7 @@ #include "bus-error.h" #include "bus-locator.h" #include "bus-map-properties.h" +#include "bus-print-properties.h" #include "format-table.h" #include "in-addr-util.h" #include "main-func.h" From 40af3d020f461520ace2309834c4bdf4c5ddf97a Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 28 Jun 2020 16:43:29 +0200 Subject: [PATCH 5/5] shared: split out property get helpers No code changes, just some refactoring. --- src/core/dbus-automount.c | 2 +- src/core/dbus-cgroup.c | 2 +- src/core/dbus-execute.c | 2 +- src/core/dbus-job.c | 1 + src/core/dbus-kill.c | 2 +- src/core/dbus-manager.c | 2 +- src/core/dbus-mount.c | 2 +- src/core/dbus-path.c | 2 +- src/core/dbus-scope.c | 2 +- src/core/dbus-service.c | 2 +- src/core/dbus-socket.c | 2 +- src/core/dbus-swap.c | 2 +- src/core/dbus-timer.c | 2 +- src/core/dbus-unit.c | 2 +- src/import/importd.c | 4 +- src/login/logind-dbus.c | 1 + src/login/logind-seat-dbus.c | 1 + src/login/logind-session-dbus.c | 1 + src/login/logind-user-dbus.c | 1 + src/machine/image-dbus.c | 2 +- src/machine/machine-dbus.c | 1 + src/machine/machined-dbus.c | 1 + src/network/networkd-link-bus.c | 2 +- src/portable/portabled-image-bus.c | 1 + src/resolve/resolved-bus.c | 2 +- src/resolve/resolved-link-bus.c | 2 +- src/shared/bus-get-properties.c | 149 +++++++++++++++++++++++++++++ src/shared/bus-get-properties.h | 103 ++++++++++++++++++++ src/shared/bus-log-control-api.c | 1 + src/shared/bus-util.c | 149 +---------------------------- src/shared/bus-util.h | 95 ------------------ src/shared/meson.build | 2 + src/timedate/timedated.c | 1 + src/timesync/timesyncd-bus.c | 1 + 34 files changed, 286 insertions(+), 261 deletions(-) create mode 100644 src/shared/bus-get-properties.c create mode 100644 src/shared/bus-get-properties.h diff --git a/src/core/dbus-automount.c b/src/core/dbus-automount.c index bd6e6a9dde..70b85d8023 100644 --- a/src/core/dbus-automount.c +++ b/src/core/dbus-automount.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "automount.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-automount.h" #include "dbus-util.h" #include "string-util.h" diff --git a/src/core/dbus-cgroup.c b/src/core/dbus-cgroup.c index 080f6fb1ae..b7d2e32639 100644 --- a/src/core/dbus-cgroup.c +++ b/src/core/dbus-cgroup.c @@ -5,7 +5,7 @@ #include "af-list.h" #include "alloc-util.h" #include "bpf-firewall.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "cgroup-util.h" #include "cgroup.h" #include "dbus-cgroup.h" diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c index 41d64e8004..50f7ada8ce 100644 --- a/src/core/dbus-execute.c +++ b/src/core/dbus-execute.c @@ -9,7 +9,7 @@ #include "af-list.h" #include "alloc-util.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "cap-list.h" #include "capability-util.h" #include "cpu-set-util.h" diff --git a/src/core/dbus-job.c b/src/core/dbus-job.c index 404984f664..33e4128909 100644 --- a/src/core/dbus-job.c +++ b/src/core/dbus-job.c @@ -3,6 +3,7 @@ #include "sd-bus.h" #include "alloc-util.h" +#include "bus-get-properties.h" #include "bus-util.h" #include "dbus-job.h" #include "dbus-unit.h" diff --git a/src/core/dbus-kill.c b/src/core/dbus-kill.c index 30597e86f0..eda3410375 100644 --- a/src/core/dbus-kill.c +++ b/src/core/dbus-kill.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-kill.h" #include "dbus-util.h" #include "kill.h" diff --git a/src/core/dbus-manager.c b/src/core/dbus-manager.c index 5cb06867cb..07e139c5ad 100644 --- a/src/core/dbus-manager.c +++ b/src/core/dbus-manager.c @@ -9,8 +9,8 @@ #include "architecture.h" #include "build.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-log-control-api.h" -#include "bus-util.h" #include "dbus-cgroup.h" #include "dbus-execute.h" #include "dbus-job.h" diff --git a/src/core/dbus-mount.c b/src/core/dbus-mount.c index 3ab5ecc425..bab12cc4ff 100644 --- a/src/core/dbus-mount.c +++ b/src/core/dbus-mount.c @@ -1,6 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-cgroup.h" #include "dbus-execute.h" #include "dbus-kill.h" diff --git a/src/core/dbus-path.c b/src/core/dbus-path.c index 1a97d62486..76cd9d3260 100644 --- a/src/core/dbus-path.c +++ b/src/core/dbus-path.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-path.h" #include "dbus-util.h" #include "list.h" diff --git a/src/core/dbus-scope.c b/src/core/dbus-scope.c index 2d877a8c55..aecfda6535 100644 --- a/src/core/dbus-scope.c +++ b/src/core/dbus-scope.c @@ -2,7 +2,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-cgroup.h" #include "dbus-kill.h" #include "dbus-scope.h" diff --git a/src/core/dbus-service.c b/src/core/dbus-service.c index 5d4f4ef506..3cc453dff5 100644 --- a/src/core/dbus-service.c +++ b/src/core/dbus-service.c @@ -4,7 +4,7 @@ #include "alloc-util.h" #include "async.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-cgroup.h" #include "dbus-execute.h" #include "dbus-kill.h" diff --git a/src/core/dbus-socket.c b/src/core/dbus-socket.c index 73e6a74914..f01489e29a 100644 --- a/src/core/dbus-socket.c +++ b/src/core/dbus-socket.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-cgroup.h" #include "dbus-execute.h" #include "dbus-kill.h" diff --git a/src/core/dbus-swap.c b/src/core/dbus-swap.c index 57c8c42091..cb4824b6bd 100644 --- a/src/core/dbus-swap.c +++ b/src/core/dbus-swap.c @@ -3,7 +3,7 @@ Copyright © 2010 Maarten Lankhorst ***/ -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-cgroup.h" #include "dbus-execute.h" #include "dbus-swap.h" diff --git a/src/core/dbus-timer.c b/src/core/dbus-timer.c index 439c276fac..da35fa8678 100644 --- a/src/core/dbus-timer.c +++ b/src/core/dbus-timer.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" -#include "bus-util.h" +#include "bus-get-properties.h" #include "dbus-timer.h" #include "dbus-util.h" #include "strv.h" diff --git a/src/core/dbus-unit.c b/src/core/dbus-unit.c index 320e830728..9e9d3b101e 100644 --- a/src/core/dbus-unit.c +++ b/src/core/dbus-unit.c @@ -5,8 +5,8 @@ #include "alloc-util.h" #include "bpf-firewall.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-polkit.h" -#include "bus-util.h" #include "cgroup-util.h" #include "condition.h" #include "dbus-job.h" diff --git a/src/import/importd.c b/src/import/importd.c index d799a10f94..038dd3a6c6 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -7,8 +7,8 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-log-control-api.h" -#include "bus-util.h" #include "bus-polkit.h" #include "def.h" #include "fd-util.h" @@ -22,8 +22,8 @@ #include "parse-util.h" #include "path-util.h" #include "process-util.h" -#include "signal-util.h" #include "service-util.h" +#include "signal-util.h" #include "socket-util.h" #include "stat-util.h" #include "string-table.h" diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 98e0a6cff7..1b6304b57f 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -12,6 +12,7 @@ #include "bootspec.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-get-properties.h" #include "bus-locator.h" #include "bus-polkit.h" #include "bus-unit-util.h" diff --git a/src/login/logind-seat-dbus.c b/src/login/logind-seat-dbus.c index e72f265185..a91765205c 100644 --- a/src/login/logind-seat-dbus.c +++ b/src/login/logind-seat-dbus.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-label.h" #include "bus-polkit.h" #include "bus-util.h" diff --git a/src/login/logind-session-dbus.c b/src/login/logind-session-dbus.c index 3857f4ab7d..5ed20ebd26 100644 --- a/src/login/logind-session-dbus.c +++ b/src/login/logind-session-dbus.c @@ -4,6 +4,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-label.h" #include "bus-polkit.h" #include "bus-util.h" diff --git a/src/login/logind-user-dbus.c b/src/login/logind-user-dbus.c index 63c77cab39..237723729e 100644 --- a/src/login/logind-user-dbus.c +++ b/src/login/logind-user-dbus.c @@ -3,6 +3,7 @@ #include #include "alloc-util.h" +#include "bus-get-properties.h" #include "bus-polkit.h" #include "bus-util.h" #include "format-util.h" diff --git a/src/machine/image-dbus.c b/src/machine/image-dbus.c index 4305f49152..7a15bcc49a 100644 --- a/src/machine/image-dbus.c +++ b/src/machine/image-dbus.c @@ -4,9 +4,9 @@ #include #include "alloc-util.h" +#include "bus-get-properties.h" #include "bus-label.h" #include "bus-polkit.h" -#include "bus-util.h" #include "copy.h" #include "dissect-image.h" #include "fd-util.h" diff --git a/src/machine/machine-dbus.c b/src/machine/machine-dbus.c index a8f9d28a87..73ef5949bf 100644 --- a/src/machine/machine-dbus.c +++ b/src/machine/machine-dbus.c @@ -12,6 +12,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-internal.h" #include "bus-label.h" #include "bus-locator.h" diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 9fb8433592..2d9c180aa4 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -8,6 +8,7 @@ #include "alloc-util.h" #include "btrfs-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-locator.h" #include "bus-polkit.h" #include "cgroup-util.h" diff --git a/src/network/networkd-link-bus.c b/src/network/networkd-link-bus.c index 76daee69e8..38b005fb63 100644 --- a/src/network/networkd-link-bus.c +++ b/src/network/networkd-link-bus.c @@ -6,8 +6,8 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-polkit.h" -#include "bus-util.h" #include "dns-domain.h" #include "networkd-link-bus.h" #include "networkd-link.h" diff --git a/src/portable/portabled-image-bus.c b/src/portable/portabled-image-bus.c index 5988a21037..9646601491 100644 --- a/src/portable/portabled-image-bus.c +++ b/src/portable/portabled-image-bus.c @@ -7,6 +7,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-label.h" #include "bus-polkit.h" #include "bus-util.h" diff --git a/src/resolve/resolved-bus.c b/src/resolve/resolved-bus.c index 38d2483c24..2ecfcf498b 100644 --- a/src/resolve/resolved-bus.c +++ b/src/resolve/resolved-bus.c @@ -2,9 +2,9 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-log-control-api.h" #include "bus-polkit.h" -#include "bus-util.h" #include "dns-domain.h" #include "memory-util.h" #include "missing_capability.h" diff --git a/src/resolve/resolved-link-bus.c b/src/resolve/resolved-link-bus.c index a2ca5cb8f6..0fa62208c3 100644 --- a/src/resolve/resolved-link-bus.c +++ b/src/resolve/resolved-link-bus.c @@ -6,8 +6,8 @@ #include "alloc-util.h" #include "bus-common-errors.h" +#include "bus-get-properties.h" #include "bus-polkit.h" -#include "bus-util.h" #include "parse-util.h" #include "resolve-util.h" #include "resolved-bus.h" diff --git a/src/shared/bus-get-properties.c b/src/shared/bus-get-properties.c new file mode 100644 index 0000000000..8ad4694046 --- /dev/null +++ b/src/shared/bus-get-properties.c @@ -0,0 +1,149 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +#include "bus-get-properties.h" +#include "rlimit-util.h" +#include "string-util.h" + +int bus_property_get_bool( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + int b = *(bool*) userdata; + + return sd_bus_message_append_basic(reply, 'b', &b); +} + +int bus_property_set_bool( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *value, + void *userdata, + sd_bus_error *error) { + + int b, r; + + r = sd_bus_message_read(value, "b", &b); + if (r < 0) + return r; + + *(bool*) userdata = b; + return 0; +} + +int bus_property_get_id128( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + sd_id128_t *id = userdata; + + if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */ + return sd_bus_message_append(reply, "ay", 0); + else + return sd_bus_message_append_array(reply, 'y', id->bytes, 16); +} + +#if __SIZEOF_SIZE_T__ != 8 +int bus_property_get_size( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + uint64_t sz = *(size_t*) userdata; + + return sd_bus_message_append_basic(reply, 't', &sz); +} +#endif + +#if __SIZEOF_LONG__ != 8 +int bus_property_get_long( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + int64_t l = *(long*) userdata; + + return sd_bus_message_append_basic(reply, 'x', &l); +} + +int bus_property_get_ulong( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + uint64_t ul = *(unsigned long*) userdata; + + return sd_bus_message_append_basic(reply, 't', &ul); +} +#endif + +int bus_property_get_rlimit( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + const char *is_soft; + struct rlimit *rl; + uint64_t u; + rlim_t x; + + assert(bus); + assert(reply); + assert(userdata); + + is_soft = endswith(property, "Soft"); + + rl = *(struct rlimit**) userdata; + if (rl) + x = is_soft ? rl->rlim_cur : rl->rlim_max; + else { + struct rlimit buf = {}; + const char *s, *p; + int z; + + /* Chop off "Soft" suffix */ + s = is_soft ? strndupa(property, is_soft - property) : property; + + /* Skip over any prefix, such as "Default" */ + assert_se(p = strstr(s, "Limit")); + + z = rlimit_from_string(p + 5); + assert(z >= 0); + + (void) getrlimit(z, &buf); + x = is_soft ? buf.rlim_cur : buf.rlim_max; + } + + /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all + * archs */ + u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x; + + return sd_bus_message_append(reply, "t", u); +} diff --git a/src/shared/bus-get-properties.h b/src/shared/bus-get-properties.h new file mode 100644 index 0000000000..d853b79d8d --- /dev/null +++ b/src/shared/bus-get-properties.h @@ -0,0 +1,103 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ +#pragma once + +#include "sd-bus.h" + +#include "sd-bus.h" + +#include "macro.h" + +int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); +int bus_property_set_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error); +int bus_property_get_id128(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); + +#define bus_property_get_usec ((sd_bus_property_get_t) NULL) +#define bus_property_set_usec ((sd_bus_property_set_t) NULL) + +assert_cc(sizeof(int) == sizeof(int32_t)); +#define bus_property_get_int ((sd_bus_property_get_t) NULL) + +assert_cc(sizeof(unsigned) == sizeof(uint32_t)); +#define bus_property_get_unsigned ((sd_bus_property_get_t) NULL) + +/* On 64bit machines we can use the default serializer for size_t and + * friends, otherwise we need to cast this manually */ +#if __SIZEOF_SIZE_T__ == 8 +#define bus_property_get_size ((sd_bus_property_get_t) NULL) +#else +int bus_property_get_size(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); +#endif + +#if __SIZEOF_LONG__ == 8 +#define bus_property_get_long ((sd_bus_property_get_t) NULL) +#define bus_property_get_ulong ((sd_bus_property_get_t) NULL) +#else +int bus_property_get_long(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); +int bus_property_get_ulong(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); +#endif + +/* uid_t and friends on Linux 32 bit. This means we can just use the + * default serializer for 32bit unsigned, for serializing it, and map + * it to NULL here */ +assert_cc(sizeof(uid_t) == sizeof(uint32_t)); +#define bus_property_get_uid ((sd_bus_property_get_t) NULL) + +assert_cc(sizeof(gid_t) == sizeof(uint32_t)); +#define bus_property_get_gid ((sd_bus_property_get_t) NULL) + +assert_cc(sizeof(pid_t) == sizeof(uint32_t)); +#define bus_property_get_pid ((sd_bus_property_get_t) NULL) + +assert_cc(sizeof(mode_t) == sizeof(uint32_t)); +#define bus_property_get_mode ((sd_bus_property_get_t) NULL) + +int bus_property_get_rlimit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); + +#define BUS_DEFINE_PROPERTY_GET_GLOBAL(function, bus_type, val) \ + int function(sd_bus *bus, \ + const char *path, \ + const char *interface, \ + const char *property, \ + sd_bus_message *reply, \ + void *userdata, \ + sd_bus_error *error) { \ + \ + assert(bus); \ + assert(reply); \ + \ + return sd_bus_message_append(reply, bus_type, val); \ + } + +#define BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, get2) \ + int function(sd_bus *bus, \ + const char *path, \ + const char *interface, \ + const char *property, \ + sd_bus_message *reply, \ + void *userdata, \ + sd_bus_error *error) { \ + \ + data_type *data = userdata; \ + \ + assert(bus); \ + assert(reply); \ + assert(data); \ + \ + return sd_bus_message_append(reply, bus_type, \ + get2(get1(data))); \ + } + +#define ident(x) (x) +#define BUS_DEFINE_PROPERTY_GET(function, bus_type, data_type, get1) \ + BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, ident) + +#define ref(x) (*(x)) +#define BUS_DEFINE_PROPERTY_GET_REF(function, bus_type, data_type, get) \ + BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, ref, get) + +#define BUS_DEFINE_PROPERTY_GET_ENUM(function, name, type) \ + BUS_DEFINE_PROPERTY_GET_REF(function, "s", type, name##_to_string) + +#define BUS_PROPERTY_DUAL_TIMESTAMP(name, offset, flags) \ + SD_BUS_PROPERTY(name, "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, realtime), (flags)), \ + SD_BUS_PROPERTY(name "Monotonic", "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, monotonic), (flags)) diff --git a/src/shared/bus-log-control-api.c b/src/shared/bus-log-control-api.c index a1d0d17291..7c487ada9f 100644 --- a/src/shared/bus-log-control-api.c +++ b/src/shared/bus-log-control-api.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1+ */ #include "alloc-util.h" +#include "bus-get-properties.h" #include "bus-log-control-api.h" #include "bus-util.h" #include "log.h" diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index 85b5bedbc0..77c1c62182 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -14,15 +14,14 @@ #include "sd-event.h" #include "sd-id128.h" -#include "alloc-util.h" +/* #include "alloc-util.h" */ #include "bus-internal.h" #include "bus-label.h" #include "bus-util.h" #include "path-util.h" -#include "rlimit-util.h" #include "socket-util.h" #include "stdio-util.h" -#include "string-util.h" +/* #include "string-util.h" */ static int name_owner_change_callback(sd_bus_message *m, void *userdata, sd_bus_error *ret_error) { sd_event *e = userdata; @@ -333,102 +332,6 @@ int bus_connect_transport_systemd(BusTransport transport, const char *host, bool return r; } -int bus_property_get_bool( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - int b = *(bool*) userdata; - - return sd_bus_message_append_basic(reply, 'b', &b); -} - -int bus_property_set_bool( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *value, - void *userdata, - sd_bus_error *error) { - - int b, r; - - r = sd_bus_message_read(value, "b", &b); - if (r < 0) - return r; - - *(bool*) userdata = b; - return 0; -} - -int bus_property_get_id128( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - sd_id128_t *id = userdata; - - if (sd_id128_is_null(*id)) /* Add an empty array if the ID is zero */ - return sd_bus_message_append(reply, "ay", 0); - else - return sd_bus_message_append_array(reply, 'y', id->bytes, 16); -} - -#if __SIZEOF_SIZE_T__ != 8 -int bus_property_get_size( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - uint64_t sz = *(size_t*) userdata; - - return sd_bus_message_append_basic(reply, 't', &sz); -} -#endif - -#if __SIZEOF_LONG__ != 8 -int bus_property_get_long( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - int64_t l = *(long*) userdata; - - return sd_bus_message_append_basic(reply, 'x', &l); -} - -int bus_property_get_ulong( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - uint64_t ul = *(unsigned long*) userdata; - - return sd_bus_message_append_basic(reply, 't', &ul); -} -#endif - /** * bus_path_encode_unique() - encode unique object path * @b: bus connection or NULL @@ -556,54 +459,6 @@ int bus_path_decode_unique(const char *path, const char *prefix, char **ret_send return 1; } -int bus_property_get_rlimit( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - const char *is_soft; - struct rlimit *rl; - uint64_t u; - rlim_t x; - - assert(bus); - assert(reply); - assert(userdata); - - is_soft = endswith(property, "Soft"); - - rl = *(struct rlimit**) userdata; - if (rl) - x = is_soft ? rl->rlim_cur : rl->rlim_max; - else { - struct rlimit buf = {}; - const char *s, *p; - int z; - - /* Chop off "Soft" suffix */ - s = is_soft ? strndupa(property, is_soft - property) : property; - - /* Skip over any prefix, such as "Default" */ - assert_se(p = strstr(s, "Limit")); - - z = rlimit_from_string(p + 5); - assert(z >= 0); - - (void) getrlimit(z, &buf); - x = is_soft ? buf.rlim_cur : buf.rlim_max; - } - - /* rlim_t might have different sizes, let's map RLIMIT_INFINITY to (uint64_t) -1, so that it is the same on all - * archs */ - u = x == RLIM_INFINITY ? (uint64_t) -1 : (uint64_t) x; - - return sd_bus_message_append(reply, "t", u); -} - int bus_track_add_name_many(sd_bus_track *t, char **l) { int r = 0; char **i; diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 4dbf74fdf7..fd88314c1f 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -37,110 +37,15 @@ int bus_connect_user_systemd(sd_bus **_bus); int bus_connect_transport(BusTransport transport, const char *host, bool user, sd_bus **bus); int bus_connect_transport_systemd(BusTransport transport, const char *host, bool user, sd_bus **bus); -int bus_property_get_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); -int bus_property_set_bool(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *value, void *userdata, sd_bus_error *error); -int bus_property_get_id128(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); - -#define bus_property_get_usec ((sd_bus_property_get_t) NULL) -#define bus_property_set_usec ((sd_bus_property_set_t) NULL) - -assert_cc(sizeof(int) == sizeof(int32_t)); -#define bus_property_get_int ((sd_bus_property_get_t) NULL) - -assert_cc(sizeof(unsigned) == sizeof(uint32_t)); -#define bus_property_get_unsigned ((sd_bus_property_get_t) NULL) - -/* On 64bit machines we can use the default serializer for size_t and - * friends, otherwise we need to cast this manually */ -#if __SIZEOF_SIZE_T__ == 8 -#define bus_property_get_size ((sd_bus_property_get_t) NULL) -#else -int bus_property_get_size(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); -#endif - -#if __SIZEOF_LONG__ == 8 -#define bus_property_get_long ((sd_bus_property_get_t) NULL) -#define bus_property_get_ulong ((sd_bus_property_get_t) NULL) -#else -int bus_property_get_long(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); -int bus_property_get_ulong(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); -#endif - -/* uid_t and friends on Linux 32 bit. This means we can just use the - * default serializer for 32bit unsigned, for serializing it, and map - * it to NULL here */ -assert_cc(sizeof(uid_t) == sizeof(uint32_t)); -#define bus_property_get_uid ((sd_bus_property_get_t) NULL) - -assert_cc(sizeof(gid_t) == sizeof(uint32_t)); -#define bus_property_get_gid ((sd_bus_property_get_t) NULL) - -assert_cc(sizeof(pid_t) == sizeof(uint32_t)); -#define bus_property_get_pid ((sd_bus_property_get_t) NULL) - -assert_cc(sizeof(mode_t) == sizeof(uint32_t)); -#define bus_property_get_mode ((sd_bus_property_get_t) NULL) - #define bus_log_parse_error(r) \ log_error_errno(r, "Failed to parse bus message: %m") #define bus_log_create_error(r) \ log_error_errno(r, "Failed to create bus message: %m") -#define BUS_DEFINE_PROPERTY_GET_GLOBAL(function, bus_type, val) \ - int function(sd_bus *bus, \ - const char *path, \ - const char *interface, \ - const char *property, \ - sd_bus_message *reply, \ - void *userdata, \ - sd_bus_error *error) { \ - \ - assert(bus); \ - assert(reply); \ - \ - return sd_bus_message_append(reply, bus_type, val); \ - } - -#define BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, get2) \ - int function(sd_bus *bus, \ - const char *path, \ - const char *interface, \ - const char *property, \ - sd_bus_message *reply, \ - void *userdata, \ - sd_bus_error *error) { \ - \ - data_type *data = userdata; \ - \ - assert(bus); \ - assert(reply); \ - assert(data); \ - \ - return sd_bus_message_append(reply, bus_type, \ - get2(get1(data))); \ - } - -#define ident(x) (x) -#define BUS_DEFINE_PROPERTY_GET(function, bus_type, data_type, get1) \ - BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, get1, ident) - -#define ref(x) (*(x)) -#define BUS_DEFINE_PROPERTY_GET_REF(function, bus_type, data_type, get) \ - BUS_DEFINE_PROPERTY_GET2(function, bus_type, data_type, ref, get) - -#define BUS_DEFINE_PROPERTY_GET_ENUM(function, name, type) \ - BUS_DEFINE_PROPERTY_GET_REF(function, "s", type, name##_to_string) - -#define BUS_PROPERTY_DUAL_TIMESTAMP(name, offset, flags) \ - SD_BUS_PROPERTY(name, "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, realtime), (flags)), \ - SD_BUS_PROPERTY(name "Monotonic", "t", bus_property_get_usec, (offset) + offsetof(struct dual_timestamp, monotonic), (flags)) - int bus_path_encode_unique(sd_bus *b, const char *prefix, const char *sender_id, const char *external_id, char **ret_path); int bus_path_decode_unique(const char *path, const char *prefix, char **ret_sender, char **ret_external); -int bus_property_get_rlimit(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); - int bus_track_add_name_many(sd_bus_track *t, char **l); int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description); diff --git a/src/shared/meson.build b/src/shared/meson.build index 3bd680295c..1868e8aede 100644 --- a/src/shared/meson.build +++ b/src/shared/meson.build @@ -27,6 +27,8 @@ shared_sources = files(''' bpf-program.h bridge-util.c bridge-util.h + bus-get-properties.c + bus-get-properties.h bus-locator.c bus-locator.h bus-log-control-api.c diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index d7d9305711..c467b85477 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -12,6 +12,7 @@ #include "alloc-util.h" #include "bus-common-errors.h" #include "bus-error.h" +#include "bus-get-properties.h" #include "bus-locator.h" #include "bus-log-control-api.h" #include "bus-map-properties.h" diff --git a/src/timesync/timesyncd-bus.c b/src/timesync/timesyncd-bus.c index 006800709d..6effdfc325 100644 --- a/src/timesync/timesyncd-bus.c +++ b/src/timesync/timesyncd-bus.c @@ -3,6 +3,7 @@ #include "sd-bus.h" #include "alloc-util.h" +#include "bus-get-properties.h" #include "bus-internal.h" #include "bus-log-control-api.h" #include "bus-protocol.h"