How much information is actually stored in the file descriptor?

This may sound like a weird question, but when I go and open the file:

int fd; fd = open("/dev/somedevice", O_RDWR); 

What exactly am I coming back from? I see the manual page saying:
The open() function shall return a file descriptor for the named file that is the lowest file descriptor not currently open for that process

But is it? Is it just an int or is there any data attached to it backstage? The reason I'm asking is to find the code (Linux / C) where we open the file from user space:

 //User space code: int fdC; if ((fdC = open(DEVICE, O_RDWR)) < 0) { printf("Error opening device %s (%s)\n", DEVICE, strerror(errno)); goto error_exit; } while (!fQuit) { if ((nRet = read(fdC, &rx_message, 1)) > 0) { 

then at the end of the kernel the file operation for this module (which supplies the fd card) is read in the n_read() function:

 struct file_operations can_fops = { owner: THIS_MODULE, lseek: NULL, read: n_read, 

Then the file descriptor is used in n_read() , but it is accessed to get the data:

 int n_read(struct file *file, char *buffer, size_t count, loff_t *loff) { data_t * dev; dev = (data_t*)file->private_data; 

So ... I suppose what happens here:

A) the file descriptor returned from open() contains more data than just a descriptive integer value
Or
B) The mapping between the β€œread” call in user space is not as simple as I do, and there is no code in this equation.

Any input that can help me?

+7
source share
3 answers

The file descriptor is just an int . The kernel uses it as an index for a table containing all the related information, including the location of the file, file operations (kernel functions that provide read() , write() , mmap() system calls, etc.) etc.

When you open() file or device, the kernel creates a new file descriptor entry for your process and fills in internal data, including ops files.

When you use read() , write() , mmap() , etc. with a valid file descriptor, the kernel simply looks for the correct built-in function to call based on the ops files in the file descriptor table. has (and which file descriptor indices). It's really that simple.

+10
source

In addition to the existing good answer, @Nominal Aminal is an integer, but it points to a structure entry in the kernel called a file descriptor table. This at least applies to Linux. Of the several fields that are part of this structure, the following is interesting:

 FILE * pointer; // descriptor to / from reference counts etc. 

You may be interested in the following api, which gives one of FILE * or a descriptor, returns another

How to get FILE * from fd and vice versa

+3
source

I think this is just an int . From Wikipedia :

Typically, a file descriptor is an index for writing to a resident data structure containing the details of all open files. On POSIX, this data structure is called a file descriptor table, and each process has its own file descriptor table.

+1
source

All Articles