Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9297223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:01:03+00:00 2026-06-18T22:01:03+00:00

Assuming we have some PID and absolute file path [not a symlink, just a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T22:01:05+00:00Added an answer on June 18, 2026 at 10:01 pm

    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.)

    struct stat buf;
    
    // Get UID and GID of the process.
    stat(path_to_proc, &buf);
    uid_t proc_uid = buf.st_uid;
    gid_t proc_gid = buf.st_gid;
    
    // Get UID and GID of the file.
    stat(path_to_file_you_want_to_check, &buf);
    
    // If the process owns the file, check if it has read access.
    if (proc_uid == buf.st_uid && buf.st_mode & S_IRUSR) {
        // Yes, the process has read access.
    }
    
    // Check if the group of the process's UID matches the file's group
    // and if so, check for read/write access.
    else if (proc_gid == buf.st_gid && buf.st_mode & S_IRGRP) {
        // Yes, the process has read access.
    }
    
    // The process's UID is neither the owner of the file nor does its GID
    // match the file's.  Check whether the file is world readable.
    else if (buf.st_mode & S_IROTH) {
        // Yes, the process has read access.
    }
    

    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).)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that uses ctypes to try to determine if the file
I have some code that does not seem to operate the way it should.
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
Assuming you have only the URL to a file (hosted on the same server
I have some search queries like so: George AND NOT Washington OR Abraham Dog
I have some simple lines of code: that.click(function(){ window.open($('.linkBox input').val()); }); Assuming I'm redirecting
I just want to know for what java.util.Collections.checkedList() is actually used. I have some
I have some trouble reading file in Java. What a file looks like: Answer
Assuming I have some array in heap doesn't matter constructed by malloc or new
Assuming I have some objects like this: Class NetworkSwitch { private String _name; String

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.