clinuxmacosstracedtruss

What is the best method to detect which files are used/modified/created/deleted by a process?


I want to write software that will detect all used/created/modified/deleted files during the execution of a process (and its child processes). The process has not yet run - the user provides a command line which will later be subprocessed via bash, so we can do things before and after execution, and control the environment the command is run in.

I have thought of four methods so far that could be useful:

I need help choosing the optimal path to go down. I have already implemented the first method because it was simple and gave me a way to work on the logging backend (http://ac.gt/log) but really i need to upgrade to one of the other methods. Your advice would be invaluable :)


Solution

  • Take a look to the source code of "strace" (and its -f to trace children). It does basically what you are trying to do. It captures all the system calls of the process (or its childs) so you can grep for operations like "open", etc.

    The following link provides some examples of implementing your own strace by using the ptrace system call:

    https://blog.nelhage.com/2010/08/write-yourself-an-strace-in-70-lines-of-code/