diff --git a/Makefile b/Makefile index 74519ff..7d2c1c3 100644 --- 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 = 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 diff --git a/caps.cc b/caps.cc index c7e168e..0dd7460 100644 --- a/caps.cc +++ b/caps.cc @@ -28,11 +28,8 @@ #include #include -extern "C" { -#include "log.h" -} - #include "common.h" +#include "log.h" #include "util.h" namespace caps { diff --git a/cgroup.cc b/cgroup.cc index 2160279..a51e3ef 100644 --- a/cgroup.cc +++ b/cgroup.cc @@ -30,10 +30,7 @@ #include #include -extern "C" { #include "log.h" -} - #include "util.h" namespace cgroup { diff --git a/cmdline.cc b/cmdline.cc index 7cb6122..2f5b74f 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -44,13 +44,10 @@ #include -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 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 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 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 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; } diff --git a/config.cc b/config.cc index ec2438c..13afbbb 100644 --- a/config.cc +++ b/config.cc @@ -33,18 +33,18 @@ #include #include -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 diff --git a/config.h b/config.h index a789c3b..157098a 100644 --- a/config.h +++ b/config.h @@ -22,17 +22,14 @@ #ifndef NS_CONFIG_H #define NS_CONFIG_H -#ifdef __cplusplus -extern "C" { -#endif - #include #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 */ diff --git a/contain.cc b/contain.cc index c16f19e..9f36bb7 100644 --- a/contain.cc +++ b/contain.cc @@ -37,13 +37,10 @@ #include #include -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" diff --git a/cpu.cc b/cpu.cc index eaa636f..1c0c97a 100644 --- a/cpu.cc +++ b/cpu.cc @@ -27,10 +27,7 @@ #include #include -extern "C" { #include "log.h" -} - #include "util.h" namespace cpu { diff --git a/log.c b/log.cc similarity index 95% rename from log.c rename to log.cc index 42ffd9c..88bbfc2 100644 --- a/log.c +++ b/log.cc @@ -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 diff --git a/log.h b/log.h index c265ac3..4129a3b 100644 --- a/log.h +++ b/log.h @@ -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 */ diff --git a/mnt.cc b/mnt.cc index 864941a..da29793 100644 --- a/mnt.cc +++ b/mnt.cc @@ -41,11 +41,8 @@ #include #include -extern "C" { -#include "log.h" -} - #include "common.h" +#include "log.h" #include "subproc.h" #include "util.h" diff --git a/net.cc b/net.cc index 1209315..d9e5d10 100644 --- a/net.cc +++ b/net.cc @@ -40,10 +40,7 @@ #include #include -extern "C" { #include "log.h" -} - #include "subproc.h" extern char** environ; diff --git a/nsjail.cc b/nsjail.cc index 0fac593..2b9b9dd 100644 --- a/nsjail.cc +++ b/nsjail.cc @@ -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; } diff --git a/nsjail.h b/nsjail.h index 9b8ebf9..2b7161d 100644 --- a/nsjail.h +++ b/nsjail.h @@ -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 */ diff --git a/pid.cc b/pid.cc index 30b9c18..b087114 100644 --- a/pid.cc +++ b/pid.cc @@ -28,10 +28,7 @@ #include #include -extern "C" { #include "log.h" -} - #include "subproc.h" namespace pid { diff --git a/sandbox.cc b/sandbox.cc index 3ac6168..b7eadb1 100644 --- a/sandbox.cc +++ b/sandbox.cc @@ -28,8 +28,8 @@ extern "C" { #include "kafel.h" -#include "log.h" } +#include "log.h" namespace sandbox { diff --git a/subproc.cc b/subproc.cc index 923a38b..3ec7928 100644 --- a/subproc.cc +++ b/subproc.cc @@ -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) diff --git a/user.cc b/user.cc index a537d2d..a43485a 100644 --- a/user.cc +++ b/user.cc @@ -39,11 +39,8 @@ #include #include -extern "C" { -#include "log.h" -} - #include "common.h" +#include "log.h" #include "subproc.h" #include "util.h" diff --git a/util.cc b/util.cc index 528767b..409d56c 100644 --- a/util.cc +++ b/util.cc @@ -41,10 +41,7 @@ #include #include "common.h" - -extern "C" { #include "log.h" -} namespace util { diff --git a/uts.cc b/uts.cc index c9129e7..1f1081d 100644 --- a/uts.cc +++ b/uts.cc @@ -24,9 +24,7 @@ #include #include -extern "C" { #include "log.h" -} namespace uts {