From: Christian Brauner Date: Sun, 17 Feb 2019 13:31:52 +0000 (+0100) Subject: memory_utils: introduce __do_close_prot_errno X-Git-Tag: lxc-3.2.0~142^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e236fe05d1e7a615c6a198a5fd0b5db0631484d4;p=thirdparty%2Flxc.git memory_utils: introduce __do_close_prot_errno Signed-off-by: Christian Brauner --- diff --git a/src/lxc/memory_utils.h b/src/lxc/memory_utils.h index d42babfd8..fdcfb5530 100644 --- a/src/lxc/memory_utils.h +++ b/src/lxc/memory_utils.h @@ -26,6 +26,7 @@ #include #include #include +#include static inline void __auto_free__(void *p) { @@ -44,6 +45,16 @@ static inline void __auto_closedir__(DIR **d) closedir(*d); } +static inline void __auto_close__(int *fd) +{ + if (*fd >= 0) { + int e = errno; + close(*fd); + errno = e; + } +} + +#define __do_close_prot_errno __attribute__((__cleanup__(__auto_close__))) #define __do_free __attribute__((__cleanup__(__auto_free__))) #define __do_fclose __attribute__((__cleanup__(__auto_fclose__))) #define __do_closedir __attribute__((__cleanup__(__auto_closedir__)))