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