[comp.unix.internals] Is it possible to get filename from file descriptor ?

kumar@gtenmc.UUCP (S.Kumar) (09/15/90)

    A Unix question
    Is there a way to get the filename in UNIX given the file descriptor.
    (in a given process)

    I think the crash program provides a list of inodes for the filenames
    which the process has opened. 

    Is there any call in unix which would do the job  (and if not why not)?

    I don't mind an external suid program which takes the pid and fd
    and gets the filename. 
    Is it possible at all to do that ?

    (This would be very useful to me to find out the filename which
    tmpfile() has created which I could then pass to other programs)

Fri Sep 14 15:53:41 PDT 1990
    Thanks in advance ,
    S. Kumar

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (09/15/90)

In article <870@gtenmc.UUCP> kumar@gtenmc.UUCP (S.Kumar) writes:
>     A Unix question
>     Is there a way to get the filename in UNIX given the file descriptor.
>     (in a given process)

Not reliably. A file descriptor may be associated with several different
filenames, or none at all. The filenames may be hidden inside
directories that you can't read. They may change at any time.

>     I think the crash program provides a list of inodes for the filenames
>     which the process has opened. 

Filenames can change; how do you know it's the same file as when the
process was running? A root process can search the filesystem and
(unreliably, unless the disks are unmounted) find a file with a given
inode, but that's slow and really suitable only for administrative
duties.

---Dan

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/KT) (09/18/90)

As quoted from <870@gtenmc.UUCP> by kumar@gtenmc.UUCP (S.Kumar):
+---------------
|     (This would be very useful to me to find out the filename which
|     tmpfile() has created which I could then pass to other programs)
+---------------

Someone else answered your question; I'll address this.

You're doing this wrong....

(1) tmpfile() creates a named file, then unlinks it.  By the time you get
    control back from tmpfile(), the file it creates doesn't *have* a name.

(2) In any case, what you want is bass-ackwards.  You have to do major work to
    get the filename from a file descriptor (that is, if it *has* one; pipes,
    un-"bind"-ed (AF_UNIX) sockets, etc. need not apply).  It's much easier to
    do it in the proper order:

		char tf_name[L_tmpnam];
		FILE *tf_fp;

		tmpnam(tf_name);
		tf_fp = fopen(tf_name, "w");

    Much easier, no?

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR/KT on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY