转自: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.

内核模块中filp->open对文件的读写【转】的更多相关文章

  1. linux内核驱动中对文件的读写 【转】

    本文转载自:http://blog.chinaunix.net/uid-13059007-id-5766941.html 有时候需要在Linux kernel--大多是在需要调试的驱动程序--中读写文 ...

  2. kernel中文件的读写操作可以使用vfs_read()和vfs_write

    需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...

  3. (六)kernel中文件的读写操作可以使用vfs_read()和vfs_write

    需要在Linux kernel--大多是在需要调试的驱动程序--中读写文件数据.在kernel中操作文件没有标准库可用,需要利用kernel的一些函数,这些函数主要有: filp_open() fil ...

  4. C++中关于文件的读写

    在C++的学习过程中,我们时常要用到对文件的操作,下面我们讲一下文件的读写. 首先,读.也就是把已有的文件读到控制台上,那么如何操作呢?首先要将文件操作的输入输出流包含进去. <fstream& ...

  5. Android中对手机文件进行读写

    参考张泽华视频 (一)读写手机内存卡中的文件 对手机中的文件进行读写操作,或者新增一个文件时,可直接使用openFileOutput  /  openFileInput 得到文件的输出.输入流. Fi ...

  6. c语言中的文件格式化读写函数fscanf和fprintf函数

    很多时候我们需要写入数据到文件中时都觉得很困扰,因为格式乱七八槽的,可读性太差了,于是我们就想有没有什么函数可以格式化的从文件中输入和输出呢,还真有.下面我将讲解一下fscanf和fprintf的强大 ...

  7. K:java中properties文件的读写

    Properties类与.properties文件:   Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集的类,不过Properties有特殊 ...

  8. C++中文件的读写

    C++中文件的读写 在C++中如何实现文件的读写? 一.ASCII 输出 为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstrea ...

  9. MATLAB中文件的读写和数据的导入导出

    http://blog.163.com/tawney_daylily/blog/static/13614643620111117853933/ 在编写一个程序时,经常需要从外部读入数据,或者将程序运行 ...

随机推荐

  1. 初识Haskell 五:自定义数据类型和类型类

    对Discrete Mathematics Using a Computer的第一章Introduction to Haskell进行总结.环境Windows 自定义数据类型 data type de ...

  2. MyBaits集合的嵌套 Select 查询

    ---恢复内容开始--- <resultMap id="slist" type="com.qujiali.model.AdShareInfo"> & ...

  3. asp.net 文件分片上传

    最近在研究文件上传,里面的门道还是挺多的,网上大多数文章比较杂乱,代码都是片段,对于新手小白来说难度较高,所以在此详细写一下今天看到的一个demo,关于文件分片上传的. <!DOCTYPE ht ...

  4. wince可用的7-zip

    7-zip下载   7-zip    

  5. WebSocket群聊与单聊

    一 . WebSocket实现群聊 py文件代码 # py文件 from flask import Flask, render_template, request from geventwebsock ...

  6. iptables对端口流量统计

    本篇文章主要介绍了Linux下如何对端口流量进行统计,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧   在不修改源代码的情况下对程序暴露端口流量进行监控统计,可以利用Li ...

  7. window.open() & iframe & tab

    window.open() & iframe & tab window.open() open pages in the same window / tab https://stack ...

  8. pc端手機端自適應佈局方案

    https://blog.csdn.net/chose_DoIt/article/details/80424341 https://blog.csdn.net/cxz792116/article/de ...

  9. NABCD--团队项目1

    N: 1.学校中有很多学生是外省的,然后不知道附近有什么地方周末可以去玩. 2.学校中学生有很多用不到的东西但是联系不到合适的买家. A: 我们可以做一个软件,通过学号注册登陆软件,这样可以防止有人去 ...

  10. 关于Tomcat

    详情转自:http://blog.csdn.net/jiuqiyuliang/article/details/48094373和http://blog.csdn.net/u012934325/arti ...