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 | 20 ++++++++++++++++++++
libvirt-utils.h | 7 +++++++
2 files changed, 27 insertions(+)
diff --git a/libvirt-utils.c b/libvirt-utils.c
index 3d9bb9a..9c29a1c 100644
--- a/libvirt-utils.c
+++ b/libvirt-utils.c
@@ -22,6 +22,7 @@
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
+#include <unistd.h>
#include "libvirt-utils.h"
/**
@@ -118,3 +119,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.1