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
This commit is contained in:
Gleb Smirnoff
2025-09-28 07:54:53 -07:00
parent 81df9522c8
commit bbec2c9a6d
2 changed files with 53 additions and 2 deletions
+1 -1
View File
@@ -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 */
+52 -1
View File
@@ -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}"