More defer-ization
authorRobert Swiecki <swiecki@google.com>
Tue, 8 Mar 2016 17:37:07 +0000 (18:37 +0100)
committerRobert Swiecki <swiecki@google.com>
Tue, 8 Mar 2016 17:37:07 +0000 (18:37 +0100)
net.c
subproc.c
util.c

diff --git a/net.c b/net.c
index 2ff6400569f1d1c660ffc94b3da0236230a2be4b..9caac278b2410297ff905a9ce0aaf5db6cb9c632 100644 (file)
--- a/net.c
+++ b/net.c
@@ -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)
index ca7eff0b87c4e84da7152007919c2a98b85979e0..9bc9a2b95c248bec52bc0a2bf22c9305502a5399 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -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]);
 }
diff --git a/util.c b/util.c
index fca97f6fa79370eeaecdf379e375a59708c9cd08..5cf7b30d1a719084239e425a5e2747961e929cf4 100644 (file)
--- a/util.c
+++ b/util.c
@@ -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);