Search code examples
linuxlinux-kernelkernelkernel-modulevfs

How are the file-descriptors allocated when an open()/socket() is called from inside kernel?


Some kernel libs (eg. rpc) open a socket inside kernel, and are associated with file-descriptors (FDs). These could be used by any process hitting the code. Given that for any process, the FDs are stored in the task_struct in its File Descriptor Table, where are these kernel-FDs stored and how are they accounted for? These are not particularly created by process (say, these FDs were created at module_init). How are the values to such FDs allocated, the process FDs start with 0, 1, 2 and could overlap these?

This is also extended to any other kind of FDs: file, pipe, etc.


Solution

  • Kernel uses file/socket objects directly, it doesn't need file descriptors which user space uses.

    For example, when socket is created inside kernel (e.g, for RPC), it just associates inet address(with port) with callback functions, which process message sending/receiving on this address. So, user space programs only need to know this address for communicate.