Assuming we have some PID and absolute file path [not a symlink, just a regular file] – what is the most efficient way to determine that PID has read access to this file?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I’m only aware of one way to do this. First, find the UID and GID of the process by constructing the path
/proc/+ the PID. For example/proc/4261. You then stat() that path and get its UID and GID. Then, you stat() the file you want to check for read access and check whether the UID/GID of the process has read permissions:(It is assumed you already constructed the “/proc/[PID]” path in
path_to_proc.)Note that the code is not perfect. It does not handle the possibility that the user of the process actually belongs to the file’s group without it being the user’s primary group. To deal with that, you will need to make use of getgrouplist() (which means you will need to convert the process UID to a string containing the actual username first, and then compare all returned groups to the file’s group and if one matches, check for group read access (S_IRGRP).)