contain: move to C++
authorRobert Swiecki <robert@swiecki.net>
Fri, 9 Feb 2018 16:09:58 +0000 (17:09 +0100)
committerRobert Swiecki <robert@swiecki.net>
Fri, 9 Feb 2018 16:09:58 +0000 (17:09 +0100)
Makefile
contain.cc [moved from contain.c with 97% similarity]
contain.h
subproc.cc

index e3e905e..3e8f500 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -35,8 +35,8 @@ LDFLAGS += -pie -Wl,-z,noexecstack -lpthread $(shell pkg-config --libs protobuf)
 
 BIN = nsjail
 LIBS = kafel/libkafel.a
-SRCS_C = caps.c contain.c log.c cgroup.c mount.c net.c pid.c sandbox.c user.c util.c uts.c cpu.c
-SRCS_CXX = cmdline.cc config.cc nsjail.cc subproc.cc
+SRCS_C = caps.c log.c cgroup.c mount.c net.c pid.c sandbox.c user.c util.c uts.c cpu.c
+SRCS_CXX = cmdline.cc config.cc contain.cc nsjail.cc subproc.cc
 SRCS_PROTO = config.proto
 SRCS_PB_CXX = $(SRCS_PROTO:.proto=.pb.cc)
 SRCS_PB_H = $(SRCS_PROTO:.proto=.pb.h)
@@ -98,8 +98,6 @@ indent:
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
 caps.o: caps.h nsjail.h common.h log.h util.h
-contain.o: contain.h nsjail.h caps.h cgroup.h cpu.h log.h mount.h net.h pid.h
-contain.o: user.h uts.h
 log.o: log.h nsjail.h
 cgroup.o: cgroup.h nsjail.h log.h util.h
 mount.o: mount.h nsjail.h common.h log.h subproc.h util.h
@@ -114,6 +112,8 @@ cmdline.o: cmdline.h nsjail.h caps.h common.h log.h mount.h sandbox.h user.h
 cmdline.o: util.h config.h
 config.o: common.h caps.h nsjail.h config.h log.h mount.h user.h util.h
 config.o: cmdline.h
+contain.o: contain.h nsjail.h caps.h cgroup.h cpu.h log.h mount.h net.h pid.h
+contain.o: user.h uts.h
 nsjail.o: nsjail.h cmdline.h common.h log.h net.h subproc.h util.h
-subproc.o: subproc.h nsjail.h cgroup.h common.h contain.h log.h net.h
+subproc.o: subproc.h nsjail.h contain.h cgroup.h common.h log.h net.h
 subproc.o: sandbox.h user.h util.h
similarity index 97%
rename from contain.c
rename to contain.cc
index 48a435b..1012a90 100644 (file)
--- a/contain.c
@@ -37,6 +37,7 @@
 #include <sys/resource.h>
 #include <unistd.h>
 
+extern "C" {
 #include "caps.h"
 #include "cgroup.h"
 #include "cpu.h"
@@ -46,6 +47,9 @@
 #include "pid.h"
 #include "user.h"
 #include "uts.h"
+}
+
+namespace contain {
 
 static bool containUserNs(struct nsjconf_t* nsjconf) { return userInitNsFromChild(nsjconf); }
 
@@ -247,7 +251,7 @@ static bool containMakeFdsCOE(struct nsjconf_t* nsjconf) {
        return false;
 }
 
-bool containSetupFD(struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err) {
+bool setupFD(struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err) {
        if (nsjconf->mode != MODE_LISTEN_TCP) {
                if (nsjconf->is_silent == false) {
                        return true;
@@ -273,7 +277,7 @@ bool containSetupFD(struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err
        return true;
 }
 
-bool containContain(struct nsjconf_t* nsjconf) {
+bool containProc(struct nsjconf_t* nsjconf) {
        if (containUserNs(nsjconf) == false) {
                return false;
        }
@@ -311,3 +315,5 @@ bool containContain(struct nsjconf_t* nsjconf) {
        }
        return true;
 }
+
+}  // namespace contain
index 71057e7..98dd77b 100644 (file)
--- a/contain.h
+++ b/contain.h
 
 #include "nsjail.h"
 
-bool containSetupFD(struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err);
-bool containContain(struct nsjconf_t* nsjconf);
+namespace contain {
+
+bool setupFD(struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err);
+bool containProc(struct nsjconf_t* nsjconf);
+
+}  // namespace contain
 
 #endif /* NS_CONTAIN_H */
index 6ab7e2b..e471ac8 100644 (file)
 #include <time.h>
 #include <unistd.h>
 
+#include "contain.h"
+
 extern "C" {
 #include "cgroup.h"
 #include "common.h"
-#include "contain.h"
 #include "log.h"
 #include "net.h"
 #include "sandbox.h"
@@ -135,7 +136,7 @@ static const char kSubprocDoneChar = 'D';
 
 static int subprocNewProc(
     struct nsjconf_t* nsjconf, int fd_in, int fd_out, int fd_err, int pipefd) {
-       if (containSetupFD(nsjconf, fd_in, fd_out, fd_err) == false) {
+       if (contain::setupFD(nsjconf, fd_in, fd_out, fd_err) == false) {
                _exit(0xff);
        }
        if (!resetEnv()) {
@@ -160,7 +161,7 @@ static int subprocNewProc(
                        _exit(0xff);
                }
        }
-       if (containContain(nsjconf) == false) {
+       if (contain::containProc(nsjconf) == false) {
                _exit(0xff);
        }
        if (nsjconf->keep_env == false) {