linux kernel read write file 读写文件
来源:互联网 发布:质量效应2梅伦数据 编辑:程序博客网 时间:2024/06/05 17:36
通常我们只会在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;
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 int write_file(struct file *fp,char *buf,int len)
{
if (fp->f_op && fp->f_op->write)
return fp->f_op->write(fp, buf, len, &fp->f_pos);
else
return -1;
}
static int close_file(struct file *fp)
{
filp_close(fp,NULL);
return 0;
}
注意的是您在使用read_file & write_file 之前需要
//read set kernel domain
set_fs(KERNEL_DS);
在read_file & write_file 完成之后,需要
//need set user domain again
set_fs(USER_DS);
一定要成对的出现,不然将直接导致Kernel Crash.
最后强调一点: 如果能在linux native/app 层读写文件,尽量不要在Kernel 中去做这样的工作。
因为这个可能带来安全性的问题,以及可能因为新增代码而影响Kernel稳定性
在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 int write_file(struct file *fp,char *buf,int len)
{
if (fp->f_op && fp->f_op->write)
return fp->f_op->write(fp, buf, len, &fp->f_pos);
else
return -1;
}
static int close_file(struct file *fp)
{
filp_close(fp,NULL);
return 0;
}
注意的是您在使用read_file & write_file 之前需要
//read set kernel domain
set_fs(KERNEL_DS);
在read_file & write_file 完成之后,需要
//need set user domain again
set_fs(USER_DS);
一定要成对的出现,不然将直接导致Kernel Crash.
最后强调一点: 如果能在linux native/app 层读写文件,尽量不要在Kernel 中去做这样的工作。
因为这个可能带来安全性的问题,以及可能因为新增代码而影响Kernel稳定性
0 0
- linux kernel read write file 读写文件
- 读写文件read/write
- Qt Write and Read XML File 读写XML文件
- Linux VFS文件系统之读写(read/write)文件
- 【Linux】使用read、write、lseek函数实现文件读写操作
- Linux VFS文件系统之读写(read/write)文件
- Linux文件结构FILE,与普通open,read,write对比
- Spark WordCount 读写hdfs文件 (read file from hadoop hdfs and write output to hdfs)
- [Ruby笔记]4. ruby 读写文件 File open read write new
- read write 调用过程 文件读写 分析
- Example : Linux kernal file read and write
- linux c通过文件描述符以及write和read方法对文件进行读写
- sys_read()/vfs_read()/vfs_write() Linux VFS文件系统之读写(read/write)文件
- sys_read()/vfs_read()/vfs_write() Linux VFS文件系统之读写(read/write)文件
- linux文件操作函数(open、write、read、close)可用于串口读写
- read-write ordinary file
- Read/Write ini file
- file read and write
- cdh安装
- 设计模式系列之五:代理模式
- git的基本用法
- 同一按钮实现不同事件,即可传入不同参数实现不同处理事件
- Caffe代码导读(1):Protobuf例子
- linux kernel read write file 读写文件
- iOS 中Block的正确使用避免循环引用和Crash
- USACO-Section 3.3 Shopping Offers (DP)
- 第五周实践项目3————时间类(1)
- 在Archive项目 时,出现了UUID报错
- Android开发之ListView实现不同品种分类分隔栏的效果(非ExpandableListView实现)
- Cocos2d-x LoadingBar(进度条)的使用
- 非最大抑制(NMS)
- maven assembly 配置详解