LD_PRELOAD library to watch files opened by an application and send them to a FIFO, without ever blocking
Compile with "make"
By default, it will use the FIFO /tmp/open_watch_fifo. You need to create it in advance with:
$ mkfifo /tmp/open_watch_fifo.
Then simply export LD_PRELOAD=/.../openWatch.so.
Note that the path needs to be absolute unless you copy the library to /usr/lib. In this case it is enough with:
export LD_PRELOAD=openWatch.so.
In one terminal you can do: cat /tmp/open_watch_fifo
.
Then go to the previous terminal and simply execute your app.
You will see the names of the opened files appear.
If you need some event-driven handling (my initial motivation), you can use something like:
while read filename < /tmp/open_watch_fifo
do
echo $filename has been opened
done
If you want a more robust client, put something like this into your crontab:
@reboot mkfifo /tmp/open_watch_fifo ; daemon -o nowlistening.log --name nowlistening -- xargs -d '\n' -a /tmp/open_watch_fifo -n1 ~/bin/onFileOpened.sh
Credits
Based on this code: https://rafalcieslak.wordpress.com/2013/04/02/dynamic-linker-tricks-using-ld_preload-to-cheat-inject-features-and-investigate-programs/
I added proper support to open()'s varargs and robust writing to a FIFO.