How to figure out the file descriptor inside a driver?

Joel Sherrill joel.sherrill at
Mon Dec 15 16:18:06 UTC 2003

Carl-Johan Hansson wrote:

>>> I'm writing a couple of new drivers and in one of them I want to map 
>>> the file descriptor that I receive when I open the driver with 
>>> another parameter when I call ioctl(). Is there some way to figure 
>>> out the file descriptor when I'm inside the driver routine?
>>> Hope you understand the question!
>> Do you mean file description in the integer sense (e.g. read()) or
>> in the FILE * sense (e.g. fread())?
> I mean in the integer number you receive when calling open() and not 
> fopen(). I want to find that number in the arguments when calling (e.g 
> read()) if possible.

OK.  Each device driver entry point is called with an argument which
ends up pointing to an rtems_libio* which includes an iop pointer.

This "iop" pointer can be converted to an integer pointer using this
code from cpukit/libcsupport/src/open.c

   return iop - rtems_libio_iops;

Honestly, there should be a macro in 
which is the opposite of rtems_libio_iop() around line 48.  Something
like this:

#define rtems_libio_iop_to_descriptor(_iop) \
   ((!(_iop)) ? -1 : (iop - rtems_libio_iops))

If this works for you, file a PR in GNATS and it can be added.

Joel Sherrill, Ph.D.             Director of Research & Development
joel at                 On-Line Applications Research
Ask me about RTEMS: a free RTOS  Huntsville AL 35805
Support Available                (256) 722-9985

More information about the users mailing list