Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 13 Mar 2009 09:10:46 +0000
From:      Stephen Blott <sblott@computing.dcu.ie>
To:        freebsd-questions@freebsd.org
Subject:   fd to fd passthrough in kernel
Message-ID:  <20090313091046.GA80982@computing.dcu.ie>

next in thread | raw e-mail | index | archive | help

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.



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090313091046.GA80982>