Cleaner impl. of DEFER
authorJagger <robert@swiecki.net>
Thu, 10 Mar 2016 21:56:26 +0000 (22:56 +0100)
committerJagger <robert@swiecki.net>
Thu, 10 Mar 2016 21:56:26 +0000 (22:56 +0100)
Makefile
common.h
contain.c
net.c
nsjail.c
subproc.c
util.c

index 3d8073628bdb8a503bf0b96deb599b3fcaa23e21..cd5a64fd94d6933518dfabda319acc7db250fd2c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -68,11 +68,11 @@ nsjail.o: nsjail.h common.h cmdline.h log.h net.h subproc.h
 cmdline.o: cmdline.h common.h log.h util.h
 contain.o: contain.h common.h log.h mount.h net.h util.h uts.h
 log.o: log.h common.h
-net.o: net.h common.h log.h
 mount.o: mount.h common.h log.h
-user.o: user.h common.h log.h util.h
-subproc.o: subproc.h common.h contain.h log.h net.h sandbox.h user.h util.h
+net.o: net.h common.h log.h
 sandbox.o: sandbox.h common.h log.h seccomp/bpf-helper.h
+subproc.o: subproc.h common.h contain.h log.h net.h sandbox.h user.h util.h
+user.o: user.h common.h log.h util.h
 util.o: util.h common.h log.h
 uts.o: uts.h common.h log.h
 seccomp/bpf-helper.o: seccomp/bpf-helper.h
index a32bd6110ac6f5198757c1b5a958ea959558bd05..4de570a40201c58076c0b3a9bd775be3ff284616 100644 (file)
--- a/common.h
+++ b/common.h
@@ -31,6 +31,7 @@
 
 #define ARRAYSIZE(array) (sizeof(array) / sizeof(*array))
 
+/* Go-style defer implementation */
 #define __STRMERGE(a, b) a##b
 #define _STRMERGE(a, b) __STRMERGE(a, b)
 
@@ -40,10 +41,12 @@ static void __attribute__ ((unused)) _clang_cleanup_func(void (^*dfunc) (void))
        (*dfunc) ();
 }
 
-#define defer(a) void (^_STRMERGE(__df_, __COUNTER__))(void) __attribute__((cleanup(_clang_cleanup_func))) __attribute__((unused)) = ^{ a; }
+#define DEFER(a) void (^_STRMERGE(__defer_f_, __COUNTER__))(void) __attribute__((cleanup(_clang_cleanup_func))) __attribute__((unused)) = ^{ a; }
 #else
 #define __block
-#define defer(a) void _STRMERGE(_cleanup_func_, __LINE__)(void *_STRMERGE(_cleanup_unused_, __LINE__) __attribute__((unused))) { a; } ; int _STRMERGE(_cleanup_var_, __LINE__) __attribute__((cleanup(_STRMERGE(_cleanup_func_, __LINE__)))) __attribute__((unused))
+#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__)
 #endif
 
 struct pids_t {
index 624e7f020c765b21c361633cc9aaf6d63b06e382..02cc8d5cf4acf1945f661201446ec1dc26b8ec1e 100644 (file)
--- a/contain.c
+++ b/contain.c
@@ -204,7 +204,7 @@ 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 806281fd305c831a9f01475ff3c6b3978e8a68be..c1c0acbc9206ffdb91500c11c8cc08ef8b0e5990 100644 (file)
--- a/net.c
+++ b/net.c
@@ -62,7 +62,7 @@ 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 +75,14 @@ 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 +327,7 @@ 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 +360,7 @@ 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 5f125b65a15dfde33f5abcac5427d7776987a1b9..c135be9db7358ca0a2543cba68a3d81cee5feb96 100644 (file)
--- a/nsjail.c
+++ b/nsjail.c
@@ -115,7 +115,7 @@ static void nsjailListenMode(struct nsjconf_t *nsjconf)
        if (listenfd == -1) {
                return;
        }
-       defer(close(listenfd));
+       DEFER(close(listenfd));
        for (;;) {
                if (nsjailSigFatal > 0) {
                        subprocKillAll(nsjconf);
index b7559942b5c38ef1147828c41498095408d28f31..4b3aa7ab99c460f9de4fea6b37ba90d5648539b2 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -248,7 +248,7 @@ 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 5cf7b30d1a719084239e425a5e2747961e929cf4..78541e925f35a068df93fe9e25ddd9acc69da422 100644 (file)
--- a/util.c
+++ b/util.c
@@ -82,7 +82,7 @@ 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);