內核模塊中filp->open對文件的讀寫【轉】


轉自:http://guiltcool.blog.chinaunix.net/uid-9950859-id-98917.html

平時網絡部分的東西碰的多些,這塊一開始還真不知道怎么寫,因為肯定和在用戶空間下是不同的。google過后,得到以下答案。一般可以用兩種方法:第一種是用系統調用。第二種方法是filp->open()等函數。下面分別來說下這兩種方法。


1 利用系統調用:
sys_open,sys_write,sys_read等。
其實分析過sys_open可以知道,最后調用的也是filp->open。
sys_open ==> do_sys_open ==> filp->open
在linuxsir上的一個帖子,上面一個版主說:sys_open和進程緊密相關,往往不在內核中使用。
而其實sys_open最后也是調用了filp->open。
其實好像Linux2.6.20后面就不推薦使用sys_open,那我們這里就就后者進行詳細的介紹

2 filp->open等函數。
在模塊中,用戶空間的open,read,write,llseek等函數都是不可以使用的。應該使用其在內核中對應的函數。可以使用filp->open配合struct file里的read/write來進行對文件的讀寫操作。


例子1:

#include 
#include 
#include 
#include 
#include 

MODULE_AUTHOR("Kenthy@163.com."); 
MODULE_DESCRIPTION("Kernel study and test."); 


void fileread(const char * filename) 

  struct file *filp; 
  struct inode *inode; 
  mm_segment_t fs; 
  off_t fsize; 
  char *buf; 
  unsigned long magic; 
  printk("<1>start....\n"); 
  filp=filp_open(filename,O_RDONLY,0); 
  inode=filp->f_dentry->d_inode;  
  
  magic=inode->i_sb->s_magic; 
  printk("<1>file system magic:%li \n",magic); 
  printk("<1>super blocksize:%li \n",inode->i_sb->s_blocksize); 
  printk("<1>inode %li \n",inode->i_ino); 
  fsize=inode->i_size; 
  printk("<1>file size:%i \n",(int)fsize); 
  buf=(char *) kmalloc(fsize+1,GFP_ATOMIC); 

  fs=get_fs(); 
  set_fs(KERNEL_DS); 
  filp->f_op->read(filp,buf,fsize,&(filp->f_pos)); 
  set_fs(fs); 

  buf[fsize]='\0'; 
  printk("<1>The File Content is:\n"); 
  printk("<1>%s",buf); 


  filp_close(filp,NULL); 


void filewrite(char* filename, char* data)
{
  struct file *filp; 
mm_segment_t fs;
filp = filp_open(filename, O_RDWR|O_APPEND, 0644);
if(IS_ERR(filp))
    {
      printk("open error...\n"); 
      return;
        }   

  fs=get_fs();
  set_fs(KERNEL_DS);
  filp->f_op->write(filp, data, strlen(data),&filp->f_pos);
  set_fs(fs);
  filp_close(filp,NULL);
}

int init_module() 

  char *filename="/root/test1.c"; 

  printk("<1>Read File from Kernel.\n"); 
  fileread(filename); 
  filewrite(filename, "kernel write test\n");
  return 0; 


void cleanup_module() 

  printk("<1>Good,Bye!\n"); 
}


eg2:

#include
#include
#include

#include

#include
#include
#include /* get_fs(),set_fs(),get_ds() */

#define FILE_DIR "/root/test.txt"

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kenthy@163.com");

char *buff = "module read/write test";
char tmp[100];

static struct file *filp = NULL;

static int __init wr_test_init(void)
{
    mm_segment_t old_fs;
    ssize_t ret;
    
    filp = filp_open(FILE_DIR, O_RDWR | O_CREAT, 0644);
    
    //    if(!filp)

    if(IS_ERR(filp))
        printk("open error...\n");
    
    old_fs = get_fs();
    set_fs(get_ds());
    
    filp->f_op->write(filp, buff, strlen(buff), &filp->f_pos);
    
    filp->f_op->llseek(filp,0,0);
    ret = filp->f_op->read(filp, tmp, strlen(buff), &filp->f_pos);
    
    set_fs(old_fs);
    
    if(ret > 0)
        printk("%s\n",tmp);
    else if(ret == 0)
        printk("read nothing.............\n");
    else 
        {
            printk("read error\n");
            return -1;
        }

    return 0;
}

static void __exit wr_test_exit(void)
{
    if(filp)
        filp_close(filp,NULL);
}

module_init(wr_test_init);
module_exit(wr_test_exit);


3.Makefile

obj-m := os_attack.o

KDIR := /lib/modules/$(uname -r)/build/
PWD := $(shell pwd)

all:module

module:
        $(MAKE) -C $(KDIR) M=$(PWD) modules


clean:
        rm -rf *.ko *.mod.c *.o Module.* modules.* .*.cmd .tmp_versions


注意:
在調用filp->f_op->read和filp->f_op->write等對文件的操作之前,應該先設置FS。
默認情況下,filp->f_op->read或者filp->f_op->write會對傳進來的參數buff進行指針檢查。如果不是在用戶空間會拒絕訪問。因為是在內核模塊中,所以buff肯定不在用戶空間,所以要增大其尋址范圍。

拿filp->f_op->write為例來說明:
filp->f_op->write最終會調用access_ok ==> range_ok.
而range_ok會判斷訪問的地址是否在0 ~ addr_limit之間。如果在,則ok,繼續。如果不在,則禁止訪問。而內核空間傳過來的buff肯定大於addr_limit。所以要set_fs(get_ds())。
這些函數在asm/uaccess.h中定義。以下是這個頭文件中的部分內容:

#define MAKE_MM_SEG(s)    ((mm_segment_t) { (s) })

#define KERNEL_DS    MAKE_MM_SEG(-1UL)
#define USER_DS        MAKE_MM_SEG(PAGE_OFFSET)

#define get_ds()    (KERNEL_DS)
#define get_fs()    (current_thread_info()->addr_limit)
#define set_fs(x)    (current_thread_info()->addr_limit = (x))

#define segment_eq(a, b)    ((a).seg == (b).seg)


可以看到set_fs(get_ds())改變了addr_limit的值。這樣就使得從模塊中傳遞進去的參數也可以正常使用了。

在寫測試模塊的時候,要實現的功能是寫進去什么,然后讀出來放在tmp數組中。但寫完了以后filp->f_ops已經在末尾了,這個時候讀是什么也讀不到的,如果想要讀到數據,則應該改變filp->f-ops的值,這就要用到filp->f_op->llseek函數了。上網查了下,其中的參數需要記下筆記:


系統調用:
off_t sys_lseek(unsigned int fd, off_t offset, unsigned int origin)
offset是偏移量。
若origin是SEEK_SET(0),則將該文件的位移量設置為距文件開始處offset 個字節。
若origin是SEEK_CUR(1),則將該文件的位移量設置為其當前值加offset, offset可為正或負。
若origin是SEEK_END(2),則將該文件的位移量設置為文件長度加offset, offset可為正或負。

ok,that's all.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM