通常我们只会在linux native/app 层 读写文件,但可能有一些非常特别的情况下,我们需要直接
在Kernel 中读写文件信息。
下面给出典型的Code:
static struct file *open_file(char *path,int flag,int mode)
{
struct file *fp;
fp=filp_open(path, flag, mode);
if (!IS_ERR_OR_NULL(fp)) return fp;
else return NULL;
}
static int read_file(struct file *fp,char *buf,int readlen)
{
if (fp->f_op && fp->f_op->read)
return fp->f_op->read(fp,buf,readlen, &fp->f_pos);
else
return -1;
}
static
本文介绍了如何在Linux内核空间直接读写文件,包括open_file、read_file、write_file和close_file四个关键函数的使用。强调在进行内核读写文件操作时,必须正确设置内存域,并警告此类操作可能导致的安全性和稳定性问题,建议避免在内核层进行文件读写。
订阅专栏 解锁全文
1026

被折叠的 条评论
为什么被折叠?



