0

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.

user31986
  • 1,558
  • 1
  • 14
  • 29
  • There is a seperate file descriptor table per process. – kaylum Sep 02 '15 at 01:16
  • Yes, that's what the question is about. When you create a socket in kernel, any process-context can use it. How do you differentiate? – user31986 Sep 02 '15 at 01:54
  • The per process file descriptor is used to look up the reference to a system wide file table. From the `open` man page: "A call to open() creates a new open file description, an entry in the system-wide table of open files... A file descriptor is a reference to one of these entries". So two file descriptors in seperate processes (or even within the same process) can refer to the same file entry (regardless of whether the descriptor value is the same or different). Is that what you meant? – kaylum Sep 02 '15 at 02:02
  • No, my question refers to the kernel mode creation of FDs. These kernel mode FDs can be used by any process. – user31986 Sep 02 '15 at 02:12
  • Not sure I get you. The FD returned from `open` is allocated and managed by the kernel. It's just that it allocates it per-process which is then used as an indirection to get the real file entry. – kaylum Sep 02 '15 at 02:14
  • I agree, this is true about when we call open() from userspace. But what about the descriptors that are created in kernel. For example: the NFS uses kernel RPC that creates a socket inside kernel. Any user process doing file operations on the NFS uses these RPCs in its kernel-context. My question is how can it do that? – user31986 Sep 02 '15 at 02:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88524/discussion-between-user31986-and-alan-au). – user31986 Sep 02 '15 at 02:21

1 Answers1

1

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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153