More defer-ization

This commit is contained in:
Robert Swiecki 2016-03-08 18:37:07 +01:00
parent 833cf5d2c8
commit dcf446d7f3
3 changed files with 16 additions and 24 deletions

32
net.c
View File

@ -57,32 +57,33 @@ bool netInitNsFromParent(struct nsjconf_t * nsjconf, int pid)
return true;
}
struct nl_sock *sk;
struct nl_cache *link_cache;
int err, master_index;
bool ret = false;
sk = nl_socket_alloc();
struct nl_sock *sk = nl_socket_alloc();
defer(nl_socket_free(sk));
if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
LOG_E("Unable to connect socket: %s", nl_geterror(err));
goto out_sock;
return false;
}
struct rtnl_link *rmv = rtnl_link_macvlan_alloc();
__block struct rtnl_link *rmv = rtnl_link_macvlan_alloc();
if (rmv == NULL) {
LOG_E("rtnl_link_macvlan_alloc(): %s", nl_geterror(err));
goto out_sock;
return false;
}
rtnl_link_put(rmv);
_block 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));
goto out_link;
return false;
}
defer(nl_cache_free(link_cache));
if (!(master_index = rtnl_link_name2i(link_cache, nsjconf->iface))) {
LOG_E("rtnl_link_name2i(): Did not find '%s' interface", nsjconf->iface);
goto out_cache;
return false;
}
rtnl_link_set_name(rmv, IFACE_NAME);
@ -91,17 +92,10 @@ bool netInitNsFromParent(struct nsjconf_t * nsjconf, int pid)
if ((err = rtnl_link_add(sk, rmv, NLM_F_CREATE)) < 0) {
LOG_E("rtnl_link_add(): %s", nl_geterror(err));
goto out_cache;
return false;
}
ret = true;
out_cache:
nl_cache_free(link_cache);
out_link:
rtnl_link_put(rmv);
out_sock:
nl_socket_free(sk);
return ret;
return true;
}
#else // defined(NSJAIL_NL3_WITH_MACVLAN)
static bool netSystemSbinIp(struct nsjconf_t *nsjconf, char *const *argv)

View File

@ -247,6 +247,8 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
PLOG_E("socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC) failed");
return;
}
__block int subproc_sock = sv[1];
defer(close(subproc_sock));
pid_t pid = syscall(__NR_clone, (uintptr_t) flags, NULL, NULL, NULL, (uintptr_t) 0);
if (pid == 0) {
@ -259,13 +261,11 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
"doesn't support CLONE_NEWUSER. Alternatively, you might want to recompile your "
"kernel with support for namespaces or check the setting of the "
"kernel.unprivileged_userns_clone sysctl", flags);
close(sv[1]);
return;
}
subprocAdd(nsjconf, pid, fd_in);
if (subprocInitParent(nsjconf, pid, sv[1]) == false) {
close(sv[1]);
return;
}
@ -279,5 +279,4 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
log_buf[sz] = '\0';
logDirectlyToFD(log_buf);
}
close(sv[1]);
}

3
util.c
View File

@ -82,14 +82,13 @@ 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));
if (utilWriteToFd(fd, buf, len) == false) {
PLOG_E("Couldn't write '%zu' bytes to file '%s' (fd='%d')", len, filename, fd);
close(fd);
unlink(filename);
return false;
}
close(fd);
LOG_D("Written '%zu' bytes to '%s'", len, filename);