设为首页 友情链接
在线留言 发表文章
加入收藏 广告联系

刺猬首页

| 专案技术 | 网络技术 | 图形图象 | 网络编程 | 网页设计 | 操作系统 | 服务器 | 技术白皮书 | 在线实验室 | 刺猬论坛 |
小说专版  | 数据库 | 设计赏析 | 存储频道 | 网络安全 | 私服架设 |  Solaris | 网站评估 | PC维护技巧 | 下载中心 | 博 客 |
专   题: | Linux | java | cisco | 防病毒 | 刀片 | SOA | iscsi | ASP.NET | SQL | Oracle |
您现在的位置: IT公社 IT community >> Linux专题 >> 内核研究 >> 教程正文 用户登录 新用户注册
专 题 栏 目
最 新 热 门
最 新 推 荐
相 关 文 章
Windows 2000/2003/XP对…
FC6用ntfs-3g解决ntfs挂…
Linux下NFS网络文件系统…
GFS与RHCS安装记录
FC6自动挂载NTFS,完美读…
JFS 文件系统概述及布局…
Linux网络文件系统NFS的…
LFS (版本6.1.1) 安装心…
在Linux环境下如何使用X…
在Linux环境下使用Reise…
  VFS层拦截read/write file_operations函数无效         
VFS层拦截read/write file_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联盟收集整理

频道声明:本频道的文章除部分特别声明禁止转载的专稿外,可以自由转载.但请务必注明出出处和原始作者 文章版权归本频道与文章作者所有.对于被频道转载文章的个人和网站,我们表示深深的谢意。

原始作者:佚名 录入时间:2007-1-2 3:39:44
信息来源:不详 投稿信箱:itqoo@126.com
教程录入:itqoo    责任编辑:itqoo 
  • 上一个教程:

  • 下一个教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)
    - 关于我们 - 合作伙伴 - 友情链接 - 广告刊登 - 投稿热线 - 在线留言版权声明联系方式 -
    IT公社版权所有 粤ICP备05127012号
    Copyrigh@2005-2006 itqoo.com.Inc All Rights Reserved  推荐分辨率 1024*768
    联系站长:E-Mail:itqoo@126.com     MSN:urchincc@hotmail.com    QQ:点击这里给我发消息
    特别感谢:亿太网络提供空间支持