Reputation: 15109
In a linux kernel module is there a way to get a file name/path from an unsigned int fd
?
I'm aware of this answer: How can I get a filename from a file descriptor inside a kernel module? but if I understand the code right, I need a struct files_struct
too.
EDIT:
Please stop voting as duplicated as it isn't. I'm asking for a way to get file's name/path in plain C from a kernel module, not using system tools. Said in another way: running readlink on /procself/fd/ is not a good answer.
EDIT 2:
Kernel's syscall read ssize_t read(int fd, void *buf, size_t count);
takes 3 arguments, one of them being a fd. It's obvious that somehow read
is able to read from a single file (instead of all files inside an inode). The question is how.
Upvotes: 5
Views: 10482
Reputation: 239041
The code in the answer to the question that you reference is what you need to do. And yes, a struct files_struct
from a task is needed, because a file descriptor is only meaningful in the context of a files_struct
(usually, there is one of these per process). File descriptors aren't globally unique, just an index within an individual open file table.
If your code is running in process-context (eg. invoked through a syscall) then you can use current->files
for the current task's files_struct
. This is what read()
does.
Upvotes: 3