[libvirt] [PATCH] Sanitize qemu monitor output

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The attached patch cleans up output we read from the qemu monitor. This
is a simplified form of a patch I posted awhile ago. From the patch:

/* The monitor doesn't dump clean output after we have written to
 * it. Every character we write dumps a bunch of useless stuff,
 * so the result looks like "cXcoXcomXcommXcommaXcommanXcommand"
 * Try to throw away everything before the first full command
 * occurence, and inbetween the command and the newline starting
 * the response
 */

This extra output makes our qemu log files _huge_ if doing things like
polling disk/net stats, and prevents us from returning any useful error
message printed from the monitor (say for media/disk eject, disk/net
stats, etc).

I've been running with this patch for a while and haven't hit any issues.

Thanks,
Cole
 src/qemu_driver.c |   26 ++++++++++++++++++++++----
 1 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index 0130d61..f60ccf4 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -1107,7 +1107,7 @@ qemudMonitorCommand (const struct qemud_driver *driver ATTRIBUTE_UNUSED,
                      const char *cmd,
                      char **reply) {
     int size = 0;
-    char *buf = NULL;
+    char *buf = NULL, *commptr = NULL;
     size_t cmdlen = strlen(cmd);
 
     if (safewrite(vm->monitor, cmd, cmdlen) != cmdlen)
@@ -1145,7 +1145,25 @@ qemudMonitorCommand (const struct qemud_driver *driver ATTRIBUTE_UNUSED,
 
         /* Look for QEMU prompt to indicate completion */
         if (buf && ((tmp = strstr(buf, "\n(qemu) ")) != NULL)) {
-            tmp[0] = '\0';
+            /* Preserve the newline */
+            tmp[1] = '\0';
+
+            /* The monitor doesn't dump clean output after we have written to
+             * it. Every character we write dumps a bunch of useless stuff,
+             * so the result looks like "cXcoXcomXcommXcommaXcommanXcommand"
+             * Try to throw away everything before the first full command
+             * occurence, and inbetween the command and the newline starting
+             * the response
+             */
+            if ((commptr = strstr(buf, cmd))) {
+                char *dupptr = strchr(commptr, '\n');
+                if (dupptr) {
+                    char *tmpbuf = strdup(dupptr);
+                    VIR_FREE(buf);
+                    buf = strdup(tmpbuf);
+                }
+            }
+
             break;
         }
     pollagain:
@@ -2596,7 +2614,7 @@ static int qemudDomainChangeEjectableMedia(virDomainPtr dom,
 
     if (qemudMonitorCommand(driver, vm, cmd, &reply) < 0) {
         qemudReportError(dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                         "%s", _("cannot change cdrom media"));
+                         "%s", _("could not change cdrom media"));
         VIR_FREE(cmd);
         return -1;
     }
@@ -2607,7 +2625,7 @@ static int qemudDomainChangeEjectableMedia(virDomainPtr dom,
     DEBUG ("ejectable media change reply: %s", reply);
     if (strstr(reply, "\ndevice ")) {
         qemudReportError (dom->conn, dom, NULL, VIR_ERR_OPERATION_FAILED,
-                          "%s", _("changing cdrom media failed"));
+                          _("changing cdrom media failed: %s"), reply);
         VIR_FREE(reply);
         VIR_FREE(cmd);
         return -1;
--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]