config: simplify log/logfd setting
authorRobert Swiecki <robert@swiecki.net>
Wed, 2 Oct 2019 17:43:58 +0000 (19:43 +0200)
committerRobert Swiecki <robert@swiecki.net>
Wed, 2 Oct 2019 17:43:58 +0000 (19:43 +0200)
cmdline.cc
config.cc
config.proto
configs/znc-with-net.cfg
logs.cc
logs.h

index 39795ef..25c6555 100644 (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;
index d7e32df..47f400d 100644 (file)
--- 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:
index c82ef93..1463701 100644 (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 */
index 88b9640..fc04f33 100644 (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
diff --git a/logs.cc b/logs.cc
index 7099429..d377505 100644 (file)
--- 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 (file)
--- 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