Track File Access
I have a file and i would like to track all pids of processes that are accessing file. Are there any functions available in C/c++ to do this. I would also li开发者_StackOverflow社区ke to grant access to a file only if the pid of the process is in a predefined list of pids.
On Linux, you can find out the files open by a process by calling readlink()
on the links within /proc/<pid>/fd/
. To find all the processes with a given file open, you can iterate over every PID
in /proc
, looking for the file in question. Note that you will often not be able to see the files open by processes owned by other users, unless you are root.
There is no way to restrict access to a particular PID - PIDs are assigned to processes when they are started, and are re-used for other processes after the process exits. File permissions are based on the UID, GID and supplementary GIDs of a process, rather than on PID.
Perhaps if you describe what you are trying to do more fully (in a new question), we will be able to help you.
What you are asking are operating system specific tasks for which I am quite sure no standard libraries exist. If you are lucky, there might be third-party libraries that do it for you, but I wouldn't hold my breath :)
If you specify which operating systems and environment you want to do this in, chances are better that someone might be able to give a detailed reply, for that platform.
精彩评论