newsyslog.conf(5): Accept human unit suffix in the size filed

MFC after:	1 week
Pull Request:	https://github.com/freebsd/freebsd-src/pull/1505
This commit is contained in:
K Rin 2024-11-01 13:12:06 +08:00 committed by Li-Wen Hsu
parent ab540d44ba
commit 1f78bbb5c4
No known key found for this signature in database
GPG Key ID: 7377A4A02A2954DD
3 changed files with 23 additions and 7 deletions

View File

@ -6,7 +6,7 @@ CONFS= newsyslog.conf
PROG= newsyslog PROG= newsyslog
MAN= newsyslog.8 newsyslog.conf.5 MAN= newsyslog.8 newsyslog.conf.5
SRCS= newsyslog.c ptimes.c SRCS= newsyslog.c ptimes.c
LIBADD= sbuf LIBADD= sbuf util
HAS_TESTS= HAS_TESTS=
SUBDIR.${MK_TESTS}+= tests SUBDIR.${MK_TESTS}+= tests

View File

@ -77,6 +77,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <libgen.h> #include <libgen.h>
#include <libutil.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
@ -1335,9 +1336,21 @@ parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
badline("malformed line (missing fields):\n%s", badline("malformed line (missing fields):\n%s",
errline); errline);
*parse = '\0'; *parse = '\0';
if (isdigitch(*q)) if (isdigitch(*q)) {
working->trsize = atoi(q); char last_digit = q[strlen(q) - 1];
else if (strcmp(q, "*") == 0) if (isdigitch(last_digit))
working->trsize = atoi(q);
else {
uint64_t trsize = 0;
if (expand_number(q, &trsize) == 0)
working->trsize = trsize / 1024;
else {
working->trsize = -1;
warnx("Invalid value of '%s' for 'size' in line:\n%s",
q, errline);
}
}
} else if (strcmp(q, "*") == 0)
working->trsize = -1; working->trsize = -1;
else { else {
warnx("Invalid value of '%s' for 'size' in line:\n%s", warnx("Invalid value of '%s' for 'size' in line:\n%s",

View File

@ -18,7 +18,7 @@
.\" the suitability of this software for any purpose. It is .\" the suitability of this software for any purpose. It is
.\" provided "as is" without express or implied warranty. .\" provided "as is" without express or implied warranty.
.\" .\"
.Dd December 25, 2023 .Dd November 11, 2024
.Dt NEWSYSLOG.CONF 5 .Dt NEWSYSLOG.CONF 5
.Os .Os
.Sh NAME .Sh NAME
@ -116,8 +116,10 @@ Specify the maximum number of archive files which may exist.
This does not consider the current log file. This does not consider the current log file.
.It Ar size .It Ar size
When the size of the log file reaches When the size of the log file reaches
.Ar size .Ar size ,
in kilobytes, the log file will be trimmed as described above. in kilobytes by default, or with suffix suffixes like k, M, G, ... as supported by
.Xr expand_number 3 ,
the log file will be trimmed as described above.
If this field contains an asterisk If this field contains an asterisk
.Pq Ql * , .Pq Ql * ,
the log file will not be trimmed based on size. the log file will not be trimmed based on size.
@ -438,6 +440,7 @@ entry:
.Xr gzip 1 , .Xr gzip 1 ,
.Xr xz 1 , .Xr xz 1 ,
.Xr zstd 1 , .Xr zstd 1 ,
.Xr expand_number 3 ,
.Xr syslog 3 , .Xr syslog 3 ,
.Xr chown 8 , .Xr chown 8 ,
.Xr newsyslog 8 , .Xr newsyslog 8 ,