Is there a way to monitor and redirect file access of a thread or process?
For example a thread wants to read /etc/mysql/my.cnf and I want to change the access to ~/my.cnf or if I run touch /etc/test.config I want the file to be redirected to ~/somefolder/etc/test.config.
I am looking for a libary preferably for C, C++ which works for Linux/Unix.
Thanks in advance
You could write a shared object that gets pre-loaded when your program starts running. In the .so you'd redefine the libc function
open(). When the caller (the program that's getting fooled) passes as argument the string/etc/mysql/my.cnf, you'd instead open~/my.cnfand return the opened file descriptor. The caller wouldn't know the difference.Your shared object would of course need to call the "real"
open()to to open the file handle; you can get a pointer to the original libc function usingdlsym().This seems overly complicated, but in fact it isn't, it works like a charm. I've used it on several occasions where I had to fool a program that I didn't have the sources for; and it simply works like a clockwork.
If you want to see a proof of concept, check out my blog where I wrote it up. Happy coding!