From 05f47839e1feca299191aaa1251c041d61ada1fc Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 29 May 2024 09:33:27 +0200 Subject: varlink: add varlink_many_*() helpers that send messages to a set of connections at once If we have a Varlink interface that allows man subscribers to the same source of events we typically want to notify all of them at once with the same message. Let's add some helpers for this purpose: varlink_many_*() can be used to send messages, similar to the corresponding varlink_*() calls, but they take a Set of Varlink objects and we issue the operation on every connection once. This ports over one existing case where this is beneficial, but the main user for this is supposed to be importd later. --- src/resolve/resolved-manager.c | 53 ++++++++++++++++++++---------------------- src/shared/varlink.c | 48 ++++++++++++++++++++++++++++++++++++++ src/shared/varlink.h | 9 ++++++- 3 files changed, 81 insertions(+), 29 deletions(-) diff --git a/src/resolve/resolved-manager.c b/src/resolve/resolved-manager.c index 2eee418adf..9bbdc648fc 100644 --- a/src/resolve/resolved-manager.c +++ b/src/resolve/resolved-manager.c @@ -1165,7 +1165,6 @@ static int dns_question_to_json(DnsQuestion *q, sd_json_variant **ret) { int manager_monitor_send(Manager *m, DnsQuery *q) { _cleanup_(sd_json_variant_unrefp) sd_json_variant *jquestion = NULL, *jcollected_questions = NULL, *janswer = NULL; _cleanup_(dns_question_unrefp) DnsQuestion *merged = NULL; - Varlink *connection; DnsAnswerItem *rri; int r; @@ -1220,34 +1219,32 @@ int manager_monitor_send(Manager *m, DnsQuery *q) { return log_debug_errno(r, "Failed to append notification entry to array: %m"); } - SET_FOREACH(connection, m->varlink_subscription) { - r = varlink_notifybo( - connection, - SD_JSON_BUILD_PAIR("state", SD_JSON_BUILD_STRING(dns_transaction_state_to_string(q->state))), - SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_DNSSEC_FAILED, - "result", SD_JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), - SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_RCODE_FAILURE, - "rcode", SD_JSON_BUILD_INTEGER(q->answer_rcode)), - SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_ERRNO, - "errno", SD_JSON_BUILD_INTEGER(q->answer_errno)), - SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, - DNS_TRANSACTION_DNSSEC_FAILED, - DNS_TRANSACTION_RCODE_FAILURE) && - q->answer_ede_rcode >= 0, - "extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), - SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, - DNS_TRANSACTION_DNSSEC_FAILED, - DNS_TRANSACTION_RCODE_FAILURE) && - q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), - "extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)), - SD_JSON_BUILD_PAIR("question", SD_JSON_BUILD_VARIANT(jquestion)), - SD_JSON_BUILD_PAIR_CONDITION(!!jcollected_questions, - "collectedQuestions", SD_JSON_BUILD_VARIANT(jcollected_questions)), - SD_JSON_BUILD_PAIR_CONDITION(!!janswer, + r = varlink_many_notifybo( + m->varlink_subscription, + SD_JSON_BUILD_PAIR("state", SD_JSON_BUILD_STRING(dns_transaction_state_to_string(q->state))), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_DNSSEC_FAILED, + "result", SD_JSON_BUILD_STRING(dnssec_result_to_string(q->answer_dnssec_result))), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_RCODE_FAILURE, + "rcode", SD_JSON_BUILD_INTEGER(q->answer_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(q->state == DNS_TRANSACTION_ERRNO, + "errno", SD_JSON_BUILD_INTEGER(q->answer_errno)), + SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, + DNS_TRANSACTION_DNSSEC_FAILED, + DNS_TRANSACTION_RCODE_FAILURE) && + q->answer_ede_rcode >= 0, + "extendedDNSErrorCode", SD_JSON_BUILD_INTEGER(q->answer_ede_rcode)), + SD_JSON_BUILD_PAIR_CONDITION(IN_SET(q->state, + DNS_TRANSACTION_DNSSEC_FAILED, + DNS_TRANSACTION_RCODE_FAILURE) && + q->answer_ede_rcode >= 0 && !isempty(q->answer_ede_msg), + "extendedDNSErrorMessage", SD_JSON_BUILD_STRING(q->answer_ede_msg)), + SD_JSON_BUILD_PAIR("question", SD_JSON_BUILD_VARIANT(jquestion)), + SD_JSON_BUILD_PAIR_CONDITION(!!jcollected_questions, + "collectedQuestions", SD_JSON_BUILD_VARIANT(jcollected_questions)), + SD_JSON_BUILD_PAIR_CONDITION(!!janswer, "answer", SD_JSON_BUILD_VARIANT(janswer))); - if (r < 0) - log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); - } + if (r < 0) + log_debug_errno(r, "Failed to send monitor event, ignoring: %m"); return 0; } diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 8329f39021..d0a86157a2 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -4162,3 +4162,51 @@ int varlink_error_to_errno(const char *error, sd_json_variant *parameters) { return -EBADR; /* Catch-all */ } + +int varlink_many_notifyb(Set *s, ...) { + int r; + + /* Equivalent to varlink_notifyb(), but does this for each entry of the supplied set of Varlink connections */ + + if (set_isempty(s)) + return 0; + + _cleanup_(sd_json_variant_unrefp) sd_json_variant *parameters = NULL; + va_list ap; + va_start(ap, s); + r = sd_json_buildv(¶meters, ap); + va_end(ap); + if (r < 0) + return r; + + int ret = 1; + Varlink *link; + SET_FOREACH(link, s) + RET_GATHER(ret, varlink_notify(link, parameters)); + + return ret; +} + +int varlink_many_reply(Set *s, sd_json_variant *parameters) { + if (set_isempty(s)) + return 0; + + int ret = 1; + Varlink *link; + SET_FOREACH(link, s) + RET_GATHER(ret, varlink_reply(link, parameters)); + + return ret; +} + +int varlink_many_error(Set *s, const char *error_id, sd_json_variant *parameters) { + if (set_isempty(s)) + return 0; + + int ret = 1; + Varlink *link; + SET_FOREACH(link, s) + RET_GATHER(ret, varlink_error(link, error_id, parameters)); + + return ret; +} diff --git a/src/shared/varlink.h b/src/shared/varlink.h index 739786b975..40a8fd086f 100644 --- a/src/shared/varlink.h +++ b/src/shared/varlink.h @@ -2,9 +2,10 @@ #pragma once #include "sd-event.h" - #include "sd-json.h" + #include "pidref.h" +#include "set.h" #include "time-util.h" #include "varlink-idl.h" @@ -264,6 +265,12 @@ int varlink_invocation(VarlinkInvocationFlags flags); int varlink_error_to_errno(const char *error, sd_json_variant *parameters); +int varlink_many_notifyb(Set *s, ...); +#define varlink_many_notifybo(s, ...) \ + varlink_many_notifyb((s), SD_JSON_BUILD_OBJECT(__VA_ARGS__)) +int varlink_many_reply(Set *s, sd_json_variant *parameters); +int varlink_many_error(Set *s, const char *error_id, sd_json_variant *parameters); + DEFINE_TRIVIAL_CLEANUP_FUNC(Varlink *, varlink_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(Varlink *, varlink_close_unref); DEFINE_TRIVIAL_CLEANUP_FUNC(Varlink *, varlink_flush_close_unref); -- cgit v1.2.3