From: Glynn Clements <glynn@gclements.plus.com>
To: kaushal@rocsys.com
Cc: linux prg <linux-c-programming@vger.kernel.org>
Subject: Re: how to the filename
Date: Thu, 14 Apr 2005 22:10:15 +0100 [thread overview]
Message-ID: <16990.56375.187052.48678@gargle.gargle.HOWL> (raw)
In-Reply-To: <1113472539.5952.62.camel@localhost.localdomain>
kaushal wrote:
> How can I get the filename/pathname given the open file descriptor?Does
> fstat provide this feature internally?Can somebody give the code snippet
> for this.
If a descriptor corresponds to a file, the kernel records the name
under which the file was opened; the name is available via
the link /proc/<pid>/fd/<descriptor#>. If the file is renamed, the
link will be updated; if the file is deleted, " (deleted)" will be
added to the link.
Note that this is Linux-specific; it won't work on other Unices. Also,
it won't work if the /proc filesystem doesn't exist.
The historical behaviour is that any system calls which accept a
filename as an argument (e.g. open(), stat() etc) immediately resolve
the filename to a device/inode pair, then forget all about the
filename.
The only portable solution is to retrieve the device and inode numbers
via fstat(), locate the mount point for the device, then scan the
entire filesystem beneath the mount point until you find a file with
the correct inode number. If you don't find such a file, it has been
deleted since it was opened (programs which create temporary files
often delete them as soon as they are opened so they don't get left
around if the process terminates abnormally).
[A deleted file will exist so long as it remains open, it just won't
have a filename, so you can't use open(), stat() etc on it.]
--
Glynn Clements <glynn@gclements.plus.com>
next prev parent reply other threads:[~2005-04-14 21:10 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-04-14 9:56 how to the filename kaushal
2005-04-14 10:37 ` Steve Graegert
2005-04-14 11:39 ` kaushal
2005-04-14 12:07 ` Steve Graegert
2005-04-15 1:45 ` Ron Michael Khu
2005-04-15 4:39 ` kaushal
2005-04-15 6:19 ` Steve Graegert
2005-04-15 6:42 ` kaushal
2005-04-15 6:59 ` Steve Graegert
2005-04-15 6:36 ` Steve Graegert
2005-04-15 6:43 ` kaushal
2005-04-15 7:04 ` Steve Graegert
2005-04-15 9:54 ` kaushal
2005-04-14 21:10 ` Glynn Clements [this message]
2005-04-14 21:31 ` Ozgur Sefik Altunyurt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=16990.56375.187052.48678@gargle.gargle.HOWL \
--to=glynn@gclements.plus.com \
--cc=kaushal@rocsys.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).