改變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);
module_init(patch_init);