]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
vbox: Rewrite vboxDomainSuspend
authorTaowei <uaedante@gmail.com>
Mon, 11 Aug 2014 10:06:27 +0000 (18:06 +0800)
committerMichal Privoznik <mprivozn@redhat.com>
Fri, 15 Aug 2014 07:25:10 +0000 (09:25 +0200)
src/vbox/vbox_common.c
src/vbox/vbox_tmpl.c
src/vbox/vbox_uniformed_api.h

index 989369c39d8820faa3221e53d26962fbf86139cb..f8868eaf3d04abcb64c2530c286087823bc8e9dc 100644 (file)
@@ -2362,3 +2362,50 @@ int vboxDomainIsUpdated(virDomainPtr dom)
     vboxIIDUnalloc(&iid);
     return ret;
 }
+
+int vboxDomainSuspend(virDomainPtr dom)
+{
+    VBOX_OBJECT_CHECK(dom->conn, int, -1);
+    IMachine *machine    = NULL;
+    vboxIIDUnion iid;
+    IConsole *console    = NULL;
+    PRBool isAccessible  = PR_FALSE;
+    PRUint32 state;
+
+    if (openSessionForMachine(data, dom->uuid, &iid, &machine, false) < 0)
+        goto cleanup;
+
+    if (!machine)
+        goto cleanup;
+
+    gVBoxAPI.UIMachine.GetAccessible(machine, &isAccessible);
+    if (!isAccessible)
+        goto cleanup;
+
+    gVBoxAPI.UIMachine.GetState(machine, &state);
+
+    if (gVBoxAPI.machineStateChecker.Running(state)) {
+        /* set state pause */
+        gVBoxAPI.UISession.OpenExisting(data, &iid, machine);
+        gVBoxAPI.UISession.GetConsole(data->vboxSession, &console);
+        if (console) {
+            gVBoxAPI.UIConsole.Pause(console);
+            VBOX_RELEASE(console);
+            ret = 0;
+        } else {
+            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                           _("error while suspending the domain"));
+            goto cleanup;
+        }
+        gVBoxAPI.UISession.Close(data->vboxSession);
+    } else {
+        virReportError(VIR_ERR_OPERATION_FAILED, "%s",
+                       _("machine not in running state to suspend it"));
+        goto cleanup;
+    }
+
+ cleanup:
+    VBOX_RELEASE(machine);
+    vboxIIDUnalloc(&iid);
+    return ret;
+}
index 12b8188d780e4bf3223eecc0a697c765f967cb25..19fa213c3265c4072d387272541a50aab0f27395 100644 (file)
@@ -933,58 +933,6 @@ vboxSocketParseAddrUtf16(vboxGlobalData *data, const PRUnichar *utf16,
     return result;
 }
 
-static int vboxDomainSuspend(virDomainPtr dom)
-{
-    VBOX_OBJECT_CHECK(dom->conn, int, -1);
-    IMachine *machine    = NULL;
-    vboxIID iid = VBOX_IID_INITIALIZER;
-    IConsole *console    = NULL;
-    PRBool isAccessible  = PR_FALSE;
-    PRUint32 state;
-    nsresult rc;
-
-    vboxIIDFromUUID(&iid, dom->uuid);
-    rc = VBOX_OBJECT_GET_MACHINE(iid.value, &machine);
-    if (NS_FAILED(rc)) {
-        virReportError(VIR_ERR_NO_DOMAIN,
-                       _("no domain with matching id %d"), dom->id);
-        goto cleanup;
-    }
-
-    if (!machine)
-        goto cleanup;
-
-    machine->vtbl->GetAccessible(machine, &isAccessible);
-    if (isAccessible) {
-        machine->vtbl->GetState(machine, &state);
-
-        if (state == MachineState_Running) {
-            /* set state pause */
-            VBOX_SESSION_OPEN_EXISTING(iid.value, machine);
-            data->vboxSession->vtbl->GetConsole(data->vboxSession, &console);
-            if (console) {
-                console->vtbl->Pause(console);
-                VBOX_RELEASE(console);
-                ret = 0;
-            } else {
-                virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                               _("error while suspending the domain"));
-                goto cleanup;
-            }
-            VBOX_SESSION_CLOSE();
-        } else {
-            virReportError(VIR_ERR_OPERATION_FAILED, "%s",
-                           _("machine not in running state to suspend it"));
-            goto cleanup;
-        }
-    }
-
- cleanup:
-    VBOX_RELEASE(machine);
-    vboxIIDUnalloc(&iid);
-    return ret;
-}
-
 static int vboxDomainResume(virDomainPtr dom)
 {
     VBOX_OBJECT_CHECK(dom->conn, int, -1);
@@ -10045,6 +9993,12 @@ _consoleSaveState(IConsole *console, IProgress **progress)
     return console->vtbl->SaveState(console, progress);
 }
 
+static nsresult
+_consolePause(IConsole *console)
+{
+    return console->vtbl->Pause(console);
+}
+
 static nsresult
 _progressWaitForCompletion(IProgress *progress, PRInt32 timeout)
 {
@@ -10473,6 +10427,11 @@ static bool _machineStateNotStart(PRUint32 state)
             (state == MachineState_Aborted));
 }
 
+static bool _machineStateRunning(PRUint32 state)
+{
+    return state == MachineState_Running;
+}
+
 static vboxUniformedPFN _UPFN = {
     .Initialize = _pfnInitialize,
     .Uninitialize = _pfnUninitialize,
@@ -10553,6 +10512,7 @@ static vboxUniformedISession _UISession = {
 
 static vboxUniformedIConsole _UIConsole = {
     .SaveState = _consoleSaveState,
+    .Pause = _consolePause,
 };
 
 static vboxUniformedIProgress _UIProgress = {
@@ -10638,6 +10598,7 @@ static vboxUniformedIMedium _UIMedium = {
 static uniformedMachineStateChecker _machineStateChecker = {
     .Online = _machineStateOnline,
     .NotStart = _machineStateNotStart,
+    .Running = _machineStateRunning,
 };
 
 void NAME(InstallUniformedAPI)(vboxUniformedAPI *pVBoxAPI)
index 15689489bac149fbe3a785c83aa2bdff48e1c9ce..8334437877c94628a4e21af625fda0dfb3f58b7f 100644 (file)
@@ -237,6 +237,7 @@ typedef struct {
 /* Functions for IConsole */
 typedef struct {
     nsresult (*SaveState)(IConsole *console, IProgress **progress);
+    nsresult (*Pause)(IConsole *console);
 } vboxUniformedIConsole;
 
 /* Functions for IProgress */
@@ -339,6 +340,7 @@ typedef struct {
 typedef struct {
     bool (*Online)(PRUint32 state);
     bool (*NotStart)(PRUint32 state);
+    bool (*Running)(PRUint32 state);
 } uniformedMachineStateChecker;
 
 typedef struct {
@@ -414,6 +416,7 @@ virDomainPtr vboxDomainCreateXML(virConnectPtr conn, const char *xml,
 int vboxDomainIsActive(virDomainPtr dom);
 int vboxDomainIsPersistent(virDomainPtr dom);
 int vboxDomainIsUpdated(virDomainPtr dom);
+int vboxDomainSuspend(virDomainPtr dom);
 
 /* Version specified functions for installing uniformed API */
 void vbox22InstallUniformedAPI(vboxUniformedAPI *pVBoxAPI);