如何在 linux 内核中以编程方式读取 linux 文件权限
How to read linux file permission programmatically in linux kernel
如果我在 linux 内核中将权限声明为 umode_t 类型的变量模式,如何检查它是否具有读取或写入权限
例如 - 我将权限存储到 umode_t file_mode,现在如何检查它是否以编程方式在 linux
中具有读写权限
我尝试使用 filp->f_op->read,但它总是抛出一个错误,即使文件具有读取权限
umode_t input_file_mode;
filp = filp_open( args->inputfile,O_RDONLY,0 );
input_file_mode = filp->f_inode->i_mode;
if (!filp->f_op->read)
{
error = -EACCES;
printk("reading input file failed\n");
}
要检查用户是否对给定的 inode 具有特定权限,请使用 inode_permissions
函数。它在 linux/fs.h
中声明并具有以下定义(在 fs/namei.c 中):
/**
* inode_permission - Check for access rights to a given inode
* @inode: Inode to check permission on
* @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
*
* Check for read/write/execute permissions on an inode. We use fs[ug]id for
* this, letting us set arbitrary permissions for filesystem access without
* changing the "normal" UIDs which are used for other things.
*
* When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
*/
int inode_permission(struct inode *inode, int mask)
用法示例:
if(inode_permission(inode, MAY_WRITE | MAY_READ) == 0) {
// Current user has permissions to read and write the file.
}
如果我在 linux 内核中将权限声明为 umode_t 类型的变量模式,如何检查它是否具有读取或写入权限
例如 - 我将权限存储到 umode_t file_mode,现在如何检查它是否以编程方式在 linux
中具有读写权限我尝试使用 filp->f_op->read,但它总是抛出一个错误,即使文件具有读取权限
umode_t input_file_mode;
filp = filp_open( args->inputfile,O_RDONLY,0 );
input_file_mode = filp->f_inode->i_mode;
if (!filp->f_op->read)
{
error = -EACCES;
printk("reading input file failed\n");
}
要检查用户是否对给定的 inode 具有特定权限,请使用 inode_permissions
函数。它在 linux/fs.h
中声明并具有以下定义(在 fs/namei.c 中):
/**
* inode_permission - Check for access rights to a given inode
* @inode: Inode to check permission on
* @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
*
* Check for read/write/execute permissions on an inode. We use fs[ug]id for
* this, letting us set arbitrary permissions for filesystem access without
* changing the "normal" UIDs which are used for other things.
*
* When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
*/
int inode_permission(struct inode *inode, int mask)
用法示例:
if(inode_permission(inode, MAY_WRITE | MAY_READ) == 0) {
// Current user has permissions to read and write the file.
}