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

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@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:

On Thu, Jan 06, 2011 at 04:35:03PM +0800, Lai Jiangshan wrote:
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.
What if the user *wants* to use QMP? There will be commands available in QMP that are different to those in HMP, so there will definitely be a need to send true QMP commands here. In addition this patch changes semantics for *all* users of the API (virDomainQemuMonitorCommand) not solely virsh, preventing any apps from using QMP. If we want todo automatic HMP passthrough, then it must be optional with the default being off. It could perhaps be done with a flag to virDomainQemuMonitorCommand Regards, Daniel
participants (2)
-
Daniel P. Berrange
-
Lai Jiangshan