简体   繁体   English

如何在Linux内核中以编程方式读取linux文件权限

[英]How to read linux file permission programmatically in linux kernel

if i declare the permissions into a variable mode of type umode_t in linux kernel, how to check if it is has read or write permissions 如果我在Linux内核中将权限声明为umode_t类型的可变模式,如何检查它是否具有读取或写入权限

For example - I am storing the permissions into umode_t file_mode, now how to check if it has read and write permissions programatically in linux 例如-我将权限存储到umode_t file_mode,现在如何检查它是否在Linux中以编程方式具有读写权限

I tried using filp->f_op->read, but it is always throwing me an error even when file has read access 我尝试使用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");
}

For check whether a user has specific permissions for given inode, use inode_permissions function. 要检查用户是否具有给定inode的特定权限,请使用inode_permissions函数。 It is declared in linux/fs.h and has following definition (in fs/namei.c ): 它在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)

Usage example: 用法示例:

if(inode_permission(inode, MAY_WRITE | MAY_READ) == 0) {
    // Current user has permissions to read and write the file.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM