From owner-freebsd-bugs@FreeBSD.ORG Tue Nov 27 18:00:04 2007 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id E53D116A46C for ; Tue, 27 Nov 2007 18:00:04 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 0845F13C4E3 for ; Tue, 27 Nov 2007 18:00:03 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.2/8.14.2) with ESMTP id lARI02i5019888 for ; Tue, 27 Nov 2007 18:00:02 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.2/8.14.1/Submit) id lARI02Xm019887; Tue, 27 Nov 2007 18:00:02 GMT (envelope-from gnats) Resent-Date: Tue, 27 Nov 2007 18:00:02 GMT Resent-Message-Id: <200711271800.lARI02Xm019887@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Charles Hardin Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id A1F0B16A421 for ; Tue, 27 Nov 2007 17:59:51 +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 901BC13C4CE for ; Tue, 27 Nov 2007 17:59:51 +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 lARHxjCx064228 for ; Tue, 27 Nov 2007 17:59:45 GMT (envelope-from nobody@www.freebsd.org) Received: (from nobody@localhost) by www.freebsd.org (8.14.2/8.14.1/Submit) id lARHxi16064227; Tue, 27 Nov 2007 17:59:44 GMT (envelope-from nobody) Message-Id: <200711271759.lARHxi16064227@www.freebsd.org> Date: Tue, 27 Nov 2007 17:59:44 GMT From: Charles Hardin To: freebsd-gnats-submit@FreeBSD.org X-Send-Pr-Version: www-3.1 Cc: Subject: kern/118287: [PATCH] tty write is not always atomic for small lines X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Nov 2007 18:00:05 -0000 >Number: 118287 >Category: kern >Synopsis: [PATCH] tty write is not always atomic for small lines >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: sw-bug >Submitter-Id: current-users >Arrival-Date: Tue Nov 27 18:00:02 UTC 2007 >Closed-Date: >Last-Modified: >Originator: Charles Hardin >Release: FreedBSD-CURRENT >Organization: 2Wire Inc. >Environment: >Description: During test automation there has been some unexpected failures because output from different processes has been interspersed. >How-To-Repeat: Using the following test code #include #include #include int main(int argc, char **argv) { char buf[512]; int res, len; if (daemon(0, 1) != 0) { return 1; } len = snprintf(buf, sizeof(buf), "\n", getpid()); if (len >= sizeof(buf)) { return 2; } while (1) { res = write(1, buf, len); if (res != len) { return 3; } } } when running a couple copies of this, it sometimes prints out this: ...more lines like that... In other words, several writes from pid 30 happen in the middle of a write from pid 28 >Fix: So, the attached diff modifies the behavior of ttwrite to try and dump at least OBUFSIZ at a time in the clist so that lines shorter than OBUFSIZ should never be intermingled... This solves 99% of the cases and is easy enough for us to fix the user code to keep it solved... Patch attached with submission follows: Index: tty.c =================================================================== RCS file: /home/ncvs/src/sys/kern/tty.c,v retrieving revision 1.273 diff -u -r1.273 tty.c --- tty.c 20 Jul 2007 09:41:54 -0000 1.273 +++ tty.c 27 Nov 2007 17:49:13 -0000 @@ -2046,13 +2046,16 @@ int i, hiwat, cnt, error, s; char obuf[OBUFSIZ]; - hiwat = tp->t_ohiwat; cnt = uio->uio_resid; error = 0; cc = 0; td = curthread; p = td->td_proc; loop: + /* set a slightly different hi water mark to transfer at least + * OBUFSIZ characters from the user write at a time. + */ + hiwat = imax(tp->t_ohiwat - OBUFSIZ, tp->t_olowat); s = spltty(); if (ISSET(tp->t_state, TS_ZOMBIE)) { splx(s); @@ -2165,7 +2168,7 @@ cp++; cc--; if (ISSET(tp->t_lflag, FLUSHO) || - tp->t_outq.c_cc > hiwat) + tp->t_outq.c_cc > tp->t_ohiwat) goto ovhiwat; continue; } @@ -2198,7 +2201,7 @@ goto loop; } if (ISSET(tp->t_lflag, FLUSHO) || - tp->t_outq.c_cc > hiwat) + tp->t_outq.c_cc > tp->t_ohiwat) break; } ttstart(tp); >Release-Note: >Audit-Trail: >Unformatted: