在 C 中重定向 posix 文件调用
我们有一个“库”(我们不想更改的代码选择),它是从可以直接访问 2 个文件的角度编写的。它直接在文件描述符上使用“open”、“read”和“seek” posix 调用。
然而,现在我们有一个专有的文件系统,无法通过标准 IO 调用进行访问。鉴于我们不想重写代码,如果我们可以将 IO 调用重定向到已知的函数,然后将其用作接口,那就太好了。
有没有什么方法可以更改上面使用的调用,以便可以用新的函数调用覆盖“read”和“seek”?
谢谢。
We have a "library" (a selection of code we would rather not change) that is written from the perspective that it has access to 2 files directly. It uses "open", "read" and "seek" posix calls directly on a file descriptor.
However, now we have a proprietary file system that cannot be accessed through standard IO calls. Seeing that we do not want to re-write the code, it would be great if we could redirect the IO calls to known functions that could then be used as an interface.
Is there any way of changing the calls used above so that the "read" and "seek" can be over-written with new function calls?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您说不想更改库代码时,您的意思是想使用现有的二进制代码还是仅使用源代码?如果您有源代码并且可以重新编译,我只需在构建库时将
-Dread=my_read -Dopen=my_open
等传递给编译器,然后提供您自己的my_read
等功能。When you say you don't want to change the library code, do you mean you want to use existing binary code, or just source? If you have the source and can recompile, I would simply pass
-Dread=my_read -Dopen=my_open
etc. to the compiler when building the library, and then provide your ownmy_read
etc. functions.您可以尝试的一件事是库函数插入。
One thing you can try is library function interposition.
除了已经提到的使用宏进行函数插入和重命名函数调用之外,另一个仅限 Linux 的选项是使用用户空间中的文件系统。通过这种方式,您可以使使用标准 POSIX 文件系统 API 的其他应用程序可以访问您的专有文件系统。 FUSE hello world 示例出奇的短。
In addition to already mentioned function interposition and renaming function calls using a macro, another Linux-only option is to use Filesystem in Userspace. This way you can make your proprietary filesystem accessible to other applications which use the standard POSIX filesystem API. FUSE hello world example is surprisingly short.