From nobody Fri May 10 06:21:19 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 1530284542342711.107801184934; Fri, 29 Jun 2018 08:02:22 -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 7D93090917; Fri, 29 Jun 2018 15:02:20 +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 B48F289F06; Fri, 29 Jun 2018 15:02:19 +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 6AECA3F7FA; Fri, 29 Jun 2018 15:02:18 +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 w5TF1s5w032613 for ; Fri, 29 Jun 2018 11:01:54 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2FF212026D6A; Fri, 29 Jun 2018 15:01:54 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id C73292026D68 for ; Fri, 29 Jun 2018 15:01:53 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Fri, 29 Jun 2018 17:01:47 +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 1/5] virStorageBackendIQNFound: Fix ret value assignment 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.29]); Fri, 29 Jun 2018 15:02:21 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" The return value of this function is overwritten more times than I am comfortable with. Signed-off-by: Michal Privoznik Reviewed-by: John Ferlan --- src/util/viriscsi.c | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c index d4c745a1af..a308ee4bac 100644 --- a/src/util/viriscsi.c +++ b/src/util/viriscsi.c @@ -117,15 +117,16 @@ static int virStorageBackendIQNFound(const char *initiatoriqn, char **ifacename) { - int ret =3D IQN_MISSING, fd =3D -1; + 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; virCommandPtr cmd =3D virCommandNewArgList(ISCSIADM, "--mode", "iface", NULL); =20 + *ifacename =3D NULL; + if (VIR_ALLOC_N(line, LINE_SIZE) !=3D 0) { - ret =3D IQN_ERROR; virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not allocate memory for output of '%s'"), ISCSIADM); @@ -135,24 +136,20 @@ virStorageBackendIQNFound(const char *initiatoriqn, memset(line, 0, LINE_SIZE); =20 virCommandSetOutputFD(cmd, &fd); - if (virCommandRunAsync(cmd, NULL) < 0) { - ret =3D IQN_ERROR; + if (virCommandRunAsync(cmd, NULL) < 0) goto out; - } =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))); - ret =3D IQN_ERROR; goto out; } =20 while (fgets(line, LINE_SIZE, fp) !=3D NULL) { newline =3D strrchr(line, '\n'); if (newline =3D=3D NULL) { - ret =3D IQN_ERROR; virReportError(VIR_ERR_INTERNAL_ERROR, _("Unexpected line > %d characters " "when parsing output of '%s'"), @@ -169,27 +166,27 @@ virStorageBackendIQNFound(const char *initiatoriqn, if (STREQ(iqn, initiatoriqn)) { token =3D strchr(line, ' '); if (!token) { - ret =3D IQN_ERROR; virReportError(VIR_ERR_INTERNAL_ERROR, _("Missing space when parsing output " "of '%s'"), ISCSIADM); goto out; } - if (VIR_STRNDUP(*ifacename, line, token - line) < 0) { - ret =3D IQN_ERROR; + + if (VIR_STRNDUP(*ifacename, line, token - line) < 0) goto out; - } + VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iq= n); - ret =3D IQN_FOUND; break; } } =20 if (virCommandWait(cmd, NULL) < 0) - ret =3D IQN_ERROR; + goto out; + + ret =3D *ifacename ? IQN_FOUND : IQN_MISSING; =20 out: - if (ret =3D=3D IQN_MISSING) + if (ret !=3D IQN_FOUND) VIR_DEBUG("Could not find interface with IQN '%s'", iqn); =20 VIR_FREE(line); --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 10 06:21:19 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 1530284592071869.4405321577507; Fri, 29 Jun 2018 08:03:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 845DE30001EA; Fri, 29 Jun 2018 15:03:10 +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 452F275AD2; Fri, 29 Jun 2018 15:03:10 +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 D776D18037EF; Fri, 29 Jun 2018 15:03:09 +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 w5TF1sZ3032622 for ; Fri, 29 Jun 2018 11:01:55 -0400 Received: by smtp.corp.redhat.com (Postfix) id CC7752026D6A; Fri, 29 Jun 2018 15:01:54 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7165F2026D68 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:48 +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 2/5] virStorageBackendIQNFound: Rename out label 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.42]); Fri, 29 Jun 2018 15:03:11 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This is in fact 'cleanup' label and it should be named as such. Signed-off-by: Michal Privoznik Reviewed-by: John Ferlan --- src/util/viriscsi.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c index a308ee4bac..2e55b3c10b 100644 --- a/src/util/viriscsi.c +++ b/src/util/viriscsi.c @@ -130,21 +130,21 @@ virStorageBackendIQNFound(const char *initiatoriqn, virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not allocate memory for output of '%s'"), ISCSIADM); - goto out; + goto cleanup; } =20 memset(line, 0, LINE_SIZE); =20 virCommandSetOutputFD(cmd, &fd); if (virCommandRunAsync(cmd, NULL) < 0) - goto out; + goto cleanup; =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 out; + goto cleanup; } =20 while (fgets(line, LINE_SIZE, fp) !=3D NULL) { @@ -154,7 +154,7 @@ virStorageBackendIQNFound(const char *initiatoriqn, _("Unexpected line > %d characters " "when parsing output of '%s'"), LINE_SIZE, ISCSIADM); - goto out; + goto cleanup; } *newline =3D '\0'; =20 @@ -169,11 +169,11 @@ virStorageBackendIQNFound(const char *initiatoriqn, virReportError(VIR_ERR_INTERNAL_ERROR, _("Missing space when parsing output " "of '%s'"), ISCSIADM); - goto out; + goto cleanup; } =20 if (VIR_STRNDUP(*ifacename, line, token - line) < 0) - goto out; + goto cleanup; =20 VIR_DEBUG("Found interface '%s' with IQN '%s'", *ifacename, iq= n); break; @@ -181,11 +181,11 @@ virStorageBackendIQNFound(const char *initiatoriqn, } =20 if (virCommandWait(cmd, NULL) < 0) - goto out; + goto cleanup; =20 ret =3D *ifacename ? IQN_FOUND : IQN_MISSING; =20 - out: + cleanup: if (ret !=3D IQN_FOUND) VIR_DEBUG("Could not find interface with IQN '%s'", iqn); =20 --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 10 06:21:19 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 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 From nobody Fri May 10 06:21:19 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 1530284550292874.4122258335035; Fri, 29 Jun 2018 08:02:30 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B62E130C3406; Fri, 29 Jun 2018 15:02: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 3BCDE83089; Fri, 29 Jun 2018 15:02: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 E04923F646; Fri, 29 Jun 2018 15:02: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 w5TF1u5c032635 for ; Fri, 29 Jun 2018 11:01:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id 11CB52026D6A; Fri, 29 Jun 2018 15:01:56 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id AB2282026D68 for ; Fri, 29 Jun 2018 15:01:55 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Fri, 29 Jun 2018 17:01:50 +0200 Message-Id: <2d80d9804059123ca27b5add1e54f55d5aed7c2a.1530284263.git.mprivozn@redhat.com> 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 4/5] virISCSIScanTargets: Honour iSCSI interface 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.15 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:02:29 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" When scanning for targets, iSCSI might give different results depending on the interface used. This is basically just name of config file under /etc/iscsi/ifaces to use. The file contains initiator IQN thus different results claim. Signed-off-by: Michal Privoznik Reviewed-by: John Ferlan --- src/storage/storage_backend_iscsi.c | 4 +- src/util/viriscsi.c | 78 +++++++++++++++++++++++++++++++++= +--- src/util/viriscsi.h | 1 + tests/viriscsitest.c | 2 +- 4 files changed, 77 insertions(+), 8 deletions(-) diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_back= end_iscsi.c index 7871d1915b..3b9dddb4fd 100644 --- a/src/storage/storage_backend_iscsi.c +++ b/src/storage/storage_backend_iscsi.c @@ -194,7 +194,9 @@ virStorageBackendISCSIFindPoolSources(const char *srcSp= ec, if (!(portal =3D virStorageBackendISCSIPortal(source))) goto cleanup; =20 - if (virISCSIScanTargets(portal, &ntargets, &targets) < 0) + if (virISCSIScanTargets(portal, + source->initiator.iqn, + &ntargets, &targets) < 0) goto cleanup; =20 if (VIR_ALLOC_N(list.sources, ntargets) < 0) diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c index 44788056fd..365669aac2 100644 --- a/src/util/viriscsi.c +++ b/src/util/viriscsi.c @@ -40,6 +40,13 @@ VIR_LOG_INIT("util.iscsi"); =20 =20 +static int +virISCSIScanTargetsInternal(const char *portal, + const char *ifacename, + size_t *ntargetsret, + char ***targetsret); + + struct virISCSISessionData { char *session; const char *devpath; @@ -286,9 +293,10 @@ virISCSIConnection(const char *portal, * iscsiadm doesn't let you send commands to the Interface IQN, * unless you've first issued a 'sendtargets' command to the * portal. Without the sendtargets all that is received is a - * "iscsiadm: No records found" + * "iscsiadm: No records found". However, we must ensure that + * the command is issued over interface name we invented above. */ - if (virISCSIScanTargets(portal, NULL, NULL) < 0) + if (virISCSIScanTargetsInternal(portal, ifacename, NULL, NULL)= < 0) goto cleanup; =20 break; @@ -371,10 +379,11 @@ virISCSIGetTargets(char **const groups, } =20 =20 -int -virISCSIScanTargets(const char *portal, - size_t *ntargetsret, - char ***targetsret) +static int +virISCSIScanTargetsInternal(const char *portal, + const char *ifacename, + size_t *ntargetsret, + char ***targetsret) { /** * @@ -400,6 +409,12 @@ virISCSIScanTargets(const char *portal, "--op", "nonpersistent", NULL); =20 + if (ifacename) { + virCommandAddArgList(cmd, + "--interface", ifacename, + NULL); + } + memset(&list, 0, sizeof(list)); =20 if (virCommandRunRegex(cmd, @@ -425,6 +440,57 @@ virISCSIScanTargets(const char *portal, return ret; } =20 + +/** + * virISCSIScanTargets: + * @portal: iSCSI portal + * @initiatoriqn: Initiator IQN + * @ntargets: number of items in @targetsret array + * @targets: array of targets + * + * For given @portal issue sendtargets command. Optionally, + * @initiatoriqn can be set to override default configuration. + * The targets are stored into @targets array and the size of + * the array is stored into @ntargets. + * + * Returns: 0 on success, + * -1 otherwise (with error reported) + */ +int +virISCSIScanTargets(const char *portal, + const char *initiatoriqn, + size_t *ntargets, + char ***targets) +{ + char *ifacename =3D NULL; + int ret =3D -1; + + if (ntargets) + *ntargets =3D 0; + if (targets) + *targets =3D NULL; + + if (initiatoriqn) { + switch ((virStorageBackendIQNFound(initiatoriqn, &ifacename))) { + case IQN_FOUND: + break; + + case IQN_MISSING: + virReportError(VIR_ERR_OPERATION_FAILED, + _("no iSCSI interface defined for IQN %s"), + initiatoriqn); + case IQN_ERROR: + default: + return -1; + } + } + + ret =3D virISCSIScanTargetsInternal(portal, ifacename, ntargets, targe= ts); + VIR_FREE(ifacename); + return ret; +} + + /* * virISCSINodeNew: * @portal: address for iSCSI target diff --git a/src/util/viriscsi.h b/src/util/viriscsi.h index a44beeaf67..31b589dbf9 100644 --- a/src/util/viriscsi.h +++ b/src/util/viriscsi.h @@ -49,6 +49,7 @@ virISCSIRescanLUNs(const char *session) =20 int virISCSIScanTargets(const char *portal, + const char *initiatoriqn, size_t *ntargetsret, char ***targetsret) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; diff --git a/tests/viriscsitest.c b/tests/viriscsitest.c index aa4ba57707..4bdb782e36 100644 --- a/tests/viriscsitest.c +++ b/tests/viriscsitest.c @@ -145,7 +145,7 @@ testISCSIScanTargets(const void *data) =20 virCommandSetDryRun(NULL, testIscsiadmCb, NULL); =20 - if (virISCSIScanTargets(info->portal, &ntargets, &targets) < 0) + if (virISCSIScanTargets(info->portal, NULL, &ntargets, &targets) < 0) goto cleanup; =20 if (info->nexpected !=3D ntargets) { --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 10 06:21:19 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 1530284915645414.3258228381609; Fri, 29 Jun 2018 08:08:35 -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 CAA2030BA21D; Fri, 29 Jun 2018 15:08:32 +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 9613289F00; Fri, 29 Jun 2018 15:08: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 3218818037EC; Fri, 29 Jun 2018 15:08:32 +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 w5TF1uXP032644 for ; Fri, 29 Jun 2018 11:01:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id A805C2026D6A; Fri, 29 Jun 2018 15:01:56 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4D6DC2026D68 for ; Fri, 29 Jun 2018 15:01:56 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Fri, 29 Jun 2018 17:01:51 +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 5/5] virISCSIScanTargets: Allow making targets persistent 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.49]); Fri, 29 Jun 2018 15:08:35 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" After new iSCSI interface is successfully set up, we issue sendtargets command. However, after 56057900dc53df490d we don't update the host config which in turn makes login fail because iscsiadm is unable to find any matching record for the interface. Signed-off-by: Michal Privoznik Reviewed-by: John Ferlan --- src/storage/storage_backend_iscsi.c | 1 + src/util/viriscsi.c | 21 ++++++++++++++++++--- src/util/viriscsi.h | 1 + tests/viriscsitest.c | 3 ++- 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/storage/storage_backend_iscsi.c b/src/storage/storage_back= end_iscsi.c index 3b9dddb4fd..6242cd0fac 100644 --- a/src/storage/storage_backend_iscsi.c +++ b/src/storage/storage_backend_iscsi.c @@ -196,6 +196,7 @@ virStorageBackendISCSIFindPoolSources(const char *srcSp= ec, =20 if (virISCSIScanTargets(portal, source->initiator.iqn, + false, &ntargets, &targets) < 0) goto cleanup; =20 diff --git a/src/util/viriscsi.c b/src/util/viriscsi.c index 365669aac2..baf41c5be1 100644 --- a/src/util/viriscsi.c +++ b/src/util/viriscsi.c @@ -43,6 +43,7 @@ VIR_LOG_INIT("util.iscsi"); static int virISCSIScanTargetsInternal(const char *portal, const char *ifacename, + bool persist, size_t *ntargetsret, char ***targetsret); =20 @@ -295,8 +296,10 @@ virISCSIConnection(const char *portal, * portal. Without the sendtargets all that is received is a * "iscsiadm: No records found". However, we must ensure that * the command is issued over interface name we invented above. + * AND that targets are made persistent. */ - if (virISCSIScanTargetsInternal(portal, ifacename, NULL, NULL)= < 0) + if (virISCSIScanTargetsInternal(portal, ifacename, + true, NULL, NULL) < 0) goto cleanup; =20 break; @@ -382,6 +385,7 @@ virISCSIGetTargets(char **const groups, static int virISCSIScanTargetsInternal(const char *portal, const char *ifacename, + bool persist, size_t *ntargetsret, char ***targetsret) { @@ -406,9 +410,14 @@ virISCSIScanTargetsInternal(const char *portal, "--mode", "discovery", "--type", "sendtargets", "--portal", portal, - "--op", "nonpersistent", NULL); =20 + if (!persist) { + virCommandAddArgList(cmd, + "--op", "nonpersistent", + NULL); + } + if (ifacename) { virCommandAddArgList(cmd, "--interface", ifacename, @@ -445,6 +454,7 @@ virISCSIScanTargetsInternal(const char *portal, * virISCSIScanTargets: * @portal: iSCSI portal * @initiatoriqn: Initiator IQN + * @persists: whether scanned targets should be saved * @ntargets: number of items in @targetsret array * @targets: array of targets * @@ -453,12 +463,16 @@ virISCSIScanTargetsInternal(const char *portal, * The targets are stored into @targets array and the size of * the array is stored into @ntargets. * + * If @persist is true, then targets returned by iSCSI portal are + * made persistent on the host (their config is saved). + * * Returns: 0 on success, * -1 otherwise (with error reported) */ int virISCSIScanTargets(const char *portal, const char *initiatoriqn, + bool persist, size_t *ntargets, char ***targets) { @@ -485,7 +499,8 @@ virISCSIScanTargets(const char *portal, } } =20 - ret =3D virISCSIScanTargetsInternal(portal, ifacename, ntargets, targe= ts); + ret =3D virISCSIScanTargetsInternal(portal, ifacename, + persist, ntargets, targets); VIR_FREE(ifacename); return ret; } diff --git a/src/util/viriscsi.h b/src/util/viriscsi.h index 31b589dbf9..4da9becfb2 100644 --- a/src/util/viriscsi.h +++ b/src/util/viriscsi.h @@ -50,6 +50,7 @@ virISCSIRescanLUNs(const char *session) int virISCSIScanTargets(const char *portal, const char *initiatoriqn, + bool persist, size_t *ntargetsret, char ***targetsret) ATTRIBUTE_NONNULL(1) ATTRIBUTE_RETURN_CHECK; diff --git a/tests/viriscsitest.c b/tests/viriscsitest.c index 4bdb782e36..3bb3993196 100644 --- a/tests/viriscsitest.c +++ b/tests/viriscsitest.c @@ -145,7 +145,8 @@ testISCSIScanTargets(const void *data) =20 virCommandSetDryRun(NULL, testIscsiadmCb, NULL); =20 - if (virISCSIScanTargets(info->portal, NULL, &ntargets, &targets) < 0) + if (virISCSIScanTargets(info->portal, NULL, + false, &ntargets, &targets) < 0) goto cleanup; =20 if (info->nexpected !=3D ntargets) { --=20 2.16.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list