render / rpms / libvirt

Forked from rpms/libvirt 9 months ago
Clone
c480ed
From 3d08d15b8fb214233e6b426bffe8f8b89969529a Mon Sep 17 00:00:00 2001
c480ed
Message-Id: <3d08d15b8fb214233e6b426bffe8f8b89969529a@dist-git>
c480ed
From: Shi Lei <shi_lei@massclouds.com>
c480ed
Date: Fri, 21 Jun 2019 09:25:42 +0200
c480ed
Subject: [PATCH] util: file: introduce VIR_AUTOCLOSE macro to close fd of the
c480ed
 file automatically
c480ed
MIME-Version: 1.0
c480ed
Content-Type: text/plain; charset=UTF-8
c480ed
Content-Transfer-Encoding: 8bit
c480ed
c480ed
Signed-off-by: Shi Lei <shi_lei@massclouds.com>
c480ed
(cherry picked from commit 09d35afd2c3058688290d5d818343d0f6aa2dd6e)
c480ed
c480ed
https://bugzilla.redhat.com/show_bug.cgi?id=1697627
c480ed
c480ed
Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
c480ed
Message-Id: <93cb7516e852b96a82eac8dd71acdccc81ef13f9.1561068591.git.jdenemar@redhat.com>
c480ed
Reviewed-by: Ján Tomko <jtomko@redhat.com>
c480ed
---
c480ed
 src/util/virfile.h | 18 ++++++++++++++++--
c480ed
 1 file changed, 16 insertions(+), 2 deletions(-)
c480ed
c480ed
diff --git a/src/util/virfile.h b/src/util/virfile.h
c480ed
index 51c221e069..fa03269289 100644
c480ed
--- a/src/util/virfile.h
c480ed
+++ b/src/util/virfile.h
c480ed
@@ -53,6 +53,11 @@ int virFileClose(int *fdptr, virFileCloseFlags flags)
c480ed
 int virFileFclose(FILE **file, bool preserve_errno) ATTRIBUTE_RETURN_CHECK;
c480ed
 FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
c480ed
 
c480ed
+static inline void virForceCloseHelper(int *fd)
c480ed
+{
c480ed
+    ignore_value(virFileClose(fd, VIR_FILE_CLOSE_PRESERVE_ERRNO));
c480ed
+}
c480ed
+
c480ed
 /* For use on normal paths; caller must check return value,
c480ed
    and failure sets errno per close. */
c480ed
 # define VIR_CLOSE(FD) virFileClose(&(FD), 0)
c480ed
@@ -63,8 +68,7 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
c480ed
 
c480ed
 /* For use on cleanup paths; errno is unaffected by close,
c480ed
    and no return value to worry about. */
c480ed
-# define VIR_FORCE_CLOSE(FD) \
c480ed
-    ignore_value(virFileClose(&(FD), VIR_FILE_CLOSE_PRESERVE_ERRNO))
c480ed
+# define VIR_FORCE_CLOSE(FD) virForceCloseHelper(&(FD))
c480ed
 # define VIR_FORCE_FCLOSE(FILE) ignore_value(virFileFclose(&(FILE), true))
c480ed
 
c480ed
 /* Similar VIR_FORCE_CLOSE() but ignores EBADF errors since they are expected
c480ed
@@ -79,6 +83,16 @@ FILE *virFileFdopen(int *fdptr, const char *mode) ATTRIBUTE_RETURN_CHECK;
c480ed
                  VIR_FILE_CLOSE_PRESERVE_ERRNO | \
c480ed
                  VIR_FILE_CLOSE_DONT_LOG))
c480ed
 
c480ed
+/**
c480ed
+ * VIR_AUTOCLOSE:
c480ed
+ *
c480ed
+ * Macro to automatically force close the fd by calling virForceCloseHelper
c480ed
+ * when the fd goes out of scope. It's used to eliminate VIR_FORCE_CLOSE
c480ed
+ * in cleanup sections.
c480ed
+ */
c480ed
+# define VIR_AUTOCLOSE __attribute__((cleanup(virForceCloseHelper))) int
c480ed
+
c480ed
+
c480ed
 /* Opaque type for managing a wrapper around a fd.  */
c480ed
 struct _virFileWrapperFd;
c480ed
 
c480ed
-- 
c480ed
2.22.0
c480ed