How to read/write files within a Linux kernel module?
来源:互联网 发布:ubuntu删除文件夹 编辑:程序博客网 时间:2024/06/01 08:04
You should be aware that that you should avoid file I/O when possible. The main idea is to go "one level deeper" and call VFS level functions instead of the syscall handler directly:
Includes:
#include <linux/fs.h>#include <asm/segment.h>#include <asm/uaccess.h>#include <linux/buffer_head.h>
Opening a file (similar to open):
struct file* file_open(const char* path, int flags, int rights) { struct file* filp = NULL; mm_segment_t oldfs; int err = 0; oldfs = get_fs(); set_fs(get_ds()); filp = filp_open(path, flags, rights); set_fs(oldfs); if(IS_ERR(filp)) { err = PTR_ERR(filp); return NULL; } return filp;}
Close a file (similar to close):
void file_close(struct file* file) { filp_close(file, NULL);}
Reading data from a file (similar to pread):
int file_read(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_read(file, data, size, &offset); set_fs(oldfs); return ret;}
Writing data to a file (similar to pwrite):
int file_write(struct file* file, unsigned long long offset, unsigned char* data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_write(file, data, size, &offset); set_fs(oldfs); return ret;}
Syncing changes a file (similar to fsync):
int file_sync(struct file* file) { vfs_fsync(file, 0); return 0;}
[Edit] Originally, I proposed using file_fsync, which is gone in newer kernel versions. Thanks to the poor guy suggesting the change, but whose change was rejected. The edit was rejected before I could review it.
在VFS的支持下,用户态进程读写任何类型的文件系统都可以使用read和write着两个系统调用,但是在linux内核中没有这样的系统调用我们如何操作文件呢?我们知道read和write在进入内核态之后,实际执行的是sys_read和sys_write,但是查看内核源代码,发现这些操作文件的函数都没有导出(使用EXPORT_SYMBOL导出),也就是说在内核模块中是不能使用的,那如何是好?
通过查看sys_open的源码我们发现,其主要使用了do_filp_open()函数,该函数在fs/namei.c中,而在改文件中,filp_open函数也是调用了do_filp_open函数,并且接口和sys_open函数极为相似,调用参数也和sys_open一样,并且使用EXPORT_SYMBOL导出了,所以我们猜想该函数可以打开文件,功能和open一样。使用同样的查找方法,我们找出了一组在内核中操作文件的函数,如下:
功能函数原型打开文件struct file *filp_open(const char *filename,int flags, int mode)读取文件ssize_t vfs_read(struct file *file,char __user *buf, size_t count, loff_t *pos)写文件ssize_t vfs_write(struct file *file,const char __user *buf,size_t count, loff_t *pos)关闭文件int filp_close(struct file *filp, fl_owner_t id)
我们注意到在vfs_read和vfs_write函数中,其参数buf指向的用户空间的内存地址,如果我们直接使用内核空间的指针,则会返回-EFALUT。所以我们需要使用
set_fs()和get_fs()宏来改变内核对内存地址检查的处理方式,所以在内核空间对文件的读写流程为:
- mm_segment_tfs = get_fs();
- set_fs(KERNEL_FS);
- //vfs_write();
- vfs_read();
- set_fs(fs);
下面为一个在内核中对文件操作的例子:
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/fs.h>
- #include <linux/uaccess.h>
- static charbuf[] ="你好";
- static charbuf1[10];
- int __inithello_init(void)
- {
- struct file *fp;
- mm_segment_t fs;
- loff_t pos;
- printk("hello enter/n");
- fp =filp_open("/home/niutao/kernel_file",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);
- pos =0;
- vfs_read(fp,buf1, sizeof(buf), &pos);
- printk("read: %s/n",buf1);
- filp_close(fp,NULL);
- set_fs(fs);
- return 0;
- }
- void __exithello_exit(void)
- {
- printk("hello exit/n");
- }
- module_init(hello_init);
- module_exit(hello_exit);
- MODULE_LICENSE("GPL");
- How to read/write files within a Linux kernel module?
- How to Write a Linux usb module
- How to Write Your Own Linux Kernel Module with a Simple Example
- how to write a module dumper & disassembler
- how to write a module dumper & disassembler
- How to read and write JSON files in Java(Gson)
- How to compile a kernel module outside the kernel
- how to write a nginx module for http living stream
- How to write a Linux driver
- How to write a script in Linux
- How to load a camera module in kernel space
- How to unload a kernel module in use
- Ways to write & read HDFS files
- How the Kernel compile .c files for Kernel Module
- How to add or replace files within MSI file.
- How-to: Write an Asterisk Module
- How to write Makefile for kernel modules
- linux kernel read write file 读写文件
- 十年未变!安全,谁之责?(下)
- (x,n)woP.50
- dwz切换显示页数失效
- 电商网站的初期技术选型
- [菜鸟初学nodejs系列] 安装socket.io库
- How to read/write files within a Linux kernel module?
- LightOJ1074 Extended Traffic (SPFA+DFS)
- mysql如何防止插入重复数据?
- Android handler
- Android 教你打造炫酷的ViewPagerIndicator 不仅仅是高仿MIUI
- 结构体比较
- MongoDB Sql语法
- 计算机中数据的表示存储和处理
- 伺服驱动器动力电和控制电