From nobody@FreeBSD.org  Sun Aug 17 13:23:03 2008
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id 1FD971065671
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Aug 2008 13:23:03 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 079468FC1C
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Aug 2008 13:23:03 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id m7HDN24U087623
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 17 Aug 2008 13:23:02 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id m7HDN2Ha087622;
	Sun, 17 Aug 2008 13:23:02 GMT
	(envelope-from nobody)
Message-Id: <200808171323.m7HDN2Ha087622@www.freebsd.org>
Date: Sun, 17 Aug 2008 13:23:02 GMT
From: Jacek Wikiera <jacek@wikiera.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: Write routine called forever in Sample Echo Pseudo-Device Driver
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         126590
>Category:       docs
>Synopsis:       [patch] Write routine called forever in Sample Echo Pseudo-Device Driver in Architecture Handbook
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    eadler
>State:          closed
>Quarter:        
>Keywords:       archhandbook
>Date-Required:  
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Aug 17 13:30:01 UTC 2008
>Closed-Date:    Thu Nov 29 05:11:52 UTC 2012
>Last-Modified:  Thu Nov 29 05:11:52 UTC 2012
>Originator:     Jacek Wikiera
>Release:        n/a
>Organization:
n/a
>Environment:
n/a
>Description:
Location:
FreeBSD Architecture Handbook, 9.4 Character Devices,  example driver code.

http://www.freebsd.org/doc/en_US.ISO8859-1/books/arch-handbook/driverbasics-char.html

Description:
The function echo_write() is incorrect - it does not zero an important variable and makes the example a bad example.
If code is built from the example, the function echo_write() does not zeroe uio->uio_resid and thus fails to tell the OS that all the data has been consumed.

The function is called again and again forever (until the caller is terminated, I guess).


>How-To-Repeat:
Build the example code, and type at the command prompt:
cat > /dev/echo

Next type a line followed by return. This will cause the file /dev/echo to be open, and continuous calls to echo_write subroutine.

The cat program will hang now and not able to read Ctrl-D from the terminal. Ctrl-C is needed.
>Fix:
In the function echo_write() add the following line:

uio->uio_resid = 0; 

This will tell the OS that all the data has been consumed.
See the source of the null device for this hint.



Patch attached with submission follows:

--- driverbasics-char.html      2008-08-16 06:08:26.000000000 +0200
+++ driverbasics-char_fix.html  2008-08-17 15:10:37.531250000 +0200
@@ -200,6 +200,8 @@
     *(echomsg-&gt;msg + MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE - 1)) = 0;
     echomsg-&gt;len = MIN(uio-&gt;uio_iov-&gt;iov_len, BUFFERSIZE);
 
+    uio->uio_resid = 0; /* signall that all data is consumed */
+
     if (err != 0) {
         uprintf("Write failed: bad address!\n");
     }


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-doc->eadler 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Wed Nov 14 23:42:06 UTC 2012 
Responsible-Changed-Why:  
I can't believe this has sat all this time untaken - take and attempt to 
figure out what is going on 

http://www.freebsd.org/cgi/query-pr.cgi?pr=126590 
State-Changed-From-To: open->closed 
State-Changed-By: eadler 
State-Changed-When: Thu Nov 29 05:11:51 UTC 2012 
State-Changed-Why:  
I've rewritten this example such that the attached patch is no longer 
required. Thanks for making FreeBSD better and I'm sorry that this PR 
took so long to be closed. 

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