捕获正在写入打开文件描述符的数据

Capturing data being written to open file descriptor

是否可以编写一个程序,使其能够获取另一个应用程序的打开文件描述符并仅传递其内容而不进行任何转换?

假设应用程序 A 有一个打开的 FD 到它正在写入数据的磁盘上的某个文件。

我希望能够以某种方式访问​​打开的 FD,以便 App A 向该文件写入数据时,我可以将该写入广播给对该操作感兴趣的其他应用程序。

我希望能够在打开的 FD 上复用 read/write 操作。

一个更具体的例子; 我有一个 midi 键盘和一些合成器,我希望能够打开 midi 键盘文件描述符并将所有传入的写入操作传递给 0-N 个感兴趣的合成器。

strace 有一个选项可以完成您想要的主要部分。

       -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.
  • 如果您的应用 A 已经是 运行:strace -ewrite -ewrite=<i>FD</i> - p<i>PID</i>
  • 如果您的应用 A 尚未启动:strace -ewrite -ewrite=<i>FD</i> <i>A</i>

将生成的十六进制转储转换回原始数据并将其提供给其他应用程序是微不足道的。