Implement forced log file rotation upon SIGUSR2

This commit is contained in:
Steffen Nurpmeso 2022-01-20 14:34:51 +01:00 committed by Joachim Wiberg
parent 23a6d81ea2
commit 949e80f150
2 changed files with 82 additions and 43 deletions

View File

@ -483,6 +483,10 @@ SIGTERM.
.It USR1
In debug mode this switches debugging on/off. In normal operation
it is ignored.
.It USR2
.Nm
will rotate all files for which rotation is configured when receiving
this signal.
.El
.Pp
For convenience the PID is by default stored in

View File

@ -103,6 +103,7 @@ char ctty[] = _PATH_CONSOLE;
static volatile sig_atomic_t debugging_on;
static volatile sig_atomic_t restart;
static volatile sig_atomic_t rotate_signal;
/*
* Intervals at which we flush out "message repeated" messages,
@ -169,6 +170,9 @@ static void parsemsg(const char *from, char *msg);
static int opensys(const char *file);
static void printsys(char *msg);
static void logmsg(struct buf_msg *buffer);
static void logrotate(struct filed *f);
static void rotate_file(struct filed *f);
static void rotate_all_files(void);
static void fprintlog_first(struct filed *f, struct buf_msg *buffer);
static void fprintlog_successive(struct filed *f, int flags);
void endtty();
@ -192,6 +196,7 @@ static void notifier_add(struct notifiers *newn, const char *program);
static void notifier_invoke(const char *logfile);
static void notifier_free_all(void);
void reload(int);
static void signal_rotate(int sig);
static int validate(struct sockaddr *sa, const char *hname);
static int waitdaemon(int);
static void timedout(int);
@ -589,6 +594,12 @@ no_klogd:
continue;
}
if (rotate_signal > 0) {
rotate_signal = 0;
logit("\nReceived SIGUSR2, forcing log rotation.\n");
rotate_all_files();
}
if (rc < 0 && errno != EINTR)
ERR("select()");
@ -1560,7 +1571,7 @@ static void logmsg(struct buf_msg *buffer)
sigprocmask(SIG_UNBLOCK, &mask, NULL);
}
void logrotate(struct filed *f)
static void logrotate(struct filed *f)
{
struct stat statf;
@ -1571,7 +1582,12 @@ void logrotate(struct filed *f)
return;
/* bug (mostly harmless): can wrap around if file > 4gb */
if (S_ISREG(statf.st_mode) && statf.st_size > f->f_rotatesz) {
if (S_ISREG(statf.st_mode) && statf.st_size > f->f_rotatesz)
rotate_file(f);
}
static void rotate_file(struct filed *f)
{
if (f->f_rotatecount > 0) { /* always 0..999 */
int len = strlen(f->f_un.f_fname) + 10 + 5;
int i;
@ -1618,6 +1634,15 @@ void logrotate(struct filed *f)
}
ftruncate(f->f_file, 0);
}
static void rotate_all_files(void)
{
struct filed *f;
SIMPLEQ_FOREACH(f, &fhead, f_link) {
if (f->f_type == F_FILE && f->f_rotatesz)
rotate_file(f);
}
}
#define pushiov(iov, cnt, val) do { \
@ -2443,6 +2468,7 @@ static void signal_init(void)
SIGNAL(SIGINT, Debug ? die : SIG_IGN);
SIGNAL(SIGQUIT, Debug ? die : SIG_IGN);
SIGNAL(SIGUSR1, Debug ? debug_switch : SIG_IGN);
SIGNAL(SIGUSR2, signal_rotate);
SIGNAL(SIGXFSZ, SIG_IGN);
SIGNAL(SIGHUP, reload);
SIGNAL(SIGCHLD, reapchild);
@ -3445,6 +3471,15 @@ void reload(int signo)
restart++;
}
/*
* SIGUSR2: forced rotation for so-configured files as soon as possible.
*/
static void signal_rotate(int sig)
{
(void)sig;
rotate_signal++;
}
/**
* Local Variables:
* indent-tabs-mode: t