在UNIX C,一个进程怎么能说没有打开它的权限是什么它有一个文件? [英] In C on Unix, how can a process tell what permissions it has to a file without opening it?

查看:72
本文介绍了在UNIX C,一个进程怎么能说没有打开它的权限是什么它有一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用stat()要弄清楚什么权限是所有者,组或其他人,我可以用geteuid()和getpwuid()来获取进程的用户名。我不太知道如何让用户所属的组没有一个系统调用虽然。

I can use stat() to figure out what permissions the owner, group, or others have and I can use geteuid() and getpwuid() to get the user name of the process. I'm not quite sure how to get the groups a user belongs to without a system call though.

即使知道如何获得团体,这似乎是一个大量的工作,所有这些信息整合。是否有更简单的方法?

Even knowing how to get the groups, it seems like a lot of work to integrate all of this information. Is there an easier way?

推荐答案

借助访问()POSIX函数可以检查的权限,而无需打开它。然而,它需要一个系统调用

The access() POSIX function can check the permissions without opening it. However, it needs to a syscall.

访问()函数将检查由路径名命名的文件根据包含在AMODE位模式指向可访问的路径说法,地方的有效用户ID和真实组ID使用实际用户ID到位的有效组ID的。

The access() function shall check the file named by the pathname pointed to by the path argument for accessibility according to the bit pattern contained in amode, using the real user ID in place of the effective user ID and the real group ID in place of the effective group ID.

例如:

access("/etc/passwd",W_OK)

检查,如果你有passwd文件的写权限。随着R_OK,读取权限检查。

checks if you have write access to the passwd file. With R_OK, read permissions are checked.

EACCESS()函数( euidaccess 的代名词),使用有效的用户和组ID。虽然EACCESS似乎得到广泛支持,据我知道这不是POSIX标准的一部分。

The eaccess() function (euidaccess is a synonym) uses the effective user and group id. While eaccess seems to be widely supported, as far as I know it is not part of the POSIX standard.

这篇关于在UNIX C,一个进程怎么能说没有打开它的权限是什么它有一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆