我正在尝试挂钩unlinkat.my挂钩功能。
但我只得到文件名而不是绝对路径。所以我想要比较字符串的绝对路径。当我尝试rm -r而不是我只得到文件名如果我得到绝对路径然后它的工作。所以请告诉我我是怎么做的得到绝对的道路 我的代码是
long mw_sys_unlink(int dfd, const char *filename ,int flag)
{
long ret;
if( strstr(filename,"/tmp/a/"))
{
printk(KERN_INFO "file %s has not been deleted by kernel module\n", filename);
return -1;
}
else
{
ret = orig_sys_unlink(dfd ,filename,flag);
printk(KERN_INFO "file %s has been deleted", filename);
return ret;
}
}
答案 0 :(得分:3)
尝试以下方法:
char *tmp = (char*)__get_free_page(GFP_TEMPORARY);
file *file = fget(dfd);
if (!file) {
goto out
}
char *path = d_path(&file->f_path, tmp, PAGE_SIZE);
if (IS_ERR(path)) {
printk("error: %d\n", (int)path);
goto out;
}
printk("path: %s\n", path);
out:
free_page((unsigned long)tmp);