@@ -968,7 +968,7 @@ static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc,
{
char ifname[IF_NAMESIZE];
_auto_(l_free) char *filename = NULL;
- int fd;
+ _auto_(close) int fd = -1;
int r;
if (unlikely(!if_indextoname(nc->ifindex, ifname)))
@@ -982,8 +982,7 @@ static int netconfig_proc_write_ipv6_setting(struct l_netconfig *nc,
return -errno;
r = L_TFR(write(fd, value, strlen(value)));
- L_TFR(close(fd));
- return r;
+ return r > 0 ? 0 : -errno;
}
LIB_EXPORT struct l_netconfig *l_netconfig_new(uint32_t ifindex)
@@ -20,6 +20,11 @@
*
*/
+#include <unistd.h>
+#include <errno.h>
+
+#include <ell/util.h>
+
#define align_len(len, boundary) (((len)+(boundary)-1) & ~((boundary)-1))
#define likely(x) __builtin_expect(!!(x), 1)
@@ -65,6 +70,15 @@ static inline unsigned char bit_field(const unsigned char oct,
#define _auto_(func) \
__AUTODESTRUCT(func)
+/* Enables declaring _auto_(close) int fd = <-1 or L_TFR(open(...))>; */
+inline __attribute__((always_inline)) void close_cleanup(void *p)
+{
+ int fd = *(int *) p;
+
+ if (fd >= 0)
+ L_TFR(close(fd));
+}
+
/*
* Trick the compiler into thinking that var might be changed somehow by
* the asm