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

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

假设App A对磁盘上的某个文件有一个开放FD,它正在写入数据.

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

我希望能够在开放FD上复用读/写操作.

一个更具体的例子;
我有一个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 = FD -pPID
>如果您的应用程序A尚未启动:strace -ewrite -ewrite = FD A.

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

点赞