syslog overflow fix

Gleb Smirnoff glebius at freebsd.org
Wed Oct 6 05:43:43 PDT 2004


  Dear collegues,

do you have any objections for merging syslog overflow fix
from OpenBSD?  The patch I'm going to commit is at end of this
message. Problem description and PoC can be found in bin/72366.

Index: syslog.c
===================================================================
RCS file: /home/ncvs/src/lib/libc/gen/syslog.c,v
retrieving revision 1.30
diff -u -r1.30 syslog.c
--- syslog.c	10 May 2004 17:12:52 -0000	1.30
+++ syslog.c	6 Oct 2004 12:32:05 -0000
@@ -243,17 +243,27 @@
 	if (!opened)
 		openlog(LogTag, LogStat | LOG_NDELAY, 0);
 	connectlog();
-	if (send(LogFile, tbuf, cnt, 0) >= 0)
-		return;
 
 	/*
-	 * If the send() failed, the odds are syslogd was restarted.
-	 * Make one (only) attempt to reconnect to /dev/log.
+	 * If the send() failed, there are two likely scenarios: 
+	 *  1) syslogd was restarted
+	 *  2) /var/run/log is out of socket buffer space
+	 * We attempt to reconnect to /var/run/log to take care of
+	 * case #1 and keep send()ing data to cover case #2
+	 * to give syslogd a chance to empty its socket buffer.
 	 */
-	disconnectlog();
-	connectlog();
-	if (send(LogFile, tbuf, cnt, 0) >= 0)
-		return;
+
+	if (send(LogFile, tbuf, cnt, 0) < 0) {
+		if (errno != ENOBUFS) {
+			disconnectlog();
+			connectlog();
+		}
+		do {
+			usleep(1);
+			if (send(LogFile, tbuf, cnt, 0) >= 0)
+				break;
+		} while (errno == ENOBUFS);
+	}
 
 	/*
 	 * Output the message to the console; try not to block

-- 
Totus tuus, Glebius.
GLEBIUS-RIPN GLEB-RIPE


More information about the freebsd-current mailing list