Cleaner impl. of DEFER

This commit is contained in:
Jagger 2016-03-10 22:56:26 +01:00
parent 75f96e4ca8
commit 4ae2c027ac
7 changed files with 17 additions and 14 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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);

10
net.c
View File

@ -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);

View File

@ -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);

View File

@ -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) {

2
util.c
View File

@ -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);