From xdivac02@stud.fit.vutbr.cz  Wed Jan 21 01:15:28 2004
Return-Path: <xdivac02@stud.fit.vutbr.cz>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9D2CA16A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 21 Jan 2004 01:15:28 -0800 (PST)
Received: from eva.fit.vutbr.cz (eva.fit.vutbr.cz [147.229.10.14])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 4399C43D2D
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 21 Jan 2004 01:15:26 -0800 (PST)
	(envelope-from xdivac02@stud.fit.vutbr.cz)
Received: from eva.fit.vutbr.cz (localhost [127.0.0.1])
	by eva.fit.vutbr.cz (8.12.10/8.12.9) with ESMTP id i0L9FMYH094732
	(version=TLSv1/SSLv3 cipher=EDH-RSA-DES-CBC3-SHA bits=168 verify=NO)
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 21 Jan 2004 10:15:23 +0100 (CET)
Received: (from xdivac02@localhost)
	by eva.fit.vutbr.cz (8.12.10/8.12.5/Submit) id i0L9FMgv094731;
	Wed, 21 Jan 2004 10:15:22 +0100 (CET)
Message-Id: <200401210915.i0L9FMgv094731@eva.fit.vutbr.cz>
Date: Wed, 21 Jan 2004 10:15:22 +0100 (CET)
From: Divacky Roman <xdivac02@stud.fit.vutbr.cz>
Reply-To: Divacky Roman <xdivac02@stud.fit.vutbr.cz>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: syslogd speedup
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         61664
>Category:       bin
>Synopsis:       syslogd speedup
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          update
>Submitter-Id:   current-users
>Arrival-Date:   Wed Jan 21 01:20:06 PST 2004
>Closed-Date:    Mon Mar 22 03:28:47 PST 2004
>Last-Modified:  Mon Mar 22 03:28:47 PST 2004
>Originator:     Divacky Roman
>Release:        FreeBSD 4.9-STABLE i386
>Organization:
>Environment:
System: FreeBSD eva.fit.vutbr.cz 4.9-STABLE FreeBSD 4.9-STABLE #2: Thu Nov 20 11:20:53 CET 2003 root@tereza.fit.vutbr.cz:/home/src/sys/sys-49/compile/EVA i386


	
>Description:
	When syslogd logs kernel messages it does it quite weird way. It reads
	buffer from /dev/kmsg and then writes it into log file fsync()ing after
	EACH newline.. this is very slow and much less secure than fsync()ing
	it after whole buffer... this patch implements such behviour
>How-To-Repeat:
	apply this patch ;)
>Fix:
--- syslogd.8	Mon Sep  8 21:57:22 2003
+++ /home/roman/kernel/patches/syslogd/syslogd.8	Wed Jan 14 17:21:14 2004
@@ -224,6 +224,9 @@
 logged with each locally-written message.  If specified more than once,
 the names of the facility and priority are logged with each locally-written
 message.
+.It Fl x
+Turns on old behaviour of fsync()ing after each line. This might make syslogd
+slower!
 .El
 .Pp
 The
--- syslogd.c	Sun Nov 16 22:51:06 2003
+++ /home/roman/kernel/patches/syslogd/syslogd.c	Wed Jan 14 17:27:14 2004
@@ -288,6 +288,7 @@
 static int	LogFacPri;	/* Put facility and priority in log message: */
 				/* 0=no, 1=numeric, 2=names */
 static int	KeepKernFac;	/* Keep remotely logged kernel facility */
+static int	DoFsync;	/* wheter do a fsync() on boot */
 
 volatile sig_atomic_t MarkSet, WantDie;
 
@@ -310,7 +311,7 @@
 static void	markit(void);
 static int	skip_message(const char *, const char *, int);
 static void	printline(const char *, char *);
-static void	printsys(char *);
+static void	printsys(char *, int);
 static int	p_open(const char *, pid_t *);
 static void	readklog(void);
 static void	reapchild(int);
@@ -338,7 +339,7 @@
 	socklen_t len;
 
 	bindhostname = NULL;
-	while ((ch = getopt(argc, argv, "46Aa:b:cdf:kl:m:nop:P:suv")) != -1)
+	while ((ch = getopt(argc, argv, "46Aa:b:cdf:kl:m:nop:P:suvx")) != -1)
 		switch (ch) {
 		case '4':
 			family = PF_INET;
@@ -401,6 +402,9 @@
 		case 'v':		/* log facility and priority */
 		  	LogFacPri++;
 			break;
+		case 'x':
+		        DoFsync++;	/* Do fsync() */	
+			break;
 		case '?':
 		default:
 			usage();
@@ -716,29 +720,34 @@
 
 		for (p = line; (q = strchr(p, '\n')) != NULL; p = q + 1) {
 			*q = '\0';
-			printsys(p);
+			printsys(p, ISKERNEL | ADDDATE);
 		}
 		len = strlen(p);
 		if (len >= MAXLINE - 1) {
-			printsys(p);
+			printsys(p, ISKERNEL | ADDDATE);
 			len = 0;
 		}
+		/* XXX: This could be as well on the end of this function */
+		if (!DoFsync)
+		   fsync (Files->f_file);
 		if (len > 0) 
 			memmove(line, p, len + 1);
 	}
 	if (len > 0)
-		printsys(line);
+		printsys(line, ISKERNEL | ADDDATE | SYNC_FILE);
 }
 
 /*
  * Take a raw input line from /dev/klog, format similar to syslog().
  */
 static void
-printsys(char *p)
+printsys(char *p, int flags)
 {
-	int pri, flags;
+	int pri;
 
-	flags = ISKERNEL | SYNC_FILE | ADDDATE;	/* fsync after write */
+	if (DoFsync) 
+	   flags |= SYNC_FILE;	/* fsync after write */
+	   
 	pri = DEFSPRI;
 	if (*p == '<') {
 		pri = 0;


>Release-Note:
>Audit-Trail:

From: Kris Kennaway <kris@obsecurity.org>
To: Divacky Roman <xdivac02@stud.fit.vutbr.cz>
Cc: FreeBSD-gnats-submit@FreeBSD.org
Subject: Re: bin/61664: syslogd speedup
Date: Thu, 22 Jan 2004 14:44:14 -0800

 On Wed, Jan 21, 2004 at 10:15:22AM +0100, Divacky Roman wrote:
 
 > >Description:
 > 	When syslogd logs kernel messages it does it quite weird way. It reads
 > 	buffer from /dev/kmsg and then writes it into log file fsync()ing after
 > 	EACH newline.. this is very slow and much less secure than fsync()ing
 > 	it after whole buffer... this patch implements such behviour
 
 Why do you say "much less secure"?
 
 It's possible the fsync() behaviour is deliberate - this may allow
 kernel messages to be written to disk if a panic occurs before the
 entire buffer is filled, which is useful for analysis of the crash.
 
 Kris
State-Changed-From-To: open->closed 
State-Changed-By: dwmalone 
State-Changed-When: Mon Mar 22 03:20:40 PST 2004 
State-Changed-Why:  
I'm going to close this PR because PR 63388 addresses the same issue 
but in a way that is likely to be acceptable to more people. (I 
think the patch in this PR also has a bug: it only fsyncs the first 
file syslogd has open as opposed to the file(s) that kernel messages 
are being logged to...) 

Thanks for being up the issue though - hopefully I'll be able to 
close the other PR soon. 

David. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=61664 
>Unformatted:
