From nobody Thu May 15 07:16:08 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1527700046512384.60603605601455; Wed, 30 May 2018 10:07:26 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0028230B9BDE; Wed, 30 May 2018 17:07:25 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C4AEA17011; Wed, 30 May 2018 17:07:24 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 56E3A180BA82; Wed, 30 May 2018 17:07:24 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4UH6qSF007297 for ; Wed, 30 May 2018 13:06:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id D86152026988; Wed, 30 May 2018 17:06:52 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.136]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7CF5A2026DFD for ; Wed, 30 May 2018 17:06:52 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 30 May 2018 19:06:28 +0200 Message-Id: In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 04/13] qemu: monitor: Add better APIs for adding of objects to qemu X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Wed, 30 May 2018 17:07:25 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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: Peter Krempa Reviewed-by: J=EF=BF=BDn Tomko --- src/qemu/qemu_monitor.c | 116 +++++++++++++++++++++++++++++++++++++++= ++-- src/qemu/qemu_monitor.h | 13 +++++ src/qemu/qemu_monitor_json.c | 15 ++---- src/qemu/qemu_monitor_json.h | 2 - 4 files changed, 129 insertions(+), 17 deletions(-) diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c index 77ad47cb74..fe8fdfaf9a 100644 --- a/src/qemu/qemu_monitor.c +++ b/src/qemu/qemu_monitor.c @@ -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 virJSONValueObjectAd= dVArgs. + * + * Returns a JSONValue containing everything on success and NULL on error. + */ +int +qemuMonitorCreateObjectProps(virJSONValuePtr *propsret, + const char *type, + const char *alias, + ...) +{ + virJSONValuePtr props =3D NULL; + int ret =3D -1; + va_list args; + + *propsret =3D NULL; + + va_start(args, alias); + + if (!(virJSONValueObjectCreateVArgs(&props, args))) + goto cleanup; + + if (!(*propsret =3D qemuMonitorCreateObjectPropsWrap(type, alias, &pro= ps))) + goto cleanup; + + ret =3D 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 a= nd + * the pointer is cleared. + * @alias: If not NULL, returns the alias of the added object if it was ad= ded + * 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 =3D virJSONValueObjectGetString(*props, "qom-type"); + const char *id =3D virJSONValueObjectGetString(*props, "id"); + char *tmp =3D NULL; + int ret =3D -1; + + VIR_DEBUG("type=3D%s id=3D%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(ty= pe)); + goto cleanup; + } + + if (alias && VIR_STRDUP(tmp, id) < 0) + goto cleanup; + + ret =3D qemuMonitorJSONAddObject(mon, *props); + *props =3D NULL; + + if (alias) + VIR_STEAL_PTR(*alias, tmp); + + cleanup: + VIR_FREE(tmp); + virJSONValueFree(*props); + *props =3D NULL; + return ret; +} + + + /** * qemuMonitorAddObjectType: * @mon: Pointer to monitor object @@ -3007,15 +3110,20 @@ qemuMonitorAddObjectType(qemuMonitorPtr mon, const char *objalias, virJSONValuePtr props) { + virJSONValuePtr tmpprops =3D NULL; + int ret =3D -1; + VIR_DEBUG("type=3D%s objalias=3D%s props=3D%p", type, objalias, props); - QEMU_CHECK_MONITOR_GOTO(mon, error); + if (!(tmpprops =3D qemuMonitorCreateObjectPropsWrap(type, objalias, &p= rops))) + goto cleanup; - return qemuMonitorJSONAddObject(mon, type, objalias, props); + ret =3D qemuMonitorAddObject(mon, &tmpprops, NULL); - error: + cleanup: virJSONValueFree(props); - return -1; + virJSONValueFree(tmpprops); + return ret; } diff --git a/src/qemu/qemu_monitor.h b/src/qemu/qemu_monitor.h index 77a26d4a8a..0c13391520 100644 --- a/src/qemu/qemu_monitor.h +++ b/src/qemu/qemu_monitor.h @@ -797,6 +797,19 @@ int qemuMonitorAddDeviceWithFd(qemuMonitorPtr mon, int qemuMonitorDelDevice(qemuMonitorPtr mon, 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, const char *type, const char *objalias, diff --git a/src/qemu/qemu_monitor_json.c b/src/qemu/qemu_monitor_json.c index 9f5c358795..7522eaeef0 100644 --- a/src/qemu/qemu_monitor_json.c +++ b/src/qemu/qemu_monitor_json.c @@ -4003,21 +4003,15 @@ qemuMonitorJSONAddDevice(qemuMonitorPtr mon, } -int qemuMonitorJSONAddObject(qemuMonitorPtr mon, - const char *type, - const char *objalias, - virJSONValuePtr props) +int +qemuMonitorJSONAddObject(qemuMonitorPtr mon, + virJSONValuePtr props) { int ret =3D -1; virJSONValuePtr cmd; virJSONValuePtr reply =3D NULL; - cmd =3D qemuMonitorJSONMakeCommand("object-add", - "s:qom-type", type, - "s:id", objalias, - "A:props", &props, - NULL); - if (!cmd) + if (!(cmd =3D qemuMonitorJSONMakeCommandInternal("object-add", props, = false))) goto cleanup; if (qemuMonitorJSONCommand(mon, cmd, &reply) < 0) @@ -4030,7 +4024,6 @@ int qemuMonitorJSONAddObject(qemuMonitorPtr mon, cleanup: virJSONValueFree(cmd); virJSONValueFree(reply); - virJSONValueFree(props); return ret; } diff --git a/src/qemu/qemu_monitor_json.h b/src/qemu/qemu_monitor_json.h index f4ac8319ac..5fc51b1d6b 100644 --- a/src/qemu/qemu_monitor_json.h +++ b/src/qemu/qemu_monitor_json.h @@ -230,8 +230,6 @@ int qemuMonitorJSONDelDevice(qemuMonitorPtr mon, const char *devalias); int qemuMonitorJSONAddObject(qemuMonitorPtr mon, - const char *type, - const char *objalias, virJSONValuePtr props); int qemuMonitorJSONDelObject(qemuMonitorPtr mon, --=20 2.16.2 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list