On Wed, Jul 14, 2004 at 01:50:40AM -0700, Tyler Durdan wrote:
> Is there a way to get the filename of an mmap()ed file
> in the current process? For example, in Linux I can
> open /proc/self/maps and get the filenames right
> there. However, if I try to open /proc/getpid()/map on
> FBSD, the only mapping info is "vnode" or "default" on
> FBSD 4.10-BETA.          
>                                                       
>                          
> Is there any way to convert this info into the
> filenames of mapped files?       
>                                                       
>                          
> Or perhaps an alternate method to accomplish this
> goal?                         

That's a generally hard problem -- given some sort of open file, find
the file name it was opened as.  Most unixoid OSes don't record
filenames used on open(2) to go with file descriptors or areas of
mmapped data because it's a waste of space.  And there's no reliable
way to work backwards from the open file to a file name.

The problem is the filename comes out of the directory structure: it's
not kept with the file contents.  And any particular file my have
links from any number of directories under as many different names
(none of which is /the/ name of the file: hard links are all equal
precedence).  It can even have *no* filename at all -- if you (for
example) open the file in one process, and then delete it from
another, the first process will be able to read and write to the file
completely normally, although closing and then re-opening the file
will be impossible.  Similarly for renaming an already open file.

You might think of extracting the inode number (easily done using
fstat(2)) and searching the whole partition for file paths matching
that inode number.  That is, if you like horribly inefficient code
vulnerable to all sorts of race conditions and other nastyness.

The best answer is to redesign your application so that if it needs to
know filenames, it saves the filenames it originally used to open
those files with.

        Cheers,

        Matthew

-- 
Dr Matthew J Seaman MA, D.Phil.                       26 The Paddocks
                                                      Savill Way
PGP: http://www.infracaninophile.co.uk/pgpkey         Marlow
Tel: +44 1628 476614                                  Bucks., SL7 1TH UK

Attachment: pgp3LFFO4OPJp.pgp
Description: PGP signature

Reply via email to