summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-08-24 09:28:42 +0200
committerLennart Poettering <lennart@poettering.net>2023-08-24 13:19:52 +0200
commitc91f581c4706af36f87f911067c4569c0ff08676 (patch)
tree15fbe93f8c7cad4e2928d7e1558239b805718a36 /src
parentjson: add json_variant_append_arrayb() helper (diff)
downloadsystemd-c91f581c4706af36f87f911067c4569c0ff08676.tar.xz
systemd-c91f581c4706af36f87f911067c4569c0ff08676.zip
tree-wide: use json_variant_append_arrayb() at many places
Diffstat (limited to '')
-rw-r--r--src/boot/measure.c19
-rw-r--r--src/network/networkd-json.c65
-rw-r--r--src/resolve/resolved-dns-cache.c14
-rw-r--r--src/resolve/resolved-manager.c16
-rw-r--r--src/resolve/resolved-varlink.c14
5 files changed, 51 insertions, 77 deletions
diff --git a/src/boot/measure.c b/src/boot/measure.c
index c08386d9fc..fbeb4473f7 100644
--- a/src/boot/measure.c
+++ b/src/boot/measure.c
@@ -693,21 +693,16 @@ static int verb_calculate(int argc, char *argv[], void *userdata) {
printf("%" PRIu32 ":%s=%s\n", TPM_PCR_INDEX_KERNEL_IMAGE, pcr_states[i].bank, hd);
} else {
- _cleanup_(json_variant_unrefp) JsonVariant *bv = NULL, *array = NULL;
+ _cleanup_(json_variant_unrefp) JsonVariant *array = NULL;
array = json_variant_ref(json_variant_by_key(w, pcr_states[i].bank));
- r = json_build(&bv,
- JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_CONDITION(!isempty(*phase), "phase", JSON_BUILD_STRING(*phase)),
- JSON_BUILD_PAIR("pcr", JSON_BUILD_INTEGER(TPM_PCR_INDEX_KERNEL_IMAGE)),
- JSON_BUILD_PAIR("hash", JSON_BUILD_HEX(pcr_states[i].value, pcr_states[i].value_size))
- )
- );
- if (r < 0)
- return log_error_errno(r, "Failed to build JSON object: %m");
-
- r = json_variant_append_array(&array, bv);
+ r = json_variant_append_arrayb(
+ &array,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_CONDITION(!isempty(*phase), "phase", JSON_BUILD_STRING(*phase)),
+ JSON_BUILD_PAIR("pcr", JSON_BUILD_INTEGER(TPM_PCR_INDEX_KERNEL_IMAGE)),
+ JSON_BUILD_PAIR("hash", JSON_BUILD_HEX(pcr_states[i].value, pcr_states[i].value_size))));
if (r < 0)
return log_error_errno(r, "Failed to append JSON object to array: %m");
diff --git a/src/network/networkd-json.c b/src/network/networkd-json.c
index 78bf870507..7e4e4ff22f 100644
--- a/src/network/networkd-json.c
+++ b/src/network/networkd-json.c
@@ -140,15 +140,11 @@ static int nexthop_group_build_json(NextHop *nexthop, JsonVariant **ret) {
assert(ret);
HASHMAP_FOREACH(g, nexthop->group) {
- _cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
-
- r = json_build(&e, JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_UNSIGNED("ID", g->id),
- JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1)));
- if (r < 0)
- return r;
-
- r = json_variant_append_array(&array, e);
+ r = json_variant_append_arrayb(
+ &array,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_UNSIGNED("ID", g->id),
+ JSON_BUILD_PAIR_UNSIGNED("Weight", g->weight+1)));
if (r < 0)
return r;
}
@@ -431,9 +427,6 @@ static int device_append_json(sd_device *device, JsonVariant **v) {
}
static int dns_append_json_one(Link *link, const struct in_addr_full *a, NetworkConfigSource s, const union in_addr_union *p, JsonVariant **array) {
- _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
- int r;
-
assert(link);
assert(a);
assert(array);
@@ -441,18 +434,16 @@ static int dns_append_json_one(Link *link, const struct in_addr_full *a, Network
if (a->ifindex != 0 && a->ifindex != link->ifindex)
return 0;
- r = json_build(&v, JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_INTEGER("Family", a->family),
- JSON_BUILD_PAIR_IN_ADDR("Address", &a->address, a->family),
- JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port),
- JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", JSON_BUILD_INTEGER(a->ifindex)),
- JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name),
- JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
- JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, a->family)));
- if (r < 0)
- return r;
-
- return json_variant_append_array(array, v);
+ return json_variant_append_arrayb(
+ array,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_INTEGER("Family", a->family),
+ JSON_BUILD_PAIR_IN_ADDR("Address", &a->address, a->family),
+ JSON_BUILD_PAIR_UNSIGNED_NON_ZERO("Port", a->port),
+ JSON_BUILD_PAIR_CONDITION(a->ifindex != 0, "InterfaceIndex", JSON_BUILD_INTEGER(a->ifindex)),
+ JSON_BUILD_PAIR_STRING_NON_EMPTY("ServerName", a->server_name),
+ JSON_BUILD_PAIR_STRING("ConfigSource", network_config_source_to_string(s)),
+ JSON_BUILD_PAIR_IN_ADDR_NON_NULL("ConfigProvider", p, a->family)));
}
static int dns_append_json(Link *link, JsonVariant **v) {
@@ -939,23 +930,19 @@ static int dhcp_server_offered_leases_append_json(Link *link, JsonVariant **v) {
return 0;
HASHMAP_FOREACH(lease, link->dhcp_server->bound_leases_by_client_id) {
- _cleanup_(json_variant_unrefp) JsonVariant *e = NULL;
struct in_addr address = { .s_addr = lease->address };
- r = json_build(&e,
- JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_BYTE_ARRAY(
- "ClientId",
- lease->client_id.data,
- lease->client_id.length),
- JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Address", &address),
- JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", lease->hostname),
- JSON_BUILD_PAIR_FINITE_USEC(
- "ExpirationUSec", lease->expiration)));
- if (r < 0)
- return r;
-
- r = json_variant_append_array(&array, e);
+ r = json_variant_append_arrayb(
+ &array,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_BYTE_ARRAY(
+ "ClientId",
+ lease->client_id.data,
+ lease->client_id.length),
+ JSON_BUILD_PAIR_IN4_ADDR_NON_NULL("Address", &address),
+ JSON_BUILD_PAIR_STRING_NON_EMPTY("Hostname", lease->hostname),
+ JSON_BUILD_PAIR_FINITE_USEC(
+ "ExpirationUSec", lease->expiration)));
if (r < 0)
return r;
}
diff --git a/src/resolve/resolved-dns-cache.c b/src/resolve/resolved-dns-cache.c
index 3805150d98..7a1a1620af 100644
--- a/src/resolve/resolved-dns-cache.c
+++ b/src/resolve/resolved-dns-cache.c
@@ -1407,7 +1407,7 @@ int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret) {
_cleanup_(json_variant_unrefp) JsonVariant *l = NULL;
LIST_FOREACH(by_key, j, i) {
- _cleanup_(json_variant_unrefp) JsonVariant *rj = NULL, *item = NULL;
+ _cleanup_(json_variant_unrefp) JsonVariant *rj = NULL;
assert(j->rr);
@@ -1419,13 +1419,11 @@ int dns_cache_dump_to_json(DnsCache *cache, JsonVariant **ret) {
if (r < 0)
return r;
- r = json_build(&item, JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_VARIANT("rr", rj),
- JSON_BUILD_PAIR_BASE64("raw", j->rr->wire_format, j->rr->wire_format_size)));
- if (r < 0)
- return r;
-
- r = json_variant_append_array(&l, item);
+ r = json_variant_append_arrayb(
+ &l,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_VARIANT("rr", rj),
+ JSON_BUILD_PAIR_BASE64("raw", j->rr->wire_format, j->rr->wire_format_size)));
if (r < 0)
return r;
}
diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c
index 43e7b95e17..ed3800ed8a 100644
--- a/src/resolve/resolved-manager.c
+++ b/src/resolve/resolved-manager.c
@@ -1134,7 +1134,7 @@ int manager_monitor_send(
return log_error_errno(r, "Failed to convert question to JSON: %m");
DNS_ANSWER_FOREACH_ITEM(rri, answer) {
- _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
+ _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
r = dns_resource_record_to_json(rri->rr, &v);
if (r < 0)
@@ -1144,14 +1144,12 @@ int manager_monitor_send(
if (r < 0)
return log_error_errno(r, "Failed to generate RR wire format: %m");
- r = json_build(&w, JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)),
- JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)),
- JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", JSON_BUILD_INTEGER(rri->ifindex))));
- if (r < 0)
- return log_error_errno(r, "Failed to make answer RR object: %m");
-
- r = json_variant_append_array(&janswer, w);
+ r = json_variant_append_arrayb(
+ &janswer,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_CONDITION(v, "rr", JSON_BUILD_VARIANT(v)),
+ JSON_BUILD_PAIR("raw", JSON_BUILD_BASE64(rri->rr->wire_format, rri->rr->wire_format_size)),
+ JSON_BUILD_PAIR_CONDITION(rri->ifindex > 0, "ifindex", JSON_BUILD_INTEGER(rri->ifindex))));
if (r < 0)
return log_debug_errno(r, "Failed to append notification entry to array: %m");
}
diff --git a/src/resolve/resolved-varlink.c b/src/resolve/resolved-varlink.c
index b27cf90435..1ce1a55876 100644
--- a/src/resolve/resolved-varlink.c
+++ b/src/resolve/resolved-varlink.c
@@ -427,7 +427,6 @@ static void vl_method_resolve_address_complete(DnsQuery *query) {
question = dns_query_question_for_protocol(q, q->answer_protocol);
DNS_ANSWER_FOREACH_IFINDEX(rr, ifindex, q->answer) {
- _cleanup_(json_variant_unrefp) JsonVariant *entry = NULL;
_cleanup_free_ char *normalized = NULL;
r = dns_question_matches_rr(question, rr, NULL);
@@ -440,14 +439,11 @@ static void vl_method_resolve_address_complete(DnsQuery *query) {
if (r < 0)
goto finish;
- r = json_build(&entry,
- JSON_BUILD_OBJECT(
- JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)),
- JSON_BUILD_PAIR("name", JSON_BUILD_STRING(normalized))));
- if (r < 0)
- goto finish;
-
- r = json_variant_append_array(&array, entry);
+ r = json_variant_append_arrayb(
+ &array,
+ JSON_BUILD_OBJECT(
+ JSON_BUILD_PAIR_CONDITION(ifindex > 0, "ifindex", JSON_BUILD_INTEGER(ifindex)),
+ JSON_BUILD_PAIR("name", JSON_BUILD_STRING(normalized))));
if (r < 0)
goto finish;
}