aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>2003-10-09 09:43:18 +0000
committerEric Andersen <andersen@codepoet.org>2003-10-09 09:43:18 +0000
commit29c77f71ba788fe9d63893e555c239d45905ebbc (patch)
tree02298f6f20d982cb8daed726a8d68e0abdd14798
parentdae099b2f96ba46bfdd6b598b06dc88713b2d0e8 (diff)
downloadbusybox-w32-29c77f71ba788fe9d63893e555c239d45905ebbc.tar.gz
busybox-w32-29c77f71ba788fe9d63893e555c239d45905ebbc.tar.bz2
busybox-w32-29c77f71ba788fe9d63893e555c239d45905ebbc.zip
Arnd Ben Otto writes:
Hi Eric I have written a small patch for the Busybox syslogd. With this patch one can limit the size of the messagfile. As soon as the limit is reached the syslogd can rotate or purge the messagefile(s) on his own. There is no necessity to use an external rotatescript. Even if logread does something similar, its very handy to have some messagefile after your box crash. I wrote this patch initial vor BB 0.6x where no cron daemon was avail. Now I adapted it for the new Version and i hope it is still useful. At least I still use it :-) bye Arnd
-rw-r--r--include/usage.h8
-rw-r--r--sysklogd/Config.in8
-rw-r--r--sysklogd/syslogd.c49
3 files changed, 64 insertions, 1 deletions
diff --git a/include/usage.h b/include/usage.h
index ba808d39a..c109d0cf6 100644
--- a/include/usage.h
+++ b/include/usage.h
@@ -2214,6 +2214,11 @@
2214 "Write all buffered filesystem blocks to disk." 2214 "Write all buffered filesystem blocks to disk."
2215 2215
2216 2216
2217#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
2218 #define USAGE_ROTATE_LOGFILE(a) a
2219#else
2220 #define USAGE_ROTATE_LOGFILE(a)
2221#endif
2217#ifdef CONFIG_FEATURE_REMOTE_LOG 2222#ifdef CONFIG_FEATURE_REMOTE_LOG
2218 #define USAGE_REMOTE_LOG(a) a 2223 #define USAGE_REMOTE_LOG(a) a
2219#else 2224#else
@@ -2234,6 +2239,9 @@
2234 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n" \ 2239 "\t-m NUM\t\tInterval between MARK lines (default=20min, 0=off)\n" \
2235 "\t-n\t\tRun as a foreground process\n" \ 2240 "\t-n\t\tRun as a foreground process\n" \
2236 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)" \ 2241 "\t-O FILE\t\tUse an alternate log file (default=/var/log/messages)" \
2242 USAGE_ROTATE_LOGFILE( \
2243 "\n\t-s SIZE\t\tMax size (KB) bevor rotate (default=200KB, 0=off)\n" \
2244 "\t-b NUM\t\tNumber of rotated log files (default=1, 0=purge log)") \
2237 USAGE_REMOTE_LOG( \ 2245 USAGE_REMOTE_LOG( \
2238 "\n\t-R HOST[:PORT]\tLog to IP or hostname on PORT (default PORT=514/UDP)\n" \ 2246 "\n\t-R HOST[:PORT]\tLog to IP or hostname on PORT (default PORT=514/UDP)\n" \
2239 "\t-L\t\tLog locally and via network logging (default is network only)") \ 2247 "\t-L\t\tLog locally and via network logging (default is network only)") \
diff --git a/sysklogd/Config.in b/sysklogd/Config.in
index 87b9ed7ac..83640bb3f 100644
--- a/sysklogd/Config.in
+++ b/sysklogd/Config.in
@@ -20,6 +20,14 @@ config CONFIG_SYSLOGD
20 wrong. And something almost always will go wrong if 20 wrong. And something almost always will go wrong if
21 you wait long enough.... 21 you wait long enough....
22 22
23config CONFIG_FEATURE_ROTATE_LOGFILE
24 bool " Rotate message files"
25 default n
26 depends on CONFIG_SYSLOGD
27 help
28 This enables syslogd to rotate the message files
29 on his own. No need to use an external rotatescript.
30
23config CONFIG_FEATURE_REMOTE_LOG 31config CONFIG_FEATURE_REMOTE_LOG
24 bool " Remote Log support" 32 bool " Remote Log support"
25 default n 33 default n
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 3ba239882..74b242c42 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -58,6 +58,14 @@ static char lfile[MAXPATHLEN];
58 58
59static const char *logFilePath = __LOG_FILE; 59static const char *logFilePath = __LOG_FILE;
60 60
61#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
62/* max size of message file bevor being rotated */
63static int logFileSize = 200 * 1024;
64
65/* number of rotated message files */
66static int logFileRotate = 1;
67#endif
68
61/* interval between marks in seconds */ 69/* interval between marks in seconds */
62static int MarkInterval = 20 * 60; 70static int MarkInterval = 20 * 60;
63 71
@@ -305,6 +313,36 @@ static void message(char *fmt, ...)
305 O_NONBLOCK)) >= 0) { 313 O_NONBLOCK)) >= 0) {
306 fl.l_type = F_WRLCK; 314 fl.l_type = F_WRLCK;
307 fcntl(fd, F_SETLKW, &fl); 315 fcntl(fd, F_SETLKW, &fl);
316#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
317 if ( logFileSize > 0 ) {
318 struct stat statf;
319 int r = fstat(fd, &statf);
320 if( !r && (statf.st_mode & S_IFREG)
321 && (lseek(fd,0,SEEK_END) > logFileSize) ) {
322 if(logFileRotate > 0) {
323 int i;
324 char oldFile[(strlen(logFilePath)+3)], newFile[(strlen(logFilePath)+3)];
325 for(i=logFileRotate-1;i>0;i--) {
326 sprintf(oldFile, "%s.%d", logFilePath, i-1);
327 sprintf(newFile, "%s.%d", logFilePath, i);
328 rename(oldFile, newFile);
329 }
330 sprintf(newFile, "%s.%d", logFilePath, 0);
331 fl.l_type = F_UNLCK;
332 fcntl (fd, F_SETLKW, &fl);
333 close(fd);
334 rename(logFilePath, newFile);
335 fd = device_open (logFilePath,
336 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
337 O_NONBLOCK);
338 fl.l_type = F_WRLCK;
339 fcntl (fd, F_SETLKW, &fl);
340 } else {
341 ftruncate( fd, 0 );
342 }
343 }
344 }
345#endif
308 va_start(arguments, fmt); 346 va_start(arguments, fmt);
309 vdprintf(fd, fmt, arguments); 347 vdprintf(fd, fmt, arguments);
310 va_end(arguments); 348 va_end(arguments);
@@ -578,7 +616,7 @@ extern int syslogd_main(int argc, char **argv)
578 char *p; 616 char *p;
579 617
580 /* do normal option parsing */ 618 /* do normal option parsing */
581 while ((opt = getopt(argc, argv, "m:nO:R:LC::")) > 0) { 619 while ((opt = getopt(argc, argv, "m:nO:s:b:R:LC::")) > 0) {
582 switch (opt) { 620 switch (opt) {
583 case 'm': 621 case 'm':
584 MarkInterval = atoi(optarg) * 60; 622 MarkInterval = atoi(optarg) * 60;
@@ -589,6 +627,15 @@ extern int syslogd_main(int argc, char **argv)
589 case 'O': 627 case 'O':
590 logFilePath = optarg; 628 logFilePath = optarg;
591 break; 629 break;
630#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
631 case 's':
632 logFileSize = atoi(optarg) * 1024;
633 break;
634 case 'b':
635 logFileRotate = atoi(optarg);
636 if( logFileRotate > 99 ) logFileRotate = 99;
637 break;
638#endif
592#ifdef CONFIG_FEATURE_REMOTE_LOG 639#ifdef CONFIG_FEATURE_REMOTE_LOG
593 case 'R': 640 case 'R':
594 RemoteHost = bb_xstrdup(optarg); 641 RemoteHost = bb_xstrdup(optarg);