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
---
daemon/checksum.c | 8 +++-----
1 file changed, 3 insertions(+), 5 deletions(-)
diff --git a/daemon/checksum.c b/daemon/checksum.c
index 019c04c..6ec48c0 100644
--- a/daemon/checksum.c
+++ b/daemon/checksum.c
@@ -72,10 +72,8 @@ checksum (const char *csumtype, int fd)
size_t len;
program = program_of_csum (csumtype);
- if (program == NULL) {
- close (fd);
+ if (program == NULL)
return NULL;
- }
pulse_mode_start ();
@@ -100,7 +98,7 @@ checksum (const char *csumtype, int fd)
char *
do_checksum (const char *csumtype, const char *path)
{
- int fd;
+ CLEANUP_CLOSE int fd = -1;
CHROOT_IN;
fd = open (path, O_RDONLY|O_CLOEXEC);
@@ -117,7 +115,7 @@ do_checksum (const char *csumtype, const char *path)
char *
do_checksum_device (const char *csumtype, const char *device)
{
- int fd;
+ CLEANUP_CLOSE int fd = -1;
fd = open (device, O_RDONLY|O_CLOEXEC);
if (fd == -1) {
--
1.9.3
Richard W.M. Jones
2014-Aug-08 18:30 UTC
Re: [Libguestfs] [PATCH] daemon: add CLEANUP_CLOSE
On Fri, Aug 08, 2014 at 04:03:52PM +0200, Pino Toscano wrote:> 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); > +} > --ACK. I needed this -- I don't at the moment remember what for, but it might have been for an earlier version of qemu-speed-test. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com Fedora Windows cross-compiler. Compile Windows programs, test, and build Windows installers. Over 100 libraries supported. http://fedoraproject.org/wiki/MinGW
Richard W.M. Jones
2014-Aug-08 18:32 UTC
Re: [Libguestfs] [PATCH] daemon: use CLEANUP_CLOSE
On Fri, Aug 08, 2014 at 04:03:53PM +0200, Pino Toscano wrote:> --- > daemon/checksum.c | 8 +++----- > 1 file changed, 3 insertions(+), 5 deletions(-) > > diff --git a/daemon/checksum.c b/daemon/checksum.c > index 019c04c..6ec48c0 100644 > --- a/daemon/checksum.c > +++ b/daemon/checksum.c > @@ -72,10 +72,8 @@ checksum (const char *csumtype, int fd) > size_t len; > > program = program_of_csum (csumtype); > - if (program == NULL) { > - close (fd); > + if (program == NULL) > return NULL; > - } > > pulse_mode_start (); > > @@ -100,7 +98,7 @@ checksum (const char *csumtype, int fd) > char * > do_checksum (const char *csumtype, const char *path) > { > - int fd; > + CLEANUP_CLOSE int fd = -1; > > CHROOT_IN; > fd = open (path, O_RDONLY|O_CLOEXEC); > @@ -117,7 +115,7 @@ do_checksum (const char *csumtype, const char *path) > char * > do_checksum_device (const char *csumtype, const char *device) > { > - int fd; > + CLEANUP_CLOSE int fd = -1; > > fd = open (device, O_RDONLY|O_CLOEXEC); > if (fd == -1) {Makes sense, ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top