2011-11-21 36 views
2

我有filedescriptor和喜欢得到真正的路径。目前,我打电话sys_readlink /proc/self/fd/<fd>有时工作,但通常我得到一个错误-14(-EFAULT)。sys_readlink失败EFAULT - 替代

这里是一些代码:

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

是否有其他(可能更好)的方式来获得从内核中的真实路径?

回答

4

从任务结构中的filep中获取它,例如像

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); 

为简洁起见,错误处理消失了。

+0

顺便说一下,这将返回类似于从打开时间文件时进程根目录路径的内容。 – Joshua

2

-EFAULT意味着“buf”指针有问题。你有没有先分配内存?

编辑:它看起来像你在内核模式编程。事实证明,“真正的路径在内核模式下是没有意义的。