Commit 5cfa21b6 authored by jan.koester's avatar jan.koester
Browse files

logs

parent ff8cab71
Loading
Loading
Loading
Loading
+21 −3
Changes for src/main.cpp: 21 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -234,9 +234,27 @@ void daemonize() {
    int devnull = ::open("/dev/null", O_RDWR);
    if (devnull >= 0) {
        ::dup2(devnull, STDIN_FILENO);
        ::dup2(devnull, STDOUT_FILENO);
        ::dup2(devnull, STDERR_FILENO);
        if (devnull > STDERR_FILENO) ::close(devnull);
        ::close(devnull);
    }

    // stdout/stderr go to a real file instead of /dev/null: this process's own uncaught-
    // exception handlers (this file's, and several in libnetplus's event loop -- e.g.
    // EventWorker's worker threads) only ever std::cerr, never proxyplus's own structured
    // logging.targets (a separate, higher-level mechanism for request/access logs, not for
    // these low-level panics) -- with stderr silently going to /dev/null as before, none of
    // that ever left a trace. Opened here (still root, before dropPrivileges()) so it exists
    // regardless of the configured process user; the fd stays valid across the later setuid/
    // setgid since dropping privileges doesn't close already-open descriptors. Falls back to
    // /dev/null on failure (e.g. /var/log/proxyplus missing) -- non-fatal, matches this
    // codebase's convention elsewhere (see raiseFileDescriptorLimit()) of never refusing to
    // start over a best-effort diagnostic feature.
    int logfd = ::open("/var/log/proxyplus/proxyplus.log",
                        O_WRONLY | O_CREAT | O_APPEND, 0640);
    if (logfd < 0) logfd = ::open("/dev/null", O_RDWR);
    if (logfd >= 0) {
        ::dup2(logfd, STDOUT_FILENO);
        ::dup2(logfd, STDERR_FILENO);
        if (logfd > STDERR_FILENO) ::close(logfd);
    }
}
#endif