1.filp_open()在kernel中可以打开文件,其原形如下:
Struct file* filp_open(const char* filename, int open_mode, int mode);
该函数返回strcut file*结构指针,供后继函数操作使用,该返回值用IS_ERR()来检验其有效性。
2. 读写文件(vfs_read/vfs_write)
kernel中文件的读写操作可以使用vfs_read()和vfs_write,在使用这两个函数前需要说明一下get_fs()和 set_fs()这两个函数。
vfs_read() vfs_write()两函数的原形如下:
ssize_t vfs_read(struct file* filp, char __user* buffer, size_t len, loff_t* pos);
ssize_t vfs_write(struct file* filp, const char __user* buffer, size_t len, loff_t* pos);
注意这两个函数的第二个参数buffer,前面都有__user修饰符,这就要求这两个buffer指针都应该指向用空的内存,如果对该参数传递kernel空间的指针,这两个函数都会返回失败-EFAULT。但在Kernel中,我们一般不容易生成用户空间的指针,或者不方便独立使用用户空间内存。要使这两个读写函数使用kernel空间的buffer指针也能正确工作,需要使用set_fs()函数或宏(set_fs()可能是宏定义),如果为函数,其原形如下:
void set_fs(mm_segment_t fs);
该函数的作用是改变kernel对内存地址检查的处理方式,其实该函数的参数fs只有两个取值:USER_DS,KERNEL_DS,分别代表用户空间和内核空间,默认情况下,kernel取值为USER_DS,即对用户空间地址检查并做变换。那么要在这种对内存地址做检查变换的函数中使用内核空间地址,就需要使用set_fs(KERNEL_DS)进行设置。get_fs()一般也可能是宏定义,它的作用是取得当前的设置,这两个函数的一般用法为:
mm_segment_t old_fs;
old_fs = get_fs();
set_fs(KERNEL_DS);
...... //与内存有关的操作
set_fs(old_fs);
还有一些其它的内核函数也有用__user修饰的参数,在kernel中需要用kernel空间的内存代替时,都可以使用类似办法。
使用vfs_read()和vfs_write()最后需要注意的一点是最后的参数loff_t * pos,pos所指向的值要初始化,表明从文件的什么地方开始读写。
代码:写入hello world到output.txt
#include "linux/init.h"
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/fs.h"
#include "asm/uaccess.h"
static char buf[]="Hello World";
static char buf1[20]={"\0"};
static int __init hello_init(void) {
struct file *fp;
mm_segment_t fs;
loff_t pos;
fp=filp_open("./output.txt",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;
}
static void __exit hello_exit(void) {
printk(KERN_ALERT "Goodbye!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("hello");
代码2:创建线程循环写入1~9
#include "linux/init.h"
#include "linux/kernel.h"
#include "linux/module.h"
#include "linux/fs.h"
#include "asm/uaccess.h"
#include "linux/sched.h"
#include "linux/kthread.h"
#include "linux/delay.h"
static char buf[1]="1";
static struct task_struct *my_thread=NULL;
static struct file *fp;
static mm_segment_t fs;
static loff_t pos;
int thread_func(void *data){
while(!kthread_should_stop()){
fs=get_fs();
set_fs(KERNEL_DS);
vfs_write(fp,buf,sizeof(buf),&pos);
if(buf[0]<'9'){
buf[0]++;
}else{
buf[0]='1';
}
set_fs(fs);
ssleep(3);
}
filp_close(fp,NULL);
return 0;
}
static int __init hello_init(void){
fp=filp_open("output.txt",O_RDWR|O_CREAT,0644);
if(IS_ERR(fp)){
printk("create file error\n");
return -1;
}
my_thread=kthread_run(thread_func,NULL,"my_thread");
return 0;
}
static void __exit hello_exit(void) {
if(my_thread){
printk("stop mythread\n");
kthread_stop(my_thread);
}
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("hello");
Makefile:
obj-m:=*.o
KERNELBUILD :=/lib/modules/$(shell uname -r)/build
default:
make -C $(KERNELBUILD) M=$(shell pwd) modules
clean:
rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers .tmp_versions