Skip to content

SCM_RIGHTS

SCM_RIGHTS is the UNIX domain socket ancillary data type used to pass file descriptors between processes.

Concept

The sender places one or more file descriptors into the control message area of sendmsg(). The receiver extracts them from the control message area returned by recvmsg().

Key Macros

Macro Purpose
CMSG_SPACE() Allocates enough buffer space for control data
CMSG_LEN() Computes the actual control message length
CMSG_FIRSTHDR() Gets the first control message header
CMSG_DATA() Gets the payload area of a control message

Control Message Fields

cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(int));

Common Pitfalls

Warning

The received descriptor number is local to the receiving process. It does not have to match the sender's descriptor number.

Warning

Descriptor ownership must be explicit. Close descriptors that are no longer needed.