From ebdf0312a590ba60f3d304f338737155f469dd7a Mon Sep 17 00:00:00 2001
From: AlexChen <alex.chen@huawei.com>
Date: Fri, 9 Jul 2021 10:50:07 +0800
Subject: [PATCH] hotpatch: check vm id and pid before using hotpatch api

Check if the vm is alive before using hotpatch api by calling
virDomainObjCheckActive() to check vm id and calling
qemuDomainHotpatchCheckPid() to check vm pid.

Signed-off-by: Bihong Yu <yubihong@huawei.com>
Signed-off-by: AlexChen <alex.chen@huawei.com>
---
 src/qemu/qemu_driver.c   |  9 ++++++---
 src/qemu/qemu_hotpatch.c | 36 ++++++++++++++++++++++++++++++------
 2 files changed, 36 insertions(+), 9 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 31917ef591..05cc0db3ae 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -19954,11 +19954,14 @@ qemuDomainHotpatchManage(virDomainPtr domain,
     if (!(vm = qemuDomainObjFromDomain(domain)))
         goto cleanup;
 
-    if (VirDomainObjBeginAsyncJob(driver, vm, QEMU_ASYNC_JOB_HOTPATCH,
+    if (virDomainObjBeginAsyncJob(vm, VIR_ASYNC_JOB_HOTPATCH,
                                    VIR_DOMAIN_JOB_OPERATION_HOTPATCH, 0) < 0)
         goto cleanup;
 
-    qemuDomainObjSetAsyncJobMask(vm, QEMU_JOB_DEFAULT_MASK);
+    if (virDomainObjCheckActive(vm) < 0)
+        goto endjob;
+
+    qemuDomainObjSetAsyncJobMask(vm, VIR_JOB_DEFAULT_MASK);
 
     switch (action) {
     case VIR_DOMAIN_HOTPATCH_APPLY:
@@ -19987,7 +19990,7 @@ qemuDomainHotpatchManage(virDomainPtr domain,
         ret[len - 1] = '\0';
 
  endjob:
-    qemuDomainObjEndAsyncJob(driver, vm);
+    virDomainObjEndAsyncJob(vm);
 
  cleanup:
     virDomainObjEndAPI(&vm);
diff --git a/src/qemu/qemu_hotpatch.c b/src/qemu/qemu_hotpatch.c
index c1a4ab7aca..31ef5bb7f2 100644
--- a/src/qemu/qemu_hotpatch.c
+++ b/src/qemu/qemu_hotpatch.c
@@ -37,12 +37,25 @@
 
 VIR_LOG_INIT("qemu_hotpatch");
 
+static int
+qemuDomainHotpatchCheckPid(pid_t pid)
+{
+    if (pid <= 0) {
+        virReportError(VIR_ERR_INVALID_ARG,
+                       "%s", _("Invalid pid"));
+        return -1;
+    }
+
+    return 0;
+}
+
 char *
 qemuDomainHotpatchQuery(virDomainObj *vm)
 {
     g_autoptr(virCommand) cmd = NULL;
     g_autofree char *binary = NULL;
     char *output = NULL;
+    pid_t pid = vm->pid;
     int ret = -1;
 
     if (!(binary = virFindFileInPath(LIBCARE_CTL))) {
@@ -51,12 +64,15 @@ qemuDomainHotpatchQuery(virDomainObj *vm)
         return NULL;
     }
 
+    if (qemuDomainHotpatchCheckPid(pid) < 0)
+        return NULL;
+
     cmd = virCommandNewArgList(binary, "info", "-p", NULL);
-    virCommandAddArgFormat(cmd, "%d", vm->pid);
+    virCommandAddArgFormat(cmd, "%d", pid);
     virCommandSetOutputBuffer(cmd, &output);
 
     VIR_DEBUG("Querying hotpatch for domain %s. (%s info -p %d)",
-              vm->def->name, binary, vm->pid);
+              vm->def->name, binary, pid);
 
     if (virCommandRun(cmd, &ret) < 0)
         goto error;
@@ -80,6 +96,7 @@ qemuDomainHotpatchApply(virDomainObj *vm,
     g_autoptr(virCommand) cmd = NULL;
     g_autofree char *binary = NULL;
     char *output = NULL;
+    pid_t pid = vm->pid;
     int ret = -1;
 
     if (!patch || !virFileExists(patch)) {
@@ -94,13 +111,16 @@ qemuDomainHotpatchApply(virDomainObj *vm,
         return NULL;
     }
 
+    if (qemuDomainHotpatchCheckPid(pid) < 0)
+        return NULL;
+
     cmd = virCommandNewArgList(binary, "patch", "-p", NULL);
-    virCommandAddArgFormat(cmd, "%d", vm->pid);
+    virCommandAddArgFormat(cmd, "%d", pid);
     virCommandAddArgList(cmd, patch, NULL);
     virCommandSetOutputBuffer(cmd, &output);
 
     VIR_DEBUG("Applying hotpatch for domain %s. (%s patch -p %d %s)",
-              vm->def->name, binary, vm->pid, patch);
+              vm->def->name, binary, pid, patch);
 
     if (virCommandRun(cmd, &ret) < 0)
         goto error;
@@ -144,6 +164,7 @@ qemuDomainHotpatchUnapply(virDomainObj *vm,
     g_autoptr(virCommand) cmd = NULL;
     g_autofree char *binary = NULL;
     char *output = NULL;
+    pid_t pid = vm->pid;
     int ret = -1;
 
     if (!id || !qemuDomainHotpatchIsPatchidValid(id)) {
@@ -158,13 +179,16 @@ qemuDomainHotpatchUnapply(virDomainObj *vm,
         return NULL;
     }
 
+    if (qemuDomainHotpatchCheckPid(pid) < 0)
+        return NULL;
+
     cmd = virCommandNewArgList(binary, "unpatch", "-p", NULL);
-    virCommandAddArgFormat(cmd, "%d", vm->pid);
+    virCommandAddArgFormat(cmd, "%d", pid);
     virCommandAddArgList(cmd, "-i", id, NULL);
     virCommandSetOutputBuffer(cmd, &output);
 
     VIR_DEBUG("Unapplying hotpatch for domain %s. (%s unpatch -p %d -i %s)",
-              vm->def->name, binary, vm->pid, id);
+              vm->def->name, binary, pid, id);
 
     if (virCommandRun(cmd, &ret) < 0)
         goto error;
-- 
2.27.0