LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 08-05-2004, 11:03 PM   #1
appas
Member
 
Registered: Jul 2004
Posts: 72

Rep: Reputation: 15
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 .
 
Old 08-06-2004, 12:53 AM   #2
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
 
Old 08-06-2004, 02:31 AM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,794

Rep: Reputation: 496Reputation: 496Reputation: 496Reputation: 496Reputation: 496
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.
 
Old 08-06-2004, 03:58 AM   #4
Kumar
Member
 
Registered: Sep 2003
Location: Pune, India
Distribution: Red Hat
Posts: 106

Rep: Reputation: 15
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
 
Old 08-06-2004, 04:22 AM   #5
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
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.
 
Old 08-06-2004, 06:30 AM   #6
appas
Member
 
Registered: Jul 2004
Posts: 72

Original Poster
Rep: Reputation: 15
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'.
 
Old 08-06-2004, 08:40 AM   #7
cppkid
Member
 
Registered: Jul 2004
Location: Pakistan
Distribution: Ubuntu
Posts: 185

Rep: Reputation: 30
i don't know exactly if will work or not, see command "lsof" maybe it can help you.
 
Old 01-19-2012, 11:47 AM   #8
CassandraToday
LQ Newbie
 
Registered: May 2011
Location: Atlanta
Distribution: Mostly RHEL
Posts: 5

Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bad File Descriptor tech_user Linux - Hardware 0 10-14-2005 03:21 PM
File Descriptor Limit rbd Linux - General 3 10-11-2004 01:57 PM
apt-file returns nothing; 'bad file descriptor' overbored Debian 3 10-03-2004 09:13 PM
[c++] How can I get a file descriptor from a c++ i/o stream? chuanyung Programming 8 03-08-2004 11:23 PM
File descriptor lido Linux - Newbie 5 07-17-2003 11:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration