当前位置:操作系统 > Unix/Linux >>

VFS层拦截read/writefile_operations函数无效

改变read,write,readdir,open,release等函数指针,发现readdir可以正常获得目录名,open,release只能截获目录的操作, read,write完全截获不了任何操作。 请问各位大虾有遇到类似问题吗。现在感觉上是对目录的操作都能截获,但是对文件操作的就完全没有。

  readdir(工作正常) 和 read 代码如下

  int my_readdir(struct file *fp, void *buf, filldir_t filldir)

  {

   char *path = (char*)kmalloc(MAX_DIR_LENTH);

   unsigned int offset = MAX_DIR_LENTH-1;

   struct dentry* cwd = fp->f_dentry;

   int rs;

   path[MAX_DIR_LENTH-1] = '\0';

   // path add one slash in the end

   path[--offset] = '/';

   while( cwd->d_parent != cwd ) {

   offset -= cwd->d_name.len;

   strncpy(path + offset, cwd->d_name.name, cwd->d_name.len);

   path[--offset] = '/';

   cwd = cwd->d_parent;

   }

   if (offset == MAX_DIR_LENTH-1) path[--offset] = '/';

   printk("<1> Read Dir %s\n", path+offset);

   kfree(path);

   if (orig_readdir == NULL) {

   printk("<1> orig read dir function is NULL\n");

   return -1;

   }

   rs = orig_readdir(fp, buf, filldir);

   return rs;

  }

  ssize_t my_read (struct file *fp, char *buf, size_t len, loff_t *off)

  {

   int rs;

   printk("<1> enter my read \n"); //这也没有输出

   char *path = (char*)kmalloc(MAX_DIR_LENTH);

   unsigned int offset = MAX_DIR_LENTH-1;

   struct dentry* cwd = fp->f_dentry;

   path[MAX_DIR_LENTH-1] = '\0';

   while( cwd->d_parent != cwd ) {

   offset -= cwd->d_name.len;

   strncpy(path + offset, cwd->d_name.name, cwd->d_name.len);

   path[--offset] = '/';

   cwd = cwd->d_parent;

   }

   printk("<1> Read file %s\n", path+offset);

   kfree(path);

   if (orig_read == NULL) {

   printk("<1> orig read function is NULL\n");

   return -1;

   }

   rs = orig_read(fp, buf, len, off);

   return rs;

  }

  替换file_operations,打开一个文件(我打开的是/),得到该fs的file_operations指针,替换。

  下面是代码。

  int patch_vfs(const char* p)

  {

   struct file* filep;

   filep = filp_open(p, O_RDONLY, 0);

   if (IS_ERR(filep)){

   printk("<1> can not open file\n");

   return -1;

   }

   orig_read = filep->f_op->read;

   orig_write = filep->f_op->write;

   orig_readdir = filep->f_op->readdir;

   orig_ioctl = filep->f_op->ioctl;

   orig_open = filep->f_op->open;

   orig_lock = filep->f_op->lock;

   orig_mmap = filep->f_op->mmap;

   orig_release = filep->f_op->release;

  filep->f_op->read = my_read;

   filep->f_op->write = my_write;

   filep->f_op->readdir = my_readdir;

   filep->f_op->ioctl = my_ioctl;

   filep->f_op->open = my_open;

   filep->f_op->lock = my_lock;

   filep->f_op->mmap = my_mmap;

   filep->f_op->release = my_release;

  

   filp_close(filep, 0);

   return 0;

  }

  static int patch_init(void)

  {

   if (patch_vfs(root_fs) != 0) return -1;

   printk("<1> VFS patched\n");

   return 0;

  }

  module_init(patch_init);

  

  

上一个:Linux网桥源码框架分析初
下一个:Linux内核管理基础知识概述

更多Unix/Linux疑问解答:
路由原理介绍
子网掩码快速算法
改变网络接口的速度和协商方式的工具miitool和ethtool
Loopback口的作用汇总
OSPF的童话
增强的ACL修改功能
三层交换机和路由器的比较
用三层交换机组建校园网
4到7层交换识别内容
SPARC中如何安装Linux系统(2)
SPARC中如何安装Linux系统(1)
用Swatch做Linux日志分析
实战多种Linux操作系统共存
浅析Linux系统帐户的管理和审计
Linux2.6对新型CPU的支持(2)
电脑通通透
玩转网络
IE/注册表
DOS/Win9x
Windows Xp
Windows 2000
Windows 2003
Windows Vista
Windows 2008
Windows7
Unix/Linux
苹果机Mac OS
windows8
安卓/Android
Windows10
如果你遇到操作系统难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,