From root@capella.spg.more.net  Tue Mar  6 23:10:20 2007
Return-Path: <root@capella.spg.more.net>
Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 580B316A402
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  6 Mar 2007 23:10:20 +0000 (UTC)
	(envelope-from root@capella.spg.more.net)
Received: from capella.spg.more.net (capella.spg.more.net [204.185.42.113])
	by mx1.freebsd.org (Postfix) with ESMTP id 38FF013C441
	for <FreeBSD-gnats-submit@freebsd.org>; Tue,  6 Mar 2007 23:10:20 +0000 (UTC)
	(envelope-from root@capella.spg.more.net)
Received: by capella.spg.more.net (Postfix, from userid 0)
	id BC50117055; Tue,  6 Mar 2007 16:32:02 -0600 (CST)
Message-Id: <20070306223238.BC50117055@capella.spg.more.net>
Date: Tue,  6 Mar 2007 16:32:02 -0600 (CST)
From: dan@more.net
Reply-To: dan@more.net
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: serial port console output garbled (patch)
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         110017
>Category:       kern
>Synopsis:       [libexec] [patch] serial port console output garbled
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Mar 06 23:20:04 GMT 2007
>Closed-Date:    
>Last-Modified:  Wed Sep 15 22:10:01 UTC 2010
>Originator:     Dan D Niles
>Release:        FreeBSD 6.2-RELEASE-p2 i386
>Organization:
MOREnet - Missouri Research and Education Network
>Environment:
System: FreeBSD server.net 6.2-RELEASE-p2 FreeBSD 6.2-RELEASE-p2 #0: Tue Feb 27 22:56:09 UTC 2007 root@i386-builder.daemonology.net:/usr/obj/usr/src/sys/SMP i386

Dell 2950 with console redirection on, ttyd0 enabled in /etc/ttys.
>Description:
	When using a serial port for a console, entering a blank line,
a line that starts with a '-', or a line containing only spaces causes
the output to become garbled.  This can result in loss of console output
when you are using a console server that stores console output (such
as the Cyclades AlterPath console servers).

The problem has been there for a while, but is more pronounced on a
Dell 1950 or 2950.
>How-To-Repeat:
	On a Dell 2950, enable console redirection and set up ttyd0
in /etc/ttys.
	Connect to the serial port.
	Hit Enter.
	The output will look something like:

nooo~:Woo{;>6(|uww~now~nou})|t}}t9-

>Fix:

	Inserting a sleep(1) before the setttymode(0) inside the
main for loop fixes the problem.  I'm not sure _why_ it works,
but I don't think it is unreasonable to have a small delay after
entering invalid input.

Here are two potential patches.  The first one is the simplest.
The second one can be used instead if a 1 sec delay is undesireable 
when AB or PS are set.  The output might get garbled when AB or
PS are set, I did not test that.  If they do, the second patch
would not work.

--- libexec/getty/main.c.orig	Tue Mar  6 15:55:35 2007
+++ libexec/getty/main.c	Tue Mar  6 15:58:06 2007
@@ -295,6 +295,8 @@
 		    /* remove any noise */
 		    (void)tcflush(STDIN_FILENO, TCIOFLUSH);
 		}
+		if (!first_sleep)
+		    sleep(1);
 		first_sleep = 0;
 
 		setttymode(0);
@@ -376,6 +378,7 @@
 				continue;
 			if (name[0] == '-') {
 				puts("user names may not start with '-'.");
+				oflush();
 				continue;
 			}
 			if (!(upper || lower || digit)) {


And the alternate patch:


--- libexec/getty/main.c.orig	Tue Mar  6 15:55:35 2007
+++ libexec/getty/main.c.alt	Tue Mar  6 16:06:50 2007
@@ -372,10 +372,14 @@
 			oflush();
 			alarm(0);
 			signal(SIGALRM, SIG_DFL);
-			if (name[0] == '\0')
+			if (name[0] == '\0'){
+				sleep(1);
 				continue;
+			}
 			if (name[0] == '-') {
 				puts("user names may not start with '-'.");
+				oflush();
+				sleep(1);
 				continue;
 			}
 			if (!(upper || lower || digit)) {
@@ -384,6 +388,7 @@
 					    "invalid auto-login name: %s", AL);
 					exit(1);
 				} else
+					sleep(1);
 					continue;
 			}
 			set_flags(2);
>Release-Note:
>Audit-Trail:

From: "Heath N. Caldwell" <hcaldwell@fastsoft.com>
To: bug-followup@FreeBSD.org, dan@more.net
Cc: Ji Li <jli@fastsoft.com>
Subject: Re: kern/110017: [libexec] [patch] serial port console output
 garbled
Date: Wed, 15 Sep 2010 14:49:44 -0700

 We ran into this same problem with the PowerEdge 1950 and PowerEdge
 R710.
 
 I spent some time looking at the problem, and it looks like what is
 happening is that oflush() is called right before a case that will end
 up continuing back to the top of the loop, at which time setttymode()
 gets called, which calls tcflush() right off the bat (with TCIOFLUSH).
 So the buffered data is sent, but for at least these machines, it
 doesn't get a chance to be completely written before the call to
 tcflush(), causing it to get into a garbled state.
 
 Here is a patch that uses tcdrain() to make sure that the data is
 completely written to the terminal before returning from oflush().  I
 think that this addresses the problem more directly.
 
 --- getty/main.c	(revision 212627)
 +++ getty/main.c	(working copy)
 @@ -689,8 +689,10 @@
  static void
  oflush(void)
  {
 -	if (obufcnt)
 +	if (obufcnt) {
  		write(STDOUT_FILENO, outbuf, obufcnt);
 +		tcdrain(STDOUT_FILENO);
 +	}
  	obufcnt = 0;
  }
  
 -- 
 Heath Caldwell
 hncaldwell@fastsoft.com
>Unformatted:
