From nobody Sat Apr 27 13:16:47 2024 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 152725485082179.56315293587215; Fri, 25 May 2018 06:27:30 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C5F72C2360C3; Fri, 25 May 2018 13:27:28 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 28C779D7C1; Fri, 25 May 2018 13:27:28 +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 86FFA4CA84; Fri, 25 May 2018 13:27:27 +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 w4PDRPWQ023577 for ; Fri, 25 May 2018 09:27:25 -0400 Received: by smtp.corp.redhat.com (Postfix) id 829B62023488; Fri, 25 May 2018 13:27:25 +0000 (UTC) Received: from pessoa.redhat.com (unknown [10.40.205.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 381522023487; Fri, 25 May 2018 13:27:24 +0000 (UTC) From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= To: libvir-list@redhat.com Date: Fri, 25 May 2018 15:27:15 +0200 Message-Id: <20180525132717.17628-2-fidencio@redhat.com> In-Reply-To: <20180525132717.17628-1-fidencio@redhat.com> References: <20180525132717.17628-1-fidencio@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= , =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Subject: [libvirt] [PATCH 1/3] xen_vm: convert to typesafe virConf accessors 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: , Content-Type: text/plain; charset="utf-8" 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.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.32]); Fri, 25 May 2018 13:27:29 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Signed-off-by: Fabiano Fid=C3=AAncio --- src/xenconfig/xen_xm.c | 268 ++++++++++++++++++++--------------------- 1 file changed, 132 insertions(+), 136 deletions(-) diff --git a/src/xenconfig/xen_xm.c b/src/xenconfig/xen_xm.c index 4becb40b4..fc88ac823 100644 --- a/src/xenconfig/xen_xm.c +++ b/src/xenconfig/xen_xm.c @@ -112,172 +112,168 @@ xenParseXMDisk(virConfPtr conf, virDomainDefPtr def) { virDomainDiskDefPtr disk =3D NULL; int hvm =3D def->os.type =3D=3D VIR_DOMAIN_OSTYPE_HVM; - virConfValuePtr list =3D virConfGetValue(conf, "disk"); + char **disks =3D NULL, **entries; =20 - if (list && list->type =3D=3D VIR_CONF_LIST) { - list =3D list->list; - while (list) { - char *head; - char *offset; - char *tmp; - const char *src; + if (virConfGetValueStringList(conf, "disk", false, &disks) < 0) + goto cleanup; =20 - if ((list->type !=3D VIR_CONF_STRING) || (list->str =3D=3D NUL= L)) - goto skipdisk; + for (entries =3D disks; *entries; entries++) { + char *head =3D *entries; + char *offset; + char *tmp; + const char *src; =20 - head =3D list->str; - if (!(disk =3D virDomainDiskDefNew(NULL))) - return -1; + if (!(disk =3D virDomainDiskDefNew(NULL))) + return -1; + + /* + * Disks have 3 components, SOURCE,DEST-DEVICE,MODE + * eg, phy:/dev/HostVG/XenGuest1,xvda,w + * The SOURCE is usually prefixed with a driver type, + * and optionally driver sub-type + * The DEST-DEVICE is optionally post-fixed with disk type + */ + + /* Extract the source file path*/ + if (!(offset =3D strchr(head, ','))) + goto skipdisk; + + if (offset =3D=3D head) { + /* No source file given, eg CDROM with no media */ + ignore_value(virDomainDiskSetSource(disk, NULL)); + } else { + if (VIR_STRNDUP(tmp, head, offset - head) < 0) + goto cleanup; + + if (virDomainDiskSetSource(disk, tmp) < 0) { + VIR_FREE(tmp); + goto cleanup; + } + VIR_FREE(tmp); + } =20 - /* - * Disks have 3 components, SOURCE,DEST-DEVICE,MODE - * eg, phy:/dev/HostVG/XenGuest1,xvda,w - * The SOURCE is usually prefixed with a driver type, - * and optionally driver sub-type - * The DEST-DEVICE is optionally post-fixed with disk type - */ - - /* Extract the source file path*/ - if (!(offset =3D strchr(head, ','))) - goto skipdisk; - - if (offset =3D=3D head) { - /* No source file given, eg CDROM with no media */ - ignore_value(virDomainDiskSetSource(disk, NULL)); - } else { - if (VIR_STRNDUP(tmp, head, offset - head) < 0) + head =3D offset + 1; + /* Remove legacy ioemu: junk */ + if (STRPREFIX(head, "ioemu:")) + head =3D head + 6; + + /* Extract the dest device name */ + if (!(offset =3D strchr(head, ','))) + goto skipdisk; + + if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0) + goto cleanup; + + if (virStrncpy(disk->dst, head, offset - head, + (offset - head) + 1) =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Dest file %s too big for destination"), head= ); + goto cleanup; + } + + head =3D offset + 1; + /* Extract source driver type */ + src =3D virDomainDiskGetSource(disk); + if (src) { + size_t len; + /* The main type phy:, file:, tap: ... */ + if ((tmp =3D strchr(src, ':')) !=3D NULL) { + len =3D tmp - src; + if (VIR_STRNDUP(tmp, src, len) < 0) goto cleanup; =20 - if (virDomainDiskSetSource(disk, tmp) < 0) { + if (virDomainDiskSetDriver(disk, tmp) < 0) { VIR_FREE(tmp); goto cleanup; } VIR_FREE(tmp); - } - - head =3D offset + 1; - /* Remove legacy ioemu: junk */ - if (STRPREFIX(head, "ioemu:")) - head =3D head + 6; - - /* Extract the dest device name */ - if (!(offset =3D strchr(head, ','))) - goto skipdisk; =20 - if (VIR_ALLOC_N(disk->dst, (offset - head) + 1) < 0) - goto cleanup; + /* Strip the prefix we found off the source file name */ + if (virDomainDiskSetSource(disk, src + len + 1) < 0) + goto cleanup; =20 - if (virStrncpy(disk->dst, head, offset - head, - (offset - head) + 1) =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Dest file %s too big for destination"), = head); - goto cleanup; + src =3D virDomainDiskGetSource(disk); } =20 - head =3D offset + 1; - /* Extract source driver type */ - src =3D virDomainDiskGetSource(disk); - if (src) { - size_t len; - /* The main type phy:, file:, tap: ... */ - if ((tmp =3D strchr(src, ':')) !=3D NULL) { - len =3D tmp - src; - if (VIR_STRNDUP(tmp, src, len) < 0) - goto cleanup; - - if (virDomainDiskSetDriver(disk, tmp) < 0) { - VIR_FREE(tmp); - goto cleanup; - } - VIR_FREE(tmp); + /* And the sub-type for tap:XXX: type */ + if (STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap") || + STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap2")) { + char *driverType; =20 - /* Strip the prefix we found off the source file name = */ - if (virDomainDiskSetSource(disk, src + len + 1) < 0) - goto cleanup; + if (!(tmp =3D strchr(src, ':'))) + goto skipdisk; + len =3D tmp - src; =20 - src =3D virDomainDiskGetSource(disk); - } + if (VIR_STRNDUP(driverType, src, len) < 0) + goto cleanup; =20 - /* And the sub-type for tap:XXX: type */ - if (STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap") || - STREQ_NULLABLE(virDomainDiskGetDriver(disk), "tap2")) { - char *driverType; - - if (!(tmp =3D strchr(src, ':'))) - goto skipdisk; - len =3D tmp - src; - - if (VIR_STRNDUP(driverType, src, len) < 0) - goto cleanup; - - if (STREQ(driverType, "aio")) - virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW); - else - virDomainDiskSetFormat(disk, - virStorageFileFormatTypeFro= mString(driverType)); - VIR_FREE(driverType); - if (virDomainDiskGetFormat(disk) <=3D 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unknown driver type %s"), - src); - goto cleanup; - } - - /* Strip the prefix we found off the source file name = */ - if (virDomainDiskSetSource(disk, src + len + 1) < 0) - goto cleanup; - src =3D virDomainDiskGetSource(disk); + if (STREQ(driverType, "aio")) + virDomainDiskSetFormat(disk, VIR_STORAGE_FILE_RAW); + else + virDomainDiskSetFormat(disk, + virStorageFileFormatTypeFromStr= ing(driverType)); + VIR_FREE(driverType); + if (virDomainDiskGetFormat(disk) <=3D 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Unknown driver type %s"), + src); + goto cleanup; } + + /* Strip the prefix we found off the source file name */ + if (virDomainDiskSetSource(disk, src + len + 1) < 0) + goto cleanup; + src =3D virDomainDiskGetSource(disk); } + } =20 - /* No source, or driver name, so fix to phy: */ - if (!virDomainDiskGetDriver(disk) && - virDomainDiskSetDriver(disk, "phy") < 0) - goto cleanup; + /* No source, or driver name, so fix to phy: */ + if (!virDomainDiskGetDriver(disk) && + virDomainDiskSetDriver(disk, "phy") < 0) + goto cleanup; =20 - /* phy: type indicates a block device */ - virDomainDiskSetType(disk, - STREQ(virDomainDiskGetDriver(disk), "phy"= ) ? - VIR_STORAGE_TYPE_BLOCK : - VIR_STORAGE_TYPE_FILE); - - /* Check for a :cdrom/:disk postfix */ - disk->device =3D VIR_DOMAIN_DISK_DEVICE_DISK; - if ((tmp =3D strchr(disk->dst, ':')) !=3D NULL) { - if (STREQ(tmp, ":cdrom")) - disk->device =3D VIR_DOMAIN_DISK_DEVICE_CDROM; - tmp[0] =3D '\0'; - } + /* phy: type indicates a block device */ + virDomainDiskSetType(disk, + STREQ(virDomainDiskGetDriver(disk), "phy") ? + VIR_STORAGE_TYPE_BLOCK : + VIR_STORAGE_TYPE_FILE); + + /* Check for a :cdrom/:disk postfix */ + disk->device =3D VIR_DOMAIN_DISK_DEVICE_DISK; + if ((tmp =3D strchr(disk->dst, ':')) !=3D NULL) { + if (STREQ(tmp, ":cdrom")) + disk->device =3D VIR_DOMAIN_DISK_DEVICE_CDROM; + tmp[0] =3D '\0'; + } =20 - if (STRPREFIX(disk->dst, "xvd") || !hvm) { - disk->bus =3D VIR_DOMAIN_DISK_BUS_XEN; - } else if (STRPREFIX(disk->dst, "sd")) { - disk->bus =3D VIR_DOMAIN_DISK_BUS_SCSI; - } else { - disk->bus =3D VIR_DOMAIN_DISK_BUS_IDE; - } + if (STRPREFIX(disk->dst, "xvd") || !hvm) { + disk->bus =3D VIR_DOMAIN_DISK_BUS_XEN; + } else if (STRPREFIX(disk->dst, "sd")) { + disk->bus =3D VIR_DOMAIN_DISK_BUS_SCSI; + } else { + disk->bus =3D VIR_DOMAIN_DISK_BUS_IDE; + } =20 - if (STREQ(head, "r") || - STREQ(head, "ro")) - disk->src->readonly =3D true; - else if ((STREQ(head, "w!")) || - (STREQ(head, "!"))) - disk->src->shared =3D true; + if (STREQ(head, "r") || + STREQ(head, "ro")) + disk->src->readonly =3D true; + else if ((STREQ(head, "w!")) || + (STREQ(head, "!"))) + disk->src->shared =3D true; =20 - /* Maintain list in sorted order according to target device na= me */ - if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) - goto cleanup; + /* Maintain list in sorted order according to target device name */ + if (VIR_APPEND_ELEMENT(def->disks, def->ndisks, disk) < 0) + goto cleanup; =20 - skipdisk: - list =3D list->next; - virDomainDiskDefFree(disk); - disk =3D NULL; - } + skipdisk: + virDomainDiskDefFree(disk); + disk =3D NULL; } =20 return 0; =20 cleanup: + virStringListFree(disks); virDomainDiskDefFree(disk); return -1; } --=20 2.17.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sat Apr 27 13:16:47 2024 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 1527254855762804.1319958361698; Fri, 25 May 2018 06:27:35 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx12.intmail.prod.int.phx2.redhat.com [10.5.11.27]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5206C329DB1; Fri, 25 May 2018 13:27:33 +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 B4C1591B8C; Fri, 25 May 2018 13:27:32 +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 F253D180BA82; Fri, 25 May 2018 13:27:31 +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 w4PDRQ2M023584 for ; Fri, 25 May 2018 09:27:27 -0400 Received: by smtp.corp.redhat.com (Postfix) id D33AB2023488; Fri, 25 May 2018 13:27:26 +0000 (UTC) Received: from pessoa.redhat.com (unknown [10.40.205.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id D33B62023487; Fri, 25 May 2018 13:27:25 +0000 (UTC) From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= To: libvir-list@redhat.com Date: Fri, 25 May 2018 15:27:16 +0200 Message-Id: <20180525132717.17628-3-fidencio@redhat.com> In-Reply-To: <20180525132717.17628-1-fidencio@redhat.com> References: <20180525132717.17628-1-fidencio@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= , =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Subject: [libvirt] [PATCH 2/3] vmx: convert to typesafe virConf accessors 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: , Content-Type: text/plain; charset="utf-8" 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.27 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 25 May 2018 13:27:34 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Signed-off-by: Fabiano Fid=C3=AAncio --- src/vmx/vmx.c | 197 +++++++++++++++++++------------------------------- 1 file changed, 73 insertions(+), 124 deletions(-) diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c index df6a58a47..6247fa938 100644 --- a/src/vmx/vmx.c +++ b/src/vmx/vmx.c @@ -719,42 +719,36 @@ virVMXConvertToUTF8(const char *encoding, const char = *string) return result; } =20 - - static int -virVMXGetConfigString(virConfPtr conf, const char *name, char **string, - bool optional) +virVMXGetConfigStringHelper(virConfPtr conf, const char *name, char **stri= ng, + bool optional) { - virConfValuePtr value; - + int result; *string =3D NULL; - value =3D virConfGetValue(conf, name); =20 - if (value =3D=3D NULL) { - if (optional) - return 0; + result =3D virConfGetValueString(conf, name, string); + if (result =3D=3D 1 && *string !=3D NULL) + return 1; =20 - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); - return -1; - } + if (optional) + return 0; =20 - if (value->type !=3D VIR_CONF_STRING) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must be a string"), name); - return -1; - } + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Missing essential config entry '%s'"), name); + return -1; +} =20 - if (value->str =3D=3D NULL) { - if (optional) - return 0; =20 - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); +static int +virVMXGetConfigString(virConfPtr conf, const char *name, char **string, + bool optional) +{ + *string =3D NULL; + + if (virVMXGetConfigStringHelper(conf, name, string, optional) < 0) return -1; - } =20 - return VIR_STRDUP(*string, value->str); + return 0; } =20 =20 @@ -763,43 +757,26 @@ static int virVMXGetConfigUUID(virConfPtr conf, const char *name, unsigned char *uuid, bool optional) { - virConfValuePtr value; + char *string =3D NULL; + int result; =20 - value =3D virConfGetValue(conf, name); + result =3D virVMXGetConfigStringHelper(conf, name, &string, optional); + if (result <=3D 0) + return result; =20 - if (value =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); - return -1; - } - } - - if (value->type !=3D VIR_CONF_STRING) { + if (virUUIDParse(string, uuid) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must be a string"), name); - return -1; + _("Could not parse UUID from string '%s'"), string); + result =3D -1; + goto cleanup; } =20 - if (value->str =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); - return -1; - } - } + result =3D 0; =20 - if (virUUIDParse(value->str, uuid) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Could not parse UUID from string '%s'"), value->= str); - return -1; - } + cleanup: + VIR_FREE(string); =20 - return 0; + return result; } =20 =20 @@ -808,47 +785,35 @@ static int virVMXGetConfigLong(virConfPtr conf, const char *name, long long *number, long long default_, bool optional) { - virConfValuePtr value; + char *string =3D NULL; + int result; =20 *number =3D default_; - value =3D virConfGetValue(conf, name); =20 - if (value =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); - return -1; - } - } + result =3D virVMXGetConfigStringHelper(conf, name, &string, optional); + if (result <=3D 0) + return result; =20 - if (value->type =3D=3D VIR_CONF_STRING) { - if (value->str =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), n= ame); - return -1; - } - } + if (STRCASEEQ(string, "unlimited")) { + *number =3D -1; + result =3D 0; + goto cleanup; + } =20 - if (STRCASEEQ(value->str, "unlimited")) { - *number =3D -1; - } else if (virStrToLong_ll(value->str, NULL, 10, number) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must represent an integer = value"), - name); - return -1; - } - } else { + if (virStrToLong_ll(string, NULL, 10, number) < 0) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must be a string"), name); - return -1; + _("Config entry '%s' must represent an integer value"), + name); + result =3D -1; + goto cleanup; } =20 - return 0; + result =3D 0; + + cleanup: + VIR_FREE(string); + + return result; } =20 =20 @@ -857,49 +822,33 @@ static int virVMXGetConfigBoolean(virConfPtr conf, const char *name, bool *boolean_, bool default_, bool optional) { - virConfValuePtr value; + char *string =3D NULL; + int result; =20 *boolean_ =3D default_; - value =3D virConfGetValue(conf, name); =20 - if (value =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), name); - return -1; - } - } + result =3D virVMXGetConfigStringHelper(conf, name, &string, optional); + if (result <=3D 0) + return result; =20 - if (value->type =3D=3D VIR_CONF_STRING) { - if (value->str =3D=3D NULL) { - if (optional) { - return 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing essential config entry '%s'"), n= ame); - return -1; - } - } - - if (STRCASEEQ(value->str, "true")) { - *boolean_ =3D 1; - } else if (STRCASEEQ(value->str, "false")) { - *boolean_ =3D 0; - } else { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must represent a boolean v= alue " - "(true|false)"), name); - return -1; - } + if (STRCASEEQ(string, "true")) { + *boolean_ =3D 1; + } else if (STRCASEEQ(string, "false")) { + *boolean_ =3D 0; } else { virReportError(VIR_ERR_INTERNAL_ERROR, - _("Config entry '%s' must be a string"), name); - return -1; + _("Config entry '%s' must represent a boolean value= " + "(true|false)"), name); + result =3D -1; + goto cleanup; } =20 - return 0; + result =3D 0; + + cleanup: + VIR_FREE(string); + + return result; } =20 =20 --=20 2.17.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sat Apr 27 13:16:47 2024 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 1527254865446351.55647535634694; Fri, 25 May 2018 06:27:45 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 78B103157103; Fri, 25 May 2018 13:27:43 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0325B2009D16; Fri, 25 May 2018 13:27:43 +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 3E6434CA80; Fri, 25 May 2018 13:27:42 +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 w4PDRTZ9023595 for ; Fri, 25 May 2018 09:27:29 -0400 Received: by smtp.corp.redhat.com (Postfix) id 80A74210C6C1; Fri, 25 May 2018 13:27:29 +0000 (UTC) Received: from pessoa.redhat.com (unknown [10.40.205.24]) by smtp.corp.redhat.com (Postfix) with ESMTP id 37575200BCBB; Fri, 25 May 2018 13:27:27 +0000 (UTC) From: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= To: libvir-list@redhat.com Date: Fri, 25 May 2018 15:27:17 +0200 Message-Id: <20180525132717.17628-4-fidencio@redhat.com> In-Reply-To: <20180525132717.17628-1-fidencio@redhat.com> References: <20180525132717.17628-1-fidencio@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= , =?UTF-8?q?Fabiano=20Fid=C3=AAncio?= Subject: [libvirt] [PATCH 3/3] xen_common: convert to typesafe virConf accessors 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: , Content-Type: text/plain; charset="utf-8" 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.25 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Fri, 25 May 2018 13:27:44 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 There are still some places using virConfGetValue() and then checking the specific type of the pointers and so on. Those place are not going to be changed as: - Directly using virConfGetValue*() would trigger virReportError() on their current code - Expanding virConfGetValue*() to support strings as other types (as boolean or long) doesn't seem to be the safest path to take. Signed-off-by: Fabiano Fid=C3=AAncio --- src/xenconfig/xen_common.c | 631 ++++++++++++++++++------------------- 1 file changed, 299 insertions(+), 332 deletions(-) diff --git a/src/xenconfig/xen_common.c b/src/xenconfig/xen_common.c index a2b0708ee..3b3fe5f77 100644 --- a/src/xenconfig/xen_common.c +++ b/src/xenconfig/xen_common.c @@ -145,31 +145,18 @@ xenConfigCopyStringInternal(virConfPtr conf, char **value, int allowMissing) { - virConfValuePtr val; + int result; =20 *value =3D NULL; - if (!(val =3D virConfGetValue(conf, name))) { - if (allowMissing) - return 0; - virReportError(VIR_ERR_INTERNAL_ERROR, - _("config value %s was missing"), name); - return -1; - } =20 - if (val->type !=3D VIR_CONF_STRING) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("config value %s was not a string"), name); - return -1; - } - if (!val->str) { - if (allowMissing) - return 0; - virReportError(VIR_ERR_INTERNAL_ERROR, - _("config value %s was missing"), name); - return -1; - } + result =3D virConfGetValueString(conf, name, value); + if (result =3D=3D 1 && *value !=3D NULL) + return 0; + + if (allowMissing) + return 0; =20 - return VIR_STRDUP(*value, val->str); + return -1; } =20 =20 @@ -193,7 +180,8 @@ xenConfigCopyStringOpt(virConfPtr conf, const char *nam= e, char **value) static int xenConfigGetUUID(virConfPtr conf, const char *name, unsigned char *uuid) { - virConfValuePtr val; + char *string =3D NULL; + int result; =20 if (!uuid || !name || !conf) { virReportError(VIR_ERR_INVALID_ARG, "%s", @@ -201,35 +189,35 @@ xenConfigGetUUID(virConfPtr conf, const char *name, u= nsigned char *uuid) return -1; } =20 - if (!(val =3D virConfGetValue(conf, name))) { - if (virUUIDGenerate(uuid) < 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - "%s", _("Failed to generate UUID")); - return -1; - } else { - return 0; - } - } - - if (val->type !=3D VIR_CONF_STRING) { - virReportError(VIR_ERR_CONF_SYNTAX, - _("config value %s not a string"), name); + if (virConfGetValueString(conf, name, &string) < 0) return -1; - } =20 - if (!val->str) { + if (!string) { virReportError(VIR_ERR_CONF_SYNTAX, _("%s can't be empty"), name); return -1; } =20 - if (virUUIDParse(val->str, uuid) < 0) { + if (virUUIDGenerate(uuid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + "%s", _("Failed to generate UUID")); + result =3D -1; + goto cleanup; + } + + if (virUUIDParse(string, uuid) < 0) { virReportError(VIR_ERR_CONF_SYNTAX, - _("%s not parseable"), val->str); - return -1; + _("%s not parseable"), string); + result =3D -1; + goto cleanup; } =20 - return 0; + result =3D 1; + + cleanup: + VIR_FREE(string); + + return result; } =20 =20 @@ -242,23 +230,15 @@ xenConfigGetString(virConfPtr conf, const char **value, const char *def) { - virConfValuePtr val; + char *string =3D NULL; =20 - *value =3D NULL; - if (!(val =3D virConfGetValue(conf, name))) { + if (virConfGetValueString(conf, name, &string) < 0) { *value =3D def; - return 0; - } - - if (val->type !=3D VIR_CONF_STRING) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("config value %s was malformed"), name); return -1; } - if (!val->str) - *value =3D def; - else - *value =3D val->str; + + *value =3D string !=3D NULL ? string : def; + return 0; } =20 @@ -392,90 +372,84 @@ xenParseEventsActions(virConfPtr conf, virDomainDefPt= r def) static int xenParsePCI(virConfPtr conf, virDomainDefPtr def) { - virConfValuePtr list =3D virConfGetValue(conf, "pci"); virDomainHostdevDefPtr hostdev =3D NULL; + char **pcis =3D NULL, **entries; =20 - if (list && list->type =3D=3D VIR_CONF_LIST) { - list =3D list->list; - while (list) { - char domain[5]; - char bus[3]; - char slot[3]; - char func[2]; - char *key, *nextkey; - int domainID; - int busID; - int slotID; - int funcID; - - domain[0] =3D bus[0] =3D slot[0] =3D func[0] =3D '\0'; - - if ((list->type !=3D VIR_CONF_STRING) || (list->str =3D=3D NUL= L)) - goto skippci; - /* pci=3D['0000:00:1b.0','0000:00:13.0'] */ - if (!(key =3D list->str)) - goto skippci; - if (!(nextkey =3D strchr(key, ':'))) - goto skippci; - if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) = =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Domain %s too big for destination"), key= ); - goto skippci; - } - - key =3D nextkey + 1; - if (!(nextkey =3D strchr(key, ':'))) - goto skippci; - if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) =3D=3D = NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Bus %s too big for destination"), key); - goto skippci; - } - - key =3D nextkey + 1; - if (!(nextkey =3D strchr(key, '.'))) - goto skippci; - if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) =3D= =3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Slot %s too big for destination"), key); - goto skippci; - } - - key =3D nextkey + 1; - if (strlen(key) !=3D 1) - goto skippci; - if (virStrncpy(func, key, 1, sizeof(func)) =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Function %s too big for destination"), k= ey); - goto skippci; - } - - if (virStrToLong_i(domain, NULL, 16, &domainID) < 0) - goto skippci; - if (virStrToLong_i(bus, NULL, 16, &busID) < 0) - goto skippci; - if (virStrToLong_i(slot, NULL, 16, &slotID) < 0) - goto skippci; - if (virStrToLong_i(func, NULL, 16, &funcID) < 0) - goto skippci; - if (!(hostdev =3D virDomainHostdevDefNew())) - return -1; - - hostdev->managed =3D false; - hostdev->source.subsys.type =3D VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE= _PCI; - hostdev->source.subsys.u.pci.addr.domain =3D domainID; - hostdev->source.subsys.u.pci.addr.bus =3D busID; - hostdev->source.subsys.u.pci.addr.slot =3D slotID; - hostdev->source.subsys.u.pci.addr.function =3D funcID; - - if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev)= < 0) { - virDomainHostdevDefFree(hostdev); - return -1; - } + if (virConfGetValueStringList(conf, "pci", false, &pcis) <=3D 0) + return 0; =20 - skippci: - list =3D list->next; - } + for (entries =3D pcis; *entries; entries++) { + char domain[5]; + char bus[3]; + char slot[3]; + char func[2]; + char *key, *nextkey; + int domainID; + int busID; + int slotID; + int funcID; + + domain[0] =3D bus[0] =3D slot[0] =3D func[0] =3D '\0'; + key =3D *entries; + + /* pci=3D['0000:00:1b.0','0000:00:13.0'] */ + if (!(nextkey =3D strchr(key, ':'))) + continue; + if (virStrncpy(domain, key, (nextkey - key), sizeof(domain)) =3D=3D= NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Domain %s too big for destination"), key); + continue; + } + + key =3D nextkey + 1; + if (!(nextkey =3D strchr(key, ':'))) + continue; + if (virStrncpy(bus, key, (nextkey - key), sizeof(bus)) =3D=3D NULL)= { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Bus %s too big for destination"), key); + continue; + } + + key =3D nextkey + 1; + if (!(nextkey =3D strchr(key, '.'))) + continue; + if (virStrncpy(slot, key, (nextkey - key), sizeof(slot)) =3D=3D NUL= L) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Slot %s too big for destination"), key); + continue; + } + + key =3D nextkey + 1; + if (strlen(key) !=3D 1) + continue; + if (virStrncpy(func, key, 1, sizeof(func)) =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Function %s too big for destination"), key); + continue; + } + + if (virStrToLong_i(domain, NULL, 16, &domainID) < 0) + continue; + if (virStrToLong_i(bus, NULL, 16, &busID) < 0) + continue; + if (virStrToLong_i(slot, NULL, 16, &slotID) < 0) + continue; + if (virStrToLong_i(func, NULL, 16, &funcID) < 0) + continue; + if (!(hostdev =3D virDomainHostdevDefNew())) + return -1; + + hostdev->managed =3D false; + hostdev->source.subsys.type =3D VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI; + hostdev->source.subsys.u.pci.addr.domain =3D domainID; + hostdev->source.subsys.u.pci.addr.bus =3D busID; + hostdev->source.subsys.u.pci.addr.slot =3D slotID; + hostdev->source.subsys.u.pci.addr.function =3D funcID; + + if (VIR_APPEND_ELEMENT(def->hostdevs, def->nhostdevs, hostdev) < 0)= { + virDomainHostdevDefFree(hostdev); + return -1; + } } =20 return 0; @@ -595,7 +569,6 @@ xenParseVfb(virConfPtr conf, virDomainDefPtr def) int val; char *listenAddr =3D NULL; int hvm =3D def->os.type =3D=3D VIR_DOMAIN_OSTYPE_HVM; - virConfValuePtr list; virDomainGraphicsDefPtr graphics =3D NULL; =20 if (hvm) { @@ -651,17 +624,20 @@ xenParseVfb(virConfPtr conf, virDomainDefPtr def) } =20 if (!hvm && def->graphics =3D=3D NULL) { /* New PV guests use this for= mat */ - list =3D virConfGetValue(conf, "vfb"); - if (list && list->type =3D=3D VIR_CONF_LIST && - list->list && list->list->type =3D=3D VIR_CONF_STRING && - list->list->str) { + char **vfbs =3D NULL, **entries; + + if (virConfGetValueStringList(conf, "vfb", false, &vfbs) <=3D 0) + return 0; + + for (entries =3D vfbs; *entries; entries++) { char vfb[MAX_VFB]; char *key =3D vfb; + char *entry =3D *entries; =20 - if (virStrcpyStatic(vfb, list->list->str) =3D=3D NULL) { + if (virStrcpyStatic(vfb, entry) =3D=3D NULL) { virReportError(VIR_ERR_INTERNAL_ERROR, _("VFB %s too big for destination"), - list->list->str); + entry); goto cleanup; } =20 @@ -751,6 +727,9 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def, c= onst char *nativeFormat) virDomainChrDefPtr chr =3D NULL; =20 if (def->os.type =3D=3D VIR_DOMAIN_OSTYPE_HVM) { + char **serials =3D NULL, **entries; + int portnum =3D -1; + if (xenConfigGetString(conf, "parallel", &str, NULL) < 0) goto cleanup; if (str && STRNEQ(str, "none") && @@ -768,39 +747,7 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def, = const char *nativeFormat) } =20 /* Try to get the list of values to support multiple serial ports = */ - value =3D virConfGetValue(conf, "serial"); - if (value && value->type =3D=3D VIR_CONF_LIST) { - int portnum =3D -1; - - if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) { - virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", - _("Multiple serial devices are not supporte= d by xen-xm")); - goto cleanup; - } - - value =3D value->list; - while (value) { - char *port =3D NULL; - - if ((value->type !=3D VIR_CONF_STRING) || (value->str =3D= =3D NULL)) - goto cleanup; - port =3D value->str; - portnum++; - if (STREQ(port, "none")) { - value =3D value->next; - continue; - } - - if (!(chr =3D xenParseSxprChar(port, NULL))) - goto cleanup; - chr->deviceType =3D VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL; - chr->target.port =3D portnum; - if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) <= 0) - goto cleanup; - - value =3D value->next; - } - } else { + if (virConfGetValueStringList(conf, "serial", false, &serials) <= =3D 0) { /* If domain is not using multiple serial ports we parse data = old way */ if (xenConfigGetString(conf, "serial", &str, NULL) < 0) goto cleanup; @@ -815,6 +762,31 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr def, = const char *nativeFormat) def->serials[0] =3D chr; def->nserials++; } + + return 0; + } + + if (STREQ(nativeFormat, XEN_CONFIG_FORMAT_XM)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("Multiple serial devices are not supported by= xen-xm")); + goto cleanup; + } + + for (entries =3D serials; *entries; entries++) { + char *port =3D *entries; + + portnum++; + if (STREQ(port, "none")) { + value =3D value->next; + continue; + } + + if (!(chr =3D xenParseSxprChar(port, NULL))) + goto cleanup; + chr->deviceType =3D VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL; + chr->target.port =3D portnum; + if (VIR_APPEND_ELEMENT(def->serials, def->nserials, chr) < 0) + goto cleanup; } } else { if (VIR_ALLOC_N(def->consoles, 1) < 0) @@ -838,191 +810,186 @@ xenParseCharDev(virConfPtr conf, virDomainDefPtr de= f, const char *nativeFormat) static int xenParseVif(virConfPtr conf, virDomainDefPtr def, const char *vif_typename) { + char **vifs =3D NULL, **entries; char *script =3D NULL; virDomainNetDefPtr net =3D NULL; - virConfValuePtr list =3D virConfGetValue(conf, "vif"); - - if (list && list->type =3D=3D VIR_CONF_LIST) { - list =3D list->list; - while (list) { - char model[10]; - char type[10]; - char ip[128]; - char mac[18]; - char bridge[50]; - char vifname[50]; - char rate[50]; - char *key; - - bridge[0] =3D '\0'; - mac[0] =3D '\0'; - ip[0] =3D '\0'; - model[0] =3D '\0'; - type[0] =3D '\0'; - vifname[0] =3D '\0'; - rate[0] =3D '\0'; - - if ((list->type !=3D VIR_CONF_STRING) || (list->str =3D=3D NUL= L)) - goto skipnic; =20 - key =3D list->str; - while (key) { - char *data; - char *nextkey =3D strchr(key, ','); + if (virConfGetValueStringList(conf, "vif", false, &vifs) <=3D 0) + return 0; + + for (entries =3D vifs; *entries; entries++) { + char model[10]; + char type[10]; + char ip[128]; + char mac[18]; + char bridge[50]; + char vifname[50]; + char rate[50]; + char *key =3D *entries; + + bridge[0] =3D '\0'; + mac[0] =3D '\0'; + ip[0] =3D '\0'; + model[0] =3D '\0'; + type[0] =3D '\0'; + vifname[0] =3D '\0'; + rate[0] =3D '\0'; + + while (key) { + char *data; + char *nextkey =3D strchr(key, ','); + + if (!(data =3D strchr(key, '=3D'))) + goto skipnic; + data++; =20 - if (!(data =3D strchr(key, '=3D'))) + if (STRPREFIX(key, "mac=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(mac) - 1; + if (virStrncpy(mac, data, len, sizeof(mac)) =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("MAC address %s too big for destinati= on"), + data); goto skipnic; - data++; - - if (STRPREFIX(key, "mac=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(mac) -= 1; - if (virStrncpy(mac, data, len, sizeof(mac)) =3D=3D NUL= L) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("MAC address %s too big for desti= nation"), - data); - goto skipnic; - } - } else if (STRPREFIX(key, "bridge=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(bridge= ) - 1; - if (virStrncpy(bridge, data, len, sizeof(bridge)) =3D= =3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Bridge %s too big for destinatio= n"), - data); - goto skipnic; - } - } else if (STRPREFIX(key, "script=3D")) { - int len =3D nextkey ? (nextkey - data) : strlen(data); - VIR_FREE(script); - if (VIR_STRNDUP(script, data, len) < 0) - goto cleanup; - } else if (STRPREFIX(key, "model=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(model)= - 1; - if (virStrncpy(model, data, len, sizeof(model)) =3D=3D= NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Model %s too big for destination= "), - data); - goto skipnic; - } - } else if (STRPREFIX(key, "type=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(type) = - 1; - if (virStrncpy(type, data, len, sizeof(type)) =3D=3D N= ULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Type %s too big for destination"= ), - data); - goto skipnic; - } - } else if (STRPREFIX(key, "vifname=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(vifnam= e) - 1; - if (virStrncpy(vifname, data, len, sizeof(vifname)) = =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Vifname %s too big for destinati= on"), - data); - goto skipnic; - } - } else if (STRPREFIX(key, "ip=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(ip) - = 1; - if (virStrncpy(ip, data, len, sizeof(ip)) =3D=3D NULL)= { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("IP %s too big for destination"),= data); - goto skipnic; - } - } else if (STRPREFIX(key, "rate=3D")) { - int len =3D nextkey ? (nextkey - data) : sizeof(rate) = - 1; - if (virStrncpy(rate, data, len, sizeof(rate)) =3D=3D N= ULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("rate %s too big for destination"= ), data); - goto skipnic; - } } - - while (nextkey && (nextkey[0] =3D=3D ',' || - nextkey[0] =3D=3D ' ' || - nextkey[0] =3D=3D '\t')) - nextkey++; - key =3D nextkey; - } - - if (VIR_ALLOC(net) < 0) - goto cleanup; - - if (mac[0]) { - if (virMacAddrParse(mac, &net->mac) < 0) { + } else if (STRPREFIX(key, "bridge=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(bridge) - = 1; + if (virStrncpy(bridge, data, len, sizeof(bridge)) =3D=3D N= ULL) { virReportError(VIR_ERR_INTERNAL_ERROR, - _("malformed mac address '%s'"), mac); + _("Bridge %s too big for destination"), + data); + goto skipnic; + } + } else if (STRPREFIX(key, "script=3D")) { + int len =3D nextkey ? (nextkey - data) : strlen(data); + VIR_FREE(script); + if (VIR_STRNDUP(script, data, len) < 0) goto cleanup; + } else if (STRPREFIX(key, "model=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(model) - 1; + if (virStrncpy(model, data, len, sizeof(model)) =3D=3D NUL= L) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Model %s too big for destination"), + data); + goto skipnic; + } + } else if (STRPREFIX(key, "type=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(type) - 1; + if (virStrncpy(type, data, len, sizeof(type)) =3D=3D NULL)= { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Type %s too big for destination"), + data); + goto skipnic; + } + } else if (STRPREFIX(key, "vifname=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(vifname) -= 1; + if (virStrncpy(vifname, data, len, sizeof(vifname)) =3D=3D= NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Vifname %s too big for destination"), + data); + goto skipnic; + } + } else if (STRPREFIX(key, "ip=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(ip) - 1; + if (virStrncpy(ip, data, len, sizeof(ip)) =3D=3D NULL) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("IP %s too big for destination"), dat= a); + goto skipnic; + } + } else if (STRPREFIX(key, "rate=3D")) { + int len =3D nextkey ? (nextkey - data) : sizeof(rate) - 1; + if (virStrncpy(rate, data, len, sizeof(rate)) =3D=3D NULL)= { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("rate %s too big for destination"), d= ata); + goto skipnic; } } =20 - if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") || - STREQ_NULLABLE(script, "vif-vnic")) { - net->type =3D VIR_DOMAIN_NET_TYPE_BRIDGE; - } else { - net->type =3D VIR_DOMAIN_NET_TYPE_ETHERNET; - } - - if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_BRIDGE) { - if (bridge[0] && VIR_STRDUP(net->data.bridge.brname, bridg= e) < 0) - goto cleanup; - } - if (ip[0]) { - char **ip_list =3D virStringSplit(ip, " ", 0); - size_t i; + while (nextkey && (nextkey[0] =3D=3D ',' || + nextkey[0] =3D=3D ' ' || + nextkey[0] =3D=3D '\t')) + nextkey++; + key =3D nextkey; + } =20 - if (!ip_list) - goto cleanup; + if (VIR_ALLOC(net) < 0) + goto cleanup; =20 - for (i =3D 0; ip_list[i]; i++) { - if (virDomainNetAppendIPAddress(net, ip_list[i], 0, 0)= < 0) { - virStringListFree(ip_list); - goto cleanup; - } - } - virStringListFree(ip_list); + if (mac[0]) { + if (virMacAddrParse(mac, &net->mac) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("malformed mac address '%s'"), mac); + goto cleanup; } + } =20 - if (script && script[0] && - VIR_STRDUP(net->script, script) < 0) - goto cleanup; + if (bridge[0] || STREQ_NULLABLE(script, "vif-bridge") || + STREQ_NULLABLE(script, "vif-vnic")) { + net->type =3D VIR_DOMAIN_NET_TYPE_BRIDGE; + } else { + net->type =3D VIR_DOMAIN_NET_TYPE_ETHERNET; + } =20 - if (model[0] && - VIR_STRDUP(net->model, model) < 0) + if (net->type =3D=3D VIR_DOMAIN_NET_TYPE_BRIDGE) { + if (bridge[0] && VIR_STRDUP(net->data.bridge.brname, bridge) <= 0) goto cleanup; + } + if (ip[0]) { + char **ip_list =3D virStringSplit(ip, " ", 0); + size_t i; =20 - if (!model[0] && type[0] && STREQ(type, vif_typename) && - VIR_STRDUP(net->model, "netfront") < 0) + if (!ip_list) goto cleanup; =20 - if (vifname[0] && - VIR_STRDUP(net->ifname, vifname) < 0) - goto cleanup; + for (i =3D 0; ip_list[i]; i++) { + if (virDomainNetAppendIPAddress(net, ip_list[i], 0, 0) < 0= ) { + virStringListFree(ip_list); + goto cleanup; + } + } + virStringListFree(ip_list); + } =20 - if (rate[0]) { - virNetDevBandwidthPtr bandwidth; - unsigned long long kbytes_per_sec; + if (script && script[0] && + VIR_STRDUP(net->script, script) < 0) + goto cleanup; =20 - if (xenParseSxprVifRate(rate, &kbytes_per_sec) < 0) - goto cleanup; + if (model[0] && + VIR_STRDUP(net->model, model) < 0) + goto cleanup; =20 - if (VIR_ALLOC(bandwidth) < 0) - goto cleanup; - if (VIR_ALLOC(bandwidth->out) < 0) { - VIR_FREE(bandwidth); - goto cleanup; - } + if (!model[0] && type[0] && STREQ(type, vif_typename) && + VIR_STRDUP(net->model, "netfront") < 0) + goto cleanup; =20 - bandwidth->out->average =3D kbytes_per_sec; - net->bandwidth =3D bandwidth; - } + if (vifname[0] && + VIR_STRDUP(net->ifname, vifname) < 0) + goto cleanup; =20 - if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0) + if (rate[0]) { + virNetDevBandwidthPtr bandwidth; + unsigned long long kbytes_per_sec; + + if (xenParseSxprVifRate(rate, &kbytes_per_sec) < 0) goto cleanup; =20 - skipnic: - list =3D list->next; - virDomainNetDefFree(net); - net =3D NULL; - VIR_FREE(script); + if (VIR_ALLOC(bandwidth) < 0) + goto cleanup; + if (VIR_ALLOC(bandwidth->out) < 0) { + VIR_FREE(bandwidth); + goto cleanup; + } + + bandwidth->out->average =3D kbytes_per_sec; + net->bandwidth =3D bandwidth; } + + if (VIR_APPEND_ELEMENT(def->nets, def->nnets, net) < 0) + goto cleanup; + + skipnic: + virDomainNetDefFree(net); + net =3D NULL; + VIR_FREE(script); } =20 return 0; --=20 2.17.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list