On Tue, Nov 19, 2013 at 1:35 PM, Doug Goldstein <cardoe(a)cardoe.com> wrote:
From: "Daniel P. Berrange" <berrange(a)redhat.com>
Import the macro for safely closing file descriptors
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
libvirt-utils.c | 19 +++++++++++++++++++
libvirt-utils.h | 7 +++++++
2 files changed, 26 insertions(+)
diff --git a/libvirt-utils.c b/libvirt-utils.c
index 873552c..6866bf1 100644
--- a/libvirt-utils.c
+++ b/libvirt-utils.c
@@ -115,3 +115,22 @@ void virFree(void *ptrptr)
*(void**)ptrptr = NULL;
errno = save_errno;
}
+
+
+int virFileClose(int *fdptr)
+{
+ int saved_errno = 0;
+ int rc = 0;
+
+ saved_errno = errno;
+
+ if (*fdptr < 0)
+ return 0;
+
+ rc = close(*fdptr);
+ *fdptr = -1;
+
+ errno = saved_errno;
+
+ return rc;
+}
diff --git a/libvirt-utils.h b/libvirt-utils.h
index 30380a3..795f678 100644
--- a/libvirt-utils.h
+++ b/libvirt-utils.h
@@ -183,4 +183,11 @@ void virFree(void *ptrptr) ATTRIBUTE_NONNULL(1);
# define VIR_FREE(ptr) virFree((void *) &(ptr))
# endif
+/* Don't call this directly - use the macro below */
+int virFileClose(int *fdptr)
+ ATTRIBUTE_RETURN_CHECK;
+
+# define VIR_FORCE_CLOSE(FD) \
+ ignore_value(virFileClose(&(FD)))
+
#endif /* __LIBVIRT_UTILS_H__ */
--
1.8.3.2
Rather than repost the whole series since we're still a work in
progress. I had to add #include <unistd.h> in the headers above for
this.
--
Doug Goldstein