From 173517a484554c0a363949150b4710a658c6edae Mon Sep 17 00:00:00 2001
From: Luiz Capitulino <lcapitulino@redhat.com>
Date: Wed, 19 Oct 2011 14:53:52 +0200
Subject: [PATCH 06/13] Monitor/QMP: Don't allow cont on bad VM state

RH-Author: Luiz Capitulino <lcapitulino@redhat.com>
Message-id: <1319036039-4358-7-git-send-email-lcapitulino@redhat.com>
Patchwork-id: 34410
O-Subject: [PATCH RHEL6.2 qemu-kvm v3 06/13] Monitor/QMP: Don't allow cont on bad VM state
Bugzilla: 617889
RH-Acked-by: Paolo Bonzini <pbonzini@redhat.com>
RH-Acked-by: Markus Armbruster <armbru@redhat.com>
RH-Acked-by: Kevin Wolf <kwolf@redhat.com>

We have two states where issuing cont before system_reset can
cause problems: RSTATE_SHUTDOWN (when -no-shutdown is used) and
RSTATE_PANICKED (which only happens with kvm).

This commit fixes that by doing the following when state is
RSTATE_SHUTDOWN or RSTATE_PANICKED:

 1. returning an error to the user/client if cont is issued
 2. automatically transition to RSTATE_PAUSED during system_reset

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
(cherry picked from commit 6667b23f300ff08a47a73821ce2fb7f773754969)

Conflicts:

	qerror.c
	qerror.h

Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 monitor.c  |    5 +++++
 qemu-kvm.c |    4 ++++
 qerror.c   |    4 ++++
 qerror.h   |    3 +++
 vl.c       |    4 ++++
 5 files changed, 20 insertions(+), 0 deletions(-)

Signed-off-by: Michal Novotny <minovotn@redhat.com>
---
 monitor.c  |    5 +++++
 qemu-kvm.c |    4 ++++
 qerror.c   |    4 ++++
 qerror.h   |    3 +++
 vl.c       |    4 ++++
 5 files changed, 20 insertions(+), 0 deletions(-)

diff --git a/monitor.c b/monitor.c
index 95fc9da..c806ec2 100644
--- a/monitor.c
+++ b/monitor.c
@@ -1383,7 +1383,12 @@ static int do_cont(Monitor *mon, const QDict *qdict, QObject **ret_data)
     if (runstate_check(RSTATE_IN_MIGRATE)) {
         qerror_report(QERR_MIGRATION_EXPECTED);
         return -1;
+    } else if (runstate_check(RSTATE_PANICKED) ||
+               runstate_check(RSTATE_SHUTDOWN)) {
+        qerror_report(QERR_RESET_REQUIRED);
+        return -1;
     }
+
     bdrv_iterate(encrypted_bdrv_it, &context);
     /* only resume the vm if all keys are set and valid */
     if (!context.err) {
diff --git a/qemu-kvm.c b/qemu-kvm.c
index 8aac6d9..a111fbd 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -2228,6 +2228,10 @@ int kvm_main_loop(void)
             qemu_irq_raise(qemu_system_powerdown);
         } else if (qemu_reset_requested()) {
             qemu_kvm_system_reset();
+            if (runstate_check(RSTATE_PANICKED) ||
+                runstate_check(RSTATE_SHUTDOWN)) {
+                runstate_set(RSTATE_PAUSED);
+            }
         } else if (kvm_debug_cpu_requested) {
             gdb_set_stop_cpu(kvm_debug_cpu_requested);
             vm_stop(RSTATE_DEBUG);
diff --git a/qerror.c b/qerror.c
index d4be7f6..9487454 100644
--- a/qerror.c
+++ b/qerror.c
@@ -193,6 +193,10 @@ static const QErrorStringTable qerror_table[] = {
         .desc      = "QMP input object member '%(member)' expects '%(expected)'",
     },
     {
+        .error_fmt = QERR_RESET_REQUIRED,
+        .desc      = "Resetting the Virtual Machine is required",
+    },
+    {
         .error_fmt = QERR_SET_PASSWD_FAILED,
         .desc      = "Could not set password",
     },
diff --git a/qerror.h b/qerror.h
index d95e0f0..a49f0b5 100644
--- a/qerror.h
+++ b/qerror.h
@@ -161,6 +161,9 @@ QError *qobject_to_qerror(const QObject *obj);
 #define QERR_QMP_BAD_INPUT_OBJECT_MEMBER \
     "{ 'class': 'QMPBadInputObjectMember', 'data': { 'member': %s, 'expected': %s } }"
 
+#define QERR_RESET_REQUIRED \
+    "{ 'class': 'ResetRequired', 'data': {} }"
+
 #define QERR_SET_PASSWD_FAILED \
     "{ 'class': 'SetPasswdFailed', 'data': {} }"
 
diff --git a/vl.c b/vl.c
index 7b88d71..626cb68 100644
--- a/vl.c
+++ b/vl.c
@@ -4221,6 +4221,10 @@ static void main_loop(void)
             pause_all_vcpus();
             qemu_system_reset();
             resume_all_vcpus();
+            if (runstate_check(RSTATE_PANICKED) ||
+                runstate_check(RSTATE_SHUTDOWN)) {
+                runstate_set(RSTATE_PAUSED);
+            }
         }
         if (qemu_powerdown_requested()) {
             monitor_protocol_event(QEVENT_POWERDOWN, NULL);
-- 
1.7.4.4

