Day55 - SCM_RIGHTS / File Descriptor Passing¶
Summary¶
Today I learned Linux file descriptor passing using:
- AF_UNIX socket
- sendmsg()
- recvmsg()
- ancillary data
- SCM_RIGHTS
I first created a minimal UNIX socketpair demo and verified communication between parent and child processes.
Then I implemented:
- fdpass_send_fd()
- fdpass_recv_fd()
using:
- struct msghdr
- struct iovec
- struct cmsghdr
I also learned:
- CMSG_SPACE()
- CMSG_LEN()
- CMSG_DATA()
and how ancillary data is attached to a socket message.
Important Concepts¶
SCM_RIGHTS does not send the fd integer itself.
Instead, Linux creates a new fd entry in the receiver process that references the same kernel struct file object.
This means:
- file offset is shared
- file status flags are shared
- fd numbers are process-local
I verified this using:
- shared lseek offset
- O_RDONLY capability limitation
eventfd + epoll¶
I implemented an eventfd demo where:
- parent created eventfd
- parent passed eventfd to child
- child monitored received fd using epoll
This confirmed that:
- received fd behaves like a normal fd
- epoll works correctly on passed fd
/dev/mypoll Integration¶
Finally, I integrated /dev/mypoll.
Architecture:
The child successfully received:
- timer events
- manual trigger events
through epoll on the passed fd.
I also added socket disconnect handling using EPOLLHUP.
Key Takeaways¶
- SCM_RIGHTS behaves like cross-process dup()
- UNIX socket can be used as fd broker
- epoll works normally on received fd
- SOCK_SEQPACKET is very suitable for structured IPC
- This architecture is commonly used in Linux daemon systems