arkham knight
arkham knight

Reputation: 383

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

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

I tried using filp->f_op->read, but it is always throwing me an error even when file has read access

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");
}

Upvotes: 1

Views: 1154

Answers (1)

Tsyvarev
Tsyvarev

Reputation: 66288

For check whether a user has specific permissions for given inode, use inode_permissions function. It is declared in linux/fs.h and has following definition (in 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.
}

Upvotes: 2

Related Questions