From owner-svn-src-head@FreeBSD.ORG Fri Feb 10 22:16:18 2012 Return-Path: Delivered-To: svn-src-head@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 6A2101065670; Fri, 10 Feb 2012 22:16:18 +0000 (UTC) (envelope-from cracauer@FreeBSD.org) Received: from svn.freebsd.org (svn.freebsd.org [IPv6:2001:4f8:fff6::2c]) by mx1.freebsd.org (Postfix) with ESMTP id 547658FC12; Fri, 10 Feb 2012 22:16:18 +0000 (UTC) Received: from svn.freebsd.org (localhost [127.0.0.1]) by svn.freebsd.org (8.14.4/8.14.4) with ESMTP id q1AMGI7p098194; Fri, 10 Feb 2012 22:16:18 GMT (envelope-from cracauer@svn.freebsd.org) Received: (from cracauer@localhost) by svn.freebsd.org (8.14.4/8.14.4/Submit) id q1AMGI0m098192; Fri, 10 Feb 2012 22:16:18 GMT (envelope-from cracauer@svn.freebsd.org) Message-Id: <201202102216.q1AMGI0m098192@svn.freebsd.org> From: Martin Cracauer Date: Fri, 10 Feb 2012 22:16:18 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cc: Subject: svn commit: r231449 - head/usr.bin/tee X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Feb 2012 22:16:18 -0000 Author: cracauer Date: Fri Feb 10 22:16:17 2012 New Revision: 231449 URL: http://svn.freebsd.org/changeset/base/231449 Log: Fix bin/164947: tee looses data when writing to non-blocking file descriptors tee was not handling EAGAIN patch submitted by Diomidis Spinellis . Thanks so much reproduced and re-tested locally Modified: head/usr.bin/tee/tee.c Modified: head/usr.bin/tee/tee.c ============================================================================== --- head/usr.bin/tee/tee.c Fri Feb 10 22:14:34 2012 (r231448) +++ head/usr.bin/tee/tee.c Fri Feb 10 22:16:17 2012 (r231449) @@ -42,8 +42,10 @@ static const char rcsid[] = #endif /* not lint */ #include +#include #include #include +#include #include #include #include @@ -60,6 +62,7 @@ static LIST *head; static void add(int, const char *); static void usage(void); +static void waitfor(int fd); int main(int argc, char *argv[]) @@ -106,9 +109,14 @@ main(int argc, char *argv[]) bp = buf; do { if ((wval = write(p->fd, bp, n)) == -1) { - warn("%s", p->name); - exitval = 1; - break; + if (errno == EAGAIN) { + waitfor(p->fd); + wval = 0; + } else { + warn("%s", p->name); + exitval = 1; + break; + } } bp += wval; } while (n -= wval); @@ -137,3 +145,15 @@ add(int fd, const char *name) p->next = head; head = p; } + +/* Wait for the specified fd to be ready for writing */ +static void +waitfor(int fd) +{ + fd_set writefds; + + FD_ZERO(&writefds); + FD_SET(fd, &writefds); + if (select(fd + 1, NULL, &writefds, NULL, NULL) == -1) + err(1, "select"); +}