all: move to C++

This commit is contained in:
Robert Swiecki 2018-02-09 18:55:42 +01:00
parent a6c34999f2
commit 7f72cbd497
20 changed files with 64 additions and 107 deletions

View File

@ -35,8 +35,8 @@ LDFLAGS += -pie -Wl,-z,noexecstack -lpthread $(shell pkg-config --libs protobuf)
BIN = nsjail
LIBS = kafel/libkafel.a
SRCS_C = log.c
SRCS_CXX = caps.cc cgroup.cc cmdline.cc config.cc contain.cc cpu.cc mnt.cc net.cc nsjail.cc pid.cc sandbox.cc subproc.cc uts.cc user.cc util.cc
SRCS_C =
SRCS_CXX = caps.cc cgroup.cc cmdline.cc config.cc contain.cc cpu.cc log.cc mnt.cc net.cc nsjail.cc pid.cc sandbox.cc subproc.cc uts.cc user.cc util.cc
SRCS_PROTO = config.proto
SRCS_PB_CXX = $(SRCS_PROTO:.proto=.pb.cc)
SRCS_PB_H = $(SRCS_PROTO:.proto=.pb.h)
@ -92,28 +92,29 @@ depend:
makedepend -Y -Ykafel/include -- -- $(SRCS_C) $(SRCS_CXX) $(SRCS_PB_CXX)
indent:
clang-format -style="{BasedOnStyle: google, IndentWidth: 8, UseTab: Always, IndentCaseLabels: false, ColumnLimit: 100, AlignAfterOpenBracket: false}" -i -sort-includes *.c *.h $(SRCS_CXX)
clang-format -style="{BasedOnStyle: google, IndentWidth: 8, UseTab: Always, IndentCaseLabels: false, ColumnLimit: 100, AlignAfterOpenBracket: false}" -i -sort-includes *.h $(SRCS_CXX)
clang-format -style="{BasedOnStyle: google, IndentWidth: 4, UseTab: Always, ColumnLimit: 100}" -i $(SRCS_PROTO)
# DO NOT DELETE THIS LINE -- make depend depends on it.
log.o: log.h nsjail.h
caps.o: caps.h nsjail.h log.h common.h util.h
caps.o: caps.h nsjail.h common.h log.h util.h
cgroup.o: cgroup.h nsjail.h log.h util.h
cmdline.o: cmdline.h nsjail.h log.h caps.h common.h config.h mnt.h sandbox.h
cmdline.o: cmdline.h nsjail.h caps.h common.h config.h log.h mnt.h sandbox.h
cmdline.o: user.h util.h
config.o: log.h nsjail.h caps.h cmdline.h common.h config.h mnt.h user.h
config.o: util.h
contain.o: contain.h nsjail.h log.h caps.h cgroup.h cpu.h mnt.h net.h pid.h
config.o: caps.h nsjail.h cmdline.h common.h config.h config.pb.h log.h mnt.h
config.o: user.h util.h
contain.o: contain.h nsjail.h caps.h cgroup.h cpu.h log.h mnt.h net.h pid.h
contain.o: user.h uts.h
cpu.o: cpu.h nsjail.h log.h util.h
mnt.o: mnt.h nsjail.h log.h common.h subproc.h util.h
log.o: log.h nsjail.h
mnt.o: mnt.h nsjail.h common.h log.h subproc.h util.h
net.o: net.h nsjail.h log.h subproc.h
nsjail.o: nsjail.h cmdline.h common.h log.h net.h subproc.h util.h
pid.o: pid.h nsjail.h log.h subproc.h
sandbox.o: sandbox.h nsjail.h kafel/include/kafel.h log.h
subproc.o: subproc.h nsjail.h cgroup.h common.h contain.h net.h sandbox.h
subproc.o: user.h util.h log.h
subproc.o: subproc.h nsjail.h cgroup.h common.h contain.h log.h net.h
subproc.o: sandbox.h user.h util.h
uts.o: uts.h nsjail.h log.h
user.o: user.h nsjail.h log.h common.h subproc.h util.h
user.o: user.h nsjail.h common.h log.h subproc.h util.h
util.o: util.h nsjail.h common.h log.h
config.pb.o: config.pb.h

View File

@ -28,11 +28,8 @@
#include <sys/types.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "common.h"
#include "log.h"
#include "util.h"
namespace caps {

View File

@ -30,10 +30,7 @@
#include <sys/stat.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "util.h"
namespace cgroup {

View File

@ -44,13 +44,10 @@
#include <memory>
extern "C" {
#include "log.h"
}
#include "caps.h"
#include "common.h"
#include "config.h"
#include "log.h"
#include "mnt.h"
#include "sandbox.h"
#include "user.h"
@ -441,7 +438,7 @@ std::unique_ptr<struct nsjconf_t> parseArgs(int argc, char* argv[]) {
nsjconf->cwd = optarg;
break;
case 'C':
if (configParse(nsjconf.get(), optarg) == false) {
if (config::parseFile(nsjconf.get(), optarg) == false) {
LOG_F("Couldn't parse configuration from '%s' file", optarg);
}
break;
@ -460,13 +457,13 @@ std::unique_ptr<struct nsjconf_t> parseArgs(int argc, char* argv[]) {
break;
case 'l':
nsjconf->logfile = optarg;
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}
break;
case 'L':
nsjconf->log_fd = strtol(optarg, NULL, 0);
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}
break;
@ -475,19 +472,19 @@ std::unique_ptr<struct nsjconf_t> parseArgs(int argc, char* argv[]) {
break;
case 'v':
nsjconf->loglevel = DEBUG;
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}
break;
case 'q':
nsjconf->loglevel = WARNING;
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}
break;
case 'Q':
nsjconf->loglevel = FATAL;
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}
break;
@ -829,7 +826,7 @@ std::unique_ptr<struct nsjconf_t> parseArgs(int argc, char* argv[]) {
TAILQ_INSERT_HEAD(&nsjconf->gids, p, pointers);
}
if (logInitLogFile(nsjconf.get()) == false) {
if (log::initLogFile(nsjconf.get()) == false) {
return nullptr;
}

View File

@ -33,18 +33,18 @@
#include <string>
#include <vector>
extern "C" {
#include "log.h"
}
#include "caps.h"
#include "cmdline.h"
#include "common.h"
#include "config.h"
#include "config.pb.h"
#include "log.h"
#include "mnt.h"
#include "user.h"
#include "util.h"
namespace config {
#define DUP_IF_SET(njc, val) (njc.has_##val() ? njc.val().c_str() : NULL)
static uint64_t configRLimit(
@ -122,7 +122,7 @@ static bool configParseInternal(struct nsjconf_t* nsjconf, const nsjail::NsJailC
}
if (njc.has_log_fd() || njc.has_log_file() || njc.has_log_level()) {
if (logInitLogFile(nsjconf) == false) {
if (log::initLogFile(nsjconf) == false) {
return false;
}
}
@ -305,7 +305,7 @@ static void LogHandler(
LOG_W("config.cc: '%s'", message.c_str());
}
extern "C" bool configParse(struct nsjconf_t* nsjconf, const char* file) {
bool parseFile(struct nsjconf_t* nsjconf, const char* file) {
LOG_I("Parsing configuration from '%s'", file);
int fd = open(file, O_RDONLY | O_CLOEXEC);
@ -334,3 +334,5 @@ extern "C" bool configParse(struct nsjconf_t* nsjconf, const char* file) {
return true;
}
} // namespace config

View File

@ -22,17 +22,14 @@
#ifndef NS_CONFIG_H
#define NS_CONFIG_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
#include "nsjail.h"
bool configParse(struct nsjconf_t* nsjconf, const char* file);
namespace config {
bool parseFile(struct nsjconf_t* nsjconf, const char* file);
} // namespace config
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* NS_CONFIG_H */

View File

@ -37,13 +37,10 @@
#include <sys/resource.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "caps.h"
#include "cgroup.h"
#include "cpu.h"
#include "log.h"
#include "mnt.h"
#include "net.h"
#include "pid.h"

3
cpu.cc
View File

@ -27,10 +27,7 @@
#include <string.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "util.h"
namespace cpu {

View File

@ -37,6 +37,8 @@
#include "nsjail.h"
namespace log {
static int log_fd = STDERR_FILENO;
static bool log_fd_isatty = true;
static enum llevel_t log_level = INFO;
@ -49,7 +51,7 @@ __attribute__((constructor)) static void log_init(void) { log_fd_isatty = isatty
* Log to stderr by default. Use a dup()d fd, because in the future we'll associate the
* connection socket with fd (0, 1, 2).
*/
bool logInitLogFile(struct nsjconf_t* nsjconf) {
bool initLogFile(struct nsjconf_t* nsjconf) {
/* Close previous log_fd */
if (log_fd > STDERR_FILENO) {
close(log_fd);
@ -75,7 +77,7 @@ bool logInitLogFile(struct nsjconf_t* nsjconf) {
return true;
}
void logLog(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
if (ll < log_level) {
return;
}
@ -138,3 +140,5 @@ void logLog(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt
}
void logStop(int sig) { LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig); }
} // namespace log

36
log.h
View File

@ -27,32 +27,28 @@
#include "nsjail.h"
#define LOG_HELP(...) logLog(HELP, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_HELP_BOLD(...) logLog(HELP_BOLD, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_HELP(...) log::logMsg(HELP, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_HELP_BOLD(...) log::logMsg(HELP_BOLD, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_D(...) logLog(DEBUG, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_I(...) logLog(INFO, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_W(...) logLog(WARNING, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_E(...) logLog(ERROR, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_F(...) logLog(FATAL, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_D(...) log::logMsg(DEBUG, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_I(...) log::logMsg(INFO, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_W(...) log::logMsg(WARNING, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_E(...) log::logMsg(ERROR, __func__, __LINE__, false, __VA_ARGS__);
#define LOG_F(...) log::logMsg(FATAL, __func__, __LINE__, false, __VA_ARGS__);
#define PLOG_D(...) logLog(DEBUG, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_I(...) logLog(INFO, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_W(...) logLog(WARNING, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_E(...) logLog(ERROR, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_F(...) logLog(FATAL, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_D(...) log::logMsg(DEBUG, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_I(...) log::logMsg(INFO, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_W(...) log::logMsg(WARNING, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_E(...) log::logMsg(ERROR, __func__, __LINE__, true, __VA_ARGS__);
#define PLOG_F(...) log::logMsg(FATAL, __func__, __LINE__, true, __VA_ARGS__);
#ifdef __cplusplus
extern "C" {
#endif
namespace log {
bool logInitLogFile(struct nsjconf_t* nsjconf);
void logLog(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...)
bool initLogFile(struct nsjconf_t* nsjconf);
void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...)
__attribute__((format(printf, 5, 6)));
void logStop(int sig);
#ifdef __cplusplus
} // extern "C"
#endif
} // namespace log
#endif /* NS_LOG_H */

5
mnt.cc
View File

@ -41,11 +41,8 @@
#include <syscall.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "common.h"
#include "log.h"
#include "subproc.h"
#include "util.h"

3
net.cc
View File

@ -40,10 +40,7 @@
#include <sys/types.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "subproc.h"
extern char** environ;

View File

@ -112,7 +112,7 @@ static void nsjailListenMode(struct nsjconf_t* nsjconf) {
for (;;) {
if (nsjailSigFatal > 0) {
subproc::killAll(nsjconf);
logStop(nsjailSigFatal);
log::logStop(nsjailSigFatal);
close(listenfd);
return;
}
@ -147,7 +147,7 @@ static int nsjailStandaloneMode(struct nsjconf_t* nsjconf) {
}
if (nsjailSigFatal > 0) {
subproc::killAll(nsjconf);
logStop(nsjailSigFatal);
log::logStop(nsjailSigFatal);
return -1;
}

View File

@ -44,10 +44,6 @@
}))
#endif /* !defined(TEMP_FAILURE_RETRY) */
#ifdef __cplusplus
extern "C" {
#endif
static const int nssigs[] = {
SIGINT,
SIGQUIT,
@ -202,8 +198,4 @@ struct nsjconf_t {
caps;
};
#ifdef __cplusplus
} // extern "C"
#endif
#endif /* _NSJAIL_H */

3
pid.cc
View File

@ -28,10 +28,7 @@
#include <sys/prctl.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "subproc.h"
namespace pid {

View File

@ -28,8 +28,8 @@
extern "C" {
#include "kafel.h"
#include "log.h"
}
#include "log.h"
namespace sandbox {

View File

@ -45,15 +45,12 @@
#include "cgroup.h"
#include "common.h"
#include "contain.h"
#include "log.h"
#include "net.h"
#include "sandbox.h"
#include "user.h"
#include "util.h"
extern "C" {
#include "log.h"
} // extern "C"
namespace subproc {
#if !defined(CLONE_NEWCGROUP)

View File

@ -39,11 +39,8 @@
#include <sys/types.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
#include "common.h"
#include "log.h"
#include "subproc.h"
#include "util.h"

View File

@ -41,10 +41,7 @@
#include <unistd.h>
#include "common.h"
extern "C" {
#include "log.h"
}
namespace util {

2
uts.cc
View File

@ -24,9 +24,7 @@
#include <string.h>
#include <unistd.h>
extern "C" {
#include "log.h"
}
namespace uts {