diff --git a/src/shrpx.cc b/src/shrpx.cc index 9273f7e9..eb1a82b0 100644 --- a/src/shrpx.cc +++ b/src/shrpx.cc @@ -422,6 +422,7 @@ void reopen_log_signal_cb(struct ev_loop *loop, ev_signal *w, int revents) { } (void)reopen_log_files(); + redirect_stderr_to_errorlog(); if (get_config()->num_worker > 1) { conn_handler->worker_reopen_log_files(); @@ -636,6 +637,10 @@ int event_loop() { // We get new PID after successful daemon(). mod_config()->pid = getpid(); + + // daemon redirects stderr file descriptor to /dev/null, so we + // need this. + redirect_stderr_to_errorlog(); } if (get_config()->pid_file) { @@ -1916,6 +1921,8 @@ int main(int argc, char **argv) { exit(EXIT_FAILURE); } + redirect_stderr_to_errorlog(); + if (get_config()->uid != 0) { if (log_config()->accesslog_fd != -1 && fchown(log_config()->accesslog_fd, get_config()->uid, diff --git a/src/shrpx_log.cc b/src/shrpx_log.cc index 955c5dc7..421dcb06 100644 --- a/src/shrpx_log.cc +++ b/src/shrpx_log.cc @@ -324,4 +324,14 @@ int reopen_log_files() { return res; } +void redirect_stderr_to_errorlog() { + auto lgconf = log_config(); + + if (get_config()->errorlog_syslog || lgconf->errorlog_fd == -1) { + return; + } + + dup2(lgconf->errorlog_fd, STDERR_FILENO); +} + } // namespace shrpx diff --git a/src/shrpx_log.h b/src/shrpx_log.h index 14604633..65e67960 100644 --- a/src/shrpx_log.h +++ b/src/shrpx_log.h @@ -143,6 +143,8 @@ void upstream_accesslog(const std::vector &lf, LogSpec *lgsp); int reopen_log_files(); +void redirect_stderr_to_errorlog(); + } // namespace shrpx #endif // SHRPX_LOG_H