Package: logrotate
Version: 3.7.8-6
Severity: normal
Tags: patch

Hi,

while testing my logrotate setup, I found that logrotate would never rotate a
new logfile on the first run (when using date-based criteria like daily or
monthly). Only a day after the first run the files are actually rotated.

This was a bit unexpected to me and made debugging using the -d option hard,
since it would always just say that the logs needed no rotation. Also, when
using daily rotation on a new log file, this causes the first rotation of the
file to contain two days worth of logs.

Investigation shows this issue is caused when logrotate looks at a file that
is not in its index file yet. It then assigns today as the "last rotated"
date, meaning it won't be rotated tomorrow at its earliest.

The attached patch changes this to use yesterday as the "last rotated" date
for new files. This causes behaviour to become as expected: A new file is
immediately rotated with daily, if today is the first day of the week with
weekly and if today is the first day of the month with montly.

Please consider adding this patch.

Gr.

Matthijs
Treat new files as rotated yesterday (instead of today), so they will be
rotated right away if appropriate (e.g. always for daily rotations, or when
today is the first day of the month for monthly rotations).

Patch from: Matthijs Kooijman <matth...@stdin.nl>
Index: logrotate-3.7.8/logrotate.c
===================================================================
--- logrotate-3.7.8.orig/logrotate.c    2010-05-03 11:09:21.000000000 +0200
+++ logrotate-3.7.8/logrotate.c 2010-05-03 11:19:34.696646754 +0200
@@ -56,7 +56,7 @@
 int numLogs = 0;
 int debug = 0;
 char *mailCommand = DEFAULT_MAIL_COMMAND;
-time_t nowSecs = 0;
+time_t nowSecs = 0, yesterdaySecs = 0;
 
 static int shred_file(char *filename, struct logInfo *log);
 
@@ -124,7 +124,7 @@
 
 static struct logState *newState(const char *fn)
 {
-       struct tm now = *localtime(&nowSecs);
+       struct tm now = *localtime(&yesterdaySecs);
        struct logState *new;
        time_t lr_time;
 
@@ -1644,6 +1644,7 @@
        {"verbose", 'v', 0, 0, 'v', "Display messages during rotation"},
        POPT_AUTOHELP {0, 0, 0, 0, 0}
     };
+    struct tm nowTm;
 
     logSetLevel(MESS_NORMAL);
     setlocale (LC_ALL, "");
@@ -1696,6 +1697,11 @@
 
     poptFreeContext(optCon);
     nowSecs = time(NULL);
+    /* Use mktime to calculate yesterday's date (since we can't rely on time_t
+     * being seconds since 1970, according to libc specs) */
+    nowTm = *gmtime(&nowSecs);
+    nowTm.tm_mday -= 1;
+    yesterdaySecs = mktime(&nowTm);
 
        if (allocateHash() != 0)
                return 1;

Reply via email to