Use defer {} instead of DEFER()
authorJagger <robert@swiecki.net>
Sat, 23 Apr 2016 02:22:31 +0000 (04:22 +0200)
committerJagger <robert@swiecki.net>
Sat, 23 Apr 2016 02:22:31 +0000 (04:22 +0200)
common.h
contain.c
net.c
nsjail.c
subproc.c
util.c

index 3c3e396510b447b495d677fdfd1e5e024ed6483d..e400960c192c7bcd40b83498fe82124e83b7affb 100644 (file)
--- a/common.h
+++ b/common.h
 #define _STRMERGE(a, b) __STRMERGE(a, b)
 
 #ifdef __clang__
-static void __attribute__ ((unused)) _clang_cleanup_func(void (^*dfunc) (void))
+static void __attribute__ ((unused)) __clang_cleanup_func(void (^*dfunc) (void))
 {
        (*dfunc) ();
 }
 
-#define DEFER(a) void (^_STRMERGE(__defer_f_, __COUNTER__))(void) __attribute__((cleanup(_clang_cleanup_func))) __attribute__((unused)) = ^{ a; }
+#define defer void (^_STRMERGE(__defer_f_, __COUNTER__))(void) __attribute__((cleanup(__clang_cleanup_func))) __attribute__((unused)) = ^
 #else
 #define __block
-#define _DEFER(a, count) void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused))) { a; } ; \
-    int _STRMERGE(_defer_var_, count) __attribute__((cleanup(_STRMERGE(__defer_f_, count)))) __attribute__((unused))
-#define DEFER(a) _DEFER(a, __COUNTER__)
+#define _DEFER(a, count) \
+    auto void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused))); \
+    int _STRMERGE(__defer_var_, count) __attribute__((cleanup(_STRMERGE(__defer_f_, count)))) __attribute__((unused)); \
+    void _STRMERGE(__defer_f_, count)(void *_defer_arg __attribute__((unused)))
+#define defer _DEFER(a, __COUNTER__)
 #endif
 
 struct pids_t {
index a77d9662498b36e8498f6b8b8fa677a23e2ac737..fdff21ec8fe6fe6a128db6056217d15b56ae1673 100644 (file)
--- a/contain.c
+++ b/contain.c
@@ -204,7 +204,9 @@ static bool containMakeFdsCOEProc(void)
                PLOG_D("opendir('/proc/self/fd')");
                return false;
        }
-       DEFER(closedir(dir));
+       defer {
+               closedir(dir);
+       };
        for (;;) {
                errno = 0;
                struct dirent *entry = readdir(dir);
diff --git a/net.c b/net.c
index c1c0acbc9206ffdb91500c11c8cc08ef8b0e5990..4f8aebbf7689018cc571dfb5305feef59938e2b7 100644 (file)
--- a/net.c
+++ b/net.c
@@ -62,7 +62,9 @@ bool netInitNsFromParent(struct nsjconf_t * nsjconf, int pid)
                LOG_E("Could not allocate socket with nl_socket_alloc()");
                return false;
        }
-       DEFER(nl_socket_free(sk));
+       defer {
+               nl_socket_free(sk);
+       };
 
        int err;
        if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
@@ -75,14 +77,18 @@ bool netInitNsFromParent(struct nsjconf_t * nsjconf, int pid)
                LOG_E("rtnl_link_macvlan_alloc(): %s", nl_geterror(err));
                return false;
        }
-       DEFER(rtnl_link_put(rmv));
+       defer {
+               rtnl_link_put(rmv);
+       };
 
        struct nl_cache *link_cache;
        if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
                LOG_E("rtnl_link_alloc_cache(): %s", nl_geterror(err));
                return false;
        }
-       DEFER(nl_cache_free(link_cache));
+       defer {
+               nl_cache_free(link_cache);
+       };
 
        int master_index = rtnl_link_name2i(link_cache, nsjconf->iface);
        if (master_index == 0) {
@@ -327,7 +333,9 @@ static bool netIfaceUp(const char *ifacename)
                PLOG_E("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
                return false;
        }
-       DEFER(close(sock));
+       defer {
+               close(sock);
+       };
 
        struct ifreq ifr;
        memset(&ifr, '\0', sizeof(ifr));
@@ -360,7 +368,9 @@ static bool netConfigureVs(struct nsjconf_t *nsjconf)
                PLOG_E("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
                return false;
        }
-       DEFER(close(sock));
+       defer {
+               close(sock);
+       };
 
        if (inet_pton(AF_INET, nsjconf->iface_vs_ip, &addr) != 1) {
                PLOG_E("Cannot convert '%s' into an IPv4 address", nsjconf->iface_vs_ip);
index c135be9db7358ca0a2543cba68a3d81cee5feb96..4f03d9c17601843d8108bc242c2e3c12cb3b7549 100644 (file)
--- a/nsjail.c
+++ b/nsjail.c
@@ -115,7 +115,9 @@ static void nsjailListenMode(struct nsjconf_t *nsjconf)
        if (listenfd == -1) {
                return;
        }
-       DEFER(close(listenfd));
+       defer {
+               close(listenfd);
+       };
        for (;;) {
                if (nsjailSigFatal > 0) {
                        subprocKillAll(nsjconf);
index 1739460e61fb013e01e640e3ba3e2e6b74a2b59f..d96df89da3fe8e9d7f1a18debb6f6547e8b8f14d 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -248,7 +248,9 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
                return;
        }
        int subproc_sock = sv[1];
-       DEFER(close(subproc_sock));
+       defer {
+               close(subproc_sock);
+       };
 
        pid_t pid = syscall(__NR_clone, (uintptr_t) flags, NULL, NULL, NULL, (uintptr_t) 0);
        if (pid == 0) {
diff --git a/util.c b/util.c
index 78541e925f35a068df93fe9e25ddd9acc69da422..a4e5c5c2c636e1c00ed1325e48a61530cc3fb0a5 100644 (file)
--- a/util.c
+++ b/util.c
@@ -82,7 +82,9 @@ bool utilWriteBufToFile(char *filename, const void *buf, size_t len, int open_fl
                PLOG_E("Couldn't open '%s' for R/O", filename);
                return false;
        }
-       DEFER(close(fd));
+       defer {
+               close(fd);
+       };
 
        if (utilWriteToFd(fd, buf, len) == false) {
                PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);