Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 530
user1610950 Avatar asked Nov 08 '22 07:11

user1610950


1 Answers

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.
  • if your app A is already running: strace -ewrite -ewrite=FD -pPID
  • if your app A is yet to be started: strace -ewrite -ewrite=FD A

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

like image 136
Armali Avatar answered Nov 15 '22 04:11

Armali