From 58121fbc3085296364e6b90bc16cb56eeaf36f77 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   |  3 +++
 src/qemu/qemu_hotpatch.c | 36 ++++++++++++++++++++++++++++++------
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index d4c5f073bb..2b24881f75 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -23196,6 +23196,9 @@ qemuDomainHotpatchManage(virDomainPtr domain,
                                    VIR_DOMAIN_JOB_OPERATION_HOTPATCH, 0) < 0)
         goto cleanup;
 
+    if (virDomainObjCheckActive(vm) < 0)
+        goto endjob;
+
     qemuDomainObjSetAsyncJobMask(vm, QEMU_JOB_DEFAULT_MASK);
 
     switch (action) {
diff --git a/src/qemu/qemu_hotpatch.c b/src/qemu/qemu_hotpatch.c
index 45796b3f24..03e63c1341 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(virDomainObjPtr 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(virDomainObjPtr 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(virDomainObjPtr 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(virDomainObjPtr 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(virDomainObjPtr 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(virDomainObjPtr 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