From ea24aa537862e8287ed32a146ee47ae54fa1ea28 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Tue, 25 Sep 2018 17:14:10 +0200 Subject: [PATCH] Usability, size modifiers to log rotate: 100k:5, 1G:3, and 100M:5 This applies to both the command line '-b SIZE' option and the optional per log file setting. Modifiers supported are: k, M, G Signed-off-by: Joachim Nilsson --- man/sysklogd.8 | 3 +++ man/syslog.conf.5 | 9 +++++---- src/syslogd.c | 45 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/man/sysklogd.8 b/man/sysklogd.8 index b60330f..0a63a8f 100644 --- a/man/sysklogd.8 +++ b/man/sysklogd.8 @@ -116,6 +116,9 @@ per log file, see .BR syslog.conf (5) for details. +The size argument takes optional modifiers; k, M, G. E.g., 100M is +100MB, 42k is 42 kB, etc. + Default: disabled (0). .TP .BI "\-c " "count" diff --git a/man/syslog.conf.5 b/man/syslog.conf.5 index 96f6758..1bb1f03 100644 --- a/man/syslog.conf.5 +++ b/man/syslog.conf.5 @@ -336,15 +336,16 @@ and store them in the file .fi .LP The following is almost the same but will also log rotate and compress -aged out messages. Notice the leading '-' to ensure the file is flushed -to disk after each message. +aged out messages. The size argument takes the same modifiers as the +command line '-b' option. Notice the leading '-' to ensure the file is +flushed to disk after each message. .IP .nf # Log all messages, including kernel, to messages file -# rotated every 100 kiB and keep up to 10 aged out and +# rotated every 100 kB and keep up to 10 aged out and # compressed files. -*.*;kern,kern.none -/log/messages 1048576:10 +*.*;kern,kern.none -/log/messages 100k:10 .fi .LP diff --git a/src/syslogd.c b/src/syslogd.c index cd269b5..c5949a1 100644 --- a/src/syslogd.c +++ b/src/syslogd.c @@ -842,6 +842,7 @@ void die(int sig); void doexit(int sig); #endif void init(); +static int strtobytes(char *arg); void cfline(char *line, struct filed *f); int decode(char *name, struct code *codetab); static void logit(char *, ...); @@ -930,7 +931,7 @@ int main(int argc, char *argv[]) break; case 'b': /* Max file size (bytes) before rotating log file. */ - RotateSz = atoi(optarg); + RotateSz = strtobytes(optarg); break; case 'c': /* Number (count) of log files to keep. */ @@ -2744,6 +2745,35 @@ void init(void) } /* balance parentheses for emacs */ #endif +static int strtobytes(char *arg) +{ + int mod = 0, bytes; + size_t pos; + + if (!arg) + return -1; + + pos = strspn(arg, "0123456789"); + if (arg[pos] != 0) { + if (arg[pos] == 'G') + mod = 3; + else if (arg[pos] == 'M') + mod = 2; + else if (arg[pos] == 'k') + mod = 1; + else + return -1; + + arg[pos] = 0; + } + + bytes = atoi(arg); + while (mod--) + bytes *= 1000; + + return bytes; +} + /* * Crack a configuration file line */ @@ -2930,16 +2960,23 @@ void cfline(char *line, struct filed *f) case '|': case '/': /* Look for optional per-file rotate BYTES:COUNT */ - for (q = p; !isspace(*q); q++) + for (q = p; *q && !isspace(*q); q++) ; if (isspace(*q)) { + char *c; int sz = 0, cnt = 0; *q++ = 0; - while (*q == '\t' || *q == ' ') + while (*q && isspace(*q)) q++; - sscanf(q, "%d:%d", &sz, &cnt); + c = strchr(q, ':'); + if (c) { + *c++ = 0; + cnt = atoi(c); + } + + sz = strtobytes(q); if (sz > 0 && cnt > 0) { f->f_rotatecount = cnt; f->f_rotatesz = sz;