[BusyBox-cvs] busybox/sysklogd Config.in, 1.3, 1.4 syslogd.c, 1.103, 1.104

Erik Andersen andersen at busybox.net
Thu Oct 9 09:43:21 UTC 2003


Update of /var/cvs/busybox/sysklogd
In directory winder:/tmp/cvs-serv9416/sysklogd

Modified Files:
	Config.in syslogd.c 
Log Message:
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



Index: Config.in
===================================================================
RCS file: /var/cvs/busybox/sysklogd/Config.in,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- Config.in	26 Jul 2003 08:20:46 -0000	1.3
+++ Config.in	9 Oct 2003 09:43:18 -0000	1.4
@@ -20,6 +20,14 @@
 	  wrong.  And something almost always will go wrong if
 	  you wait long enough....
 
+config CONFIG_FEATURE_ROTATE_LOGFILE
+	bool "  Rotate message files"
+	default n
+	depends on CONFIG_SYSLOGD
+	help
+	  This enables syslogd to rotate the message files
+	  on his own. No need to use an external rotatescript.
+
 config CONFIG_FEATURE_REMOTE_LOG
 	bool "  Remote Log support"
 	default n

Index: syslogd.c
===================================================================
RCS file: /var/cvs/busybox/sysklogd/syslogd.c,v
retrieving revision 1.103
retrieving revision 1.104
diff -u -d -r1.103 -r1.104
--- syslogd.c	26 Sep 2003 00:49:05 -0000	1.103
+++ syslogd.c	9 Oct 2003 09:43:18 -0000	1.104
@@ -58,6 +58,14 @@
 
 static const char *logFilePath = __LOG_FILE;
 
+#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
+/* max size of message file bevor being rotated */
+static int logFileSize = 200 * 1024;
+
+/* number of rotated message files */
+static int logFileRotate = 1;
+#endif
+
 /* interval between marks in seconds */
 static int MarkInterval = 20 * 60;
 
@@ -305,6 +313,36 @@
 							 O_NONBLOCK)) >= 0) {
 		fl.l_type = F_WRLCK;
 		fcntl(fd, F_SETLKW, &fl);
+#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
+		if ( logFileSize > 0 ) {
+			struct stat statf;
+			int r = fstat(fd, &statf);
+			if( !r && (statf.st_mode & S_IFREG)
+				&& (lseek(fd,0,SEEK_END) > logFileSize) ) {
+				if(logFileRotate > 0) {
+					int i;
+					char oldFile[(strlen(logFilePath)+3)], newFile[(strlen(logFilePath)+3)];
+					for(i=logFileRotate-1;i>0;i--) {
+						sprintf(oldFile, "%s.%d", logFilePath, i-1);
+						sprintf(newFile, "%s.%d", logFilePath, i);
+						rename(oldFile, newFile);
+					}
+					sprintf(newFile, "%s.%d", logFilePath, 0);
+					fl.l_type = F_UNLCK;
+					fcntl (fd, F_SETLKW, &fl);
+					close(fd);
+					rename(logFilePath, newFile);
+					fd = device_open (logFilePath,
+						   O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
+						   O_NONBLOCK);
+					fl.l_type = F_WRLCK;
+					fcntl (fd, F_SETLKW, &fl);
+				} else {
+					ftruncate( fd, 0 );
+				}
+			}
+		}
+#endif
 		va_start(arguments, fmt);
 		vdprintf(fd, fmt, arguments);
 		va_end(arguments);
@@ -578,7 +616,7 @@
 	char *p;
 
 	/* do normal option parsing */
-	while ((opt = getopt(argc, argv, "m:nO:R:LC::")) > 0) {
+	while ((opt = getopt(argc, argv, "m:nO:s:b:R:LC::")) > 0) {
 		switch (opt) {
 		case 'm':
 			MarkInterval = atoi(optarg) * 60;
@@ -589,6 +627,15 @@
 		case 'O':
 			logFilePath = optarg;
 			break;
+#ifdef CONFIG_FEATURE_ROTATE_LOGFILE
+		case 's':
+			logFileSize = atoi(optarg) * 1024;
+			break;
+		case 'b':
+			logFileRotate = atoi(optarg);
+			if( logFileRotate > 99 ) logFileRotate = 99;
+			break;
+#endif
 #ifdef CONFIG_FEATURE_REMOTE_LOG
 		case 'R':
 			RemoteHost = bb_xstrdup(optarg);




More information about the busybox-cvs mailing list