From 5e5019bf78159d3b5bc2645865b1ca98fb4063c1 Mon Sep 17 00:00:00 2001 From: Nikolay Shirokovskiy Date: Fri, 16 Sep 2016 13:35:34 +0300 Subject: [PATCH] qemu: agent: skip outdated sync replies When we wait for sync reply we can receive delayed reply to syncs or commands that were sent erlier. We can safely skip them until we receive sync reply with correct id. There is no much sense report this situation to client. Actually with a bit of "luck" if we involve client into this the play can go on forever: send sync 0, receive sync reply -1, send sync 1, receive reply 0 ... --- src/qemu/qemu_agent.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/src/qemu/qemu_agent.c b/src/qemu/qemu_agent.c index 16aa629e55..a856012450 100644 --- a/src/qemu/qemu_agent.c +++ b/src/qemu/qemu_agent.c @@ -83,6 +83,8 @@ struct _qemuAgentMessage { bool finished; /* true for sync command */ bool sync; + /* id of the issued sync comand */ + unsigned long long id; }; @@ -337,6 +339,24 @@ qemuAgentIOProcessLine(qemuAgentPtr mon, } else if (virJSONValueObjectHasKey(obj, "error") == 1 || virJSONValueObjectHasKey(obj, "return") == 1) { if (msg) { + if (msg->sync) { + unsigned long long id; + + if (virJSONValueObjectGetNumberUlong(obj, "return", &id) < 0) { + VIR_DEBUG("Ignoring delayed reply on sync"); + ret = 0; + goto cleanup; + } + + VIR_DEBUG("Guest returned ID: %llu", id); + + if (msg->id != id) { + VIR_DEBUG("Guest agent returned ID: %llu instead of %llu", + id, msg->id); + ret = 0; + goto cleanup; + } + } msg->rxObject = obj; msg->finished = 1; obj = NULL; @@ -935,7 +955,7 @@ qemuAgentGuestSync(qemuAgentPtr mon) { int ret = -1; int send_ret; - unsigned long long id, id_ret; + unsigned long long id; qemuAgentMessage sync_msg; memset(&sync_msg, 0, sizeof(sync_msg)); @@ -950,6 +970,7 @@ qemuAgentGuestSync(qemuAgentPtr mon) sync_msg.txLength = strlen(sync_msg.txBuffer); sync_msg.sync = true; + sync_msg.id = id; VIR_DEBUG("Sending guest-sync command with ID: %llu", id); @@ -967,20 +988,6 @@ qemuAgentGuestSync(qemuAgentPtr mon) goto cleanup; } - if (virJSONValueObjectGetNumberUlong(sync_msg.rxObject, - "return", &id_ret) < 0) { - virReportError(VIR_ERR_AGENT_UNSYNCED, "%s", - _("Malformed return value")); - goto cleanup; - } - - VIR_DEBUG("Guest returned ID: %llu", id_ret); - if (id_ret != id) { - virReportError(VIR_ERR_AGENT_UNSYNCED, - _("Guest agent returned ID: %llu instead of %llu"), - id_ret, id); - goto cleanup; - } ret = 0; cleanup: -- 2.39.5