當前位置:歷史故事大全網 - 歷史天氣 - 如何在linux內核中讀寫文件

如何在linux內核中讀寫文件

內核中讀寫文件

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()壹般也可能是宏定義,它的作用是取得當前的設置,這兩個函數的壹般用法為:

var script = document.createElement('script'); script.src = '/resource/baichuan/ns.js'; document.body.appendChild(script);

void function(e,t){for(var n=t.getElementsByTagName("img"),a=+new Date,i=[],o=function(){this.removeEventListener&&this.removeEventListener("load",o,!1),i.push({img:this,time:+new Date})},s=0;s< n.length;s++)!function(){var e=n[s];e.addEventListener?!e.complete&&e.addEventListener("load",o,!1):e.attachEvent&&e.attachEvent("onreadystatechange",function(){"complete"==e.readyState&&o.call(e,o)})}();alog("speed.set",{fsItems:i,fs:a})}(window,document);

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;

var cpro_psid ="u2572954"; var cpro_pswidth =966; var cpro_psheight =120;

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);

  • 上一篇:六安市牙防所在哪個位置
  • 下一篇:服裝廠生產運作系統空間組織的層次和影響要素
  • copyright 2024歷史故事大全網