user1058382
user1058382

Reputation: 23

sys_readlink fails EFAULT - alternative

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?

Upvotes: 2

Views: 727

Answers (2)

ephemient
ephemient

Reputation: 204926

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.

Upvotes: 4

Joshua
Joshua

Reputation: 43327

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

Upvotes: 2

Related Questions