diff options
author | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-10-24 12:29:57 +0200 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2024-10-31 02:59:14 +0100 |
commit | f283816acb6e7fd444df06b103dd179ce5249126 (patch) | |
tree | 36310b2ac6859fe03fa2b8063ad32c80b70d1b7f /src/machine/machined-varlink.c | |
parent | NEWS: fix typo (diff) | |
download | systemd-f283816acb6e7fd444df06b103dd179ce5249126.tar.xz systemd-f283816acb6e7fd444df06b103dd179ce5249126.zip |
machine: use sd_json_variant_append_arraybo() and JSON_BUILD_PAIR_VARIANT_NON_NULL()
Follow-up for 45755275e5ae747bf79e6c7bdd9a055711ebc71f.
Diffstat (limited to '')
-rw-r--r-- | src/machine/machined-varlink.c | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/src/machine/machined-varlink.c b/src/machine/machined-varlink.c index 2c2eb2d918..723c755f70 100644 --- a/src/machine/machined-varlink.c +++ b/src/machine/machined-varlink.c @@ -392,56 +392,49 @@ static int vl_method_get_memberships(sd_varlink *link, sd_json_variant *paramete } static int json_build_local_addresses(const struct local_address *addresses, size_t n_addresses, sd_json_variant **ret) { + _cleanup_(sd_json_variant_unrefp) sd_json_variant *array = NULL; int r; - if (n_addresses == 0) - return 0; - - assert(addresses); + assert(addresses || n_addresses == 0); assert(ret); FOREACH_ARRAY(a, addresses, n_addresses) { - _cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; - r = sd_json_buildo( - &entry, + r = sd_json_variant_append_arraybo( + &array, JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("ifindex", a->ifindex), SD_JSON_BUILD_PAIR_INTEGER("family", a->family), SD_JSON_BUILD_PAIR_BYTE_ARRAY("address", &a->address.bytes, FAMILY_ADDRESS_SIZE(a->family))); if (r < 0) return r; - - r = sd_json_variant_append_array(ret, entry); - if (r < 0) - return r; } + *ret = TAKE_PTR(array); return 0; } static int list_machine_one_and_maybe_read_metadata(sd_varlink *link, Machine *m, bool more, AcquireMetadata am) { - _cleanup_(sd_json_variant_unrefp) sd_json_variant *addr_array = NULL; + _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *addr_array = NULL; _cleanup_strv_free_ char **os_release = NULL; uid_t shift = UID_INVALID; - int r, n = 0; + int r; assert(link); assert(m); - _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; - if (should_acquire_metadata(am)) { _cleanup_free_ struct local_address *addresses = NULL; - n = machine_get_addresses(m, &addresses); - if (n < 0 && am == ACQUIRE_METADATA_GRACEFUL) - log_debug_errno(n, "Failed to get address (graceful mode), ignoring: %m"); - else if (n == -ENONET) + + r = machine_get_addresses(m, &addresses); + if (r < 0 && am == ACQUIRE_METADATA_GRACEFUL) + log_debug_errno(r, "Failed to get address (graceful mode), ignoring: %m"); + else if (r == -ENONET) return sd_varlink_error(link, "io.systemd.Machine.NoPrivateNetworking", NULL); - else if (ERRNO_IS_NEG_NOT_SUPPORTED(n)) + else if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) return sd_varlink_error(link, "io.systemd.Machine.NotAvailable", NULL); - else if (n < 0) - return log_debug_errno(n, "Failed to get addresses: %m"); + else if (r < 0) + return log_debug_errno(r, "Failed to get addresses: %m"); else { - r = json_build_local_addresses(addresses, n, &addr_array); + r = json_build_local_addresses(addresses, r, &addr_array); if (r < 0) return r; } @@ -480,7 +473,7 @@ static int list_machine_one_and_maybe_read_metadata(sd_varlink *link, Machine *m SD_JSON_BUILD_PAIR_CONDITION(m->vsock_cid != VMADDR_CID_ANY, "vSockCid", SD_JSON_BUILD_UNSIGNED(m->vsock_cid)), JSON_BUILD_PAIR_STRING_NON_EMPTY("sshAddress", m->ssh_address), JSON_BUILD_PAIR_STRING_NON_EMPTY("sshPrivateKeyPath", m->ssh_private_key_path), - SD_JSON_BUILD_PAIR_CONDITION(n > 0, "addresses", SD_JSON_BUILD_VARIANT(addr_array)), + JSON_BUILD_PAIR_VARIANT_NON_NULL("addresses", addr_array), SD_JSON_BUILD_PAIR_CONDITION(!strv_isempty(os_release), "OSRelease", JSON_BUILD_STRV_ENV_PAIR(os_release)), JSON_BUILD_PAIR_UNSIGNED_NOT_EQUAL("UIDShift", shift, UID_INVALID)); if (r < 0) |