]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
varlink: get rid of "reply" field
authorLennart Poettering <lennart@poettering.net>
Wed, 5 Apr 2023 13:57:44 +0000 (15:57 +0200)
committerLennart Poettering <lennart@poettering.net>
Wed, 12 Apr 2023 13:14:21 +0000 (15:14 +0200)
So far, if we do a synchronous varlink call from the client side via
varlink_call(), we'll
move the returned json object from "v->current" into "v->reply", and
keep it referenced there until the next call. We then return a pointer
to it. This ensures that the json object remains valid between two
varlink_call() invocations.

But the thing is, we don't need a separate field for that, we can just
leave the data in "v->current". This means VARLINK_IDLE_CLIENT state
will be permitted with and without v->current initialized. Initially,
after connection setup it will be set to NULL, but after the first
varlink_call() it will be set to the most recent response, pinning it
into memory.

src/shared/varlink.c

index 8244f599a9a0a86c66160fb2e1637f69ee13aa5f..fe1cfeb0a1084bc16447b1413ec2c49f5d035713 100644 (file)
@@ -128,7 +128,6 @@ struct Varlink {
         VarlinkReply reply_callback;
 
         JsonVariant *current;
-        JsonVariant *reply;
 
         struct ucred ucred;
         bool ucred_acquired:1;
@@ -372,7 +371,6 @@ static void varlink_clear(Varlink *v) {
         v->output_buffer = mfree(v->output_buffer);
 
         v->current = json_variant_unref(v->current);
-        v->reply = json_variant_unref(v->reply);
 
         v->event = sd_event_unref(v->event);
 }
@@ -1478,6 +1476,8 @@ int varlink_call(
 
         assert(v->n_pending == 0); /* n_pending can't be > 0 if we are in VARLINK_IDLE_CLIENT state */
 
+        v->current = json_variant_unref(v->current);
+
         r = varlink_sanitize_parameters(&parameters);
         if (r < 0)
                 return varlink_log_errno(v, r, "Failed to sanitize parameters: %m");
@@ -1514,17 +1514,14 @@ int varlink_call(
         case VARLINK_CALLED:
                 assert(v->current);
 
-                json_variant_unref(v->reply);
-                v->reply = TAKE_PTR(v->current);
-
                 varlink_set_state(v, VARLINK_IDLE_CLIENT);
                 assert(v->n_pending == 1);
                 v->n_pending--;
 
                 if (ret_parameters)
-                        *ret_parameters = json_variant_by_key(v->reply, "parameters");
+                        *ret_parameters = json_variant_by_key(v->current, "parameters");
                 if (ret_error_id)
-                        *ret_error_id = json_variant_string(json_variant_by_key(v->reply, "error"));
+                        *ret_error_id = json_variant_string(json_variant_by_key(v->current, "error"));
                 if (ret_flags)
                         *ret_flags = 0;