[libvirt-python PATCHv2 11/44] Import VIR_FORCE_CLOSE macro from libvirt

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: "Daniel P. Berrange" <berrange@xxxxxxxxxx>

Import the macro for safely closing file descriptors

Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx>
---
 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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]