You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@httpd.apache.org by "Victor J. Orlikowski" <v....@gte.net> on 2000/11/06 18:44:37 UTC

[PATCH] Piped logs, rotatelogs, and 1.3.x

The following patch repairs rotatelogs to be more reliable in the case
that open(2) or write(2) to the error log fail, whatever the cause.
Further, we make piped logs ignore write errors, since this can simply
be the result of a full pipe.

Index: src/main/http_log.c
===================================================================
RCS file: /cvs/apache/apache-1.3/src/main/http_log.c,v
retrieving revision 1.85
diff -u -d -b -r1.85 http_log.c
--- http_log.c	2000/06/19 20:44:27	1.85
+++ http_log.c	2000/11/06 17:42:11
@@ -664,9 +664,8 @@
 	break;
     
     case OC_REASON_UNWRITABLE:
-	if (pl->pid != -1) {
-	    kill(pl->pid, SIGTERM);
-	}
+        /* We should not kill off the pipe here, since it may only be full.
+         * If it really is locked, we should kill it off manually. */
 	break;
     
     case OC_REASON_RESTART:
Index: src/support/rotatelogs.c
===================================================================
RCS file: /cvs/apache/apache-1.3/src/support/rotatelogs.c,v
retrieving revision 1.12
diff -u -d -b -r1.12 rotatelogs.c
--- rotatelogs.c	2000/06/21 22:54:45	1.12
+++ rotatelogs.c	2000/11/06 17:42:11
@@ -13,18 +13,19 @@
 #include <fcntl.h>
 
 #define BUFSIZE		65536
+#define ERRMSGSZ	82
 #ifndef MAX_PATH
 #define MAX_PATH	1024
 #endif
 
 int main (int argc, char **argv)
 {
-    char buf[BUFSIZE], buf2[MAX_PATH];
+    char buf[BUFSIZE], buf2[MAX_PATH], errbuf[ERRMSGSZ];
     time_t tLogEnd = 0;
     time_t tRotation;
-    int nLogFD = -1;
-    int nRead;
+    int nLogFD = -1, nLogFDprev = -1, nMessCount = 0, nRead, nWrite;
     char *szLogRoot;
+    const char *errmsg = "Error writing to log file. Lost a message.\n";
 
     if (argc != 3) {
 	fprintf(stderr,
@@ -63,7 +64,7 @@
 	    if (errno != EINTR)
 		exit(4);
 	if (nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0)) {
-	    close(nLogFD);
+            nLogFDprev = nLogFD;
 	    nLogFD = -1;
 	}
 	if (nLogFD < 0) {
@@ -72,15 +73,44 @@
 	    tLogEnd = tLogStart + tRotation;
 	    nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666);
 	    if (nLogFD < 0) {
+                /* Uh-oh. Failed to open the new log file. Try to clear
+                 * the previous log file, note the lost log entries,
+                 * and keep on truckin'. */
+                if (nLogFDprev == -1) {
 		perror(buf2);
 		exit(2);
 	    }
+                else {
+                    nLogFD = nLogFDprev;
+                    ftruncate(nLogFD, 0);
+                    sprintf(errbuf,
+                             "Resetting log file due to error opening "
+                             "new log file. %10d messages lost.\n",
+                             nMessCount); 
+                    nWrite = strlen(errbuf) + 1;
+                    write(nLogFD, errbuf, nWrite);
 	}
-	if (write(nLogFD, buf, nRead) != nRead) {
-	    perror(buf2);
-	    exit(5);
 	}
+            else {
+                close(nLogFDprev);
     }
-    /* We never get here, but surpress the compile warning */
+            nMessCount = 0;
+	}
+        do {
+	    nWrite = write(nLogFD, buf, nRead);
+	} while (nWrite < 0 && errno == EINTR);
+        if (nWrite != nRead) {
+            /* Problem with the log. Try to seek back and 
+             * state that a message was lost. */
+            nWrite = strlen(errmsg) + 1;
+            lseek (nLogFD, nWrite, SEEK_CUR);
+            write (nLogFD, errbuf, nWrite);
+                    
+        } 
+        else {
+            nMessCount++; 
+        }
+    }
+    /* We never get here, but suppress the compile warning */
     return (0);
 }

Victor
-- 
Victor J. Orlikowski
======================
v.j.orlikowski@gte.net
vjo@raleigh.ibm.com
vjo@us.ibm.com