How can I override C functions (like with LD_PRELOAD) at runtime?

824 views Asked by At

I have some Python code that uses a library that implements virtual file systems. For the drivers for those virtual file systems to work a bunch of C functions (like readdir(), opendir(), fseek()) need to be overridden/replaced - with the replacements defined in a .so/.cpp file. Usually this could be done by setting LD_PRELOAD to that .so file. However, I need to be able to override these functions at runtime, and ideally revert back to the non-overridden functions once the Python functions that use those overridden functions have executed. Is this possible?

1

There are 1 answers

3
jim mcnamara On

I am guessing you want ctypes. Here is a discussion: https://docs.python.org/2/library/ctypes.html