Capturing data being written to open file descriptor
Asked Answered
H

1

6

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.

Hay answered 7/5, 2018 at 16:45 Comment(2)
This is OS-dependent; which OS do you have in mind?Herlindaherm
yes, this is Linux, Unix not windows.Hay
H
2

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.

Herlindaherm answered 18/5, 2018 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.