linux内核中读写文件
来源:互联网 发布:淘宝客源码卷皮 编辑:程序博客网 时间:2024/05/16 15:26
内核中可以使用sys_open系统调用来打开文件,也可以直接使用filp_open函数,前者返回一个文件句柄,后者返回一个struct file*型的指针。不过刚开始不清楚,使用:
fd = sys_open(path, O_RDONLY, 0);
打开文件却不能成功。而:
filp = filp_open(path, O_RDONLY, 0);
却能成功。
但使用filp->f_op->read(filp, buf, size, &filp->f_pos);
读文件却不能将内容读到buf中去。
后面分析sys_open的原型为:
asmlinkage long sys_open(const char __user * filename, int flags, int mode);
在filename前面有个__user的修饰符,这里似乎说明了这个filename是来自用户空间,进而查看struct file中f_op对应用类型struct file_operation结构体对read指向的函数原型的定义中,buf参数也是用__user修饰的,也就是说,它只能向用户空间传递。
有人使用set_fs(get_ds())来设置进程寻址界限,使其可以对内核空间进行寻址,从而可以读写内核缓冲区。
1.读文件:
struct file *fp;
mm_segment_t fs;
loff_tpos;
fp = filp_open("/data/xx.conf", O_RDWR | O_CREAT, 0644);
if (IS_ERR(fp)) { printk("create file error\n"); return -1;}
fs = get_fs();
set_fs(KERNEL_DS);
pos = 0;
vfs_write(fp, buf, sizeof(buf), &pos);
filp_close(fp, NULL);
set_fs(fs);
2.读文件:
struct file *fp;
mm_segment_t fs;
loff_t pos;
fp = filp_open("/data/xx.conf",O_RDWR,0644);
if (IS_ERR(fp)){printk("read file error\n");return -1;}
fs =get_fs();
set_fs(KERNEL_DS);
pos =0;
vfs_read(fp, buf1, sizeof(buf), &pos);
filp_close(fp,NULL);
set_fs(fs);
- Linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- linux内核中读写文件
- Linux内核中读写文件
- Linux内核中读写文件
- 如何在linux内核中读写文件
- 在linux内核(驱动)中读写文件
- 在linux内核中读写文件
- 如何在linux内核中读写文件
- 在linux内核中 读写上层文件
- 在linux内核中读写文件
- 在linux内核中读写文件
- ios 避免弹出键盘挡住屏幕
- 黑马程序员---面试题破解之二_银行业务调度系统
- Storm集群安装
- B. Fence
- Linux时间子系统之六:高精度定时器(HRTIMER)的原理和实现
- linux内核中读写文件
- C++必知必会之(8)指向指针的指针
- 算法习题66:颠倒栈
- Maven按照目标环境生成指定可执行的jar
- Syntax error: word unexpected (expecting ")")错误的解决方法
- 仿网易新闻效果源码分析
- linux 查看文件系统
- eclipse中几种加入jar包方式的区别
- Linux 下Sqlite3 的安装及应用