1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 19:19:47 +01:00

Implement simple log rotation

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-08-16 00:10:03 +02:00
parent 4311b9a2f1
commit d62ee95689
3 changed files with 68 additions and 12 deletions

View file

@ -26,6 +26,8 @@ typedef struct log_backend_t {
void (*cleanup)(struct log_backend_t *log); void (*cleanup)(struct log_backend_t *log);
int (*write)(struct log_backend_t *log, const syslog_msg_t *msg); int (*write)(struct log_backend_t *log, const syslog_msg_t *msg);
void (*rotate)(struct log_backend_t *log);
} log_backend_t; } log_backend_t;

View file

@ -85,6 +85,22 @@ typedef struct {
} log_backend_file_t; } log_backend_file_t;
static int logfile_open(logfile_t *file)
{
file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640);
if (file->fd < 0) {
perror(file->filename);
return -1;
}
if (lseek(file->fd, 0, SEEK_END)) {
close(file->fd);
return -1;
}
return 0;
}
static logfile_t *logfile_create(const char *filename) static logfile_t *logfile_create(const char *filename)
{ {
logfile_t *file = calloc(1, sizeof(*file) + strlen(filename) + 1); logfile_t *file = calloc(1, sizeof(*file) + strlen(filename) + 1);
@ -96,29 +112,23 @@ static logfile_t *logfile_create(const char *filename)
strcpy(file->filename, filename); strcpy(file->filename, filename);
file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640); if (logfile_open(file)) {
if (file->fd < 0) {
perror(file->filename);
goto fail;
}
if (lseek(file->fd, 0, SEEK_END))
goto fail_fd;
return file;
fail_fd:
close(file->fd);
fail:
free(file); free(file);
return NULL; return NULL;
} }
return file;
}
static int logfile_write(logfile_t *file, const syslog_msg_t *msg) static int logfile_write(logfile_t *file, const syslog_msg_t *msg)
{ {
const char *lvl_str; const char *lvl_str;
char timebuf[32]; char timebuf[32];
struct tm tm; struct tm tm;
if (file->fd < 0 && logfile_open(file) != 0)
return -1;
lvl_str = enum_to_name(levels, msg->level); lvl_str = enum_to_name(levels, msg->level);
if (lvl_str == NULL) if (lvl_str == NULL)
return -1; return -1;
@ -133,6 +143,30 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg)
return 0; return 0;
} }
static int logfile_rotate(logfile_t *f)
{
char timebuf[32];
char *filename;
struct tm tm;
time_t now;
now = time(NULL);
gmtime_r(&now, &tm);
strftime(timebuf, sizeof(timebuf), "%FT%T", &tm);
filename = alloca(strlen(f->filename) + strlen(timebuf) + 2);
sprintf(filename, "%s.%s", f->filename, timebuf);
if (rename(f->filename, filename)) {
perror(filename);
return -1;
}
close(f->fd);
logfile_open(f);
return 0;
}
/*****************************************************************************/ /*****************************************************************************/
static int file_backend_init(log_backend_t *log) static int file_backend_init(log_backend_t *log)
@ -210,11 +244,21 @@ static int file_backend_write(log_backend_t *backend, const syslog_msg_t *msg)
return logfile_write(f, msg); return logfile_write(f, msg);
} }
static void file_backend_rotate(log_backend_t *backend)
{
log_backend_file_t *log = (log_backend_file_t *)backend;
logfile_t *f;
for (f = log->list; f != NULL; f = f->next)
logfile_rotate(f);
}
log_backend_file_t filebackend = { log_backend_file_t filebackend = {
.base = { .base = {
.init = file_backend_init, .init = file_backend_init,
.cleanup = file_backend_cleanup, .cleanup = file_backend_cleanup,
.write = file_backend_write, .write = file_backend_write,
.rotate = file_backend_rotate,
}, },
.list = NULL, .list = NULL,
}; };

View file

@ -34,6 +34,7 @@
static volatile sig_atomic_t syslog_run = 1; static volatile sig_atomic_t syslog_run = 1;
static volatile sig_atomic_t syslog_rotate = 0;
static void sighandler(int signo) static void sighandler(int signo)
@ -43,6 +44,9 @@ static void sighandler(int signo)
case SIGTERM: case SIGTERM:
syslog_run = 0; syslog_run = 0;
break; break;
case SIGHUP:
syslog_rotate = 1;
break;
default: default:
break; break;
} }
@ -57,6 +61,7 @@ static void signal_setup(void)
sigaction(SIGINT, &act, NULL); sigaction(SIGINT, &act, NULL);
sigaction(SIGTERM, &act, NULL); sigaction(SIGTERM, &act, NULL);
sigaction(SIGHUP, &act, NULL);
} }
static int handle_data(int fd) static int handle_data(int fd)
@ -91,6 +96,11 @@ int main(void)
goto out; goto out;
while (syslog_run) { while (syslog_run) {
if (syslog_rotate) {
logmgr->rotate(logmgr);
syslog_rotate = 0;
}
handle_data(sfd); handle_data(sfd);
} }