From 70629d73da5b0de709bece2d0ff38ac0b165ef77 Mon Sep 17 00:00:00 2001 From: Matthijs Mekking Date: Wed, 5 Apr 2023 17:50:12 +0200 Subject: [PATCH] Fix purging old log files with absolute file path Removing old timestamp or increment versions of log backup files did not work when the file is an absolute path: only the entry name was provided to the file remove function. The dirname was also bogus, since the file separater was put back too soon. Fix these issues to make log file rotation work when the file is configured to be an absolute path. --- lib/isc/log.c | 80 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 52 insertions(+), 28 deletions(-) diff --git a/lib/isc/log.c b/lib/isc/log.c index a24e1c37fe..e94f54c43c 100644 --- a/lib/isc/log.c +++ b/lib/isc/log.c @@ -1036,18 +1036,11 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) { isc_dir_init(&dir); result = isc_dir_open(&dir, dirname); - /* - * Replace the file separator if it was taken out. - */ - if (bname != file->name) { - *(bname - 1) = sep; - } - /* * Return if the directory open failed. */ if (result != ISC_R_SUCCESS) { - return (result); + goto out; } while (isc_dir_read(&dir) == ISC_R_SUCCESS) { @@ -1061,14 +1054,25 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) { * Remove any backup files that exceed versions. */ if (*digit_end == '\0' && version >= versions) { - result = isc_file_remove(dir.entry.name); + char rmfile[PATH_MAX + 1]; + int n = snprintf(rmfile, sizeof(rmfile), + "%s/%s", dirname, + dir.entry.name); + if (n >= (int)sizeof(rmfile) || n < 0) { + result = ISC_R_NOSPACE; + syslog(LOG_ERR, + "unable to remove log files: %s", + isc_result_totext(result)); + break; + } + result = isc_file_remove(rmfile); if (result != ISC_R_SUCCESS && - result != ISC_R_FILENOTFOUND) + result != ISC_R_NOTFOUND) { syslog(LOG_ERR, - "unable to remove " - "log file '%s': %s", - dir.entry.name, + "unable to remove log file " + "'%s': %s", + rmfile, isc_result_totext(result)); } } else if (*digit_end == '\0' && version > greatest) { @@ -1079,8 +1083,16 @@ greatest_version(isc_logfile_t *file, int versions, int *greatestp) { isc_dir_close(&dir); *greatestp = greatest; + result = ISC_R_SUCCESS; - return (ISC_R_SUCCESS); +out: + /* + * Replace the file separator if it was taken out. + */ + if (bname != file->name) { + *(bname - 1) = sep; + } + return (result); } static void @@ -1168,18 +1180,11 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { isc_dir_init(&dir); result = isc_dir_open(&dir, dirname); - /* - * Replace the file separator if it was taken out. - */ - if (bname != file->name) { - *(bname - 1) = sep; - } - /* * Return if the directory open failed. */ if (result != ISC_R_SUCCESS) { - return (result); + goto out; } last = last_to_keep(versions, &dir, bname, bnamelen); @@ -1198,14 +1203,25 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { * Remove any backup files that exceed versions. */ if (*digit_end == '\0' && version < last) { - result = isc_file_remove(dir.entry.name); + char rmfile[PATH_MAX + 1]; + int n = snprintf(rmfile, sizeof(rmfile), + "%s/%s", dirname, + dir.entry.name); + if (n >= (int)sizeof(rmfile) || n < 0) { + result = ISC_R_NOSPACE; + syslog(LOG_ERR, + "unable to remove log files: %s", + isc_result_totext(result)); + break; + } + result = isc_file_remove(rmfile); if (result != ISC_R_SUCCESS && - result != ISC_R_FILENOTFOUND) + result != ISC_R_NOTFOUND) { syslog(LOG_ERR, - "unable to remove " - "log file '%s': %s", - dir.entry.name, + "unable to remove log file " + "'%s': %s", + rmfile, isc_result_totext(result)); } } @@ -1213,8 +1229,16 @@ remove_old_tsversions(isc_logfile_t *file, int versions) { } isc_dir_close(&dir); + result = ISC_R_SUCCESS; - return (ISC_R_SUCCESS); +out: + /* + * Replace the file separator if it was taken out. + */ + if (bname != file->name) { + *(bname - 1) = sep; + } + return (result); } static isc_result_t