From owner-freebsd-questions@FreeBSD.ORG Fri Mar 13 09:20:58 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B3E001065674 for ; Fri, 13 Mar 2009 09:20:58 +0000 (UTC) (envelope-from blott@computing.dcu.ie) Received: from mailhost.computing.dcu.ie (mailhost.computing.dcu.ie [136.206.11.247]) by mx1.freebsd.org (Postfix) with ESMTP id 4C5A68FC12 for ; Fri, 13 Mar 2009 09:20:57 +0000 (UTC) (envelope-from blott@computing.dcu.ie) Received: from sblott-desktop.computing.dcu.ie (dhcp11143.computing.dcu.ie [136.206.11.143]) by mailhost.computing.dcu.ie (8.13.2/8.13.2) with ESMTP id n2D9Apl2026856 for ; Fri, 13 Mar 2009 09:10:51 GMT Received: from sblott-desktop.computing.dcu.ie (localhost [127.0.0.1]) by sblott-desktop.computing.dcu.ie (8.14.3/8.14.3) with ESMTP id n2D9Ak6v081566 for ; Fri, 13 Mar 2009 09:10:46 GMT (envelope-from blott@sblott-desktop.computing.dcu.ie) Received: (from blott@localhost) by sblott-desktop.computing.dcu.ie (8.14.3/8.14.3/Submit) id n2D9Ak4r081565 for freebsd-questions@freebsd.org; Fri, 13 Mar 2009 09:10:46 GMT (envelope-from blott) Date: Fri, 13 Mar 2009 09:10:46 +0000 From: Stephen Blott To: freebsd-questions@freebsd.org Message-ID: <20090313091046.GA80982@computing.dcu.ie> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.5.19 (2009-01-05) Subject: fd to fd passthrough in kernel X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 13 Mar 2009 09:20:59 -0000 Folks, Does anybody know if the following is possible in FreeBSD? I would like to write a program that: 1. copies its standard input to standard output (verbatim, a la cat with no arguments) 2. exits with an error code of 0 if at least one byte was copied, or a non-zero error code otherwise (that is, if there was no data on standard input) This is easy enough to do, of course. However, the obvious implementation requires all data to be copied from stdin into the process's address space, then written back to the kernel via stdout. A more efficient alternative would be to read one byte in this way, and then issue some system call that arranges for any data on stdin to automatically be transfered to stdout within the kernel --- no extra copying. Effectively, the two file descriptors would be spliced together. I think this is probably possible in Linux; it appears to be what the splice system call there does. Is the same thing possible in FreeBSD? Thanks, Steve Background: I have a little utility called "catok" that does this in the obvious way with copying. I use it in a little server I wrote for playing audiobooks through squeezecenter and remembering the position within the books ... so next time you start you pick up where you left off. The server is just a shell script (sox, lame + some glue) that's called by tcpserver. The catok is needed to determine when there is no more audio from a file or directory, and therefore when it's necessary to move on to the next file or directory. It's a very small issue, but for each listener there are two catok processes running, and it's been gnawing at me for some time whether it's possible to avoid all the copying involved.