sys_readlink fails EFAULT - alternative

半城伤御伤魂 提交于 2019-12-24 01:27:35

问题


I have the filedescriptor and like to get the real path. Currently i call sys_readlink /proc/self/fd/<fd> which works sometimes, but often i get an error -14 (-EFAULT).

Here some Code:

fs = get_fs();
set_fs(KERNEL_DS);
err = sys_readlink(path, buf, size-1);
set_fs(fs);

Is there an alternative (probably better) way to get the realpath from kernel?


回答1:


Get it from the filep in the task struct, e.g. something like

struct task_struct *task;
struct files_struct *files;
struct file *file;
char buf[buflen], *realpath;

task = current /* or some other task */;
get_task_struct(task);
files = get_files_struct(task);
put_task_struct(task);
spin_lock(&files->file_lock);
file = fcheck_files(files, fdno);
realpath = d_path(file->f_path, buf, buflen);
spin_unlock(&files->file_lock);
put_files_struct(files);

Error handling elided for brevity.




回答2:


-EFAULT means something's wrong with the "buf" pointer. Did you allocate memory first?

EDIT: It looks like you are programming in kernel mode. It turns out "the" real path is meaningless in kernel mode.



来源:https://stackoverflow.com/questions/8216871/sys-readlink-fails-efault-alternative

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!