From: Lennart Poettering Date: Wed, 5 Apr 2023 13:57:44 +0000 (+0200) Subject: varlink: get rid of "reply" field X-Git-Tag: v254-rc1~741^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=8531631763b37cd158e6d417e051c8a110223e0f;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/shared/varlink.c b/src/shared/varlink.c index 8244f599a9a..fe1cfeb0a10 100644 --- a/src/shared/varlink.c +++ b/src/shared/varlink.c @@ -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(¶meters); 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;