int file descriptor to filename?

int file descriptor to filename?

Post by Clem Tayl » Thu, 14 Jul 1994 07:26:43



    Without being root (being able to look at kernel space), is it
possible to get a filename from an int file descriptor?  I am trying
to find a file descriptor leak with a wrapper around open that would
list the types and names of all the open files.  File type (reg,
socket, fifo, special...) is returned by fstat, but fstat does not
provide any info about the name of the file.  I could maintain a table
of the names of all the open files when the are opened, but I would
prefer to do something like:

if ( open ( ... ) == -1 )
    if ( errno == EMFILE )
        for ( i = 0; i < getdtablesize ( ); i++ )
            if ( !fstat ( i, &fstatus ) && S_ISREG ( fstatus.st_mode ) )
                Get and print filename...

   I could use a program like lsof, but I would really like to know
how to do this... Any ideas?

                 Thanks,
                 Clem Taylor

 
 
 

int file descriptor to filename?

Post by Paul Stephen Bori » Sat, 16 Jul 1994 00:01:26



>    Without being root (being able to look at kernel space), is it
>possible to get a filename from an int file descriptor?  I am trying
>to find a file descriptor leak with a wrapper around open that would
>list the types and names of all the open files.  File type (reg,
>socket, fifo, special...) is returned by fstat, but fstat does not
>provide any info about the name of the file.  I could maintain a table
>of the names of all the open files when the are opened, but I would
>prefer to do something like:

The only place under Unix where the filename is kept is the directory
which contains the file itself. A directory actually implements the mapping
between i-number and file-name. A ( not easy ) way of finding out the name
of a file given a open file descriptor could be :

- Use fstat(2) the obtain st_ino (file i-number) and st_dev (device which
  contains the file system on which the file is allocated)
- find the mount point of the file system built on device st_dev. Read
  mtab(4) , stat(2) all devices found looking for the one matching st_dev
- Walk this file system ( using for ex. ftw(3) ) looking for directories
  and foreach directory opendir() and readdir() looking for the st_ino
  you need. The file name is inside there.

Any follow-up to this is appreciated.

-Paul
--
----------------------------------------------------------------------------




 
 
 

1. *FILE and int as file descriptors.

Hi!  I'm confused by the different representation methods of file/stream
descriptors.  For example:

(1) the man page for 'read':
SYNOPSIS
     int read(fd, buf, nbyte)
     int fd;
     char *buf;
     int nbyte;

(2) the man page for 'fopen':
SYNOPSIS
     #include <stdio.h>

     FILE *fopen(filename, type)
     char *filename, *type;

So, why is a file descriptor sometimes 'int', sometimes '*FILE'?
Is 'int' used in original/old systems when integers are not distinguished
from pointers?  

Those things relate to my current problems:

- I opened a serial port /dev/tty03 with fopen() which returns *FILE.  I
  can use fgets() to read in data as if it's a disk file.  Now I need to
  do non-blocking read.  But the fcntl() which sets the non-blocking
  option requires "int" as file descriptor, not *FILE.
  Does that mean I'm stuck here?

- I do error checking as I read data from /dev/tty03.  If an error occurs in
  the middle of transmission, I should immediatly
  send back NAK char to inform the sender to retransmit.  This
  sounds fine, but how do I flush the rest of the data that are still
  pending/buffered on /dev/tty03?  

--

Vincent Q. Yin

2. Using SGI machine to print from aix machine

3. convert FILE* to a file descriptor (int)

4. va_lists on linuxPPC

5. recovering filenames from file descriptors

6. launching x0rfbserver with xinetd

7. Filename for File descriptor

8. Darwin Streaming Server on Alpha

9. Is it possible to get filename from file descriptor ?

10. getting filename from file descriptor

11. How to get the filename of a file descriptor?

12. translate file descriptor <-> filename

13. finding filename from file descriptor