我的C程序如何检查是否有一个给定的文件执行的权限? [英] How can my C program check if it has execution permission on a given file?
问题描述
有没有一种方法,以确定过程中的可能的执行文件,而无需实际执行它(例如,通过调用 execv(文件路径,参数)
只是失败,并发现的errno == EACCES
)?
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
)?
我能统计
文件并观察 ST_MODE
,但后来我还是不要&rsquo的;知道如何,涉及到过程
I could stat
the file and observe st_mode
, but then I still don’t know how that pertains to the process.
在理想情况下,I&rsquo的;要找的沿
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.
先谢谢了。
推荐答案
的http:// Linux操作系统。 die.net/man/2/access
请注意周围的suid小幅tricksiness - 如果进程具有有效的超级用户权限是不考虑,因为该函数的总体思路是,以检查是否原调用用户的应的访问到该文件,不检查是否这个过程中的可能的访问文件。
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.
这篇关于我的C程序如何检查是否有一个给定的文件执行的权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!