This is present in the daemon, but the function could be used
throughout the code.
---
common/utils/cleanups.c | 9 +++++++++
common/utils/cleanups.h | 4 ++++
2 files changed, 13 insertions(+)
diff --git a/common/utils/cleanups.c b/common/utils/cleanups.c
index c9a34c2ef..47da587bb 100644
--- a/common/utils/cleanups.c
+++ b/common/utils/cleanups.c
@@ -87,6 +87,15 @@ guestfs_int_cleanup_unlink_free (char **ptr)
}
void
+guestfs_int_cleanup_close (void *ptr)
+{
+ const int fd = * (int *) ptr;
+
+ if (fd >= 0)
+ close (fd);
+}
+
+void
guestfs_int_cleanup_fclose (void *ptr)
{
FILE *f = * (FILE **) ptr;
diff --git a/common/utils/cleanups.h b/common/utils/cleanups.h
index df62cafd6..75df0074c 100644
--- a/common/utils/cleanups.h
+++ b/common/utils/cleanups.h
@@ -26,6 +26,8 @@
__attribute__((cleanup(guestfs_int_cleanup_hash_free)))
#define CLEANUP_UNLINK_FREE \
__attribute__((cleanup(guestfs_int_cleanup_unlink_free)))
+#define CLEANUP_CLOSE \
+ __attribute__((cleanup(guestfs_int_cleanup_close)))
#define CLEANUP_FCLOSE \
__attribute__((cleanup(guestfs_int_cleanup_fclose)))
#define CLEANUP_PCLOSE \
@@ -50,6 +52,7 @@
#define CLEANUP_FREE
#define CLEANUP_HASH_FREE
#define CLEANUP_UNLINK_FREE
+#define CLEANUP_CLOSE
#define CLEANUP_FCLOSE
#define CLEANUP_PCLOSE
#define CLEANUP_FREE_STRING_LIST
@@ -68,6 +71,7 @@
extern void guestfs_int_cleanup_free (void *ptr);
extern void guestfs_int_cleanup_hash_free (void *ptr);
extern void guestfs_int_cleanup_unlink_free (char **ptr);
+extern void guestfs_int_cleanup_close (void *ptr);
extern void guestfs_int_cleanup_fclose (void *ptr);
extern void guestfs_int_cleanup_pclose (void *ptr);
extern void guestfs_int_cleanup_free_string_list (char ***ptr);
--
2.13.0