config: simplify log/logfd setting

This commit is contained in:
Robert Swiecki
2019-10-02 19:43:58 +02:00
parent 0b12cedc01
commit b3d544d155
6 changed files with 19 additions and 14 deletions

View File

@@ -507,10 +507,10 @@ std::unique_ptr<nsjconf_t> 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<nsjconf_t> 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;

View File

@@ -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:

View File

@@ -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 */

View File

@@ -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

16
logs.cc
View File

@@ -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);
}

2
logs.h
View File

@@ -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