Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-05-2004, 11:03 PM
|
#1
|
Member
Registered: Jul 2004
Posts: 72
Rep:
|
Get the absolute path of file using file descriptor.
I am writing a kernel module which intercepts the file close operation by trapping the sys_close function.
One of my requirement is to get the absolute path of the file being closed.
I have only the file descriptor which is to be closed.
Can anyone help me to get absolute file path from the filedescriptor at kernel .
|
|
|
08-06-2004, 12:53 AM
|
#2
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
Threre is a command "fstat" that returns the information of a file and takes file discriptor as an aurgument, look if that helps you, also there is a command "fcntl" that also manuplated file descriptors. But I don't thing that you can get the absolute path from file descriptor, why don't you store it when you open that file???
One more thing, when a process is running, its information is stored in /proc folder.
/proc contains that folder of same name as of PID of the process, and that folder contains the information about the process. So for inside your program you can call
readlink ("/proc/self/fd", buffer, lenth); to access the detail of the file descriptors opend by your process. I don't know exactly that is there any information about the path of these file descriptors or not, but to get the path of the process executable we have "/proc/self/exe", so may be you'll find something of your interest.
Wish you all the best.
|
|
|
08-06-2004, 02:31 AM
|
#3
|
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794
|
Beware too that there is not always a single relation between a file descriptor and a file path.
When a file in use by one process is removed by another process, the file descriptor has no more an existing file path associated with, while the first process can still use the file.
A file-descriptor can also have more than on file path associated to it, when hard links exists.
For the method to use, you may look at lsof code, that is very good finding information about open files.
|
|
|
08-06-2004, 03:58 AM
|
#4
|
Member
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106
Rep:
|
how about using getdents() call and getting the file name from it. But I guess the best way is to retrive it from the /proc directory ie /proc/<pid>/fd
|
|
|
08-06-2004, 04:22 AM
|
#5
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
Quote:
Originally posted by Kumar
how about using getdents() call and getting the file name from it. But I guess the best way is to retrive it from the /proc directory ie /proc/<pid>/fd
|
You may confuse /proc/<pid>/fd that how to go get the pid, thre are commands but simply if you use
/proc/self/fd it will automatically go to the folder for calling process.
|
|
|
08-06-2004, 06:30 AM
|
#6
|
Member
Registered: Jul 2004
Posts: 72
Original Poster
Rep:
|
Thank you for your replies, since i am working in kernel space i cannot use the 'sys_readlink' symbol which is not exported
in my kernel version.
Can you suggest me an alternative.
Is there any way to get the path from struct 'inode'.
|
|
|
08-06-2004, 08:40 AM
|
#7
|
Member
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185
Rep:
|
i don't know exactly if will work or not, see command "lsof" maybe it can help you.
|
|
|
01-19-2012, 11:47 AM
|
#8
|
LQ Newbie
Registered: May 2011
Location: Atlanta
Distribution: Mostly RHEL
Posts: 5
Rep: 
|
For Solaris 10
/proc/<pid>/path has the fd-to-path mapping for a process.
Yeah, I know this is an ancient thread, but somebody else could get here someday via google, as I did, and might be happy to find this.
|
|
|
All times are GMT -5. The time now is 10:30 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|