From nobody Mon Feb 9 02:51:19 2026 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