git: 1bfd6cb606da - stable/13 - newsyslog.conf(5): Accept human unit suffix in the size filed

From: Li-Wen Hsu <lwhsu_at_FreeBSD.org>
Date: Fri, 29 Nov 2024 05:21:32 UTC
The branch stable/13 has been updated by lwhsu:

URL: https://cgit.FreeBSD.org/src/commit/?id=1bfd6cb606da39604ea70e1e5fe8d28534010f99

commit 1bfd6cb606da39604ea70e1e5fe8d28534010f99
Author:     K Rin <rin@sandb0x.tw>
AuthorDate: 2024-11-01 05:12:06 +0000
Commit:     Li-Wen Hsu <lwhsu@FreeBSD.org>
CommitDate: 2024-11-29 05:20:41 +0000

    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
    
    (cherry picked from commit 1f78bbb5c415efefa782c1be7f9a2e4a5008903b)
    
    newsyslog.conf(5): Remove duplicate word
    
    Reported by:    jlduran
    Fixes:  1f78bbb5c415 newsyslog.conf(5): Accept human unit suffix in the size filed
    
    (cherry picked from commit af2015b01f7e741125aeb4a04109cc42f988cf0f)
---
 usr.sbin/newsyslog/Makefile         |  2 +-
 usr.sbin/newsyslog/newsyslog.c      | 19 ++++++++++++++++---
 usr.sbin/newsyslog/newsyslog.conf.5 |  9 ++++++---
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/usr.sbin/newsyslog/Makefile b/usr.sbin/newsyslog/Makefile
index b2e945b6215a..57881770a901 100644
--- a/usr.sbin/newsyslog/Makefile
+++ b/usr.sbin/newsyslog/Makefile
@@ -7,7 +7,7 @@ CONFS=	newsyslog.conf
 PROG=	newsyslog
 MAN=	newsyslog.8 newsyslog.conf.5
 SRCS=	newsyslog.c ptimes.c
-LIBADD=	sbuf
+LIBADD=	sbuf util
 
 HAS_TESTS=
 SUBDIR.${MK_TESTS}+= tests
diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c
index 115192058c82..a043e891aba0 100644
--- a/usr.sbin/newsyslog/newsyslog.c
+++ b/usr.sbin/newsyslog/newsyslog.c
@@ -76,6 +76,7 @@
 #include <signal.h>
 #include <stdio.h>
 #include <libgen.h>
+#include <libutil.h>
 #include <stdlib.h>
 #include <string.h>
 #include <syslog.h>
@@ -1223,9 +1224,21 @@ parse_file(FILE *cf, struct cflist *work_p, struct cflist *glob_p,
 			badline("malformed line (missing fields):\n%s",
 			    errline);
 		*parse = '\0';
-		if (isdigitch(*q))
-			working->trsize = atoi(q);
-		else if (strcmp(q, "*") == 0)
+		if (isdigitch(*q)) {
+			char last_digit = q[strlen(q) - 1];
+			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;
 		else {
 			warnx("Invalid value of '%s' for 'size' in line:\n%s",
diff --git a/usr.sbin/newsyslog/newsyslog.conf.5 b/usr.sbin/newsyslog/newsyslog.conf.5
index 0a516fc56785..fd23837a3990 100644
--- a/usr.sbin/newsyslog/newsyslog.conf.5
+++ b/usr.sbin/newsyslog/newsyslog.conf.5
@@ -20,7 +20,7 @@
 .\" the suitability of this software for any purpose.  It is
 .\" provided "as is" without express or implied warranty.
 .\"
-.Dd February 26, 2021
+.Dd November 11, 2024
 .Dt NEWSYSLOG.CONF 5
 .Os
 .Sh NAME
@@ -105,8 +105,10 @@ Specify the maximum number of archive files which may exist.
 This does not consider the current log file.
 .It Ar size
 When the size of the log file reaches
-.Ar size
-in kilobytes, the log file will be trimmed as described above.
+.Ar size ,
+in kilobytes by default, or with 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
 .Pq Ql * ,
 the log file will not be trimmed based on size.
@@ -414,6 +416,7 @@ entry:
 .Xr bzip2 1 ,
 .Xr gzip 1 ,
 .Xr xz 1 ,
+.Xr expand_number 3 ,
 .Xr syslog 3 ,
 .Xr chown 8 ,
 .Xr newsyslog 8 ,