I need to get the name of a file from a given file descriptor, inside a small linux kernel module that I wrote. I tried the solution given at Getting Filename from file descriptor in C, but for some reason, it prints out garbage values (on using readlink on /proc/self/fd/NNN as mentioned in the solution). How can I do it?
I need to get the name of a file from a given file descriptor,
Share
Don’t call
SYS_readlink– use the same method thatprocfsdoes when one of those links is read. Start with the code inproc_pid_readlink()andproc_fd_link()infs/proc/base.c.Broadly, given an
int fdand astruct files_struct *filesfrom the task you’re interested in (which you have taken a reference to), you want to do:If your code is running in process-context (eg. invoked through a syscall) and the file descriptor is from the current process, then you can use
current->filesfor the current task’sstruct files_struct *.