From nobody Tue May 13 20:34:03 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 1530284908172739.6849679973035; Fri, 29 Jun 2018 08:08:28 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 252F230B9E1A; Fri, 29 Jun 2018 15:08:27 +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 D45749231D; Fri, 29 Jun 2018 15:08:26 +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 78B4D18037ED; Fri, 29 Jun 2018 15:08:26 +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 w5TF1t4Y032627 for ; Fri, 29 Jun 2018 11:01:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id 6FDB42026D6A; Fri, 29 Jun 2018 15:01:55 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 14D822026D68 for ; Fri, 29 Jun 2018 15:01:54 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Fri, 29 Jun 2018 17:01:49 +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 3/5] virStorageBackendIQNFound: Rework iscsiadm output parsing 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.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Fri, 29 Jun 2018 15:08:27 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Firstly, we can utilize virCommandSetOutputBuffer() API which will collect the command output for us. Secondly, sscanf()-ing through each line is easier to understand (and more robust) than jumping over a string with strchr(). Signed-off-by: Michal Privoznik --- src/util/viriscsi.c | 85 +++++++++++++++++++++----------------------------= ---- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c index 2e55b3c10b..44788056fd 100644 --- a/src/util/viriscsi.c +++ b/src/util/viriscsi.c @@ -108,7 +108,6 @@ virISCSIGetSession(const char *devpath, =20 =20 =20 -#define LINE_SIZE 4096 #define IQN_FOUND 1 #define IQN_MISSING 0 #define IQN_ERROR -1 @@ -117,71 +116,56 @@ static int virStorageBackendIQNFound(const char *initiatoriqn, char **ifacename) { - int ret =3D IQN_ERROR, fd =3D -1; - char ebuf[64]; - FILE *fp =3D NULL; - char *line =3D NULL, *newline =3D NULL, *iqn =3D NULL, *token =3D NULL; + int ret =3D IQN_ERROR; + char *outbuf =3D NULL; + char *line =3D NULL; + char *iface =3D NULL; + char *iqn =3D NULL; virCommandPtr cmd =3D virCommandNewArgList(ISCSIADM, "--mode", "iface", NULL); =20 *ifacename =3D NULL; =20 - if (VIR_ALLOC_N(line, LINE_SIZE) !=3D 0) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Could not allocate memory for output of '%s'"), - ISCSIADM); + virCommandSetOutputBuffer(cmd, &outbuf); + if (virCommandRun(cmd, NULL) < 0) goto cleanup; - } =20 - memset(line, 0, LINE_SIZE); + /* Example of data we are dealing with: + * default tcp,,,, + * iser iser,,,, + * libvirt-iface-253db048 tcp,,,,iqn.2017-03.com.= user:client + */ =20 - virCommandSetOutputFD(cmd, &fd); - if (virCommandRunAsync(cmd, NULL) < 0) - goto cleanup; + line =3D outbuf; + while (line) { + char *newline; + int num; =20 - if ((fp =3D VIR_FDOPEN(fd, "r")) =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Failed to open stream for file descriptor " - "when reading output from '%s': '%s'"), - ISCSIADM, virStrerror(errno, ebuf, sizeof(ebuf))); - goto cleanup; - } + if (!(newline =3D strchr(line, '\n'))) + break; =20 - while (fgets(line, LINE_SIZE, fp) !=3D NULL) { - newline =3D strrchr(line, '\n'); - if (newline =3D=3D NULL) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Unexpected line > %d characters " - "when parsing output of '%s'"), - LINE_SIZE, ISCSIADM); - goto cleanup; - } *newline =3D '\0'; =20 - iqn =3D strrchr(line, ','); - if (iqn =3D=3D NULL) - continue; - iqn++; + VIR_FREE(iface); + VIR_FREE(iqn); + num =3D sscanf(line, "%ms %*[^,],%*[^,],%*[^,],%*[^,],%ms", &iface= , &iqn); + + if (num !=3D 2) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("malformed output of %s: %s"), + ISCSIADM, line); + goto cleanup; + } =20 if (STREQ(iqn, initiatoriqn)) { - token =3D strchr(line, ' '); - if (!token) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("Missing space when parsing output " - "of '%s'"), ISCSIADM); - goto cleanup; - } - - if (VIR_STRNDUP(*ifacename, line, token - line) < 0) - goto cleanup; + VIR_STEAL_PTR(*ifacename, iface); =20 VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iq= n); break; } - } =20 - if (virCommandWait(cmd, NULL) < 0) - goto cleanup; + line =3D newline + 1; + } =20 ret =3D *ifacename ? IQN_FOUND : IQN_MISSING; =20 @@ -189,11 +173,10 @@ virStorageBackendIQNFound(const char *initiatoriqn, if (ret !=3D IQN_FOUND) VIR_DEBUG("Could not find interface with IQN '%s'", iqn); =20 - VIR_FREE(line); - VIR_FORCE_FCLOSE(fp); - VIR_FORCE_CLOSE(fd); + VIR_FREE(iqn); + VIR_FREE(iface); + VIR_FREE(outbuf); virCommandFree(cmd); - return ret; } =20 --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list