[Libguestfs] [PATCH] daemon: add CLEANUP_CLOSE

Pino Toscano ptoscano at redhat.com
Fri Aug 8 14:03:52 UTC 2014


Just call close on the specified fd, if valid.
---
 daemon/daemon.h   | 3 +++
 daemon/guestfsd.c | 9 +++++++++
 2 files changed, 12 insertions(+)

diff --git a/daemon/daemon.h b/daemon/daemon.h
index fb74e91..0caad45 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -173,6 +173,7 @@ asprintf_nowarn (char **strp, const char *fmt, ...)
 extern void cleanup_free (void *ptr);
 extern void cleanup_free_string_list (void *ptr);
 extern void cleanup_unlink_free (void *ptr);
+extern void cleanup_close (void *ptr);
 
 /*-- in names.c (auto-generated) --*/
 extern const char *function_names[];
@@ -405,10 +406,12 @@ is_zero (const char *buffer, size_t size)
 #define CLEANUP_FREE_STRING_LIST                        \
     __attribute__((cleanup(cleanup_free_string_list)))
 #define CLEANUP_UNLINK_FREE __attribute__((cleanup(cleanup_unlink_free)))
+#define CLEANUP_CLOSE __attribute__((cleanup(cleanup_close)))
 #else
 #define CLEANUP_FREE
 #define CLEANUP_FREE_STRING_LIST
 #define CLEANUP_UNLINK_FREE
+#define CLEANUP_CLOSE
 #endif
 
 #endif /* GUESTFSD_DAEMON_H */
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 622bda1..0a59c84 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -1482,3 +1482,12 @@ cleanup_unlink_free (void *ptr)
     free (filename);
   }
 }
+
+void
+cleanup_close (void *ptr)
+{
+  int fd = * (int *) ptr;
+
+  if (fd >= 0)
+    close (fd);
+}
-- 
1.9.3




More information about the Libguestfs mailing list