Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much information is actually stored in a file descriptor?

This may sound like an odd question, but when I go and open a file:

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

What exactly am I getting back? I can see the man page says:
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 that it? Is it just an int or is there data attached to it behind the scenes? The reason I'm asking is I found some code (Linux/C) where we're opening 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 on the kernel end, the file operations for this module (which supplies the fd) map reads to the n_read() function:

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

Then the file descriptor is used in the n_read(), but it's being accessed to get 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 figure what's happening here is either:

A) a file descriptor returned from open() contains more data than just a descriptive integer value
Or
B)The mapping between a call to "read" in the user space isn't as simple as I'm making it out to be and there's some code missing in this equation.

Any input that might help direct me?

like image 254
Mike Avatar asked Oct 02 '12 18:10

Mike


People also ask

Is file descriptor a limit?

Thus, we know that this system's hard limit for the number of open file descriptors is 4096.

What is stored in a file descriptor?

A file descriptor is a number that uniquely identifies an open file in a computer's operating system. It describes a data resource, and how that resource may be accessed. When a program asks to open a file — or another data resource, like a network socket — the kernel: Grants access.

What is the value of file descriptor?

Range of possible values of file descriptors is from 0 to 1023 for Linux system (32-bit or 64-bit system). You cannot create a file descriptor with value more then 1023. In case of file descriptor of value 1024, it will return an error of EBADF (bad file descriptor, error no-9).

Why is there a file descriptor limit?

A Too many open files error happens when a process needs to open more files than the operating system allows. This number is controlled by the maximum number of file descriptors the process has. If you experience such an issue, you can increase the operating system file descriptor limit.


1 Answers

File descriptor is just an int. The kernel uses it as an index to a table containing all the related information, including file position, file ops (kernel functions that provide the read(), write(), mmap() etc. syscalls), and so on.

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

When you use read(), write(), mmap(), etc. with a valid file descriptor, the kernel simply looks up the correct in-kernel function to call based on the file ops in the file descriptor table it has (and which the file descriptor indexes). It really is that simple.

like image 84
Nominal Animal Avatar answered Oct 17 '22 10:10

Nominal Animal