cfile-handlingfile-descriptorfdopen

Capturing data being written to open file descriptor


Is it possible to write a program that's able to take another application's open file descriptors and just pass along their contents without any conversion?

Let's say App A has an open FD to some file on disk that it's writing data to.

I'd like to be able to somehow get access to the open FD so that anytime App A writes data to that file I can broadcast that write to some other App that's interested in that operation.

I'd like to be able to multiplex the read/write operations on open FD.

A more concrete example; I have a midi keyboard and some synthesizers, i'd like to be able to open the midi keyboard file descriptor and pass all the incoming write operations to 0-N interested synthesizers.


Solution

  • strace has an option that does the main part of what you want.

           -e write=set
                       Perform a full hexadecimal and ASCII dump of all the
                       data written to file descriptors listed in the spec-
                       ified  set.  For example, to see all output activity
                       on file descriptors 3 and 5 use -e write=3,5.   Note
                       that  this is independent from the normal tracing of
                       the write(2) system call which is controlled by  the
                       option -e trace=write.
    

    It is trivial to convert the produced hexadecimal dump back to raw data and feed that to other apps.