From nobody Fri May 3 22:24:10 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1536745620015609.0126126699035; Wed, 12 Sep 2018 02:47:00 -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 31F8F30820D5; Wed, 12 Sep 2018 09:46:57 +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 BF5B960142; Wed, 12 Sep 2018 09:46:56 +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 48F8D180613A; Wed, 12 Sep 2018 09:46:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8C9kq3M024861 for ; Wed, 12 Sep 2018 05:46:52 -0400 Received: by smtp.corp.redhat.com (Postfix) id 7617C201598B; Wed, 12 Sep 2018 09:46:52 +0000 (UTC) Received: from mx1.redhat.com (ext-mx17.extmail.prod.ext.phx2.redhat.com [10.5.110.46]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6ED5C2015889 for ; Wed, 12 Sep 2018 09:46:50 +0000 (UTC) Received: from m6561.mail.qiye.163.com (m6561.mail.qiye.163.com [123.126.65.61]) by mx1.redhat.com (Postfix) with ESMTP id 4A6803082DD5 for ; Wed, 12 Sep 2018 09:46:48 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp11 (Coremail) with SMTP id WdOowABXm_WD4Jhbz8LMAQ--.7S3; Wed, 12 Sep 2018 17:46:47 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:46:34 +0800 Message-Id: <20180912094638.17279-2-shi_lei@massclouds.com> In-Reply-To: <20180912094638.17279-1-shi_lei@massclouds.com> References: <20180912094638.17279-1-shi_lei@massclouds.com> X-CM-TRANSID: WdOowABXm_WD4Jhbz8LMAQ--.7S3 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRto7KUUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiCRG-T1kXOZgLpAAAso X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Wed, 12 Sep 2018 09:46:49 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.46]); Wed, 12 Sep 2018 09:46:49 +0000 (UTC) for IP:'123.126.65.61' DOMAIN:'m6561.mail.qiye.163.com' HELO:'m6561.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.126.65.61 m6561.mail.qiye.163.com 123.126.65.61 m6561.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.46 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 1/5] util: file: introduce VIR_AUTOCLOSE macro to close fd of the file automatically 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, 12 Sep 2018 09:46:57 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- src/util/virfile.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/util/virfile.h b/src/util/virfile.h index b30a1d3..2bc3cf0 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -54,6 +54,11 @@ int virFileClose(int *fdptr, virFileCloseFlags flags) int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK; FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK; =20 +static inline void virForceCloseHelper(int *fd) +{ + ignore_value(virFileClose(fd, VIR_FILE_CLOSE_PRESERVE_ERRNO)); +} + /* For use on normal paths; caller must check return value, and failure sets errno per close. */ # define VIR_CLOSE(FD) virFileClose(&(FD), 0) @@ -64,8 +69,7 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBU= TE_RETURN_CHECK; =20 /* For use on cleanup paths; errno is unaffected by close, and no return value to worry about. */ -# define VIR_FORCE_CLOSE(FD) \ - ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO)) +# define VIR_FORCE_CLOSE(FD) virForceCloseHelper(&(FD)) # define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true)) =20 /* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expec= ted @@ -80,6 +84,16 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIB= UTE_RETURN_CHECK; VIR_FILE_CLOSE_PRESERVE_ERRNO | \ VIR_FILE_CLOSE_DONT_LOG)) =20 +/** + * VIR_AUTOCLOSE: + * + * Macro to automatically force close the fd by calling virForceCloseHelper + * when the fd goes out of scope. It's used to eliminate VIR_FORCE_CLOSE + * in cleanup sections. + */ +# define VIR_AUTOCLOSE __attribute__((cleanup(virForceCloseHelper))) int + + /* Opaque type for managing a wrapper around a fd. */ struct _virFileWrapperFd; =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 22:24:10 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15367456324151006.2458524507934; Wed, 12 Sep 2018 02:47:12 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4DF3D5115F; Wed, 12 Sep 2018 09:47:10 +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 0ABD65DD6F; Wed, 12 Sep 2018 09:47: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 A22314A465; Wed, 12 Sep 2018 09:47:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8C9kveW024882 for ; Wed, 12 Sep 2018 05:46:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id 5591E608FA; Wed, 12 Sep 2018 09:46:57 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4732060921 for ; Wed, 12 Sep 2018 09:46:54 +0000 (UTC) Received: from m6561.mail.qiye.163.com (m6561.mail.qiye.163.com [123.126.65.61]) by mx1.redhat.com (Postfix) with ESMTP id EDCDB88317 for ; Wed, 12 Sep 2018 09:46:52 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp11 (Coremail) with SMTP id WdOowABXm_WD4Jhbz8LMAQ--.7S4; Wed, 12 Sep 2018 17:46:47 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:46:35 +0800 Message-Id: <20180912094638.17279-3-shi_lei@massclouds.com> In-Reply-To: <20180912094638.17279-1-shi_lei@massclouds.com> References: <20180912094638.17279-1-shi_lei@massclouds.com> X-CM-TRANSID: WdOowABXm_WD4Jhbz8LMAQ--.7S4 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRto7KUUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbifwi-T1rpNQjF6QAAsl X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 12 Sep 2018 09:46:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 12 Sep 2018 09:46:54 +0000 (UTC) for IP:'123.126.65.61' DOMAIN:'m6561.mail.qiye.163.com' HELO:'m6561.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.126.65.61 m6561.mail.qiye.163.com 123.126.65.61 m6561.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 2/5] cfg.mk: change syntax-check rule for VIR_AUTOCLOSE variable initialization 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.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 12 Sep 2018 09:47:11 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- cfg.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cfg.mk b/cfg.mk index 609ae86..eddd110 100644 --- a/cfg.mk +++ b/cfg.mk @@ -1063,7 +1063,7 @@ sc_prohibit_backslash_alignment: # Rule to ensure that varibales declared using a cleanup macro are # always initialized. sc_require_attribute_cleanup_initialization: - @prohibit=3D'VIR_AUTO(FREE|PTR)\(.+\) *[^=3D]+;' \ + @prohibit=3D'VIR_AUTO((FREE|PTR)\(.+\)|CLOSE) *[^=3D]+;' \ in_vc_files=3D'\.[chx]$$' \ halt=3D'variable declared with a cleanup macro must be initialized' \ $(_sc_search_regexp) --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 22:24:10 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1536745637403583.2634002736133; Wed, 12 Sep 2018 02:47:17 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.26]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 7CC578A1B; Wed, 12 Sep 2018 09:47:15 +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 37397309138F; Wed, 12 Sep 2018 09:47:15 +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 D37EC181A139; Wed, 12 Sep 2018 09:47:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8C9kxJc024905 for ; Wed, 12 Sep 2018 05:46:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2B13B60C66; Wed, 12 Sep 2018 09:46:59 +0000 (UTC) Received: from mx1.redhat.com (ext-mx18.extmail.prod.ext.phx2.redhat.com [10.5.110.47]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 230F660C5B for ; Wed, 12 Sep 2018 09:46:55 +0000 (UTC) Received: from m6561.mail.qiye.163.com (m6561.mail.qiye.163.com [123.126.65.61]) by mx1.redhat.com (Postfix) with ESMTP id 9350E30034D0 for ; Wed, 12 Sep 2018 09:46:50 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp11 (Coremail) with SMTP id WdOowABXm_WD4Jhbz8LMAQ--.7S5; Wed, 12 Sep 2018 17:46:48 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:46:36 +0800 Message-Id: <20180912094638.17279-4-shi_lei@massclouds.com> In-Reply-To: <20180912094638.17279-1-shi_lei@massclouds.com> References: <20180912094638.17279-1-shi_lei@massclouds.com> X-CM-TRANSID: WdOowABXm_WD4Jhbz8LMAQ--.7S5 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRto7KUUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiAQi-T1ikTeEDhAAAsu X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 12 Sep 2018 09:46:54 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 12 Sep 2018 09:46:54 +0000 (UTC) for IP:'123.126.65.61' DOMAIN:'m6561.mail.qiye.163.com' HELO:'m6561.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.126.65.61 m6561.mail.qiye.163.com 123.126.65.61 m6561.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.47 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 3/5] util: file: use VIR_AUTOCLOSE instead of VIR_FORCE_CLOSE 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.26 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Wed, 12 Sep 2018 09:47:16 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- src/util/virfile.c | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 01ebdb6..2366c11 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1969,29 +1969,22 @@ int virFileIsCDROM(const char *path) { struct stat st; - int fd; - int ret =3D -1; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D open(path, O_RDONLY | O_NONBLOCK)) < 0) - goto cleanup; + return -1; =20 if (fstat(fd, &st) < 0) - goto cleanup; + return -1; =20 - if (!S_ISBLK(st.st_mode)) { - ret =3D 0; - goto cleanup; - } + if (!S_ISBLK(st.st_mode)) + return 0; =20 /* Attempt to detect via a CDROM specific ioctl */ if (ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT) >=3D 0) - ret =3D 1; - else - ret =3D 0; + return 1; =20 - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } =20 #else --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 22:24:10 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 153674563720664.4320783121625; Wed, 12 Sep 2018 02:47:17 -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 116A487644; Wed, 12 Sep 2018 09:47:15 +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 A92965785; Wed, 12 Sep 2018 09:47:14 +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 496C34A46D; Wed, 12 Sep 2018 09:47:14 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8C9kwno024895 for ; Wed, 12 Sep 2018 05:46:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8F0D4200784A; Wed, 12 Sep 2018 09:46:58 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 857072007828 for ; Wed, 12 Sep 2018 09:46:58 +0000 (UTC) Received: from m6561.mail.qiye.163.com (m6561.mail.qiye.163.com [123.126.65.61]) by mx1.redhat.com (Postfix) with ESMTP id 194618831A for ; Wed, 12 Sep 2018 09:46:53 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp11 (Coremail) with SMTP id WdOowABXm_WD4Jhbz8LMAQ--.7S6; Wed, 12 Sep 2018 17:46:48 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:46:37 +0800 Message-Id: <20180912094638.17279-5-shi_lei@massclouds.com> In-Reply-To: <20180912094638.17279-1-shi_lei@massclouds.com> References: <20180912094638.17279-1-shi_lei@massclouds.com> X-CM-TRANSID: WdOowABXm_WD4Jhbz8LMAQ--.7S6 X-Coremail-Antispam: 1Uf129KBjvJXoWxtryDCF1UCry8Cr15ZF4Utwb_yoW3Gw13pF 1FqFyDtwsYqr4rGrWUAr1UGF1qkrZrXr4rt3yUZwnrZ3yfuaySvF4xCF12gan3JFyrWF4r JF4Y9F95Xa4UGaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pRDl1kUUUUU= X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbidwm-T1n5eIIEAgAAsS X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 12 Sep 2018 09:46:57 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Wed, 12 Sep 2018 09:46:57 +0000 (UTC) for IP:'123.126.65.61' DOMAIN:'m6561.mail.qiye.163.com' HELO:'m6561.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.126.65.61 m6561.mail.qiye.163.com 123.126.65.61 m6561.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 4/5] util: netdevbridge: use VIR_AUTOCLOSE instead of VIR_FORCE_CLOSE 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.26]); Wed, 12 Sep 2018 09:47:15 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- src/util/virnetdevbridge.c | 120 ++++++++++++------------------------- 1 file changed, 37 insertions(+), 83 deletions(-) diff --git a/src/util/virnetdevbridge.c b/src/util/virnetdevbridge.c index ed2db27..e058898 100644 --- a/src/util/virnetdevbridge.c +++ b/src/util/virnetdevbridge.c @@ -81,9 +81,8 @@ static int virNetDevBridgeCmd(const char *brname, void *arg, size_t argsize) { - int s; - int ret =3D -1; struct ifdrv ifd; + VIR_AUTOCLOSE s =3D -1; =20 memset(&ifd, 0, sizeof(ifd)); =20 @@ -97,19 +96,14 @@ static int virNetDevBridgeCmd(const char *brname, virReportSystemError(ERANGE, _("Network interface name '%s' is too long"), brname); - goto cleanup; + return -1; } =20 ifd.ifd_cmd =3D op; ifd.ifd_len =3D argsize; ifd.ifd_data =3D arg; =20 - ret =3D ioctl(s, SIOCSDRVSPEC, &ifd); - - cleanup: - VIR_FORCE_CLOSE(s); - - return ret; + return ioctl(s, SIOCSDRVSPEC, &ifd); } #endif =20 @@ -167,10 +161,9 @@ static int virNetDevBridgeGet(const char *brname, const char *paramname, /* sysfs param name = */ unsigned long *value) /* current value */ { - int ret =3D -1; - int fd =3D -1; struct ifreq ifr; VIR_AUTOFREE(char *) path =3D NULL; + VIR_AUTOCLOSE fd =3D -1; =20 if (virAsprintf(&path, SYSFS_NET_DIR "%s/bridge/%s", brname, paramname= ) < 0) return -1; @@ -180,26 +173,26 @@ static int virNetDevBridgeGet(const char *brname, =20 if (virFileReadAll(path, INT_BUFSIZE_BOUND(unsigned long), &valuestr) < 0) - goto cleanup; + return -1; =20 if (virStrToLong_ul(valuestr, NULL, 10, value) < 0) { virReportSystemError(EINVAL, _("Unable to get bridge %s %s"), brname, paramname); - goto cleanup; + return -1; } } else { struct __bridge_info info; unsigned long args[] =3D { BRCTL_GET_BRIDGE_INFO, (unsigned long)&= info, 0, 0 }; =20 if ((fd =3D virNetDevSetupControl(brname, &ifr)) < 0) - goto cleanup; + return -1; =20 ifr.ifr_data =3D (char*)&args; if (ioctl(fd, SIOCDEVPRIVATE, ifr) < 0) { virReportSystemError(errno, _("Unable to get bridge %s %s"), brname, = paramname); - goto cleanup; + return -1; } =20 if (STREQ(paramname, "stp_state")) { @@ -209,14 +202,11 @@ static int virNetDevBridgeGet(const char *brname, } else { virReportSystemError(EINVAL, _("Unable to get bridge %s %s"), brname, = paramname); - goto cleanup; + return -1; } } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #endif /* __linux__ */ =20 @@ -391,8 +381,7 @@ virNetDevBridgePortSetUnicastFlood(const char *brname A= TTRIBUTE_UNUSED, static int virNetDevBridgeCreateWithIoctl(const char *brname) { - int fd =3D -1; - int ret =3D -1; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(NULL, NULL)) < 0) return -1; @@ -400,14 +389,10 @@ virNetDevBridgeCreateWithIoctl(const char *brname) if (ioctl(fd, SIOCBRADDBR, brname) < 0) { virReportSystemError(errno, _("Unable to create bridge %s"), brname); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #endif =20 @@ -448,9 +433,8 @@ virNetDevBridgeCreate(const char *brname) int virNetDevBridgeCreate(const char *brname) { - int s; struct ifreq ifr; - int ret =3D - 1; + VIR_AUTOCLOSE s =3D -1; =20 if ((s =3D virNetDevSetupControl("bridge", &ifr)) < 0) return -1; @@ -458,16 +442,13 @@ virNetDevBridgeCreate(const char *brname) if (ioctl(s, SIOCIFCREATE2, &ifr) < 0) { virReportSystemError(errno, "%s", _("Unable to create bridge device")); - goto cleanup; + return -1; } =20 if (virNetDevSetName(ifr.ifr_name, brname) =3D=3D -1) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(s); - return ret; + return 0; } #else int virNetDevBridgeCreate(const char *brname) @@ -490,8 +471,7 @@ int virNetDevBridgeCreate(const char *brname) static int virNetDevBridgeDeleteWithIoctl(const char *brname) { - int fd =3D -1; - int ret =3D -1; + VIR_AUTOCLOSE fd =3D -1; =20 ignore_value(virNetDevSetOnline(brname, false)); =20 @@ -501,14 +481,10 @@ virNetDevBridgeDeleteWithIoctl(const char *brname) if (ioctl(fd, SIOCBRDELBR, brname) < 0) { virReportSystemError(errno, _("Unable to delete bridge %s"), brname); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #endif =20 @@ -541,9 +517,8 @@ virNetDevBridgeDelete(const char *brname) int virNetDevBridgeDelete(const char *brname) { - int s; struct ifreq ifr; - int ret =3D -1; + VIR_AUTOCLOSE s =3D -1; =20 if ((s =3D virNetDevSetupControl(brname, &ifr)) < 0) return -1; @@ -552,13 +527,10 @@ virNetDevBridgeDelete(const char *brname) virReportSystemError(errno, _("Unable to remove bridge %s"), brname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(s); - return ret; + return 0; } #else int virNetDevBridgeDelete(const char *brname ATTRIBUTE_UNUSED) @@ -582,9 +554,8 @@ int virNetDevBridgeDelete(const char *brname ATTRIBUTE_= UNUSED) int virNetDevBridgeAddPort(const char *brname, const char *ifname) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(brname, &ifr)) < 0) return -1; @@ -592,19 +563,16 @@ int virNetDevBridgeAddPort(const char *brname, if (!(ifr.ifr_ifindex =3D if_nametoindex(ifname))) { virReportSystemError(ENODEV, _("Unable to get interface index for %s"), if= name); - goto cleanup; + return -1; } =20 if (ioctl(fd, SIOCBRADDIF, &ifr) < 0) { virReportSystemError(errno, _("Unable to add bridge %s port %s"), brname,= ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #elif defined(HAVE_BSD_BRIDGE_MGMT) int virNetDevBridgeAddPort(const char *brname, @@ -651,9 +619,8 @@ int virNetDevBridgeAddPort(const char *brname, int virNetDevBridgeRemovePort(const char *brname, const char *ifname) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(brname, &ifr)) < 0) return -1; @@ -662,19 +629,16 @@ int virNetDevBridgeRemovePort(const char *brname, virReportSystemError(ENODEV, _("Unable to get interface index for %s"), if= name); =20 - goto cleanup; + return -1; } =20 if (ioctl(fd, SIOCBRDELIF, &ifr) < 0) { virReportSystemError(errno, _("Unable to remove bridge %s port %s"), brna= me, ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #elif defined(HAVE_BSD_BRIDGE_MGMT) int virNetDevBridgeRemovePort(const char *brname, @@ -723,19 +687,14 @@ int virNetDevBridgeRemovePort(const char *brname, int virNetDevBridgeSetSTPDelay(const char *brname, int delay) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(brname, &ifr)) < 0) - goto cleanup; + return -1; =20 - ret =3D virNetDevBridgeSet(brname, "forward_delay", MS_TO_JIFFIES(dela= y), + return virNetDevBridgeSet(brname, "forward_delay", MS_TO_JIFFIES(delay= ), fd, &ifr); - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; } =20 =20 @@ -776,19 +735,14 @@ int virNetDevBridgeGetSTPDelay(const char *brname, int virNetDevBridgeSetSTP(const char *brname, bool enable) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(brname, &ifr)) < 0) - goto cleanup; + return -1; =20 - ret =3D virNetDevBridgeSet(brname, "stp_state", enable ? 1 : 0, + return virNetDevBridgeSet(brname, "stp_state", enable ? 1 : 0, fd, &ifr); - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; } =20 =20 --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 22:24:10 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 15367456333171023.6237791707221; Wed, 12 Sep 2018 02:47:13 -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 DFBAB308625C; Wed, 12 Sep 2018 09:47: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 2E55E60143; Wed, 12 Sep 2018 09:47: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 A5BE7181A12F; Wed, 12 Sep 2018 09:47:09 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w8C9kvf4024887 for ; Wed, 12 Sep 2018 05:46:57 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF4B05C22B; Wed, 12 Sep 2018 09:46:57 +0000 (UTC) Received: from mx1.redhat.com (ext-mx05.extmail.prod.ext.phx2.redhat.com [10.5.110.29]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A34295C22A for ; Wed, 12 Sep 2018 09:46:54 +0000 (UTC) Received: from m6561.mail.qiye.163.com (m6561.mail.qiye.163.com [123.126.65.61]) by mx1.redhat.com (Postfix) with ESMTP id 3627B2C971E for ; Wed, 12 Sep 2018 09:46:51 +0000 (UTC) Received: from localhost.localdomain (unknown [58.56.27.130]) by smtp11 (Coremail) with SMTP id WdOowABXm_WD4Jhbz8LMAQ--.7S7; Wed, 12 Sep 2018 17:46:49 +0800 (CST) From: Shi Lei To: libvir-list@redhat.com Date: Wed, 12 Sep 2018 17:46:38 +0800 Message-Id: <20180912094638.17279-6-shi_lei@massclouds.com> In-Reply-To: <20180912094638.17279-1-shi_lei@massclouds.com> References: <20180912094638.17279-1-shi_lei@massclouds.com> X-CM-TRANSID: WdOowABXm_WD4Jhbz8LMAQ--.7S7 X-Coremail-Antispam: 1Uf129KBjvAXoW3CFWfKFWxGr4kZw43AF1xAFb_yoW8Gr1DKo W7Jr13tFykWw17WFWUJFy7trsIga1Yy3y8Z3Z5JFWqyF4rur95CayxCwsrZa9a9r1Uur4D XF1xXa4agFWDArs7n29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73VFW2AGmfu7bjvjm3 AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjTRPku4UUUUU X-Originating-IP: [58.56.27.130] X-CM-SenderInfo: pvklsz1hl6ztxvvfz0xxgvhudrp/1tbiggq-T1sfl66hPQAAsZ X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 12 Sep 2018 09:46:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Wed, 12 Sep 2018 09:46:52 +0000 (UTC) for IP:'123.126.65.61' DOMAIN:'m6561.mail.qiye.163.com' HELO:'m6561.mail.qiye.163.com' FROM:'shi_lei@massclouds.com' RCPT:'' X-RedHat-Spam-Score: -0.01 (RCVD_IN_DNSWL_NONE) 123.126.65.61 m6561.mail.qiye.163.com 123.126.65.61 m6561.mail.qiye.163.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.29 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCHv2 5/5] util: netdev: use VIR_AUTOCLOSE instead of VIR_FORCE_CLOSE 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]); Wed, 12 Sep 2018 09:47:11 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Signed-off-by: Shi Lei --- src/util/virnetdev.c | 251 +++++++++++++++---------------------------- 1 file changed, 85 insertions(+), 166 deletions(-) diff --git a/src/util/virnetdev.c b/src/util/virnetdev.c index 5d4ad24..a892251 100644 --- a/src/util/virnetdev.c +++ b/src/util/virnetdev.c @@ -200,27 +200,22 @@ virNetDevSetupControl(const char *ifname ATTRIBUTE_UN= USED, */ int virNetDevExists(const char *ifname) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; =20 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) { if (errno =3D=3D ENODEV || errno =3D=3D ENXIO) - ret =3D 0; - else - virReportSystemError(errno, - _("Unable to check interface flags for %s= "), ifname); - goto cleanup; - } + return 0; =20 - ret =3D 1; + virReportSystemError(errno, _("Unable to check interface flags for= %s"), + ifname); + return -1; + } =20 - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 1; } #else int virNetDevExists(const char *ifname) @@ -251,20 +246,20 @@ virNetDevSetMACInternal(const char *ifname, const virMacAddr *macaddr, bool quiet) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; char macstr[VIR_MAC_STRING_BUFLEN]; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; =20 /* To fill ifr.ifr_hdaddr.sa_family field */ if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { - virReportSystemError(errno, - _("Cannot get interface MAC on '%s'"), + virReportSystemError(errno, _("Cannot get interface MAC on '%s'"), ifname); - goto cleanup; + + VIR_DEBUG("SIOCSIFHWADDR %s get MAC - Fail", ifname); + return -1; } =20 virMacAddrGetRaw(macaddr, (unsigned char *)ifr.ifr_hwaddr.sa_data); @@ -272,24 +267,22 @@ virNetDevSetMACInternal(const char *ifname, if (ioctl(fd, SIOCSIFHWADDR, &ifr) < 0) { =20 if (quiet && - (errno =3D=3D EADDRNOTAVAIL || errno =3D=3D EPERM)) - goto cleanup; + (errno =3D=3D EADDRNOTAVAIL || errno =3D=3D EPERM)) { + VIR_DEBUG("SIOCSIFHWADDR %s MAC=3D%s - Fail", + ifname, virMacAddrFormat(macaddr, macstr)); + return -1; + } =20 virReportSystemError(errno, _("Cannot set interface MAC to %s on '%s'"), virMacAddrFormat(macaddr, macstr), ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; + VIR_DEBUG("SIOCSIFHWADDR %s MAC=3D%s - Success", + ifname, virMacAddrFormat(macaddr, macstr)); =20 - cleanup: - VIR_DEBUG("SIOCSIFHWADDR %s MAC=3D%s - %s", - ifname, virMacAddrFormat(macaddr, macstr), - ret < 0 ? "Fail" : "Success"); - - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } =20 =20 @@ -305,8 +298,7 @@ virNetDevSetMACInternal(const char *ifname, struct ifreq ifr; struct sockaddr_dl sdl; char mac[VIR_MAC_STRING_BUFLEN + 1] =3D ":"; - int s; - int ret =3D -1; + VIR_AUTOCLOSE s =3D -1; =20 if ((s =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -320,23 +312,19 @@ virNetDevSetMACInternal(const char *ifname, =20 if (ioctl(s, SIOCSIFLLADDR, &ifr) < 0) { if (quiet && - (errno =3D=3D EADDRNOTAVAIL || errno =3D=3D EPERM)) - goto cleanup; + (errno =3D=3D EADDRNOTAVAIL || errno =3D=3D EPERM)) { + VIR_DEBUG("SIOCSIFLLADDR %s MAC=3D%s - Fail", ifname, mac = + 1); + return -1; + } =20 virReportSystemError(errno, _("Cannot set interface MAC to %s on '%s'= "), mac + 1, ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_DEBUG("SIOCSIFLLADDR %s MAC=3D%s - %s", ifname, mac + 1, - ret < 0 ? "Fail" : "Success"); - - VIR_FORCE_CLOSE(s); - - return ret; + VIR_DEBUG("SIOCSIFLLADDR %s MAC=3D%s - Success", ifname, mac + 1); + return 0; } =20 =20 @@ -379,9 +367,8 @@ virNetDevSetMAC(const char *ifname, int virNetDevGetMAC(const char *ifname, virMacAddrPtr macaddr) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -390,16 +377,12 @@ int virNetDevGetMAC(const char *ifname, virReportSystemError(errno, _("Cannot get interface MAC on '%s'"), ifname); - goto cleanup; + return -1; } =20 virMacAddrSetRaw(macaddr, (unsigned char *)ifr.ifr_hwaddr.sa_data); =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int virNetDevGetMAC(const char *ifname, @@ -424,9 +407,8 @@ int virNetDevGetMAC(const char *ifname, */ int virNetDevGetMTU(const char *ifname) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -435,14 +417,10 @@ int virNetDevGetMTU(const char *ifname) virReportSystemError(errno, _("Cannot get interface MTU on '%s'"), ifname); - goto cleanup; + return -1; } =20 - ret =3D ifr.ifr_mtu; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return ifr.ifr_mtu; } #else int virNetDevGetMTU(const char *ifname) @@ -468,9 +446,8 @@ int virNetDevGetMTU(const char *ifname) */ int virNetDevSetMTU(const char *ifname, int mtu) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -481,14 +458,10 @@ int virNetDevSetMTU(const char *ifname, int mtu) virReportSystemError(errno, _("Cannot set interface MTU on '%s'"), ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int virNetDevSetMTU(const char *ifname, int mtu ATTRIBUTE_UNUSED) @@ -592,9 +565,8 @@ int virNetDevSetNamespace(const char *ifname, pid_t pid= InNs) */ int virNetDevSetName(const char* ifname, const char *newifname) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -604,7 +576,7 @@ int virNetDevSetName(const char* ifname, const char *ne= wifname) virReportSystemError(ERANGE, _("Network interface name '%s' is too long"), newifname); - goto cleanup; + return -1; } # else ifr.ifr_data =3D (caddr_t)newifname; @@ -614,14 +586,10 @@ int virNetDevSetName(const char* ifname, const char *= newifname) virReportSystemError(errno, _("Unable to rename '%s' to '%s'"), ifname, newifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int virNetDevSetName(const char* ifname, const char *newifname) @@ -638,10 +606,9 @@ int virNetDevSetName(const char* ifname, const char *n= ewifname) static int virNetDevSetIFFlag(const char *ifname, int flag, bool val) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; int ifflags; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -650,7 +617,7 @@ virNetDevSetIFFlag(const char *ifname, int flag, bool v= al) virReportSystemError(errno, _("Cannot get interface flags on '%s'"), ifname); - goto cleanup; + return -1; } =20 if (val) @@ -664,15 +631,11 @@ virNetDevSetIFFlag(const char *ifname, int flag, bool= val) virReportSystemError(errno, _("Cannot set interface flags on '%s'"), ifname); - goto cleanup; + return -1; } } =20 - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else static int @@ -765,9 +728,8 @@ virNetDevSetRcvAllMulti(const char *ifname, static int virNetDevGetIFFlag(const char *ifname, int flag, bool *val) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -776,15 +738,11 @@ virNetDevGetIFFlag(const char *ifname, int flag, bool= *val) virReportSystemError(errno, _("Cannot get interface flags on '%s'"), ifname); - goto cleanup; + return -1; } =20 *val =3D (ifr.ifr_flags & flag) ? true : false; - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else static int @@ -909,9 +867,8 @@ char *virNetDevGetName(int ifindex) #if defined(SIOCGIFINDEX) && defined(HAVE_STRUCT_IFREQ) int virNetDevGetIndex(const char *ifname, int *ifindex) { - int ret =3D -1; struct ifreq ifreq; - int fd =3D socket(VIR_NETDEV_FAMILY, SOCK_DGRAM, 0); + VIR_AUTOCLOSE fd =3D socket(VIR_NETDEV_FAMILY, SOCK_DGRAM, 0); =20 if (fd < 0) { virReportSystemError(errno, "%s", @@ -925,13 +882,13 @@ int virNetDevGetIndex(const char *ifname, int *ifinde= x) virReportSystemError(ERANGE, _("invalid interface name %s"), ifname); - goto cleanup; + return -1; } =20 if (ioctl(fd, SIOCGIFINDEX, &ifreq) < 0) { virReportSystemError(errno, _("Unable to get index for interface %s"), if= name); - goto cleanup; + return -1; } =20 # ifdef HAVE_STRUCT_IFREQ_IFR_INDEX @@ -939,11 +896,7 @@ int virNetDevGetIndex(const char *ifname, int *ifindex) # else *ifindex =3D ifreq.ifr_ifindex; # endif - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else /* ! SIOCGIFINDEX */ int virNetDevGetIndex(const char *ifname ATTRIBUTE_UNUSED, @@ -1013,8 +966,7 @@ int virNetDevGetVLanID(const char *ifname, int *vlanid) struct vlan_ioctl_args vlanargs =3D { .cmd =3D GET_VLAN_VID_CMD, }; - int ret =3D -1; - int fd =3D socket(PF_PACKET, SOCK_DGRAM, 0); + VIR_AUTOCLOSE fd =3D socket(PF_PACKET, SOCK_DGRAM, 0); =20 if (fd < 0) { virReportSystemError(errno, "%s", @@ -1026,22 +978,17 @@ int virNetDevGetVLanID(const char *ifname, int *vlan= id) virReportSystemError(ERANGE, _("invalid interface name %s"), ifname); - goto cleanup; + return -1; } =20 if (ioctl(fd, SIOCGIFVLAN, &vlanargs) !=3D 0) { virReportSystemError(errno, _("Unable to get VLAN for interface %s"), ifn= ame); - goto cleanup; + return -1; } =20 *vlanid =3D vlanargs.u.VID; - ret =3D 0; - - cleanup: - VIR_FORCE_CLOSE(fd); - - return ret; + return 0; } #else /* ! SIOCGIFVLAN */ int virNetDevGetVLanID(const char *ifname ATTRIBUTE_UNUSED, @@ -1070,55 +1017,43 @@ int virNetDevGetVLanID(const char *ifname ATTRIBUTE= _UNUSED, int virNetDevValidateConfig(const char *ifname, const virMacAddr *macaddr, int ifindex) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; int idx; int rc; + VIR_AUTOCLOSE fd =3D -1; =20 if ((rc =3D virNetDevExists(ifname)) < 0) return -1; - if (rc =3D=3D 0) { - ret =3D 0; - goto cleanup; - } + if (rc =3D=3D 0) + return 0; =20 if (macaddr !=3D NULL) { if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; =20 if (ioctl(fd, SIOCGIFHWADDR, &ifr) < 0) { - if (errno =3D=3D ENODEV) { - ret =3D 0; - goto cleanup; - } + if (errno =3D=3D ENODEV) + return 0; + virReportSystemError(errno, _("could not get MAC address of interface= %s"), ifname); - goto cleanup; + return -1; } =20 if (virMacAddrCmpRaw(macaddr, - (unsigned char *)ifr.ifr_hwaddr.sa_data) !=3D= 0) { - ret =3D 0; - goto cleanup; - } + (unsigned char *)ifr.ifr_hwaddr.sa_data) !=3D= 0) + return 0; } =20 if (ifindex !=3D -1) { if (virNetDevGetIndex(ifname, &idx) < 0) - goto cleanup; - if (idx !=3D ifindex) { - ret =3D 0; - goto cleanup; - } + return -1; + if (idx !=3D ifindex) + return 0; } =20 - ret =3D 1; - - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 1; } #else int virNetDevValidateConfig(const char *ifname ATTRIBUTE_UNUSED, @@ -2649,9 +2584,8 @@ virNetDevGetLinkInfo(const char *ifname, int virNetDevAddMulti(const char *ifname, virMacAddrPtr macaddr) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -2664,13 +2598,10 @@ int virNetDevAddMulti(const char *ifname, virReportSystemError(errno, _("Cannot add multicast MAC %s on '%s' interf= ace"), virMacAddrFormat(macaddr, macstr), ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int virNetDevAddMulti(const char *ifname ATTRIBUTE_UNUSED, @@ -2698,9 +2629,8 @@ int virNetDevAddMulti(const char *ifname ATTRIBUTE_UN= USED, int virNetDevDelMulti(const char *ifname, virMacAddrPtr macaddr) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; + VIR_AUTOCLOSE fd =3D -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) return -1; @@ -2713,13 +2643,10 @@ int virNetDevDelMulti(const char *ifname, virReportSystemError(errno, _("Cannot add multicast MAC %s on '%s' interf= ace"), virMacAddrFormat(macaddr, macstr), ifname); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int virNetDevDelMulti(const char *ifname ATTRIBUTE_UNUSED, @@ -3388,10 +3315,9 @@ int virNetDevSetCoalesce(const char *ifname, virNetDevCoalescePtr coalesce, bool update) { - int fd =3D -1; - int ret =3D -1; struct ifreq ifr; struct ethtool_coalesce coal =3D {0}; + VIR_AUTOCLOSE fd =3D -1; =20 if (!coalesce && !update) return 0; @@ -3433,7 +3359,7 @@ int virNetDevSetCoalesce(const char *ifname, virReportSystemError(errno, _("Cannot set coalesce info on '%s'"), ifname); - goto cleanup; + return -1; } =20 if (coalesce) { @@ -3467,10 +3393,7 @@ int virNetDevSetCoalesce(const char *ifname, } } =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } # else int virNetDevSetCoalesce(const char *ifname, @@ -3503,30 +3426,26 @@ virNetDevGetFeatures(const char *ifname, virBitmapPtr *out) { struct ifreq ifr; - int ret =3D -1; - int fd =3D -1; + VIR_AUTOCLOSE fd =3D -1; =20 if (!(*out =3D virBitmapNew(VIR_NET_DEV_FEAT_LAST))) return -1; =20 if ((fd =3D virNetDevSetupControl(ifname, &ifr)) < 0) - goto cleanup; + return -1; =20 virNetDevGetEthtoolFeatures(*out, fd, &ifr); =20 if (virNetDevGetEthtoolGFeatures(*out, fd, &ifr) < 0) - goto cleanup; + return -1; =20 if (virNetDevRDMAFeature(ifname, out) < 0) - goto cleanup; + return -1; =20 if (virNetDevSwitchdevFeature(ifname, out) < 0) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FORCE_CLOSE(fd); - return ret; + return 0; } #else int --=20 2.17.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list