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

usyslogd: keep track of log file size

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-08-16 18:20:27 +02:00
parent d62ee95689
commit 21b88d9352

View file

@ -74,6 +74,7 @@ static const enum_map_t facilities[] = {
typedef struct logfile_t { typedef struct logfile_t {
struct logfile_t *next; struct logfile_t *next;
size_t size;
int fd; int fd;
char filename[]; char filename[];
} logfile_t; } logfile_t;
@ -87,18 +88,26 @@ typedef struct {
static int logfile_open(logfile_t *file) static int logfile_open(logfile_t *file)
{ {
struct stat sb;
file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640); file->fd = open(file->filename, O_WRONLY | O_CREAT, 0640);
if (file->fd < 0) { if (file->fd < 0) {
perror(file->filename); perror(file->filename);
return -1; return -1;
} }
if (lseek(file->fd, 0, SEEK_END)) { if (lseek(file->fd, 0, SEEK_END))
close(file->fd); goto fail;
return -1;
}
if (fstat(file->fd, &sb))
goto fail;
file->size = sb.st_size;
return 0; return 0;
fail:
close(file->fd);
file->fd = -1;
return -1;
} }
static logfile_t *logfile_create(const char *filename) static logfile_t *logfile_create(const char *filename)
@ -125,6 +134,7 @@ 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;
int ret;
if (file->fd < 0 && logfile_open(file) != 0) if (file->fd < 0 && logfile_open(file) != 0)
return -1; return -1;
@ -136,10 +146,13 @@ static int logfile_write(logfile_t *file, const syslog_msg_t *msg)
gmtime_r(&msg->timestamp, &tm); gmtime_r(&msg->timestamp, &tm);
strftime(timebuf, sizeof(timebuf), "%FT%T", &tm); strftime(timebuf, sizeof(timebuf), "%FT%T", &tm);
dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str, msg->pid, ret = dprintf(file->fd, "[%s][%s][%u] %s", timebuf, lvl_str, msg->pid,
msg->message); msg->message);
fsync(file->fd); fsync(file->fd);
if (ret > 0)
file->size += ret;
return 0; return 0;
} }