Question

I need a program to list all the file that are accessed/opened by a process in Linux. It should work like this,

o/p: The full path of the files that the process is accessing.

Don't want to use 'lsof' utility or any other utility.

Is there anyway to achieve this programmatically?

Was it helpful?

Solution

If you want just the files which are accessible thru opened file descriptors by process of pid 1234, list the /proc/1234/fd/ directory (most of the entries are symlinks). You'll also get additional details thru /proc/1234/fdinfo/

Try

ls -l /proc/self/fd/

to get an idea of what these files contain.

Programatically you could use readdir(3) after opendir(3) on these directories (and also readlink(2), at least for entries in /proc/1234/fd/ ....). See also proc(5)

Notice that /proc/ is Linux specific. Some other Unixes have it (e.g. Solaris), with very different contents, properties, semantics.

If you care also about files which have been opened and closed in the past by some process, it is much more difficult. See also inotify(7) and ptrace(2)...

To convert a file path to a "canonical" absolute fiile path, use realpath(3).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top