Search code examples
clinuxpermissionsexecution

How can my C program check if it has execution permission on a given file?


Is there a way to determine if the process may execute a file without having to actually execute it (e.g. by calling execv(filepath, args) only to fail and discover that errno == EACCES)?

I could stat the file and observe st_mode, but then I still don’t know how that pertains to the process.

Ideally, I’m looking for a function along the lines of

get_me_permissions(filepath, &status); // me: the process calling this function
// when decoded, status tells if the process can read, write, and/or execute 
// the file given by filepath.

Thanks in advance.


Solution

  • http://linux.die.net/man/2/access

    Note the slight tricksiness around suid - if the process has effective superuser privilege it isn't taken into account, since the general idea of the function is to check whether the original invoking user should have access to the file, not to check whether this process could access the file.