sd-varlink: add helper VARLINK_STATE_WANTS_REPLY()
authorLennart Poettering <lennart@poettering.net>
Wed, 30 Oct 2024 14:30:16 +0000 (15:30 +0100)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 31 Oct 2024 00:50:50 +0000 (09:50 +0900)
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
src/libsystemd/sd-varlink/varlink-internal.h

index 31ce6710c60a645e70e1c7c5b0c62fab83a61475..a1b36482666b4d67b6247f35bb40f21e0b183572 100644 (file)
@@ -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;
index 5469d9e345639421ec6f37d4be15b1e33efbe2a4..b184ac77546c945b3a06564515904797268663eb 100644 (file)
@@ -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