[libvirt] [PATCH 3/5] qemu: Refactor qemuCapsParsePCIDeviceStrs using virCommand

Jiri Denemark jdenemar at redhat.com
Thu Jan 13 13:04:28 UTC 2011


---
 src/qemu/qemu_capabilities.c |   58 +++++++++++++++++++----------------------
 1 files changed, 27 insertions(+), 31 deletions(-)

diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c
index 3d10b42..0fb6ec9 100644
--- a/src/qemu/qemu_capabilities.c
+++ b/src/qemu/qemu_capabilities.c
@@ -1087,48 +1087,44 @@ fail:
     return -1;
 }
 
+
+static char *
+qemuCapsExtractDeviceProps(const char *qemu,
+                           const char *device)
+{
+    virCommandPtr cmd = virCommandNew(qemu);
+    char *output = NULL;
+
+    virCommandAddArg(cmd, "-device");
+    virCommandAddArgFormat(cmd, "%s,?", device);
+    virCommandAddEnvPassCommon(cmd);
+    virCommandClearCaps(cmd);
+    virCommandSetErrorBuffer(cmd, &output);
+
+    if (virCommandRun(cmd, NULL) < 0) {
+        VIR_ERROR(_("Unable to read %s %s device output"), qemu, device);
+        VIR_FREE(output);
+    }
+
+    virCommandFree(cmd);
+
+    return output;
+}
+
+
 static void
 qemuCapsParsePCIDeviceStrs(const char *qemu,
                            unsigned long long *flags)
 {
-    const char *const qemuarg[] = { qemu, "-device", "pci-assign,?", NULL };
-    const char *const qemuenv[] = { "LC_ALL=C", NULL };
-    pid_t child;
-    int status;
-    int newstderr = -1;
+    char *pciassign = qemuCapsExtractDeviceProps(qemu, "pci-assign");
 
-    if (virExec(qemuarg, qemuenv, NULL,
-                &child, -1, NULL, &newstderr, VIR_EXEC_CLEAR_CAPS) < 0)
+    if (!pciassign)
         return;
 
-    char *pciassign = NULL;
-    enum { MAX_PCI_OUTPUT_SIZE = 1024*4 };
-    int len = virFileReadLimFD(newstderr, MAX_PCI_OUTPUT_SIZE, &pciassign);
-    if (len < 0) {
-        virReportSystemError(errno,
-                             _("Unable to read %s pci-assign device output"),
-                             qemu);
-        goto cleanup;
-    }
-
     if (strstr(pciassign, "pci-assign.configfd"))
         *flags |= QEMUD_CMD_FLAG_PCI_CONFIGFD;
 
-cleanup:
     VIR_FREE(pciassign);
-    VIR_FORCE_CLOSE(newstderr);
-rewait:
-    if (waitpid(child, &status, 0) != child) {
-        if (errno == EINTR)
-            goto rewait;
-
-        VIR_ERROR(_("Unexpected exit status from qemu %d pid %lu"),
-                  WEXITSTATUS(status), (unsigned long)child);
-    }
-    if (WEXITSTATUS(status) != 0) {
-        VIR_WARN("Unexpected exit status '%d', qemu probably failed",
-                 WEXITSTATUS(status));
-    }
 }
 
 int qemuCapsExtractVersionInfo(const char *qemu,
-- 
1.7.4.rc1




More information about the libvir-list mailing list