Author: simon
Date: Thu Apr 21 16:31:05 2011
New Revision: 220926
URL: http://svn.freebsd.org/changeset/base/220926

Log:
  Fix an old bug in newsyslog where we kept one log file more than was
  requested in newsyslog.conf.  This was only the case using the non-time
  based filenames (.0, .1, .2 etc.).
  
  The change also makes newsyslog clean clean up the old extra logfile so
  users don't end up with a single stale logfile which won't be rotated
  out.
  
  This change also cleans up some code a bit to avoid more copy / paste
  code and removes some old copy / paste code in the process.
  
  PR:           bin/76697
  MFC after:    2 weeks

Modified:
  head/usr.sbin/newsyslog/newsyslog.c

Modified: head/usr.sbin/newsyslog/newsyslog.c
==============================================================================
--- head/usr.sbin/newsyslog/newsyslog.c Thu Apr 21 14:39:33 2011        
(r220925)
+++ head/usr.sbin/newsyslog/newsyslog.c Thu Apr 21 16:31:05 2011        
(r220926)
@@ -1592,6 +1592,43 @@ delete_oldest_timelog(const struct conf_
 }
 
 /*
+ * Generate a log filename, when using clasic filenames.
+ */
+static void
+gen_clasiclog_fname(char *fname, size_t fname_sz, const char *archive_dir,
+    const char *namepart, int numlogs_c)
+{
+
+       if (archive_dir[0] != '\0')
+               (void) snprintf(fname, fname_sz, "%s/%s.%d", archive_dir,
+                   namepart, numlogs_c);
+       else
+               (void) snprintf(fname, fname_sz, "%s.%d", namepart, numlogs_c);
+}
+
+/*
+ * Delete a rotated logfiles, when using clasic filenames.
+ */
+static void
+delete_clasiclog(const char *archive_dir, const char *namepart, int numlog_c)
+{
+       char file1[MAXPATHLEN], zfile1[MAXPATHLEN];
+       int c;
+
+       gen_clasiclog_fname(file1, sizeof(file1), archive_dir, namepart,
+           numlog_c);
+
+       for (c = 0; c < COMPRESS_TYPES; c++) {
+               (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
+                   compress_type[c].suffix);
+               if (noaction)
+                       printf("\trm -f %s\n", zfile1);
+               else
+                       (void) unlink(zfile1);
+       }
+}
+
+/*
  * Only add to the queue if the file hasn't already been added. This is
  * done to prevent circular include loops.
  */
@@ -1655,7 +1692,6 @@ do_rotate(const struct conf_entry *ent)
        struct stat st;
        struct tm tm;
        time_t now;
-       int c;
 
        flags = ent->flags;
        free_or_keep = FREE_ENT;
@@ -1685,35 +1721,29 @@ do_rotate(const struct conf_entry *ent)
                        strlcpy(namepart, ent->log, sizeof(namepart));
                else
                        strlcpy(namepart, p + 1, sizeof(namepart));
-
-               /* name of oldest log */
-               (void) snprintf(file1, sizeof(file1), "%s/%s.%d", dirpart,
-                   namepart, ent->numlogs);
        } else {
                /*
-                * Tell delete_oldest_timelog() we are not using an
-                * archive dir.
+                * Tell utility functions we are not using an archive
+                * dir.
                 */
                dirpart[0] = '\0';
-
-               /* name of oldest log */
-               (void) snprintf(file1, sizeof(file1), "%s.%d", ent->log,
-                   ent->numlogs);
+               strlcpy(namepart, ent->log, sizeof(namepart));
        }
 
        /* Delete old logs */
        if (timefnamefmt != NULL)
                delete_oldest_timelog(ent, dirpart);
        else {
-               /* name of oldest log */
-               for (c = 0; c < COMPRESS_TYPES; c++) {
-                       (void) snprintf(zfile1, sizeof(zfile1), "%s%s", file1,
-                           compress_type[c].suffix);
-                       if (noaction)
-                               printf("\trm -f %s\n", zfile1);
-                       else
-                               (void) unlink(zfile1);
-               }
+               /*
+                * Handle cleaning up after legacy newsyslog where we
+                * kept ent->numlogs + 1 files.  This code can go away
+                * at some point in the future.
+                */
+               delete_clasiclog(dirpart, namepart, ent->numlogs);
+
+               if (ent->numlogs > 0)
+                       delete_clasiclog(dirpart, namepart, ent->numlogs - 1);
+
        }
 
        if (timefnamefmt != NULL) {
@@ -1731,21 +1761,19 @@ do_rotate(const struct conf_entry *ent)
                            ent->log, datetimestr);
 
                /* Don't run the code to move down logs */
-               numlogs_c = 0;
-       } else
-               numlogs_c = ent->numlogs;               /* copy for countdown */
+               numlogs_c = -1;
+       } else {
+               gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
+                   ent->numlogs - 1);
+               numlogs_c = ent->numlogs - 2;           /* copy for countdown */
+       }
 
        /* Move down log files */
-       while (numlogs_c--) {
-
+       for (; numlogs_c >= 0; numlogs_c--) {
                (void) strlcpy(file2, file1, sizeof(file2));
 
-               if (archtodir)
-                       (void) snprintf(file1, sizeof(file1), "%s/%s.%d",
-                           dirpart, namepart, numlogs_c);
-               else
-                       (void) snprintf(file1, sizeof(file1), "%s.%d",
-                           ent->log, numlogs_c);
+               gen_clasiclog_fname(file1, sizeof(file1), dirpart, namepart,
+                   numlogs_c);
 
                logfile_suffix = get_logfile_suffix(file1);
                if (logfile_suffix == NULL)
_______________________________________________
svn-src-head@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-head
To unsubscribe, send any mail to "svn-src-head-unsubscr...@freebsd.org"

Reply via email to