My code needs to go through files in a directory, picking only those, which are currently opened (for writing) by any other process on the system.
The ideal solution would apply for all Unixes, but I'll settle for a Linux-only.
The program is written in Python, but I can add a custom C-function, if I have to -- I just need to know, what API is available for this...
One suggestion I found was to go through all file-descriptors under Linux /proc, resolving their links to see, if they point at the file of interest. But that seems rather heavy...
I know, for example, that opening a file increases its reference count -- filesystem will not deallocate blocks of an opened file even if it is deleted -- until it is closed -- the feature relied upon by tmpfile(3).
Perhaps, a user process can get access to these records in the kernel?
lsofdoes this. Download the source forlsofand read it.lsofdoes this and probably does it by reading/proc:)lsof-- andfuser-- scan/proc. But that yields more information than I need -- I don't care, which processes have the file open. I just want to know, whether any such exist. Perhaps, this information can be obtained more cheaply, than/procrescan?