diff options
author | Lennart Poettering <lennart@poettering.net> | 2025-01-14 14:39:58 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-14 14:39:58 +0100 |
commit | 857ddc2e55302de0fa187b33f0e9468917903912 (patch) | |
tree | 90234b0aa0ed4dd4226be3f853ae26835986516f /src/shared | |
parent | sysusers: some tweaks, mostly around chase() use (#35976) (diff) | |
parent | core/manager: restore bus track deserialization cleanup in manager_reload() (diff) | |
download | systemd-857ddc2e55302de0fa187b33f0e9468917903912.tar.xz systemd-857ddc2e55302de0fa187b33f0e9468917903912.zip |
core: serialize API bus id and validate before deserializing bus tracks, plus several other bus util cleanups (#35970)
Follow-up for #35406
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/bus-get-properties.c | 19 | ||||
-rw-r--r-- | src/shared/bus-get-properties.h | 2 | ||||
-rw-r--r-- | src/shared/bus-map-properties.c | 1 | ||||
-rw-r--r-- | src/shared/bus-message-util.c | 52 | ||||
-rw-r--r-- | src/shared/bus-message-util.h | 6 | ||||
-rw-r--r-- | src/shared/bus-util.c | 91 | ||||
-rw-r--r-- | src/shared/bus-util.h | 10 | ||||
-rw-r--r-- | src/shared/serialize.c | 19 | ||||
-rw-r--r-- | src/shared/serialize.h | 7 |
9 files changed, 122 insertions, 85 deletions
diff --git a/src/shared/bus-get-properties.c b/src/shared/bus-get-properties.c index 53e5d6b99f..bf267a23a5 100644 --- a/src/shared/bus-get-properties.c +++ b/src/shared/bus-get-properties.c @@ -1,6 +1,7 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ #include "bus-get-properties.h" +#include "bus-message-util.h" #include "rlimit-util.h" #include "stdio-util.h" #include "string-util.h" @@ -164,3 +165,21 @@ int bus_property_get_rlimit( return sd_bus_message_append(reply, "t", u); } + +int bus_property_get_string_set( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + Set **s = ASSERT_PTR(userdata); + + assert(bus); + assert(property); + assert(reply); + + return bus_message_append_string_set(reply, *s); +} diff --git a/src/shared/bus-get-properties.h b/src/shared/bus-get-properties.h index 4c35126502..9ddf5454de 100644 --- a/src/shared/bus-get-properties.h +++ b/src/shared/bus-get-properties.h @@ -52,6 +52,8 @@ assert_cc(sizeof(mode_t) == sizeof(uint32_t)); 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_property_get_string_set(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, \ diff --git a/src/shared/bus-map-properties.c b/src/shared/bus-map-properties.c index a6cd752894..18d83c336f 100644 --- a/src/shared/bus-map-properties.c +++ b/src/shared/bus-map-properties.c @@ -5,6 +5,7 @@ #include "bus-util.h" #include "strv.h" #include "bus-message.h" +#include "bus-message-util.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; diff --git a/src/shared/bus-message-util.c b/src/shared/bus-message-util.c index d8c483ef62..8da112cacc 100644 --- a/src/shared/bus-message-util.c +++ b/src/shared/bus-message-util.c @@ -7,6 +7,35 @@ #include "copy.h" #include "resolve-util.h" +int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) { + const void *a; + size_t sz; + int r; + + assert(m); + + r = sd_bus_message_read_array(m, 'y', &a, &sz); + if (r < 0) + return r; + + switch (sz) { + + case 0: + if (ret) + *ret = SD_ID128_NULL; + return 0; + + case sizeof(sd_id128_t): + if (ret) + memcpy(ret, a, sz); + return !memeqzero(a, sz); /* This mimics sd_id128_is_null(), but ret may be NULL, + * and a may be misaligned, so use memeqzero() here. */ + + default: + return -EINVAL; + } +} + int bus_message_read_ifindex(sd_bus_message *message, sd_bus_error *error, int *ret) { int ifindex, r; @@ -187,6 +216,25 @@ clear: return r; } +int bus_message_append_string_set(sd_bus_message *m, const Set *set) { + int r; + + assert(m); + + r = sd_bus_message_open_container(m, 'a', "s"); + if (r < 0) + return r; + + const char *s; + SET_FOREACH(s, set) { + r = sd_bus_message_append(m, "s", s); + if (r < 0) + return r; + } + + return sd_bus_message_close_container(m); +} + int bus_message_dump_string(sd_bus_message *message) { const char *s; int r; @@ -217,3 +265,7 @@ int bus_message_dump_fd(sd_bus_message *message) { return 0; } + +DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(bus_message_hash_ops, + void, trivial_hash_func, trivial_compare_func, + sd_bus_message, sd_bus_message_unref); diff --git a/src/shared/bus-message-util.h b/src/shared/bus-message-util.h index 50025766c2..d4a05f5b9c 100644 --- a/src/shared/bus-message-util.h +++ b/src/shared/bus-message-util.h @@ -6,6 +6,8 @@ #include "in-addr-util.h" #include "socket-netlink.h" +int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret); + int bus_message_read_ifindex(sd_bus_message *message, sd_bus_error *error, int *ret); int bus_message_read_family(sd_bus_message *message, sd_bus_error *error, int *ret); int bus_message_read_in_addr_auto(sd_bus_message *message, sd_bus_error *error, int *ret_family, union in_addr_union *ret_addr); @@ -17,5 +19,9 @@ int bus_message_read_dns_servers( struct in_addr_full ***ret_dns, size_t *ret_n_dns); +int bus_message_append_string_set(sd_bus_message *m, const Set *s); + int bus_message_dump_string(sd_bus_message *message); int bus_message_dump_fd(sd_bus_message *message); + +extern const struct hash_ops bus_message_hash_ops; diff --git a/src/shared/bus-util.c b/src/shared/bus-util.c index e38433ddc0..05926771d2 100644 --- a/src/shared/bus-util.c +++ b/src/shared/bus-util.c @@ -686,7 +686,7 @@ int bus_path_decode_unique(const char *path, const char *prefix, char **ret_send return 1; } -int bus_track_add_name_many(sd_bus_track *t, char **l) { +int bus_track_add_name_many(sd_bus_track *t, char * const *l) { int r = 0; assert(t); @@ -700,16 +700,15 @@ int bus_track_add_name_many(sd_bus_track *t, char **l) { int bus_track_to_strv(sd_bus_track *t, char ***ret) { _cleanup_strv_free_ char **subscribed = NULL; - int r = 0; + int r; assert(ret); for (const char *n = sd_bus_track_first(t); n; n = sd_bus_track_next(t)) { - r = sd_bus_track_count_name(t, n); - if (r < 0) - return r; + int c = sd_bus_track_count_name(t, n); + assert(c >= 0); - for (int j = 0; j < r; j++) { + for (int j = 0; j < c; j++) { r = strv_extend(&subscribed, n); if (r < 0) return r; @@ -717,7 +716,7 @@ int bus_track_to_strv(sd_bus_track *t, char ***ret) { } *ret = TAKE_PTR(subscribed); - return r; + return 0; } int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description) { @@ -862,53 +861,6 @@ int bus_register_malloc_status(sd_bus *bus, const char *destination) { return 0; } -static void bus_message_unref_wrapper(void *m) { - sd_bus_message_unref(m); -} - -const struct hash_ops bus_message_hash_ops = { - .hash = trivial_hash_func, - .compare = trivial_compare_func, - .free_value = bus_message_unref_wrapper, -}; - -int bus_message_append_string_set(sd_bus_message *m, Set *set) { - const char *s; - int r; - - assert(m); - - r = sd_bus_message_open_container(m, 'a', "s"); - if (r < 0) - return r; - - SET_FOREACH(s, set) { - r = sd_bus_message_append(m, "s", s); - if (r < 0) - return r; - } - - return sd_bus_message_close_container(m); -} - -int bus_property_get_string_set( - sd_bus *bus, - const char *path, - const char *interface, - const char *property, - sd_bus_message *reply, - void *userdata, - sd_bus_error *error) { - - Set **s = ASSERT_PTR(userdata); - - assert(bus); - assert(property); - assert(reply); - - return bus_message_append_string_set(reply, *s); -} - int bus_creds_get_pidref( sd_bus_creds *c, PidRef *ret) { @@ -953,32 +905,27 @@ int bus_query_sender_pidref( return bus_creds_get_pidref(creds, ret); } -int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret) { - const void *a; - size_t sz; +int bus_get_instance_id(sd_bus *bus, sd_id128_t *ret) { + _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; int r; - assert(m); + assert(bus); + assert(ret); - r = sd_bus_message_read_array(m, 'y', &a, &sz); + r = sd_bus_call_method(bus, + "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetId", + /* error = */ NULL, &reply, + NULL); if (r < 0) return r; - switch (sz) { - case 0: - if (ret) - *ret = SD_ID128_NULL; - return 0; + const char *id; - case sizeof(sd_id128_t): - if (ret) - memcpy(ret, a, sz); - return !memeqzero(a, sz); /* This mimics sd_id128_is_null(), but ret may be NULL, - * and a may be misaligned, so use memeqzero() here. */ + r = sd_bus_message_read_basic(reply, 's', &id); + if (r < 0) + return r; - default: - return -EINVAL; - } + return sd_id128_from_string(id, ret); } static const char* const bus_transport_table[] = { diff --git a/src/shared/bus-util.h b/src/shared/bus-util.h index 95b9adb7b1..63c3ba5ee3 100644 --- a/src/shared/bus-util.h +++ b/src/shared/bus-util.h @@ -65,7 +65,7 @@ static inline int bus_log_connect_error(int r, BusTransport transport, RuntimeSc 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_track_add_name_many(sd_bus_track *t, char **l); +int bus_track_add_name_many(sd_bus_track *t, char * const *l); int bus_track_to_strv(sd_bus_track *t, char ***ret); int bus_open_system_watch_bind_with_description(sd_bus **ret, const char *description); @@ -78,15 +78,9 @@ int bus_reply_pair_array(sd_bus_message *m, char **l); /* Listen to GetMallocInfo() calls to 'destination' and return malloc_info() via FD */ int bus_register_malloc_status(sd_bus *bus, const char *destination); -extern const struct hash_ops bus_message_hash_ops; - -int bus_message_append_string_set(sd_bus_message *m, Set *s); - -int bus_property_get_string_set(sd_bus *bus, const char *path, const char *interface, const char *property, sd_bus_message *reply, void *userdata, sd_bus_error *error); - int bus_creds_get_pidref(sd_bus_creds *c, PidRef *ret); int bus_query_sender_pidref(sd_bus_message *m, PidRef *ret); -int bus_message_read_id128(sd_bus_message *m, sd_id128_t *ret); +int bus_get_instance_id(sd_bus *bus, sd_id128_t *ret); const char* bus_transport_to_string(BusTransport transport) _const_; diff --git a/src/shared/serialize.c b/src/shared/serialize.c index 4d9ee6cc32..425e7f1ac3 100644 --- a/src/shared/serialize.c +++ b/src/shared/serialize.c @@ -164,11 +164,14 @@ int serialize_dual_timestamp(FILE *f, const char *name, const dual_timestamp *t) return serialize_item_format(f, name, USEC_FMT " " USEC_FMT, t->realtime, t->monotonic); } -int serialize_strv(FILE *f, const char *key, char **l) { +int serialize_strv(FILE *f, const char *key, char * const *l) { int ret = 0, r; /* Returns the first error, or positive if anything was serialized, 0 otherwise. */ + assert(f); + assert(key); + STRV_FOREACH(i, l) { r = serialize_item_escaped(f, key, *i); if ((ret >= 0 && r < 0) || @@ -179,6 +182,16 @@ int serialize_strv(FILE *f, const char *key, char **l) { return ret; } +int serialize_id128(FILE *f, const char *key, sd_id128_t id) { + assert(f); + assert(key); + + if (sd_id128_is_null(id)) + return 0; + + return serialize_item_format(f, key, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id)); +} + int serialize_pidref(FILE *f, FDSet *fds, const char *key, PidRef *pidref) { int r; @@ -267,8 +280,7 @@ int serialize_item_base64mem(FILE *f, const char *key, const void *p, size_t l) return 1; } -int serialize_string_set(FILE *f, const char *key, Set *s) { - const char *e; +int serialize_string_set(FILE *f, const char *key, const Set *s) { int r; assert(f); @@ -279,6 +291,7 @@ int serialize_string_set(FILE *f, const char *key, Set *s) { /* Serialize as individual items, as each element might contain separators and escapes */ + const char *e; SET_FOREACH(e, s) { r = serialize_item(f, key, e); if (r < 0) diff --git a/src/shared/serialize.h b/src/shared/serialize.h index dc753465c4..b573e6cc15 100644 --- a/src/shared/serialize.h +++ b/src/shared/serialize.h @@ -3,6 +3,8 @@ #include <stdio.h> +#include "sd-id128.h" + #include "fdset.h" #include "image-policy.h" #include "macro.h" @@ -21,10 +23,11 @@ int serialize_fd(FILE *f, FDSet *fds, const char *key, int fd); int serialize_fd_many(FILE *f, FDSet *fds, const char *key, const int fd_array[], size_t n_fd_array); int serialize_usec(FILE *f, const char *key, usec_t usec); int serialize_dual_timestamp(FILE *f, const char *key, const dual_timestamp *t); -int serialize_strv(FILE *f, const char *key, char **l); +int serialize_strv(FILE *f, const char *key, char * const *l); +int serialize_id128(FILE *f, const char *key, sd_id128_t id); int serialize_pidref(FILE *f, FDSet *fds, const char *key, PidRef *pidref); int serialize_ratelimit(FILE *f, const char *key, const RateLimit *rl); -int serialize_string_set(FILE *f, const char *key, Set *s); +int serialize_string_set(FILE *f, const char *key, const Set *s); int serialize_image_policy(FILE *f, const char *key, const ImagePolicy *p); static inline int serialize_bool(FILE *f, const char *key, bool b) { |