From b3d544d155f5d1543dce1bd3e5327ef41583815a Mon Sep 17 00:00:00 2001 From: Robert Swiecki Date: Wed, 2 Oct 2019 19:43:58 +0200 Subject: [PATCH] config: simplify log/logfd setting --- cmdline.cc | 6 +++--- config.cc | 5 +++-- config.proto | 2 +- configs/znc-with-net.cfg | 2 ++ logs.cc | 16 +++++++++------- logs.h | 2 +- 6 files changed, 19 insertions(+), 14 deletions(-) diff --git a/cmdline.cc b/cmdline.cc index 39795ef..25c6555 100644 --- a/cmdline.cc +++ b/cmdline.cc @@ -507,10 +507,10 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { nsjconf->max_conns_per_ip = strtoul(optarg, NULL, 0); break; case 'l': - logs::logFile(optarg); + logs::logFile(optarg, STDERR_FILENO); break; case 'L': - logs::logFile(std::string("/dev/fd/") + optarg); + logs::logFile("", std::strtol(optarg, NULL, 0)); break; case 'd': nsjconf->daemonize = true; @@ -862,7 +862,7 @@ std::unique_ptr parseArgs(int argc, char* argv[]) { } if (nsjconf->daemonize && !logs::logSet()) { - logs::logFile(_LOG_DEFAULT_FILE); + logs::logFile(_LOG_DEFAULT_FILE, STDERR_FILENO); } if (!setupMounts(nsjconf.get())) { return nullptr; diff --git a/config.cc b/config.cc index d7e32df..47f400d 100644 --- a/config.cc +++ b/config.cc @@ -96,11 +96,12 @@ static bool configParseInternal(nsjconf_t* nsjconf, const nsjail::NsJailConfig& nsjconf->daemonize = njc.daemon(); if (njc.has_log_fd()) { - logs::logFile(std::string("/dev/fd/") + std::to_string(njc.log_fd())); + logs::logFile("", njc.log_fd()); } if (njc.has_log_file()) { - logs::logFile(njc.log_file()); + logs::logFile(njc.log_file(), STDERR_FILENO); } + if (njc.has_log_level()) { switch (njc.log_level()) { case nsjail::LogLevel::DEBUG: diff --git a/config.proto b/config.proto index c82ef93..1463701 100644 --- a/config.proto +++ b/config.proto @@ -106,7 +106,7 @@ message NsJailConfig { /* FD to log to. */ optional int32 log_fd = 16; - /* File to save logs to */ + /* File to save logs to. */ optional string log_file = 17; /* Minimum log level displayed. See 'msg LogLevel' description for more */ diff --git a/configs/znc-with-net.cfg b/configs/znc-with-net.cfg index 88b9640..fc04f33 100644 --- a/configs/znc-with-net.cfg +++ b/configs/znc-with-net.cfg @@ -17,6 +17,8 @@ time_limit: 0 envar: "HOME=/home/znc" envar: "TMP=/tmp" +log_fd: 2 + rlimit_as: 4096 rlimit_cpu_type: INF rlimit_fsize: 4096 diff --git a/logs.cc b/logs.cc index 7099429..d377505 100644 --- a/logs.cc +++ b/logs.cc @@ -74,19 +74,21 @@ void logLevel(enum llevel_t ll) { _log_level = ll; } -void logFile(const std::string& logfile) { +void logFile(const std::string& log_file, int log_fd) { _log_set = true; - int newlogfd = TEMP_FAILURE_RETRY( - open(logfile.c_str(), O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)); - if (newlogfd == -1) { - PLOG_W("Couldn't open logfile open('%s')", logfile.c_str()); - return; + int newlogfd = -1; + if (!log_file.empty()) { + newlogfd = TEMP_FAILURE_RETRY( + open(log_file.c_str(), O_CREAT | O_RDWR | O_APPEND | O_CLOEXEC, 0640)); + if (newlogfd == -1) { + PLOG_W("Couldn't open('%s')", log_file.c_str()); + } } /* Close previous log_fd */ if (_log_fd > STDERR_FILENO) { close(_log_fd); } - setDupLogFdOr(newlogfd, STDERR_FILENO); + setDupLogFdOr(newlogfd, log_fd); close(newlogfd); } diff --git a/logs.h b/logs.h index 27727ef..36e9813 100644 --- a/logs.h +++ b/logs.h @@ -59,7 +59,7 @@ 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); void logLevel(enum llevel_t ll); -void logFile(const std::string& logfile); +void logFile(const std::string& log_file, int log_fd); bool logSet(); } // namespace logs -- 2.34.1