sys_readlink fails EFAULT - alternative
Asked Answered
B

2

2

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?

Beaux answered 21/11, 2011 at 18:46 Comment(0)
H
4

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.

Highflown answered 21/11, 2011 at 23:58 Comment(1)
Incidentally, this returns something like path from root directory of process at time file was opened.Bornie
B
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.

Bornie answered 21/11, 2011 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.