Line 0
Link Here
|
|
|
1 |
--- logrotate-3.7.1/logrotate.h.dateext 2003-08-07 13:13:14.000000000 +0200 |
2 |
+++ logrotate-3.7.1/logrotate.h 2005-05-03 14:17:28.732914132 +0200 |
3 |
@@ -15,6 +15,7 @@ |
4 |
#define LOG_FLAG_MAILFIRST (1 << 6) |
5 |
#define LOG_FLAG_SHAREDSCRIPTS (1 << 7) |
6 |
#define LOG_FLAG_COPY (1 << 8) |
7 |
+#define LOG_FLAG_DATEEXT (1 << 9) |
8 |
|
9 |
#define NO_FORCE_ROTATE 0 |
10 |
#define FORCE_ROTATE 1 |
11 |
--- logrotate-3.7.1/logrotate.c.dateext 2005-05-03 14:17:28.730914415 +0200 |
12 |
+++ logrotate-3.7.1/logrotate.c 2005-05-03 14:31:29.916899912 +0200 |
13 |
@@ -11,6 +11,7 @@ |
14 |
#include <sys/wait.h> |
15 |
#include <time.h> |
16 |
#include <unistd.h> |
17 |
+#include <glob.h> |
18 |
|
19 |
#ifdef WITH_SELINUX |
20 |
#include <selinux/selinux.h> |
21 |
@@ -23,6 +24,10 @@ |
22 |
#include "log.h" |
23 |
#include "logrotate.h" |
24 |
|
25 |
+#if !defined(GLOB_ABORTED) && defined(GLOB_ABEND) |
26 |
+#define GLOB_ABORTED GLOB_ABEND |
27 |
+#endif |
28 |
+ |
29 |
typedef struct { |
30 |
char * fn; |
31 |
struct tm lastRotated; /* only tm.mon, tm_mday, tm_year are good! */ |
32 |
@@ -43,6 +48,14 @@ |
33 |
char * mailCommand = DEFAULT_MAIL_COMMAND; |
34 |
time_t nowSecs = 0; |
35 |
|
36 |
+static int globerr(const char * pathname, int theerr) { |
37 |
+ message(MESS_ERROR, "error accessing %s: %s\n", pathname, |
38 |
+ strerror(theerr)); |
39 |
+ |
40 |
+ /* We want the glob operation to continue, so return 0 */ |
41 |
+ return 1; |
42 |
+} |
43 |
+ |
44 |
static logState * findState(const char * fn, struct stateSet * sip) { |
45 |
int i; |
46 |
logState * states = sip->states; |
47 |
@@ -93,6 +106,17 @@ |
48 |
return rc; |
49 |
} |
50 |
|
51 |
+static int removeLogFile(char * name) { |
52 |
+ message(MESS_DEBUG, "removing old log %s\n", name); |
53 |
+ |
54 |
+ if (!debug && unlink(name)) { |
55 |
+ message(MESS_ERROR, "Failed to remove old log %s: %s\n", |
56 |
+ name, strerror(errno)); |
57 |
+ return 1; |
58 |
+ } |
59 |
+ return 0; |
60 |
+} |
61 |
+ |
62 |
static int compressLogFile(char * name, logInfo * log, struct stat *sb) { |
63 |
char * compressedName; |
64 |
const char ** fullCommand; |
65 |
@@ -237,6 +261,25 @@ |
66 |
return rc; |
67 |
} |
68 |
|
69 |
+static int mailLogWrapper (char * mailFilename, char * mailCommand, int logNum, logInfo * log) { |
70 |
+ /* if the log is compressed (and we're not mailing a |
71 |
+ * file whose compression has been delayed), we need |
72 |
+ * to uncompress it */ |
73 |
+ if ((log->flags & LOG_FLAG_COMPRESS) && |
74 |
+ !((log->flags & LOG_FLAG_DELAYCOMPRESS) && |
75 |
+ (log->flags & LOG_FLAG_MAILFIRST))) { |
76 |
+ if (mailLog(mailFilename, mailCommand, |
77 |
+ log->uncompress_prog, log->logAddress, |
78 |
+ log->files[logNum])) |
79 |
+ return 1; |
80 |
+ } else { |
81 |
+ if (mailLog(mailFilename, mailCommand, NULL, |
82 |
+ log->logAddress, mailFilename)) |
83 |
+ return 1; |
84 |
+ } |
85 |
+ return 0; |
86 |
+} |
87 |
+ |
88 |
static int copyTruncate(char * currLog, char * saveLog, struct stat * sb, int flags) { |
89 |
char buf[BUFSIZ]; |
90 |
int fdcurr = -1, fdsave = -1; |
91 |
@@ -456,6 +499,9 @@ |
92 |
char * baseName; |
93 |
char * dirName; |
94 |
char * firstRotated; |
95 |
+ char * glob_pattern; |
96 |
+ glob_t globResult; |
97 |
+ int rc; |
98 |
size_t alloc_size; |
99 |
int rotateCount = log->rotateCount ? log->rotateCount : 1; |
100 |
int logStart = (log->logStart == -1) ? 1 : log->logStart; |
101 |
@@ -486,7 +532,7 @@ |
102 |
|
103 |
alloc_size = strlen(dirName) + strlen(baseName) + |
104 |
strlen(log->files[logNum]) + strlen(fileext) + |
105 |
- strlen(compext) + 10; |
106 |
+ strlen(compext) + 18; |
107 |
|
108 |
oldName = alloca(alloc_size); |
109 |
newName = alloca(alloc_size); |
110 |
@@ -508,25 +554,106 @@ |
111 |
/* First compress the previous log when necessary */ |
112 |
if (log->flags & LOG_FLAG_COMPRESS && |
113 |
log->flags & LOG_FLAG_DELAYCOMPRESS) { |
114 |
- struct stat sbprev; |
115 |
- |
116 |
- sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext); |
117 |
- if (stat(oldName, &sbprev)) { |
118 |
- message(MESS_DEBUG, "previous log %s does not exist\n", |
119 |
- oldName); |
120 |
- } else { |
121 |
- hasErrors = compressLogFile(oldName, log, &sbprev); |
122 |
+ if (log->flags & LOG_FLAG_DATEEXT) { |
123 |
+ /* glob for uncompressed files with our pattern */ |
124 |
+ glob_pattern = malloc(strlen(dirName) + strlen(baseName) |
125 |
+ + strlen(fileext) + 44 ); |
126 |
+ sprintf(glob_pattern, |
127 |
+ "%s/%s-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%s", |
128 |
+ dirName, baseName, fileext); |
129 |
+ rc = glob(glob_pattern, 0, globerr, &globResult); |
130 |
+ if (!rc && globResult.gl_pathc > 0) { |
131 |
+ for (i = 0; i < globResult.gl_pathc && !hasErrors; i++) { |
132 |
+ struct stat sbprev; |
133 |
+ |
134 |
+ sprintf(oldName,"%s",(globResult.gl_pathv)[i]); |
135 |
+ if (stat(oldName, &sbprev)) { |
136 |
+ message(MESS_DEBUG, "previous log %s does not exist\n", |
137 |
+ oldName); |
138 |
+ } else { |
139 |
+ hasErrors = compressLogFile(oldName, log, &sbprev); |
140 |
+ } |
141 |
+ } |
142 |
+ } else { |
143 |
+ message (MESS_DEBUG, "glob finding logs to compress failed\n"); |
144 |
+ /* fallback to old behaviour */ |
145 |
+ sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext); |
146 |
+ } |
147 |
+ globfree(&globResult); |
148 |
+ free(glob_pattern); |
149 |
+ } else { |
150 |
+ struct stat sbprev; |
151 |
+ |
152 |
+ sprintf(oldName, "%s/%s.%d%s", dirName, baseName, logStart, fileext); |
153 |
+ if (stat(oldName, &sbprev)) { |
154 |
+ message(MESS_DEBUG, "previous log %s does not exist\n", |
155 |
+ oldName); |
156 |
+ } else { |
157 |
+ hasErrors = compressLogFile(oldName, log, &sbprev); |
158 |
+ } |
159 |
} |
160 |
} |
161 |
|
162 |
+ firstRotated = alloca(strlen(dirName) + strlen(baseName) + |
163 |
+ strlen(fileext) + strlen(compext) + 30); |
164 |
+ |
165 |
+ if(log->flags & LOG_FLAG_DATEEXT) { |
166 |
+ /* glob for compressed files with our pattern |
167 |
+ * and compress ext */ |
168 |
+ glob_pattern = malloc(strlen(dirName)+strlen(baseName) |
169 |
+ +strlen(fileext)+strlen(compext)+44); |
170 |
+ sprintf(glob_pattern, |
171 |
+ "%s/%s-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]%s%s", |
172 |
+ dirName, baseName, fileext, compext); |
173 |
+ rc = glob(glob_pattern, 0, globerr, &globResult); |
174 |
+ if (!rc) { |
175 |
+ /* search for files to drop, if we find one remember it, |
176 |
+ * if we find another one mail and remove the first and |
177 |
+ * remember the second and so on */ |
178 |
+ struct stat fst_buf; |
179 |
+ int mail_out = -1; |
180 |
+ /* remove the first (n - rotateCount) matches |
181 |
+ * no real rotation needed, since the files have |
182 |
+ * the date in their name */ |
183 |
+ for (i = 0; i < globResult.gl_pathc; i++) { |
184 |
+ if( !stat((globResult.gl_pathv)[i],&fst_buf) ) { |
185 |
+ if (i <= ((int)globResult.gl_pathc - rotateCount)) { |
186 |
+ if ( mail_out != -1 ) { |
187 |
+ if (!hasErrors && log->logAddress) { |
188 |
+ char * mailFilename = (globResult.gl_pathv)[mail_out]; |
189 |
+ hasErrors = mailLogWrapper(mailFilename, mailCommand, logNum, log); |
190 |
+ if (!hasErrors) |
191 |
+ hasErrors = removeLogFile(mailFilename); |
192 |
+ } |
193 |
+ } |
194 |
+ mail_out = i; |
195 |
+ } |
196 |
+ } |
197 |
+ } |
198 |
+ if ( mail_out != -1 ) { |
199 |
+ /* oldName is oldest Backup found (for unlink later) */ |
200 |
+ sprintf(oldName, "%s", (globResult.gl_pathv)[mail_out]); |
201 |
+ strcpy(disposeName, oldName); |
202 |
+ } else |
203 |
+ disposeName = NULL; |
204 |
+ } else { |
205 |
+ message (MESS_DEBUG, "glob finding old rotated logs failed\n"); |
206 |
+ disposeName = NULL; |
207 |
+ } |
208 |
+ /* firstRotated is most recently created/compressed rotated log */ |
209 |
+ sprintf(firstRotated, "%s/%s-%04d%02d%02d%s%s", |
210 |
+ dirName, baseName, now.tm_year+1900, |
211 |
+ now.tm_mon+1, now.tm_mday, fileext, compext); |
212 |
+ globfree(&globResult); |
213 |
+ free(glob_pattern); |
214 |
+ } else { |
215 |
+ |
216 |
sprintf(oldName, "%s/%s.%d%s%s", dirName, baseName, |
217 |
logStart + rotateCount, fileext, compext); |
218 |
strcpy(newName, oldName); |
219 |
|
220 |
strcpy(disposeName, oldName); |
221 |
|
222 |
- firstRotated = alloca(strlen(dirName) + strlen(baseName) + |
223 |
- strlen(fileext) + strlen(compext) + 30); |
224 |
sprintf(firstRotated, "%s/%s.%d%s%s", dirName, baseName, |
225 |
logStart, fileext, |
226 |
(log->flags & LOG_FLAG_DELAYCOMPRESS) ? "" : compext); |
227 |
@@ -582,12 +709,27 @@ |
228 |
hasErrors = 1; |
229 |
} |
230 |
} |
231 |
- } |
232 |
- |
233 |
+ } |
234 |
+ } /* !LOG_FLAG_DATEEXT */ |
235 |
+ |
236 |
finalName = oldName; |
237 |
|
238 |
- /* note: the gzip extension is *not* used here! */ |
239 |
- sprintf(finalName, "%s/%s.%d%s", dirName, baseName, logStart, fileext); |
240 |
+ if(log->flags & LOG_FLAG_DATEEXT) { |
241 |
+ char * destFile = alloca(strlen(dirName) + strlen(baseName) + |
242 |
+ strlen(fileext) + strlen(compext) + 30); |
243 |
+ struct stat fst_buf; |
244 |
+ sprintf(finalName, "%s/%s-%04d%02d%02d%s", |
245 |
+ dirName, baseName, now.tm_year+1900, |
246 |
+ now.tm_mon+1, now.tm_mday, fileext); |
247 |
+ sprintf(destFile, "%s%s", finalName, compext); |
248 |
+ if(!stat(destFile,&fst_buf)) { |
249 |
+ message (MESS_DEBUG, "destination %s already exists, skipping rotation\n", firstRotated); |
250 |
+ hasErrors = 1; |
251 |
+ } |
252 |
+ } else { |
253 |
+ /* note: the gzip extension is *not* used here! */ |
254 |
+ sprintf(finalName, "%s/%s.%d%s", dirName, baseName, logStart, fileext); |
255 |
+ } |
256 |
|
257 |
/* if the last rotation doesn't exist, that's okay */ |
258 |
if (!debug && access(disposeName, F_OK)) { |
259 |
@@ -596,9 +738,6 @@ |
260 |
disposeName = NULL; |
261 |
} |
262 |
|
263 |
- free(dirName); |
264 |
- free(baseName); |
265 |
- |
266 |
if (!hasErrors) { |
267 |
if (log->pre && !(log->flags & LOG_FLAG_SHAREDSCRIPTS)) { |
268 |
message(MESS_DEBUG, "running prerotate script\n"); |
269 |
@@ -744,6 +883,8 @@ |
270 |
} |
271 |
} |
272 |
#endif |
273 |
+ free(dirName); |
274 |
+ free(baseName); |
275 |
return hasErrors; |
276 |
} |
277 |
|
278 |
--- logrotate-3.7.1/config.c.dateext 2003-08-07 13:13:14.000000000 +0200 |
279 |
+++ logrotate-3.7.1/config.c 2005-05-03 14:17:28.734913850 +0200 |
280 |
@@ -511,6 +511,14 @@ |
281 |
newlog->flags &= ~LOG_FLAG_IFEMPTY; |
282 |
|
283 |
*endtag = oldchar, start = endtag; |
284 |
+ } else if (!strcmp(start, "dateext")) { |
285 |
+ newlog->flags |= LOG_FLAG_DATEEXT; |
286 |
+ |
287 |
+ *endtag = oldchar, start = endtag; |
288 |
+ } else if (!strcmp(start, "nodateext")) { |
289 |
+ newlog->flags &= ~LOG_FLAG_DATEEXT; |
290 |
+ |
291 |
+ *endtag = oldchar, start = endtag; |
292 |
} else if (!strcmp(start, "noolddir")) { |
293 |
newlog->oldDir = NULL; |
294 |
|
295 |
--- logrotate-3.7.1/logrotate.8.dateext 2005-05-03 14:17:28.722915545 +0200 |
296 |
+++ logrotate-3.7.1/logrotate.8 2005-05-03 14:17:28.735913708 +0200 |
297 |
@@ -205,6 +205,11 @@ |
298 |
Log files are rotated every day. |
299 |
|
300 |
.TP |
301 |
+\fBdateext\fR |
302 |
+Archive old versions of log files adding a daily extension like YYYYMMDD |
303 |
+instead of simply adding a number. |
304 |
+ |
305 |
+.TP |
306 |
\fBdelaycompress\fR |
307 |
Postpone compression of the previous log file to the next rotation cycle. |
308 |
This has only effect when used in combination with \fBcompress\fR. |
309 |
|
310 |
logrotate-3.7.1-maxage.patch: |
311 |
config.c | 15 +++++++++++++++ |
312 |
logrotate.8 | 6 ++++++ |
313 |
logrotate.c | 25 +++++++++++++++++++++++-- |
314 |
logrotate.h | 1 + |
315 |
4 files changed, 45 insertions(+), 2 deletions(-) |
316 |
|
317 |
--- NEW FILE logrotate-3.7.1-maxage.patch --- |
318 |
--- logrotate-3.7.1/logrotate.c.maxage 2005-05-03 14:46:08.154599467 +0200 |
319 |
+++ logrotate-3.7.1/logrotate.c 2005-05-03 14:50:43.707983574 +0200 |
320 |
@@ -617,7 +617,10 @@ |
321 |
* the date in their name */ |
322 |
for (i = 0; i < globResult.gl_pathc; i++) { |
323 |
if( !stat((globResult.gl_pathv)[i],&fst_buf) ) { |
324 |
- if (i <= ((int)globResult.gl_pathc - rotateCount)) { |
325 |
+ if ((i <= ((int)globResult.gl_pathc - rotateCount)) |
326 |
+ || ((log->rotateAge > 0) |
327 |
+ && (((nowSecs - fst_buf.st_mtime)/60/60/24) |
328 |
+ > log->rotateAge))) { |
329 |
if ( mail_out != -1 ) { |
330 |
if (!hasErrors && log->logAddress) { |
331 |
char * mailFilename = (globResult.gl_pathv)[mail_out]; |
332 |
@@ -647,6 +650,22 @@ |
333 |
globfree(&globResult); |
334 |
free(glob_pattern); |
335 |
} else { |
336 |
+ if ( log->rotateAge ) { |
337 |
+ struct stat fst_buf; |
338 |
+ for (i=1; i <= rotateCount; i++) { |
339 |
+ sprintf(oldName, "%s/%s.%d%s%s", dirName, baseName, |
340 |
+ rotateCount + 1, fileext, compext); |
341 |
+ if(!stat(oldName,&fst_buf) |
342 |
+ && (((nowSecs - fst_buf.st_mtime)/60/60/24) |
343 |
+ > log->rotateAge)) { |
344 |
+ char * mailFilename = (globResult.gl_pathv)[i]; |
345 |
+ if (!hasErrors && log->logAddress) |
346 |
+ hasErrors = mailLogWrapper(mailFilename, mailCommand, logNum, log); |
347 |
+ if (!hasErrors) |
348 |
+ hasErrors = removeLogFile(mailFilename); |
349 |
+ } |
350 |
+ } |
351 |
+ } |
352 |
|
353 |
sprintf(oldName, "%s/%s.%d%s%s", dirName, baseName, |
354 |
logStart + rotateCount, fileext, compext); |
355 |
@@ -1171,7 +1190,9 @@ |
356 |
|
357 |
int main(int argc, const char ** argv) { |
358 |
logInfo defConfig = { NULL, NULL, 0, NULL, ROT_SIZE, |
359 |
- /* threshHold */ 1024 * 1024, 0, |
360 |
+ /* threshHold */ 1024 * 1024, |
361 |
+ /* rotateCount */ 0, |
362 |
+ /* rotateAge */ 0, |
363 |
/* log start */ -1, |
364 |
/* pre, post */ NULL, NULL, |
365 |
/* first, last */ NULL, NULL, |
366 |
--- logrotate-3.7.1/logrotate.h.maxage 2005-05-03 14:51:33.207865757 +0200 |
367 |
+++ logrotate-3.7.1/logrotate.h 2005-05-03 14:51:58.732195346 +0200 |
368 |
@@ -35,6 +35,7 @@ |
369 |
enum { ROT_DAYS, ROT_WEEKLY, ROT_MONTHLY, ROT_SIZE, ROT_FORCE } criterium; |
370 |
unsigned int threshhold; |
371 |
int rotateCount; |
372 |
+ int rotateAge; |
373 |
int logStart; |
374 |
char * pre, * post, * first, * last; |
375 |
char * logAddress; |
376 |
--- logrotate-3.7.1/config.c.maxage 2005-05-03 14:39:35.690020183 +0200 |
377 |
+++ logrotate-3.7.1/config.c 2005-05-03 14:40:33.429711948 +0200 |
378 |
@@ -678,6 +678,21 @@ |
379 |
} |
380 |
*endtag = oldchar, start = endtag; |
381 |
} |
382 |
+ } else if (!strcmp(start, "maxage")) { |
383 |
+ *endtag = oldchar, start = endtag; |
384 |
+ |
385 |
+ if (!isolateValue(configFile, lineNum, "maxage count", &start, |
386 |
+ &endtag)) { |
387 |
+ oldchar = *endtag, *endtag = '\0'; |
388 |
+ |
389 |
+ newlog->rotateAge = strtoul(start, &chptr, 0); |
390 |
+ if (*chptr || newlog->rotateAge < 0) { |
391 |
+ message(MESS_ERROR, "%s:%d bad maximum age '%s'\n", |
392 |
+ configFile, lineNum, start); |
393 |
+ return 1; |
394 |
+ } |
395 |
+ *endtag = oldchar, start = endtag; |
396 |
+ } |
397 |
} else if (!strcmp(start, "errors")) { |
398 |
message(MESS_DEBUG, "%s: %d: the errors directive is deprecated and no longer used.\n", |
399 |
configFile, lineNum); |
400 |
--- logrotate-3.7.1/logrotate.8.maxage 2005-05-03 14:41:55.736877916 +0200 |
401 |
+++ logrotate-3.7.1/logrotate.8 2005-05-03 14:45:33.464585908 +0200 |
402 |
@@ -256,6 +256,12 @@ |
403 |
instead of the just-rotated file (this is the default). |
404 |
|
405 |
.TP |
406 |
+\fBmaxage\fR \fIcount\fR |
407 |
+Remove rotated logs older than <count> days. The age is only checked |
408 |
+if the logfile is to be rotated. The files are mailed to the |
409 |
+configured address if \fBmaillast\fR and \fBmail\fR are configured. |
410 |
+ |
411 |
+.TP |
412 |
\fBmissingok\fR |
413 |
If the log file is missing, go on to the next one without issuing an error |
414 |
message. See also \fBnomissingok\fR. |