Cleaner impl. of DEFER
This commit is contained in:
parent
75f96e4ca8
commit
4ae2c027ac
6
Makefile
6
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
|
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
|
contain.o: contain.h common.h log.h mount.h net.h util.h uts.h
|
||||||
log.o: log.h common.h
|
log.o: log.h common.h
|
||||||
net.o: net.h common.h log.h
|
|
||||||
mount.o: mount.h common.h log.h
|
mount.o: mount.h common.h log.h
|
||||||
user.o: user.h common.h log.h util.h
|
net.o: net.h common.h log.h
|
||||||
subproc.o: subproc.h common.h contain.h log.h net.h sandbox.h user.h util.h
|
|
||||||
sandbox.o: sandbox.h common.h log.h seccomp/bpf-helper.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
|
util.o: util.h common.h log.h
|
||||||
uts.o: uts.h common.h log.h
|
uts.o: uts.h common.h log.h
|
||||||
seccomp/bpf-helper.o: seccomp/bpf-helper.h
|
seccomp/bpf-helper.o: seccomp/bpf-helper.h
|
||||||
|
7
common.h
7
common.h
@ -31,6 +31,7 @@
|
|||||||
|
|
||||||
#define ARRAYSIZE(array) (sizeof(array) / sizeof(*array))
|
#define ARRAYSIZE(array) (sizeof(array) / sizeof(*array))
|
||||||
|
|
||||||
|
/* Go-style defer implementation */
|
||||||
#define __STRMERGE(a, b) a##b
|
#define __STRMERGE(a, b) a##b
|
||||||
#define _STRMERGE(a, b) __STRMERGE(a, b)
|
#define _STRMERGE(a, b) __STRMERGE(a, b)
|
||||||
|
|
||||||
@ -40,10 +41,12 @@ static void __attribute__ ((unused)) _clang_cleanup_func(void (^*dfunc) (void))
|
|||||||
(*dfunc) ();
|
(*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
|
#else
|
||||||
#define __block
|
#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
|
#endif
|
||||||
|
|
||||||
struct pids_t {
|
struct pids_t {
|
||||||
|
@ -204,7 +204,7 @@ static bool containMakeFdsCOEProc(void)
|
|||||||
PLOG_D("opendir('/proc/self/fd')");
|
PLOG_D("opendir('/proc/self/fd')");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(closedir(dir));
|
DEFER(closedir(dir));
|
||||||
for (;;) {
|
for (;;) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
struct dirent *entry = readdir(dir);
|
struct dirent *entry = readdir(dir);
|
||||||
|
10
net.c
10
net.c
@ -62,7 +62,7 @@ bool netInitNsFromParent(struct nsjconf_t * nsjconf, int pid)
|
|||||||
LOG_E("Could not allocate socket with nl_socket_alloc()");
|
LOG_E("Could not allocate socket with nl_socket_alloc()");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(nl_socket_free(sk));
|
DEFER(nl_socket_free(sk));
|
||||||
|
|
||||||
int err;
|
int err;
|
||||||
if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
|
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));
|
LOG_E("rtnl_link_macvlan_alloc(): %s", nl_geterror(err));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(rtnl_link_put(rmv));
|
DEFER(rtnl_link_put(rmv));
|
||||||
|
|
||||||
struct nl_cache *link_cache;
|
struct nl_cache *link_cache;
|
||||||
if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
|
if ((err = rtnl_link_alloc_cache(sk, AF_UNSPEC, &link_cache)) < 0) {
|
||||||
LOG_E("rtnl_link_alloc_cache(): %s", nl_geterror(err));
|
LOG_E("rtnl_link_alloc_cache(): %s", nl_geterror(err));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(nl_cache_free(link_cache));
|
DEFER(nl_cache_free(link_cache));
|
||||||
|
|
||||||
int master_index = rtnl_link_name2i(link_cache, nsjconf->iface);
|
int master_index = rtnl_link_name2i(link_cache, nsjconf->iface);
|
||||||
if (master_index == 0) {
|
if (master_index == 0) {
|
||||||
@ -327,7 +327,7 @@ static bool netIfaceUp(const char *ifacename)
|
|||||||
PLOG_E("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
|
PLOG_E("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(close(sock));
|
DEFER(close(sock));
|
||||||
|
|
||||||
struct ifreq ifr;
|
struct ifreq ifr;
|
||||||
memset(&ifr, '\0', sizeof(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)");
|
PLOG_E("socket(AF_INET, SOCK_STREAM, IPPROTO_IP)");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(close(sock));
|
DEFER(close(sock));
|
||||||
|
|
||||||
if (inet_pton(AF_INET, nsjconf->iface_vs_ip, &addr) != 1) {
|
if (inet_pton(AF_INET, nsjconf->iface_vs_ip, &addr) != 1) {
|
||||||
PLOG_E("Cannot convert '%s' into an IPv4 address", nsjconf->iface_vs_ip);
|
PLOG_E("Cannot convert '%s' into an IPv4 address", nsjconf->iface_vs_ip);
|
||||||
|
2
nsjail.c
2
nsjail.c
@ -115,7 +115,7 @@ static void nsjailListenMode(struct nsjconf_t *nsjconf)
|
|||||||
if (listenfd == -1) {
|
if (listenfd == -1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
defer(close(listenfd));
|
DEFER(close(listenfd));
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (nsjailSigFatal > 0) {
|
if (nsjailSigFatal > 0) {
|
||||||
subprocKillAll(nsjconf);
|
subprocKillAll(nsjconf);
|
||||||
|
@ -248,7 +248,7 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
int subproc_sock = sv[1];
|
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);
|
pid_t pid = syscall(__NR_clone, (uintptr_t) flags, NULL, NULL, NULL, (uintptr_t) 0);
|
||||||
if (pid == 0) {
|
if (pid == 0) {
|
||||||
|
2
util.c
2
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);
|
PLOG_E("Couldn't open '%s' for R/O", filename);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
defer(close(fd));
|
DEFER(close(fd));
|
||||||
|
|
||||||
if (utilWriteToFd(fd, buf, len) == false) {
|
if (utilWriteToFd(fd, buf, len) == false) {
|
||||||
PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
|
PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
|
||||||
|
Loading…
Reference in New Issue
Block a user