sys_readlink 失败 EFAULT - 替代方案

发布于 2024-12-17 10:09:22 字数 269 浏览 2 评论 0原文

我有文件描述符并且想获取真实路径。目前我调用 sys_readlink /proc/self/fd/ 有时有效,但我经常收到错误 -14 (-EFAULT)。

这里有一些代码:

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

是否有替代(可能更好)的方法从内核获取真实路径?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-12-24 10:09:22

从任务结构中的 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);

简洁起见,省略了错误处理之类的内容。

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.

南汐寒笙箫 2024-12-24 10:09:22

-EFAULT 表示“buf”指针有问题。你先分配内存了吗?

编辑:看起来您正在内核模式下编程。事实证明,“真实”路径在内核模式下毫无意义。

-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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文