rsalinas / openedFiles2fifo

LD_PRELOAD library to watch files opened by an application and send them to a FIFO, without ever blocking

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

openedFiles2fifo

LD_PRELOAD library to watch files opened by an application and send them to a FIFO, without ever blocking

Build

Compile with "make"

Usage

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.

About

LD_PRELOAD library to watch files opened by an application and send them to a FIFO, without ever blocking

License:GNU General Public License v3.0


Languages

Language:C 77.7%Language:C++ 8.4%Language:Shell 7.9%Language:Makefile 6.0%