diff options
author | Daniel P. Berrange <berrange@redhat.com> | 2013-11-12 18:21:54 +0000 |
---|---|---|
committer | Daniel P. Berrange <berrange@redhat.com> | 2013-11-22 14:44:48 +0000 |
commit | 78b1835889a5c634bb10d9328bf387c6ef5546a8 (patch) | |
tree | 168c2fdb40a129e8b77ca920642fec82a10b8f6e | |
parent | b11c05fa6ffe3c41bf6662136caae755e7bfbed6 (diff) | |
download | libvirt-python-v8-78b1835889a5c634bb10d9328bf387c6ef5546a8.tar.gz libvirt-python-v8-78b1835889a5c634bb10d9328bf387c6ef5546a8.tar.xz libvirt-python-v8-78b1835889a5c634bb10d9328bf387c6ef5546a8.zip |
Import VIR_FORCE_CLOSE macro from libvirt
Import the macro for safely closing file descriptors
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
-rw-r--r-- | libvirt-utils.c | 20 | ||||
-rw-r--r-- | libvirt-utils.h | 7 |
2 files changed, 27 insertions, 0 deletions
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__ */ |