Get file name/path from a file descriptor from a Linux kernel module?

时光怂恿深爱的人放手 提交于 2019-12-10 17:05:24

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/17504859/get-file-name-path-from-a-file-descriptor-from-a-linux-kernel-module

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!