When you share a file descriptor over a socket, the kernel mediates. You need to prepare data using the cmsg(3)
macros, send it using sendmsg(2)
and receive it using recvmsg(2)
. The kernel is involved in the latter two operations, and it handles the conversion from a file descriptor to whatever data it needs to transmit the file descriptor, and making the file descriptor available in the receiving process.
How can same fd in different processes point to the same file? provides useful background. The sending process sends a file descriptor which means something in relation to its (private) file descriptor table; the kernel knows what that maps to in the system-wide open file table, and creates a new entry as necessary in the receiving process’ file descriptor table.