[libvirt] [PATCH v6 11/33] qemu_process: Introduce qemuProcessQMPStart

Chris Venteicher cventeic at redhat.com
Sun Jan 13 00:50:10 UTC 2019


Move a step closer to the function structure used elsewhere in
qemu_process where qemuProcessStart and qemuProcessStop are the exposed
functions.

qemuProcessQMPStart mirrors qemuProcessStart in calling sub functions to
initialize, launch the process and connect the monitor to the QEMU
process.

static functions qemuProcessQMPInit, qemuProcessQMPLaunch and
qemuProcessQMPConnectMonitor are introduced.

qemuProcessQMPLaunch is just renamed from qemuProcessQMPRun and
encapsulates all of the original code.

qemuProcessQMPInit and qemuProcessQMPMonitor are nearly empty functions
acting as placeholders for later patches where blocks of semi-complicated code
are cut/pasted into these functions without modification
(hopefully making review easier.)

Looking forward, the patch series ultimately moves the code into this
partitioning:

- qemuProcessQMPInit
    Becomes the location of ~25 lines of code to create storage
    directory, in thread safe way, and initialize paths
    for monpath, monarg and pidfile.

- qemuProcessQMPLaunch
    Becomes the location of ~48 lines of code used to create and run the
    QEMU command.

- qemuProcessQMPConnectMonitor
    Becomes the final location of ~58 lines of code used to open and
    initialize the monitor connection between libvirt and qemu.

Three smaller, purpose-identifying, functions of ~60 lines or less seem
better than a single large process "start" function of > 130 lines.

Being able to compare and contrast between the domain and non-domain
versions of process code is useful too.  There is some significant
overlap between what the non-domain and domain functions do.  There is
also significant additional functionality in the domain functions that
might be useful in the non-domain functions in the future.
Possibly there could be sharing between non-domain and
domain process code in the future but common code would have
to be carefully extracted from the domain process code (not trivial.)

Mirroring the domain process code has some value, but
partitioning the code into logical chunks of < 60 lines
is the main reason for the static functions.

Signed-off-by: Chris Venteicher <cventeic at redhat.com>
---
 src/qemu/qemu_capabilities.c |  4 +-
 src/qemu/qemu_process.c      | 93 +++++++++++++++++++++++++++++++++++-
 src/qemu/qemu_process.h      |  2 +-
 3 files changed, 94 insertions(+), 5 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index b833e0d11e..36d28ba5ea 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -4383,7 +4383,7 @@ virQEMUCapsInitQMP(virQEMUCapsPtr qemuCaps,
                                    runUid, runGid, false)))
         goto cleanup;
 
-    if (qemuProcessQMPRun(proc) < 0) {
+    if (qemuProcessQMPStart(proc) < 0) {
         if (proc->status != 0)
             virReportError(VIR_ERR_INTERNAL_ERROR,
                            _("Failed to probe QEMU binary with QMP: %s"),
@@ -4405,7 +4405,7 @@ virQEMUCapsInitQMP(virQEMUCapsPtr qemuCaps,
         procTCG = qemuProcessQMPNew(qemuCaps->binary, libDir,
                                     runUid, runGid, true);
 
-        if (qemuProcessQMPRun(procTCG) < 0)
+        if (qemuProcessQMPStart(procTCG) < 0)
             goto cleanup;
 
         if (virQEMUCapsInitQMPMonitorTCG(qemuCaps, procTCG->mon) < 0)
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 71c7fb58ad..3b785d64e5 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -8270,8 +8270,27 @@ qemuProcessQMPNew(const char *binary,
 }
 
 
-int
-qemuProcessQMPRun(qemuProcessQMPPtr proc)
+/* Initialize configuration and paths prior to starting QEMU
+ */
+static int
+qemuProcessQMPInit(qemuProcessQMPPtr proc)
+{
+    int ret = -1;
+
+    VIR_DEBUG("proc=%p, emulator=%s",
+              proc, proc->binary);
+
+    ret = 0;
+
+    VIR_DEBUG("ret=%i", ret);
+    return ret;
+}
+
+
+/* Launch QEMU Process
+ */
+static int
+qemuProcessQMPLaunch(qemuProcessQMPPtr proc)
 {
     virDomainXMLOptionPtr xmlopt = NULL;
     const char *machine;
@@ -8348,6 +8367,76 @@ qemuProcessQMPRun(qemuProcessQMPPtr proc)
 }
 
 
+/* Connect Monitor to QEMU Process
+ */
+static int
+qemuProcessQMPConnectMonitor(qemuProcessQMPPtr proc)
+{
+    int ret = -1;
+
+    VIR_DEBUG("proc=%p, emulator=%s, proc->pid=%lld",
+              proc, proc->binary, (long long)proc->pid);
+
+    ret = 0;
+
+    VIR_DEBUG("ret=%i", ret);
+    return ret;
+}
+
+
+/**
+ * qemuProcessQMPStart:
+ * @proc: Stores process and connection state
+ *
+ * Start and connect to QEMU binary so QMP queries can be made.
+ *
+ * Usage:
+ *   proc = qemuProcessQMPNew(binary, libDir, runUid, runGid, forceTCG);
+ *   qemuProcessQMPStart(proc);
+ *   ** Send QMP Queries to QEMU using monitor (proc->mon) **
+ *   qemuProcessQMPStop(proc);
+ *   qemuProcessQMPFree(proc);
+ *
+ * Check monitor is not NULL before using.
+ *
+ * QEMU probing failure results in monitor being NULL but is not considered
+ * an error case and does not result in a negative return value.
+ *
+ * Both qemuProcessQMPStop and qemuProcessQMPFree must be called to cleanup and
+ * free resources, even in activation failure cases.
+ *
+ * Process error output (proc->stderr) remains available in qemuProcessQMP
+ * struct until qemuProcessQMPFree is called.
+ */
+int
+qemuProcessQMPStart(qemuProcessQMPPtr proc)
+{
+    int ret = -1;
+
+    VIR_DEBUG("proc=%p, emulator=%s",
+              proc, proc->binary);
+
+    if (qemuProcessQMPInit(proc) < 0)
+        goto cleanup;
+
+    if (qemuProcessQMPLaunch(proc) < 0)
+        goto stop;
+
+    if (qemuProcessQMPConnectMonitor(proc) < 0)
+        goto stop;
+
+    ret = 0;
+
+ cleanup:
+    VIR_DEBUG("ret=%i", ret);
+    return ret;
+
+ stop:
+    qemuProcessQMPStop(proc);
+    goto cleanup;
+}
+
+
 void
 qemuProcessQMPStop(qemuProcessQMPPtr proc)
 {
diff --git a/src/qemu/qemu_process.h b/src/qemu/qemu_process.h
index b0b58f9424..9938dd0a69 100644
--- a/src/qemu/qemu_process.h
+++ b/src/qemu/qemu_process.h
@@ -241,7 +241,7 @@ qemuProcessQMPPtr qemuProcessQMPNew(const char *binary,
 
 void qemuProcessQMPFree(qemuProcessQMPPtr proc);
 
-int qemuProcessQMPRun(qemuProcessQMPPtr cmd);
+int qemuProcessQMPStart(qemuProcessQMPPtr proc);
 
 void qemuProcessQMPStop(qemuProcessQMPPtr proc);
 
-- 
2.17.1




More information about the libvir-list mailing list