Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 28 Sep 2025 14:55:11 GMT
From:      Gleb Smirnoff <glebius@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: bbec2c9a6d9a - main - newsyslog: fix one hour rotation with frequent execution
Message-ID:  <202509281455.58SEtB3P045204@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by glebius:

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

commit bbec2c9a6d9a9b8f6c6edbdd2386dfdcd1c81422
Author:     Gleb Smirnoff <glebius@FreeBSD.org>
AuthorDate: 2025-09-28 14:54:53 +0000
Commit:     Gleb Smirnoff <glebius@FreeBSD.org>
CommitDate: 2025-09-28 14:54:53 +0000

    newsyslog: fix one hour rotation with frequent execution
    
    If a newsyslog.conf(5) has an entry that is configured to be rotated every
    hour, and crontab(8) is set to execute newsyslog several times per hour,
    the logic of age_old_log() is broken and it would rotate the entry too
    often.  AFAIU, the extra 1800 seconds were added to allow some leeway for
    the previous newsyslog invocation, that could have been too slow and the
    timestamp on the old file is newer than actual time of the previous
    newsyslog invocation.  But 30 minutes of leeway is way to much.  Reduce
    this down to 3 minutes, which would be a compromise between a potential
    need to run newsyslogd every 5 minutes and a situation when newsyslog
    takes a significant time to rotate the logs.
    
    Provide a test case for that.
    
    Reviewed by:            delphij
    Differential Revision:  https://reviews.freebsd.org/D52700
---
 usr.sbin/newsyslog/newsyslog.c          |  2 +-
 usr.sbin/newsyslog/tests/legacy_test.sh | 53 ++++++++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 2 deletions(-)

diff --git a/usr.sbin/newsyslog/newsyslog.c b/usr.sbin/newsyslog/newsyslog.c
index 90b9c8d716a5..084aeb36b052 100644
--- a/usr.sbin/newsyslog/newsyslog.c
+++ b/usr.sbin/newsyslog/newsyslog.c
@@ -2615,7 +2615,7 @@ age_old_log(const char *file)
 		mtime = sb.st_mtime;
 	}
 
-	return ((int)(ptimeget_secs(timenow) - mtime + 1800) / 3600);
+	return ((int)(ptimeget_secs(timenow) - mtime + 180) / 3600);
 }
 
 /* Skip Over Blanks */
diff --git a/usr.sbin/newsyslog/tests/legacy_test.sh b/usr.sbin/newsyslog/tests/legacy_test.sh
index c8c18a754977..ea0b0c6fc726 100644
--- a/usr.sbin/newsyslog/tests/legacy_test.sh
+++ b/usr.sbin/newsyslog/tests/legacy_test.sh
@@ -417,6 +417,51 @@ tests_time_rotate() {
 	tmpdir_clean
 }
 
+tests_interval_rotate() {
+	local hours ext h
+
+	hours="$1"
+	ext="$2"
+
+	tmpdir_create
+
+	begin "create file" -newdir
+	run_newsyslog -C
+	ckfe ${LOGFNAME}
+	end
+
+	# old file doesn't exist - forced rotation
+	begin "rotate interval 0"
+	run_newsyslog
+	ckfe ${LOGFNAME}
+	chkfcnt 1 ${dir}${LOGFNAME}.*
+	end
+
+	# emulate newsyslog runs every 5 minutes
+	begin "rotate interval less than ${hours} hours"
+	m=0
+	while [ $(expr ${m} / 60 ) -lt ${hours} ]; do
+		touch -t $(date -v -${m}M +%Y%m%d%H%M) ${LOGFNAME}.0
+		run_newsyslog
+		ckfe ${LOGFNAME}
+		chkfcnt 1 ${dir}${LOGFNAME}.*
+		if [ $OK != 1 ]; then
+			break;
+		fi
+		m=$(expr ${m} + 5)
+	done
+	end
+
+	begin "rotate interval ${hours} hours"
+	touch -t $(date -v -${hours}H +%Y%m%d%H%M) ${LOGFNAME}.0
+	run_newsyslog
+	ckfe ${LOGFNAME}
+	chkfcnt 2 ${dir}${LOGFNAME}.*
+	end
+
+	tmpdir_clean
+}
+
 tests_rfc5424() {
 	local dir ext name_postfix newsyslog_args
 
@@ -526,7 +571,7 @@ tests_normal_rotate_recompress() {
 	tmpdir_clean
 }
 
-echo 1..185
+echo 1..193
 mkdir -p ${TMPDIR}
 cd ${TMPDIR}
 
@@ -638,4 +683,10 @@ tests_p_flag_rotate ".gz"
 echo "$LOGFPATH 640  3     *    @T00  NCZ" > newsyslog.conf
 tests_normal_rotate_recompress
 
+# Interval based rotation
+echo "$LOGFPATH	640  3	   *	1  NC" > newsyslog.conf
+tests_interval_rotate 1
+echo "$LOGFPATH	640  3	   *	2  NC" > newsyslog.conf
+tests_interval_rotate 2
+
 rm -rf "${TMPDIR}"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202509281455.58SEtB3P045204>