提交 f0a23c0c 编写于 作者: P Peter Krempa

qemu: monitor: Add better APIs for adding of objects to qemu

Use the new monitor command internal API to allow wrapping of the object
name and alias into the JSON props so that they don't have to be passed
out of band.

The new API also takes a double pointer so that it can be cleared when
the value is consumed so that it does not need to happen in every single
caller.
Signed-off-by: NPeter Krempa <pkrempa@redhat.com>
Reviewed-by: NJán Tomko <jtomko@redhat.com>
上级 bf26dd22
...@@ -2991,6 +2991,109 @@ qemuMonitorAddDeviceArgs(qemuMonitorPtr mon, ...@@ -2991,6 +2991,109 @@ qemuMonitorAddDeviceArgs(qemuMonitorPtr mon,
} }
virJSONValuePtr
qemuMonitorCreateObjectPropsWrap(const char *type,
const char *alias,
virJSONValuePtr *props)
{
virJSONValuePtr ret;
ignore_value(virJSONValueObjectCreate(&ret,
"s:qom-type", type,
"s:id", alias,
"A:props", props,
NULL));
return ret;
}
/**
* qemuMonitorCreateObjectProps:
* @propsret: returns full object properties
* @type: Type name of object to add
* @objalias: Alias of the new object
* @...: Optional arguments for the given object. See virJSONValueObjectAddVArgs.
*
* Returns a JSONValue containing everything on success and NULL on error.
*/
int
qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
const char *type,
const char *alias,
...)
{
virJSONValuePtr props = NULL;
int ret = -1;
va_list args;
*propsret = NULL;
va_start(args, alias);
if (!(virJSONValueObjectCreateVArgs(&props, args)))
goto cleanup;
if (!(*propsret = qemuMonitorCreateObjectPropsWrap(type, alias, &props)))
goto cleanup;
ret = 0;
cleanup:
virJSONValueFree(props);
va_end(args);
return ret;
}
/**
* qemuMonitorAddObject:
* @mon: Pointer to monitor object
* @props: Optional arguments for the given type. The object is consumed and
* the pointer is cleared.
* @alias: If not NULL, returns the alias of the added object if it was added
* successfully to qemu. Caller should free the returned pointer.
*
* Returns 0 on success -1 on error.
*/
int
qemuMonitorAddObject(qemuMonitorPtr mon,
virJSONValuePtr *props,
char **alias)
{
const char *type = virJSONValueObjectGetString(*props, "qom-type");
const char *id = virJSONValueObjectGetString(*props, "id");
char *tmp = NULL;
int ret = -1;
VIR_DEBUG("type=%s id=%s", NULLSTR(type), NULLSTR(id));
QEMU_CHECK_MONITOR_GOTO(mon, cleanup);
if (!id) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("missing alias for qemu object '%s'"), NULLSTR(type));
goto cleanup;
}
if (alias && VIR_STRDUP(tmp, id) < 0)
goto cleanup;
ret = qemuMonitorJSONAddObject(mon, *props);
*props = NULL;
if (alias)
VIR_STEAL_PTR(*alias, tmp);
cleanup:
VIR_FREE(tmp);
virJSONValueFree(*props);
*props = NULL;
return ret;
}
/** /**
* qemuMonitorAddObjectType: * qemuMonitorAddObjectType:
* @mon: Pointer to monitor object * @mon: Pointer to monitor object
...@@ -3007,15 +3110,20 @@ qemuMonitorAddObjectType(qemuMonitorPtr mon, ...@@ -3007,15 +3110,20 @@ qemuMonitorAddObjectType(qemuMonitorPtr mon,
const char *objalias, const char *objalias,
virJSONValuePtr props) virJSONValuePtr props)
{ {
virJSONValuePtr tmpprops = NULL;
int ret = -1;
VIR_DEBUG("type=%s objalias=%s props=%p", type, objalias, props); VIR_DEBUG("type=%s objalias=%s props=%p", type, objalias, props);
QEMU_CHECK_MONITOR_GOTO(mon, error); if (!(tmpprops = qemuMonitorCreateObjectPropsWrap(type, objalias, &props)))
goto cleanup;
return qemuMonitorJSONAddObject(mon, type, objalias, props); ret = qemuMonitorAddObject(mon, &tmpprops, NULL);
error: cleanup:
virJSONValueFree(props); virJSONValueFree(props);
return -1; virJSONValueFree(tmpprops);
return ret;
} }
......
...@@ -797,6 +797,19 @@ int qemuMonitorAddDeviceWithFd(qemuMonitorPtr mon, ...@@ -797,6 +797,19 @@ int qemuMonitorAddDeviceWithFd(qemuMonitorPtr mon,
int qemuMonitorDelDevice(qemuMonitorPtr mon, int qemuMonitorDelDevice(qemuMonitorPtr mon,
const char *devalias); const char *devalias);
virJSONValuePtr qemuMonitorCreateObjectPropsWrap(const char *type,
const char *alias,
virJSONValuePtr *props);
int qemuMonitorCreateObjectProps(virJSONValuePtr *propsret,
const char *type,
const char *alias,
...);
int qemuMonitorAddObject(qemuMonitorPtr mon,
virJSONValuePtr *props,
char **alias);
int qemuMonitorAddObjectType(qemuMonitorPtr mon, int qemuMonitorAddObjectType(qemuMonitorPtr mon,
const char *type, const char *type,
const char *objalias, const char *objalias,
......
...@@ -4003,21 +4003,15 @@ qemuMonitorJSONAddDevice(qemuMonitorPtr mon, ...@@ -4003,21 +4003,15 @@ qemuMonitorJSONAddDevice(qemuMonitorPtr mon,
} }
int qemuMonitorJSONAddObject(qemuMonitorPtr mon, int
const char *type, qemuMonitorJSONAddObject(qemuMonitorPtr mon,
const char *objalias, virJSONValuePtr props)
virJSONValuePtr props)
{ {
int ret = -1; int ret = -1;
virJSONValuePtr cmd; virJSONValuePtr cmd;
virJSONValuePtr reply = NULL; virJSONValuePtr reply = NULL;
cmd = qemuMonitorJSONMakeCommand("object-add", if (!(cmd = qemuMonitorJSONMakeCommandInternal("object-add", props, false)))
"s:qom-type", type,
"s:id", objalias,
"A:props", &props,
NULL);
if (!cmd)
goto cleanup; goto cleanup;
if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0)
...@@ -4030,7 +4024,6 @@ int qemuMonitorJSONAddObject(qemuMonitorPtr mon, ...@@ -4030,7 +4024,6 @@ int qemuMonitorJSONAddObject(qemuMonitorPtr mon,
cleanup: cleanup:
virJSONValueFree(cmd); virJSONValueFree(cmd);
virJSONValueFree(reply); virJSONValueFree(reply);
virJSONValueFree(props);
return ret; return ret;
} }
......
...@@ -230,8 +230,6 @@ int qemuMonitorJSONDelDevice(qemuMonitorPtr mon, ...@@ -230,8 +230,6 @@ int qemuMonitorJSONDelDevice(qemuMonitorPtr mon,
const char *devalias); const char *devalias);
int qemuMonitorJSONAddObject(qemuMonitorPtr mon, int qemuMonitorJSONAddObject(qemuMonitorPtr mon,
const char *type,
const char *objalias,
virJSONValuePtr props); virJSONValuePtr props);
int qemuMonitorJSONDelObject(qemuMonitorPtr mon, int qemuMonitorJSONDelObject(qemuMonitorPtr mon,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册