[libvirt] [PATCH] qemu-monitor-command: hide the low level protocol

Lai Jiangshan laijs at cn.fujitsu.com
Thu Jan 6 08:35:03 UTC 2011


When qmp protocol is available, the libvirt will use this protocol
to communicates with qemu. And when we use "virsh  qemu-monitor-command",
we need:
	virsh qemu-monitor-command dom '{ "execute": "eject", \
		"arguments": { "device": "ide1-cd0" } }'

But virsh is typical a human command line interface, it is not comfortable
that a human user has to construct such commands. This patch makes
human user can use
	virsh qemu-monitor-command dom 'eject ide1-cd0'
in any time(qmp protocol is available or not).

The result string is also converted to the same as non-qmp-protocol version.
The user do not need to concern about the low level protocol now.

Signed-off-by: Lai Jiangshan <laijs at cn.fujitsu.com>
---
diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c
index 7877731..0f11a78 100644
--- a/src/qemu/qemu_monitor_json.c
+++ b/src/qemu/qemu_monitor_json.c
@@ -2381,9 +2381,12 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
 {
     virJSONValuePtr cmd = NULL;
     virJSONValuePtr reply = NULL;
+    char *p, *q;
     int ret = -1;
 
-    cmd = virJSONValueFromString(cmd_str);
+    cmd = qemuMonitorJSONMakeCommand("human-monitor-command",
+                                     "s:command-line", cmd_str,
+                                     NULL);
     if (!cmd)
         return -1;
 
@@ -2394,6 +2397,37 @@ int qemuMonitorJSONArbitraryCommand(qemuMonitorPtr mon,
     if (!(*reply_str))
         goto cleanup;
 
+    q = *reply_str;
+
+    /* strip the head and tail */
+    p= q + strlen("{\"return\":\"");
+    *(q + strlen(q) - strlen("\"}")) = 0;
+
+    /* inplace copy and convert escapes */
+    while (*p) {
+       if (*p == '\\') {
+           char c = 0;
+
+           switch (*(p + 1)) {
+           case 'r': c = '\r'; break;
+           case 'n': c = '\n'; break;
+           case 't': c = '\t'; break;
+           case '\'': c = '\''; break;
+           case '\\': c = '\\'; break;
+           case '\"': c = '\"'; break;
+           default: break;
+           }
+
+           if (c) {
+               p += 2;
+               *(q++) = c;
+               continue;
+           }
+       }
+       *(q++) = *(p++);
+    }
+    *q = 0;
+
     ret = 0;
 
 cleanup:




More information about the libvir-list mailing list