From 78587cc3c6337c310661a445aca912bcb5abf5f3 Mon Sep 17 00:00:00 2001 From: "Richard W.M. Jones" Date: Thu, 8 Jun 2017 17:19:23 +0100 Subject: [PATCH] common/utils: cleanups: Add CLEANUP_CLOSE function. 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 cb488d9e6..0a5a012b4 100644 --- a/common/utils/cleanups.c +++ b/common/utils/cleanups.c @@ -84,6 +84,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) { diff --git a/common/utils/cleanups.h b/common/utils/cleanups.h index 488ed5642..c3a5c496d 100644 --- a/common/utils/cleanups.h +++ b/common/utils/cleanups.h @@ -28,6 +28,8 @@ __attribute__((cleanup(guestfs_int_cleanup_gl_recursive_lock_unlock))) #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 \ @@ -53,6 +55,7 @@ #define CLEANUP_HASH_FREE /* XXX no safe equivalent to CLEANUP_GL_RECURSIVE_LOCK_UNLOCK */ #define CLEANUP_UNLINK_FREE +#define CLEANUP_CLOSE #define CLEANUP_FCLOSE #define CLEANUP_PCLOSE #define CLEANUP_FREE_STRING_LIST @@ -72,6 +75,7 @@ extern void guestfs_int_cleanup_free (void *ptr); extern void guestfs_int_cleanup_hash_free (void *ptr); extern void guestfs_int_cleanup_gl_recursive_lock_unlock (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);