Files
rt-thread/components/net/af_unix/README.md
T
Bernard Xiong 991f0ffca6 Add AF_UNIX sockets implementation (#11681)
* components/dfs: support unix socket nodes

AF_UNIX pathname binding and descriptor passing require DFSv2 socket
nodes and retained open file descriptions across fd tables.

Add socket-node creation for tmpfs and devtmpfs, fd reference helpers,
and socket F_SETFL forwarding.

Impact: DFSv2 socket nodes and descriptor reference handling.
Validation: git diff --cached --check.

* components/net/sal: support local protocol providers

Local IPC protocol families do not have a backing network device.
Store the selected provider in each SAL socket and add a local provider
registry while preserving netdev checks for Internet sockets.

Handle DFSv2 close semantics, socketpair flags, and MSG_CTRUNC for
AF_UNIX integration.

Impact: SAL protocol dispatch for all socket families.
Validation: git diff --cached --check.

* components/lwp: support unix socket messages

Musl AF_UNIX addresses and ancillary data require explicit ABI and
user-memory conversion at the LWP syscall boundary.

Add the musl msghdr layout, bounded address and message copying,
control-message level conversion, and MSG_CTRUNC translation. Correct
receive buffer allocation and copy lengths while handling messages.

Impact: LWP socket syscalls when SAL is enabled.
Validation: git diff --cached --check.

* components/net/af_unix: add local sockets

Add an opt-in AF_UNIX provider for pathname-based local IPC without
a synthetic network device.

Support datagram and stream sockets, blocking and nonblocking I/O,
timeouts, poll, socketpair, and SCM_RIGHTS descriptor passing. Include
bounded Kconfig settings, component documentation, and utest coverage.

Impact: enabled only by RT_USING_AF_UNIX and requires SAL POSIX with
DFSv2.
Validation: git diff --cached --check.

* Fix AF_UNIX CI checks

Apply the repository clang-format rules to the affected source lines.
Suppress the cppcheck false positive for the devtmpfs list iterator.

No functional behavior is changed.

* components/lwp: preserve NULL optional msghdr buffers

When msg_name or msg_control is NULL, keep the kernel pointer NULL
instead of substituting an uninitialized buffer. Reject a NULL control
buffer with a nonzero length as EFAULT.

Impact: LWP sendmsg/recvmsg conversion on MMU targets.
Validation: git diff --cached --check.

* components/net/af_unix: fix poll UAF, namespace leak, and SCM_RIGHTS cycles

Poll only the local wait queue and wake writers from the receiver.
Free namespace entries on detach, and reject AF_UNIX descriptors in
SCM_RIGHTS until cycle collection exists.

Impact: AF_UNIX poll, bind lifetime, and descriptor passing.
Validation: git diff --cached --check.

* components/dfs: add DFSv2 AF_UNIX utest suite

Add opt-in tests under dfs/utest/v2/af_unix for socket nodes, IPC,
poll peer-close, namespace cleanup, and SCM_RIGHTS rejection.

Sources are built only when RT_UTEST_TC_USING_DFS_V2_AF_UNIX and the
selected test groups are enabled.

Impact: DFS utest menu and build; no production AF_UNIX behavior change.
Validation: git diff --cached --check.
2026-08-17 17:15:34 +08:00

2.5 KiB

AF_UNIX local sockets

The AF_UNIX component provides pathname-based local IPC through the existing SAL and POSIX socket APIs. It supports SOCK_DGRAM, SOCK_STREAM, and socketpair() for both socket types.

Configuration

Enable RT_USING_AF_UNIX. The component requires RT_USING_SAL, SAL_USING_POSIX, and RT_USING_DFS_V2.

  • AF_UNIX_DGRAM_MAX_SIZE bounds one datagram.
  • AF_UNIX_DGRAM_QUEUE_LEN bounds queued datagrams per socket.
  • AF_UNIX_STREAM_BUFFER_SIZE bounds each stream receive buffer.
  • AF_UNIX_LISTEN_BACKLOG_MAX caps the stream accept queue.
  • AF_UNIX_RIGHTS_MAX bounds the file descriptors in one SCM_RIGHTS send.
  • RT_AF_UNIX_USING_TESTCASES builds the component utest suite.

Pathname behavior

bind() creates an S_IFSOCK node through DFSv2. The mounted filesystem must support special nodes through create_vnode(); tmpfs and devtmpfs support socket nodes directly. Closing a bound socket leaves its pathname in the filesystem. Applications should call unlink() before rebinding, which matches common Unix daemon behavior.

Removing a pathname prevents new lookups. Existing stream connections and connected datagram endpoints continue to reference their established peers.

Descriptor passing

sendmsg() and recvmsg() support one or more file descriptors in SOL_SOCKET/SCM_RIGHTS control messages. The queued reference remains valid after the sender closes its descriptor. On receive, each reference is installed as a new descriptor in the receiving process and retains the same open file description, including its shared file position. Passing an AF_UNIX socket descriptor is not supported and returns EOPNOTSUPP.

For datagram sockets, the control message is atomic with its datagram. For stream sockets, it is associated with the first byte written by sendmsg() and is delivered when a receive consumes that byte. A receive without a control buffer discards associated descriptors and reports MSG_CTRUNC; MSG_PEEK does not install or consume descriptors. At least one payload byte is required when sending SCM_RIGHTS.

Only SCM_RIGHTS ancillary data is supported. Credentials and other control message types return EOPNOTSUPP.

Supported operations

The component implements bind, connect, listen, accept, send/receive, sendto/recvfrom, sendmsg/recvmsg with descriptor passing, shutdown, socket options, nonblocking I/O, timeouts, poll/select readiness, address queries, and socketpair.

Linux abstract namespace addresses and credential ancillary data are not supported. The component requires DFSv2.