arkham knight
arkham knight

Reputation: 383

can we check a file's read write permission before opening it in kernel

I want to open a file in read only mode in kernel but before i do that i want to check if the file has permission to read, how can i check it? Because to even check that i would need a file pointer pointing to the input file.

filp_open(args->inputfile, O_RDONLY, 0);

Is there any way to check it before opening it? I tried using, but it always fails

if (!fileptr->f_op->read)
{
     error = -EACCES;
     printk("reading input file failed\n");
}

Upvotes: 0

Views: 371

Answers (1)

Alain Merigot
Alain Merigot

Reputation: 11537

You should use access(char *filepath,int mode) that checks file access rights.

modedescribes what you want to check: F_OK (existence), or an OR combination of R_OK(read), W_OK(write) or X_OK(execute).

So for your problem, you could use:

#include <unistd.h>
...

if( access( filename, R_OK ) != -1 ) {
    // can read file
} else {
    // cannot read file
}

Upvotes: 1

Related Questions