Set-up parent in a separate function
authorJagger <robert@swiecki.net>
Sun, 28 Feb 2016 22:23:24 +0000 (23:23 +0100)
committerJagger <robert@swiecki.net>
Sun, 28 Feb 2016 22:23:24 +0000 (23:23 +0100)
net.c
subproc.c

diff --git a/net.c b/net.c
index 5b04546aede35e013e797ddfe78729d5ad5d7dc7..b48645afca2a8dad8eacc86fc554349159b81ca7 100644 (file)
--- a/net.c
+++ b/net.c
@@ -92,7 +92,9 @@ bool netCloneMacVtapAndNS(struct nsjconf_t * nsjconf, int pid)
        char pid_str[256];
        snprintf(pid_str, sizeof(pid_str), "%d", pid);
        char *const argv_netns[] =
-           { SBIN_IP_PATH, "link", "set", "dev", iface, "netns", pid_str, "name", "virt.ns", NULL };
+           { SBIN_IP_PATH, "link", "set", "dev", iface, "netns", pid_str, "name", "virt.ns",
+               NULL
+       };
        if (netSystem(SBIN_IP_PATH, argv_netns) == false) {
                LOG_E("Couldn't put interface '%s' into NS of PID '%d'", iface, pid);
                return false;
index ffae0f082f02cb5f7e70f78f3bb52225504f36b3..87fd9412b8f036fd23677c54ced814ac9d24a3f7 100644 (file)
--- a/subproc.c
+++ b/subproc.c
@@ -206,6 +206,24 @@ void subprocKillAll(struct nsjconf_t *nsjconf)
        }
 }
 
+static bool subprocInitParent(struct nsjconf_t *nsjconf, pid_t pid, int pipefd)
+{
+       if (netCloneMacVtapAndNS(nsjconf, pid) == false) {
+               LOG_E("Couldn't create and put MACVTAP interface into NS of PID '%d'", pid);
+               return false;
+       }
+       if (containInitUserNs(nsjconf, pid) == false) {
+               LOG_E("Couldn't initialize user namespaces for pid %d", pid);
+               return false;
+       }
+       if (utilWriteToFd(pipefd, &subprocDoneChar, sizeof(subprocDoneChar)) !=
+           sizeof(subprocDoneChar)) {
+               LOG_E("Couldn't signal the new process via a socketpair");
+               return false;
+       }
+       return true;
+}
+
 void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_err)
 {
        if (netLimitConns(nsjconf, fd_in) == false) {
@@ -260,18 +278,7 @@ void subprocRunChild(struct nsjconf_t *nsjconf, int fd_in, int fd_out, int fd_er
                return;
        }
 
-       if (netCloneMacVtapAndNS(nsjconf, pid) == false) {
-               LOG_E("Couldn't create and put MACVTAP interface into NS of PID '%d'", pid);
-               close(sv[1]);
-               return;
-       }
-       if (containInitUserNs(nsjconf, pid) == false) {
-               LOG_E("Couldn't initialize user namespaces for pid %d", pid);
-               close(sv[1]);
-               return;
-       }
-       if (utilWriteToFd(sv[1], &subprocDoneChar, sizeof(subprocDoneChar)) != sizeof(subprocDoneChar)) {
-               LOG_E("Couldn't signal the new process via a socketpair");
+       if (subprocInitParent(nsjconf, pid, sv[1]) == false) {
                close(sv[1]);
                return;
        }