From 76a3af06308480600f354f8ea6dcd62587555a0c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 30 Oct 2024 15:30:16 +0100 Subject: sd-varlink: add helper VARLINK_STATE_WANTS_REPLY() Let's add a helper that detects whether we still need to reply to a state. This should make the logic easier to follow. --- src/libsystemd/sd-varlink/sd-varlink.c | 9 +++++---- src/libsystemd/sd-varlink/varlink-internal.h | 7 +++++++ 2 files changed, 12 insertions(+), 4 deletions(-) (limited to 'src/libsystemd') diff --git a/src/libsystemd/sd-varlink/sd-varlink.c b/src/libsystemd/sd-varlink/sd-varlink.c index 31ce6710c6..a1b3648266 100644 --- a/src/libsystemd/sd-varlink/sd-varlink.c +++ b/src/libsystemd/sd-varlink/sd-varlink.c @@ -1369,7 +1369,7 @@ static int varlink_dispatch_method(sd_varlink *v) { varlink_log_errno(v, r, "Parameters for method %s() didn't pass validation on field '%s': %m", method, strna(bad_field)); - if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { + if (VARLINK_STATE_WANTS_REPLY(v->state)) { r = sd_varlink_error_invalid_parameter_name(v, bad_field); if (r < 0) goto fail; @@ -1384,15 +1384,16 @@ static int varlink_dispatch_method(sd_varlink *v) { if (r < 0) { varlink_log_errno(v, r, "Callback for %s returned error: %m", method); - /* We got an error back from the callback. Propagate it to the client if the method call remains unanswered. */ - if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { + /* We got an error back from the callback. Propagate it to the client if the + * method call remains unanswered. */ + if (VARLINK_STATE_WANTS_REPLY(v->state)) { r = sd_varlink_error_errno(v, r); if (r < 0) goto fail; } } } - } else if (IN_SET(v->state, VARLINK_PROCESSING_METHOD, VARLINK_PROCESSING_METHOD_MORE)) { + } else if (VARLINK_STATE_WANTS_REPLY(v->state)) { r = sd_varlink_errorbo(v, SD_VARLINK_ERROR_METHOD_NOT_FOUND, SD_JSON_BUILD_PAIR("method", SD_JSON_BUILD_STRING(method))); if (r < 0) goto fail; diff --git a/src/libsystemd/sd-varlink/varlink-internal.h b/src/libsystemd/sd-varlink/varlink-internal.h index 5469d9e345..b184ac7754 100644 --- a/src/libsystemd/sd-varlink/varlink-internal.h +++ b/src/libsystemd/sd-varlink/varlink-internal.h @@ -63,6 +63,13 @@ typedef enum VarlinkState { VARLINK_PENDING_METHOD, \ VARLINK_PENDING_METHOD_MORE) +/* Tests whether we are expected to generate a method call reply, i.e. are processing a method call, except + * one with the ONEWAY flag set. */ +#define VARLINK_STATE_WANTS_REPLY(state) \ + IN_SET(state, \ + VARLINK_PROCESSING_METHOD, \ + VARLINK_PROCESSING_METHOD_MORE) + typedef struct VarlinkJsonQueueItem VarlinkJsonQueueItem; /* A queued message we shall write into the socket, along with the file descriptors to send at the same -- cgit v1.2.3