Browse Source

[core] fdevent_cycle_logger()

fdevent_cycle_logger() re-opens log files before closing existing fd
personal/stbuehler/mod-csrf
Glenn Strauss 5 years ago
parent
commit
ad7d6a6b15
  1. 10
      src/fdevent.c
  2. 1
      src/fdevent.h
  3. 7
      src/mod_accesslog.c
  4. 21
      src/server.c

10
src/fdevent.c

@ -594,6 +594,16 @@ int fdevent_open_logger(const char *logger) {
}
}
int fdevent_cycle_logger(const char *logger, int *curfd) {
if (logger[0] != '|') {
int fd = fdevent_open_logger(logger);
if (-1 == fd) return -1; /*(error; leave *curfd as-is)*/
if (-1 != *curfd) close(*curfd);
*curfd = fd;
}
return *curfd;
}
#include <sys/ioctl.h>
#ifdef HAVE_SYS_FILIO_H

1
src/fdevent.h

@ -224,6 +224,7 @@ int fdevent_open_dirname(char *path);
int fdevent_set_stdin_stdout_stderr(int fdin, int fdout, int fderr);
pid_t fdevent_fork_execve(const char *name, char *argv[], char *envp[], int fdin, int fdout, int fderr, int dfd);
int fdevent_open_logger(const char *logger);
int fdevent_cycle_logger(const char *logger, int *curfd);
int fdevent_select_init(fdevents *ev);
int fdevent_poll_init(fdevents *ev);

7
src/mod_accesslog.c

@ -666,13 +666,8 @@ SIGHUP_FUNC(log_access_cycle) {
&& !buffer_string_is_empty(s->access_logfile)
&& s->access_logfile->ptr[0] != '|') {
if (-1 != s->log_access_fd) close(s->log_access_fd);
if (-1 == (s->log_access_fd =
fdevent_open_cloexec(s->access_logfile->ptr, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) {
if (-1 == fdevent_cycle_logger(s->access_logfile->ptr, &s->log_access_fd)) {
log_error_write(srv, __FILE__, __LINE__, "ss", "cycling access-log failed:", strerror(errno));
return HANDLER_ERROR;
}
}

21
src/server.c

@ -882,26 +882,11 @@ static int log_error_cycle(server *srv) {
if (srv->errorlog_mode == ERRORLOG_FILE) {
const char *logfile = srv->srvconf.errorlog_file->ptr;
int new_fd;
if (-1 == (new_fd = fdevent_open_logger(logfile))) {
if (-1 == fdevent_cycle_logger(logfile, &srv->errorlog_fd)) {
/* write to old log */
log_error_write(srv, __FILE__, __LINE__, "SSSSS",
log_error_write(srv, __FILE__, __LINE__, "SSSS",
"cycling errorlog '", logfile,
"' failed: ", strerror(errno),
", falling back to syslog()");
close(srv->errorlog_fd);
srv->errorlog_fd = -1;
#ifdef HAVE_SYSLOG_H
srv->errorlog_mode = ERRORLOG_SYSLOG;
#endif
}
else {
/* ok, new log is open, close the old one */
close(srv->errorlog_fd);
srv->errorlog_fd = new_fd;
fdevent_setfd_cloexec(srv->errorlog_fd);
"' failed: ", strerror(errno));
}
}

Loading…
Cancel
Save