fd to fd passthrough in kernel

Stephen Blott sblott at computing.dcu.ie
Fri Mar 13 02:20:59 PDT 2009


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

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?



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

More information about the freebsd-questions mailing list