log: rename log to logs due to clash with glibc's log
authorRobert Swiecki <robert@swiecki.net>
Sat, 10 Feb 2018 16:49:15 +0000 (17:49 +0100)
committerRobert Swiecki <robert@swiecki.net>
Sat, 10 Feb 2018 16:49:15 +0000 (17:49 +0100)
21 files changed:
Makefile
caps.cc
cgroup.cc
cmdline.cc
config.cc
contain.cc
cpu.cc
log.cc [deleted file]
log.h [deleted file]
logs.cc [new file with mode: 0644]
logs.h [new file with mode: 0644]
mnt.cc
net.cc
nsjail.cc
nsjail.h
pid.cc
sandbox.cc
subproc.cc
user.cc
util.cc
uts.cc

index 2f48990157e0ba1310ee7c81157c0e60addfaf68..f170c72ebbb3ef6565a50342cc00ba74c05ed297 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ LDFLAGS += -pie -Wl,-z,noexecstack -lpthread $(shell pkg-config --libs protobuf)
 
 BIN = nsjail
 LIBS = kafel/libkafel.a
-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_CXX = caps.cc cgroup.cc cmdline.cc config.cc contain.cc cpu.cc logs.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)
@@ -90,24 +90,24 @@ indent:
 
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
-caps.o: caps.h nsjail.h log.h macros.h util.h
-cgroup.o: cgroup.h nsjail.h log.h util.h
-cmdline.o: cmdline.h nsjail.h caps.h config.h log.h macros.h mnt.h sandbox.h
+caps.o: caps.h nsjail.h logs.h macros.h util.h
+cgroup.o: cgroup.h nsjail.h logs.h util.h
+cmdline.o: cmdline.h nsjail.h logs.h caps.h config.h macros.h mnt.h sandbox.h
 cmdline.o: user.h util.h
-config.o: caps.h nsjail.h cmdline.h config.h config.pb.h log.h macros.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
+config.o: caps.h nsjail.h logs.h cmdline.h config.h config.pb.h macros.h
+config.o: mnt.h user.h util.h
+contain.o: contain.h nsjail.h logs.h caps.h cgroup.h cpu.h mnt.h net.h pid.h
 contain.o: user.h uts.h
-cpu.o: cpu.h nsjail.h log.h util.h
-log.o: log.h nsjail.h
-mnt.o: mnt.h nsjail.h log.h macros.h subproc.h util.h
-net.o: net.h nsjail.h log.h subproc.h
-nsjail.o: nsjail.h cmdline.h log.h macros.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 contain.h log.h macros.h net.h
+cpu.o: cpu.h nsjail.h logs.h util.h
+logs.o: logs.h nsjail.h
+mnt.o: mnt.h nsjail.h logs.h macros.h subproc.h util.h
+net.o: net.h nsjail.h logs.h subproc.h
+nsjail.o: nsjail.h logs.h cmdline.h macros.h net.h subproc.h util.h
+pid.o: pid.h nsjail.h logs.h subproc.h
+sandbox.o: sandbox.h nsjail.h logs.h kafel/include/kafel.h
+subproc.o: subproc.h nsjail.h logs.h cgroup.h contain.h macros.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 macros.h subproc.h util.h
-util.o: util.h nsjail.h log.h macros.h
+uts.o: uts.h nsjail.h logs.h
+user.o: user.h nsjail.h logs.h macros.h subproc.h util.h
+util.o: util.h nsjail.h logs.h macros.h
 config.pb.o: config.pb.h
diff --git a/caps.cc b/caps.cc
index f7364f69bda81e36c006b9d81afd9028105f7e13..e2935d0d85c6105660bf8f16e428cad3523c8438 100644 (file)
--- a/caps.cc
+++ b/caps.cc
@@ -28,7 +28,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "util.h"
 
index a98e2ddd56d8a9dd490b337c7c39dca2b93bb387..f52cf1aa66ceb1fde243c71c525133a91c9e0f28 100644 (file)
--- a/cgroup.cc
+++ b/cgroup.cc
@@ -30,7 +30,7 @@
 #include <sys/stat.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "util.h"
 
 namespace cgroup {
index 5389f13224867db09a34f3da8cb6b5f39350f7e3..a43d931222e6afcbb979d601c0960aed49aaba55 100644 (file)
@@ -46,7 +46,7 @@
 
 #include "caps.h"
 #include "config.h"
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "mnt.h"
 #include "sandbox.h"
@@ -55,6 +55,8 @@
 
 namespace cmdline {
 
+#define _LOG_DEFAULT_FILE "/var/log/nsjail.log"
+
 struct custom_option {
        struct option opt;
        const char* descr;
@@ -330,8 +332,7 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
        nsjconf->cwd = "/";
        nsjconf->port = 0;
        nsjconf->bindhost = "::";
-       nsjconf->log_fd = STDERR_FILENO;
-       nsjconf->loglevel = INFO;
+       nsjconf->loglevel = logs::INFO;
        nsjconf->daemonize = false;
        nsjconf->tlimit = 0;
        nsjconf->max_cpus = 0;
@@ -439,36 +440,21 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                        break;
                case 'l':
                        nsjconf->logfile = optarg;
-                       if (!log::initLogFile(nsjconf.get())) {
-                               return nullptr;
-                       }
                        break;
                case 'L':
-                       nsjconf->log_fd = strtol(optarg, NULL, 0);
-                       if (log::initLogFile(nsjconf.get()) == false) {
-                               return nullptr;
-                       }
+                       nsjconf->logfile = "/dev/fd/" + std::to_string(strtol(optarg, NULL, 0));
                        break;
                case 'd':
                        nsjconf->daemonize = true;
                        break;
                case 'v':
-                       nsjconf->loglevel = DEBUG;
-                       if (log::initLogFile(nsjconf.get()) == false) {
-                               return nullptr;
-                       }
+                       nsjconf->loglevel = logs::DEBUG;
                        break;
                case 'q':
-                       nsjconf->loglevel = WARNING;
-                       if (log::initLogFile(nsjconf.get()) == false) {
-                               return nullptr;
-                       }
+                       nsjconf->loglevel = logs::WARNING;
                        break;
                case 'Q':
-                       nsjconf->loglevel = FATAL;
-                       if (log::initLogFile(nsjconf.get()) == false) {
-                               return nullptr;
-                       }
+                       nsjconf->loglevel = logs::FATAL;
                        break;
                case 'e':
                        nsjconf->keep_env = true;
@@ -756,6 +742,13 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                }
        }
 
+       if (nsjconf->daemonize && nsjconf->logfile.empty()) {
+               nsjconf->logfile = _LOG_DEFAULT_FILE;
+       }
+       if (!logs::initLog(nsjconf->logfile, nsjconf->loglevel)) {
+               return nullptr;
+       }
+
        if (nsjconf->mount_proc) {
                if (!mnt::addMountPtTail(nsjconf.get(), /* src= */ NULL, nsjconf->proc_path, "proc",
                        "", nsjconf->is_proc_rw ? 0 : MS_RDONLY, /* isDir= */ mnt::NS_DIR_YES,
@@ -799,10 +792,6 @@ std::unique_ptr<nsjconf_t> parseArgs(int argc, char* argv[]) {
                nsjconf->gids.push_back(gid);
        }
 
-       if (log::initLogFile(nsjconf.get()) == false) {
-               return nullptr;
-       }
-
        if (argv[optind]) {
                nsjconf->argv = (const char**)&argv[optind];
        }
index 0f3c44628eeb963eb8500385420c25848d2523dc..34c687dd5577d5c1f89421e0073efe077f8a5927 100644 (file)
--- a/config.cc
+++ b/config.cc
@@ -37,7 +37,7 @@
 #include "cmdline.h"
 #include "config.h"
 #include "config.pb.h"
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "mnt.h"
 #include "user.h"
@@ -97,7 +97,7 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig&
        nsjconf->daemonize = njc.daemon();
 
        if (njc.has_log_fd()) {
-               nsjconf->log_fd = njc.log_fd();
+               nsjconf->logfile = "/dev/fd/" + std::to_string(njc.log_fd());
        }
        if (njc.has_log_file()) {
                nsjconf->logfile = njc.log_file();
@@ -105,19 +105,19 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig&
        if (njc.has_log_level()) {
                switch (njc.log_level()) {
                case nsjail::LogLevel::DEBUG:
-                       nsjconf->loglevel = DEBUG;
+                       nsjconf->loglevel = logs::DEBUG;
                        break;
                case nsjail::LogLevel::INFO:
-                       nsjconf->loglevel = INFO;
+                       nsjconf->loglevel = logs::INFO;
                        break;
                case nsjail::LogLevel::WARNING:
-                       nsjconf->loglevel = WARNING;
+                       nsjconf->loglevel = logs::WARNING;
                        break;
                case nsjail::LogLevel::ERROR:
-                       nsjconf->loglevel = ERROR;
+                       nsjconf->loglevel = logs::ERROR;
                        break;
                case nsjail::LogLevel::FATAL:
-                       nsjconf->loglevel = FATAL;
+                       nsjconf->loglevel = logs::FATAL;
                        break;
                default:
                        LOG_E("Unknown log_level: %d", njc.log_level());
@@ -125,12 +125,6 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig&
                }
        }
 
-       if (njc.has_log_fd() || njc.has_log_file() || njc.has_log_level()) {
-               if (log::initLogFile(nsjconf) == false) {
-                       return false;
-               }
-       }
-
        nsjconf->keep_env = njc.keep_env();
        for (ssize_t i = 0; i < njc.envar_size(); i++) {
                nsjconf->envs.push_back(njc.envar(i));
index 16a1892d279bbc16f43ac519432f514e416a96c8..e47058901bd5568bb3e02b6eedfb14ab83a6ea90 100644 (file)
@@ -41,7 +41,7 @@
 #include "caps.h"
 #include "cgroup.h"
 #include "cpu.h"
-#include "log.h"
+#include "logs.h"
 #include "mnt.h"
 #include "net.h"
 #include "pid.h"
diff --git a/cpu.cc b/cpu.cc
index 88224ca0d69e954769af53b0f2d14b53a9469213..c67fa0d7a6044b4842d7f7f000903c3f76679a5b 100644 (file)
--- a/cpu.cc
+++ b/cpu.cc
@@ -27,7 +27,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "util.h"
 
 namespace cpu {
diff --git a/log.cc b/log.cc
deleted file mode 100644 (file)
index 15d118d..0000000
--- a/log.cc
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
-
-   nsjail - logging
-   -----------------------------------------
-
-   Copyright 2014 Google Inc. All Rights Reserved.
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-
-*/
-
-#include "log.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <getopt.h>
-#include <limits.h>
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <sys/syscall.h>
-#include <sys/types.h>
-#include <time.h>
-#include <unistd.h>
-
-#include "nsjail.h"
-
-namespace log {
-
-static int log_fd = STDERR_FILENO;
-static bool log_fd_isatty = true;
-static enum llevel_t log_level = INFO;
-
-#define _LOG_DEFAULT_FILE "/var/log/nsjail.log"
-
-__attribute__((constructor)) static void log_init(void) { log_fd_isatty = isatty(log_fd); }
-
-/*
- * 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 initLogFile(nsjconf_t* nsjconf) {
-       /* Close previous log_fd */
-       if (log_fd > STDERR_FILENO) {
-               close(log_fd);
-               log_fd = STDERR_FILENO;
-       }
-       log_fd = nsjconf->log_fd;
-       log_level = nsjconf->loglevel;
-
-       if (nsjconf->logfile.empty() && nsjconf->daemonize) {
-               nsjconf->logfile = _LOG_DEFAULT_FILE;
-       }
-       if (nsjconf->logfile.empty()) {
-               log_fd = fcntl(log_fd, F_DUPFD_CLOEXEC, 0);
-       } else {
-               if (TEMP_FAILURE_RETRY(log_fd = open(nsjconf->logfile.c_str(),
-                                          O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) {
-                       log_fd = STDERR_FILENO;
-                       PLOG_E("Couldn't open logfile open('%s')", nsjconf->logfile.c_str());
-                       return false;
-               }
-       }
-       log_fd_isatty = (isatty(log_fd) == 1 ? true : false);
-       return true;
-}
-
-void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
-       if (ll < log_level) {
-               return;
-       }
-
-       char strerr[512];
-       if (perr) {
-               snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
-       }
-       struct ll_t {
-               const char* const descr;
-               const char* const prefix;
-               const bool print_funcline;
-               const bool print_time;
-       };
-       static struct ll_t const logLevels[] = {
-           {"D", "\033[0;4m", true, true},
-           {"I", "\033[1m", false, true},
-           {"W", "\033[0;33m", true, true},
-           {"E", "\033[1;31m", true, true},
-           {"F", "\033[7;35m", true, true},
-           {"HR", "\033[0m", false, false},
-           {"HB", "\033[1m", false, false},
-       };
-
-       time_t ltstamp = time(NULL);
-       struct tm utctime;
-       localtime_r(&ltstamp, &utctime);
-       char timestr[32];
-       if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
-               timestr[0] = '\0';
-       }
-
-       /* Start printing logs */
-       if (log_fd_isatty) {
-               dprintf(log_fd, "%s", logLevels[ll].prefix);
-       }
-       if (logLevels[ll].print_time) {
-               dprintf(log_fd, "[%s] ", timestr);
-       }
-       if (logLevels[ll].print_funcline) {
-               dprintf(log_fd, "[%s][%d] %s():%d ", logLevels[ll].descr, (int)getpid(), fn, ln);
-       }
-
-       va_list args;
-       va_start(args, fmt);
-       vdprintf(log_fd, fmt, args);
-       va_end(args);
-       if (perr) {
-               dprintf(log_fd, ": %s", strerr);
-       }
-       if (log_fd_isatty) {
-               dprintf(log_fd, "\033[0m");
-       }
-       dprintf(log_fd, "\n");
-       /* End printing logs */
-
-       if (ll == FATAL) {
-               exit(0xff);
-       }
-}
-
-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
deleted file mode 100644 (file)
index 3db7526..0000000
--- a/log.h
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
-
-   nsjail - logging
-   -----------------------------------------
-
-   Copyright 2014 Google Inc. All Rights Reserved.
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-     http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
-
-*/
-
-#ifndef NS_LOG_H
-#define NS_LOG_H
-
-#include <getopt.h>
-#include <stdbool.h>
-
-#include "nsjail.h"
-
-#define LOG_HELP(...) log::logMsg(HELP, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-#define LOG_HELP_BOLD(...) \
-       log::logMsg(HELP_BOLD, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-
-#define LOG_D(...) log::logMsg(DEBUG, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-#define LOG_I(...) log::logMsg(INFO, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-#define LOG_W(...) log::logMsg(WARNING, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-#define LOG_E(...) log::logMsg(ERROR, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-#define LOG_F(...) log::logMsg(FATAL, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
-
-#define PLOG_D(...) log::logMsg(DEBUG, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
-#define PLOG_I(...) log::logMsg(INFO, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
-#define PLOG_W(...) log::logMsg(WARNING, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
-#define PLOG_E(...) log::logMsg(ERROR, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
-#define PLOG_F(...) log::logMsg(FATAL, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
-
-namespace log {
-
-bool initLogFile(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);
-
-}  // namespace log
-
-#endif /* NS_LOG_H */
diff --git a/logs.cc b/logs.cc
new file mode 100644 (file)
index 0000000..32115d9
--- /dev/null
+++ b/logs.cc
@@ -0,0 +1,140 @@
+/*
+
+   nsjail - logging
+   -----------------------------------------
+
+   Copyright 2014 Google Inc. All Rights Reserved.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+*/
+
+#include "logs.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/syscall.h>
+#include <sys/types.h>
+#include <time.h>
+#include <unistd.h>
+
+#include "nsjail.h"
+
+#include <string.h>
+
+namespace logs {
+
+static int log_fd = STDERR_FILENO;
+static bool log_fd_isatty = true;
+static enum llevel_t log_level = INFO;
+
+__attribute__((constructor)) static void log_init(void) { log_fd_isatty = isatty(log_fd); }
+
+/*
+ * 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 initLog(const std::string& logfile, llevel_t level) {
+       /* Close previous log_fd */
+       if (log_fd > STDERR_FILENO) {
+               close(log_fd);
+               log_fd = STDERR_FILENO;
+       }
+       log_level = level;
+       if (logfile.empty()) {
+               log_fd = fcntl(log_fd, F_DUPFD_CLOEXEC, 0);
+       } else {
+               if (TEMP_FAILURE_RETRY(log_fd = open(logfile.c_str(),
+                                          O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)) == -1) {
+                       log_fd = STDERR_FILENO;
+                       log_fd_isatty = (isatty(log_fd) == 1 ? true : false);
+                       PLOG_E("Couldn't open logfile open('%s')", logfile.c_str());
+                       return false;
+               }
+       }
+       log_fd_isatty = (isatty(log_fd) == 1 ? true : false);
+       return true;
+}
+
+void logMsg(enum llevel_t ll, const char* fn, int ln, bool perr, const char* fmt, ...) {
+       if (ll < log_level) {
+               return;
+       }
+
+       char strerr[512];
+       if (perr) {
+               snprintf(strerr, sizeof(strerr), "%s", strerror(errno));
+       }
+       struct ll_t {
+               const char* const descr;
+               const char* const prefix;
+               const bool print_funcline;
+               const bool print_time;
+       };
+       static struct ll_t const logLevels[] = {
+           {"D", "\033[0;4m", true, true},
+           {"I", "\033[1m", false, true},
+           {"W", "\033[0;33m", true, true},
+           {"E", "\033[1;31m", true, true},
+           {"F", "\033[7;35m", true, true},
+           {"HR", "\033[0m", false, false},
+           {"HB", "\033[1m", false, false},
+       };
+
+       time_t ltstamp = time(NULL);
+       struct tm utctime;
+       localtime_r(&ltstamp, &utctime);
+       char timestr[32];
+       if (strftime(timestr, sizeof(timestr) - 1, "%FT%T%z", &utctime) == 0) {
+               timestr[0] = '\0';
+       }
+
+       /* Start printing logs */
+       if (log_fd_isatty) {
+               dprintf(log_fd, "%s", logLevels[ll].prefix);
+       }
+       if (logLevels[ll].print_time) {
+               dprintf(log_fd, "[%s] ", timestr);
+       }
+       if (logLevels[ll].print_funcline) {
+               dprintf(log_fd, "[%s][%d] %s():%d ", logLevels[ll].descr, (int)getpid(), fn, ln);
+       }
+
+       va_list args;
+       va_start(args, fmt);
+       vdprintf(log_fd, fmt, args);
+       va_end(args);
+       if (perr) {
+               dprintf(log_fd, ": %s", strerr);
+       }
+       if (log_fd_isatty) {
+               dprintf(log_fd, "\033[0m");
+       }
+       dprintf(log_fd, "\n");
+       /* End printing logs */
+
+       if (ll == FATAL) {
+               exit(0xff);
+       }
+}
+
+void logStop(int sig) { LOG_I("Server stops due to fatal signal (%d) caught. Exiting", sig); }
+
+}  // namespace logs
diff --git a/logs.h b/logs.h
new file mode 100644 (file)
index 0000000..d17413e
--- /dev/null
+++ b/logs.h
@@ -0,0 +1,65 @@
+/*
+
+   nsjail - logging
+   -----------------------------------------
+
+   Copyright 2014 Google Inc. All Rights Reserved.
+
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+
+*/
+
+#ifndef NS_LOGS_H
+#define NS_LOGS_H
+
+#include <getopt.h>
+#include <stdbool.h>
+
+#include <string>
+
+namespace logs {
+
+#define LOG_HELP(...) logs::logMsg(logs::HELP, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+#define LOG_HELP_BOLD(...) \
+       logs::logMsg(logs::HELP_BOLD, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+
+#define LOG_D(...) logs::logMsg(logs::DEBUG, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+#define LOG_I(...) logs::logMsg(logs::INFO, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+#define LOG_W(...) logs::logMsg(logs::WARNING, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+#define LOG_E(...) logs::logMsg(logs::ERROR, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+#define LOG_F(...) logs::logMsg(logs::FATAL, __PRETTY_FUNCTION__, __LINE__, false, __VA_ARGS__);
+
+#define PLOG_D(...) logs::logMsg(logs::DEBUG, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
+#define PLOG_I(...) logs::logMsg(logs::INFO, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
+#define PLOG_W(...) logs::logMsg(logs::WARNING, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
+#define PLOG_E(...) logs::logMsg(logs::ERROR, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
+#define PLOG_F(...) logs::logMsg(logs::FATAL, __PRETTY_FUNCTION__, __LINE__, true, __VA_ARGS__);
+
+enum llevel_t {
+       DEBUG = 0,
+       INFO,
+       WARNING,
+       ERROR,
+       FATAL,
+       HELP,
+       HELP_BOLD,
+};
+
+bool initLog(const std::string& logfile, llevel_t loglevel);
+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);
+
+}  // namespace logs
+
+#endif /* NS_LOGS_H */
diff --git a/mnt.cc b/mnt.cc
index 5d331a790e0db05f3e73474c7fc1a12e1250fe6f..4ab11daca64fc06379db23be3442c8fb9c93f5d2 100644 (file)
--- a/mnt.cc
+++ b/mnt.cc
@@ -40,7 +40,7 @@
 #include <syscall.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "subproc.h"
 #include "util.h"
diff --git a/net.cc b/net.cc
index 0bab6fd0808af4b23269b5e6bc925a39621eabd7..d22938d733d6ddb775769e12ba1ab98fb0d99d76 100644 (file)
--- a/net.cc
+++ b/net.cc
@@ -39,7 +39,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "subproc.h"
 
 extern char** environ;
index d4e638051b17d993038e5f50d1113c9ae5f2c436..0ad8f904234ffba7d5d65f55ae19bfda99800735 100644 (file)
--- a/nsjail.cc
+++ b/nsjail.cc
@@ -30,7 +30,7 @@
 #include <unistd.h>
 
 #include "cmdline.h"
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "net.h"
 #include "subproc.h"
@@ -112,7 +112,7 @@ static void nsjailListenMode(nsjconf_t* nsjconf) {
        for (;;) {
                if (nsjailSigFatal > 0) {
                        subproc::killAll(nsjconf);
-                       log::logStop(nsjailSigFatal);
+                       logs::logStop(nsjailSigFatal);
                        close(listenfd);
                        return;
                }
@@ -147,7 +147,7 @@ static int nsjailStandaloneMode(nsjconf_t* nsjconf) {
                }
                if (nsjailSigFatal > 0) {
                        subproc::killAll(nsjconf);
-                       log::logStop(nsjailSigFatal);
+                       logs::logStop(nsjailSigFatal);
                        return -1;
                }
 
index 10d0d452cd9f225f0ee8d6ce75fbb3d9a4b1904b..acfdbe3339b4e31f99ef19a3fd2989df98ca45e3 100644 (file)
--- a/nsjail.h
+++ b/nsjail.h
@@ -35,6 +35,8 @@
 #include <string>
 #include <vector>
 
+#include "logs.h"
+
 static const int nssigs[] = {
     SIGINT,
     SIGQUIT,
@@ -79,16 +81,6 @@ enum ns_mode_t {
        MODE_STANDALONE_RERUN
 };
 
-enum llevel_t {
-       DEBUG = 0,
-       INFO,
-       WARNING,
-       ERROR,
-       FATAL,
-       HELP,
-       HELP_BOLD,
-};
-
 struct nsjconf_t {
        const char* exec_file;
        bool use_execveat;
@@ -99,9 +91,8 @@ struct nsjconf_t {
        std::string chroot;
        int port;
        const char* bindhost;
-       int log_fd;
        std::string logfile;
-       enum llevel_t loglevel;
+       logs::llevel_t loglevel;
        bool daemonize;
        time_t tlimit;
        size_t max_cpus;
diff --git a/pid.cc b/pid.cc
index 1769cd8efd3aade186342e02f5ae78809cac9866..593018b0dc28419b18f08f0390cb1a327a641a6c 100644 (file)
--- a/pid.cc
+++ b/pid.cc
@@ -28,7 +28,7 @@
 #include <sys/prctl.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "subproc.h"
 
 namespace pid {
index 9eb6b60916bba9e8d9927b6295e32b3da36f7460..6d58f43f5f271deeff76e74e670cd9255bccc8f5 100644 (file)
@@ -29,7 +29,7 @@
 extern "C" {
 #include "kafel.h"
 }
-#include "log.h"
+#include "logs.h"
 
 namespace sandbox {
 
index 04f665a3dfff5d87d3b48108c5985702e43a55ed..51e06cedf2f4ff8747a4f8e0c2d77aa50bfb5659 100644 (file)
@@ -43,7 +43,7 @@
 
 #include "cgroup.h"
 #include "contain.h"
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "net.h"
 #include "sandbox.h"
diff --git a/user.cc b/user.cc
index 2fa13a52b5f2885f54ad755ae733ef324d57c7cc..9c2963f15ffaf404eb9919ed9898617e72930d64 100644 (file)
--- a/user.cc
+++ b/user.cc
@@ -38,7 +38,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 #include "subproc.h"
 #include "util.h"
diff --git a/util.cc b/util.cc
index 2364092a138ffbc6125872f7cc77fa7a3c1a4f71..1257c885646659cb1585afdadb2bf4aaab2bed42 100644 (file)
--- a/util.cc
+++ b/util.cc
@@ -40,7 +40,7 @@
 #include <time.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 #include "macros.h"
 
 namespace util {
diff --git a/uts.cc b/uts.cc
index dea97598a4e2a3d7e856318ae78fcac21e94dedc..db6450bf0b97924b50267b1c354e549685249c22 100644 (file)
--- a/uts.cc
+++ b/uts.cc
@@ -24,7 +24,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "log.h"
+#include "logs.h"
 
 namespace uts {