From nobody Wed May 14 07:03:50 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 1526459995422344.96718727844484; Wed, 16 May 2018 01:39:55 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B5C4D3115852; Wed, 16 May 2018 08:39:53 +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 6BF95600C9; Wed, 16 May 2018 08:39:53 +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 ED5F9180B536; Wed, 16 May 2018 08:39:52 +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 w4G8diFV018328 for ; Wed, 16 May 2018 04:39:44 -0400 Received: by smtp.corp.redhat.com (Postfix) id E2183200BC1E; Wed, 16 May 2018 08:39:43 +0000 (UTC) Received: from virval.usersys.redhat.com (unknown [10.43.2.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A46142026E0E for ; Wed, 16 May 2018 08:39:43 +0000 (UTC) Received: by virval.usersys.redhat.com (Postfix, from userid 500) id A43F3102EDB; Wed, 16 May 2018 10:39:42 +0200 (CEST) From: Jiri Denemark To: libvir-list@redhat.com Date: Wed, 16 May 2018 10:39:21 +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 02/22] virsh: Extract common code from cmdCPU{Compare, Baseline} 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.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 16 May 2018 08:39:54 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Both cpu-compare and cpu-baseline commands accept more that just CPU definition XML(s). For users' convenience they are able to extract the CPU definition(s) even from domain XML or capabilities XML. The main differences between the two commands is in the number of CPU definitions they expect: cpu-compare wants only one CPU definition while cpu-baseline expects one or more CPUs. The extracted code forms a new vshExtractCPUDefXML function. Signed-off-by: Jiri Denemark Reviewed-by: J=EF=BF=BDn Tomko --- tools/virsh-host.c | 160 +++++++++++++++++++++------------------------ 1 file changed, 75 insertions(+), 85 deletions(-) diff --git a/tools/virsh-host.c b/tools/virsh-host.c index 6d6e3cfc85..51497db385 100644 --- a/tools/virsh-host.c +++ b/tools/virsh-host.c @@ -1106,6 +1106,72 @@ cmdURI(vshControl *ctl, const vshCmd *cmd ATTRIBUTE_= UNUSED) return true; } =20 + +/* Extracts the CPU definition XML strings from a file which may contain e= ither + * - just the CPU definitions, + * - domain XMLs, or + * - capabilities XMLs. + * + * Returns NULL terminated string list. + */ +static char ** +vshExtractCPUDefXMLs(vshControl *ctl, + const char *xmlFile) +{ + char **cpus =3D NULL; + char *buffer =3D NULL; + char *xmlStr =3D NULL; + xmlDocPtr xml =3D NULL; + xmlXPathContextPtr ctxt =3D NULL; + xmlNodePtr *nodes =3D NULL; + size_t i; + int n; + + if (virFileReadAll(xmlFile, VSH_MAX_XML_FILE, &buffer) < 0) + goto error; + + if (virAsprintf(&xmlStr, "%s", buffer) < 0) + goto error; + + if (!(xml =3D virXMLParseStringCtxt(xmlStr, xmlFile, &ctxt))) + goto error; + + n =3D virXPathNodeSet("/container/cpu|" + "/container/domain/cpu|" + "/container/capabilities/host/cpu", + ctxt, &nodes); + if (n < 0) + goto error; + + if (n =3D=3D 0) { + vshError(ctl, _("File '%s' does not contain any element or " + "valid domain or capabilities XML"), xmlFile); + goto error; + } + + cpus =3D vshCalloc(ctl, n + 1, sizeof(const char *)); + + for (i =3D 0; i < n; i++) { + if (!(cpus[i] =3D virXMLNodeToString(xml, nodes[i]))) { + vshSaveLibvirtError(); + goto error; + } + } + + cleanup: + VIR_FREE(buffer); + VIR_FREE(xmlStr); + xmlFreeDoc(xml); + xmlXPathFreeContext(ctxt); + VIR_FREE(nodes); + return cpus; + + error: + virStringListFree(cpus); + goto cleanup; +} + + /* * "cpu-compare" command */ @@ -1133,13 +1199,9 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) { const char *from =3D NULL; bool ret =3D false; - char *buffer; int result; - char *snippet =3D NULL; + char **cpus =3D NULL; unsigned int flags =3D 0; - xmlDocPtr xml =3D NULL; - xmlXPathContextPtr ctxt =3D NULL; - xmlNodePtr node; virshControlPtr priv =3D ctl->privData; =20 if (vshCommandOptBool(cmd, "error")) @@ -1148,27 +1210,10 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; =20 - if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) + if (!(cpus =3D vshExtractCPUDefXMLs(ctl, from))) return false; =20 - /* try to extract the CPU element from as it would appear in a domain = XML*/ - if (!(xml =3D virXMLParseStringCtxt(buffer, from, &ctxt))) - goto cleanup; - - if ((node =3D virXPathNode("/cpu|" - "/domain/cpu|" - "/capabilities/host/cpu", ctxt))) { - if (!(snippet =3D virXMLNodeToString(xml, node))) { - vshSaveLibvirtError(); - goto cleanup; - } - } else { - vshError(ctl, _("File '%s' does not contain a element or is = not " - "a valid domain or capabilities XML"), from); - goto cleanup; - } - - result =3D virConnectCompareCPU(priv->conn, snippet, flags); + result =3D virConnectCompareCPU(priv->conn, cpus[0], flags); =20 switch (result) { case VIR_CPU_COMPARE_INCOMPATIBLE: @@ -1196,10 +1241,7 @@ cmdCPUCompare(vshControl *ctl, const vshCmd *cmd) ret =3D true; =20 cleanup: - VIR_FREE(buffer); - VIR_FREE(snippet); - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); + virStringListFree(cpus); =20 return ret; } @@ -1235,17 +1277,9 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) { const char *from =3D NULL; bool ret =3D false; - char *buffer; char *result =3D NULL; char **list =3D NULL; unsigned int flags =3D 0; - int count =3D 0; - - xmlDocPtr xml =3D NULL; - xmlNodePtr *node_list =3D NULL; - xmlXPathContextPtr ctxt =3D NULL; - virBuffer buf =3D VIR_BUFFER_INITIALIZER; - size_t i; virshControlPtr priv =3D ctl->privData; =20 if (vshCommandOptBool(cmd, "features")) @@ -1256,65 +1290,21 @@ cmdCPUBaseline(vshControl *ctl, const vshCmd *cmd) if (vshCommandOptStringReq(ctl, cmd, "file", &from) < 0) return false; =20 - if (virFileReadAll(from, VSH_MAX_XML_FILE, &buffer) < 0) + if (!(list =3D vshExtractCPUDefXMLs(ctl, from))) return false; =20 - /* add a separate container around the xml */ - virBufferStrcat(&buf, "", buffer, "", NULL); - if (virBufferError(&buf)) - goto no_memory; - - VIR_FREE(buffer); - buffer =3D virBufferContentAndReset(&buf); - - - if (!(xml =3D virXMLParseStringCtxt(buffer, from, &ctxt))) - goto cleanup; - - if ((count =3D virXPathNodeSet("//cpu[not(ancestor::cpus)]", - ctxt, &node_list)) =3D=3D -1) - goto cleanup; - - if (count =3D=3D 0) { - vshError(ctl, _("No host CPU specified in '%s'"), from); - goto cleanup; - } - - list =3D vshCalloc(ctl, count, sizeof(const char *)); - - for (i =3D 0; i < count; i++) { - if (!(list[i] =3D virXMLNodeToString(xml, node_list[i]))) { - vshSaveLibvirtError(); - goto cleanup; - } - } - - result =3D virConnectBaselineCPU(priv->conn, - (const char **)list, count, flags); + result =3D virConnectBaselineCPU(priv->conn, (const char **)list, + virStringListLength((const char **)list= ), + flags); =20 if (result) { vshPrint(ctl, "%s", result); ret =3D true; } =20 - cleanup: - xmlXPathFreeContext(ctxt); - xmlFreeDoc(xml); VIR_FREE(result); - if (list !=3D NULL && count > 0) { - for (i =3D 0; i < count; i++) - VIR_FREE(list[i]); - } - VIR_FREE(list); - VIR_FREE(buffer); - VIR_FREE(node_list); - + virStringListFree(list); return ret; - - no_memory: - vshError(ctl, "%s", _("Out of memory")); - ret =3D false; - goto cleanup; } =20 /* --=20 2.17.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list