varlink: get rid of "reply" field

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.
This commit is contained in:
Lennart Poettering 2023-04-05 15:57:44 +02:00
parent a3861b4726
commit 8531631763

View 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;