Add AF_UNIX sockets implementation - #11681
Conversation
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.
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.
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.
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.
📌 Code Review Assignment🏷️ Tag: componentsReviewers: @Maihuanyi Changed Files (Click to expand)
🏷️ Tag: components_lwpReviewers: @xu18838022837 Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2026-08-03 09:50 CST)
📝 Review Instructions
|
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
|
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.
|
感觉可以在:https://github.com/RT-Thread/rt-thread/tree/master/components/dfs/utest 中添加一些测试用例进来 |
There was a problem hiding this comment.
Pull request overview
Adds pathname-based AF_UNIX IPC to SAL, including streams, datagrams, polling, LWP support, and descriptor passing.
Changes:
- Introduces the AF_UNIX protocol provider and tests.
- Extends SAL/LWP socket dispatch and ancillary-data conversion.
- Adds DFSv2 socket nodes and shared file-reference helpers.
PR metadata / PR 元数据: The title needs a lowercase prefix such as [net][af_unix]. The description still lacks BSP, .config, and CI verification details. / 标题需添加小写前缀;描述仍缺少 BSP、配置及 CI 验证信息。
Reviewed changes
Copilot reviewed 25 out of 25 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
components/net/sal/src/sal_socket.c |
Adds local protocol-family dispatch. |
components/net/sal/socket/net_sockets.c |
Updates close and socketpair flags. |
components/net/sal/include/sal_socket.h |
Defines MSG_CTRUNC. |
components/net/sal/include/sal_low_lvl.h |
Exposes local providers. |
components/net/Kconfig |
Includes AF_UNIX configuration. |
components/net/af_unix/testcases/af_unix_test.c |
Tests IPC and descriptor passing. |
components/net/af_unix/src/af_unix_stream.c |
Implements stream sockets. |
components/net/af_unix/src/af_unix_rights.c |
Implements SCM_RIGHTS. |
components/net/af_unix/src/af_unix_namespace.c |
Implements pathname binding. |
components/net/af_unix/src/af_unix_internal.h |
Defines internal structures. |
components/net/af_unix/src/af_unix_dgram.c |
Implements datagram sockets. |
components/net/af_unix/src/af_unix_core.c |
Provides core operations and polling. |
components/net/af_unix/SConscript |
Adds build integration. |
components/net/af_unix/README.md |
Documents the component. |
components/net/af_unix/Kconfig |
Adds feature limits and options. |
components/net/af_unix/include/af_unix.h |
Declares initialization API. |
components/lwp/lwp_syscall.c |
Extends socket syscall conversion. |
components/lwp/lwp_sys_socket.h |
Defines musl msghdr ABI. |
components/dfs/dfs_v2/src/dfs.c |
Adds transferable file references. |
components/dfs/dfs_v2/src/dfs_file.c |
Adds socket-node creation and flags. |
components/dfs/dfs_v2/include/dfs.h |
Declares reference helpers. |
components/dfs/dfs_v2/include/dfs_file.h |
Declares dfs_file_mknod. |
components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.h |
Defines tmpfs socket type. |
components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c |
Supports tmpfs socket nodes. |
components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c |
Supports devtmpfs socket nodes. |
Suppressed comments (1)
components/net/af_unix/src/af_unix_core.c:1064
- 🔴 [Concurrency/并发]: Stream poll leaves a node on a destructible peer queue / 流式 poll 在可销毁的对端队列上遗留节点
English: The peer's embedded wait queue can be destroyed immediately after a peer close, while the poll table still owns a node registered here and removes it only during teardown. This makes peer-close readiness capable of triggering use-after-free. Retain the peer through poll teardown or use a wait queue whose lifetime is independent of the socket object.
中文:对端关闭后,其内嵌等待队列可能立即被销毁,但 poll 表仍持有此处注册的节点,并且只会在最终清理时移除它。因此,对端关闭事件可能触发释放后使用。应让对端存活到 poll 清理完成,或使用生命周期独立于套接字对象的等待队列。
rt_poll_add(&sock->peer->wait_queue, request);
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if (entry != RT_NULL && entry->sock == sock) | ||
| { | ||
| entry->sock = RT_NULL; | ||
| } | ||
| sock->namespace_entry = RT_NULL; |
| *out_msg_control = kmsg->msg_control; | ||
| /* msg_control is the end of the iovs_buffer */ | ||
| kmsg->msg_control = iovs_buffer; | ||
| kmsg->msg_control = buffer_cursor; | ||
| buffer_cursor += kmsg->msg_controllen; | ||
| *out_msg_name = kmsg->msg_name; | ||
| kmsg->msg_name = kmsg->msg_namelen != 0 ? buffer_cursor : RT_NULL; |
| { | ||
| if (sock->peer != RT_NULL) | ||
| { | ||
| rt_poll_add(&sock->peer->wait_queue, request); |
| rt_list_init(&rights->node); | ||
| rights->count = count; | ||
|
|
||
| result = dfs_file_get_refs(fds, count, rights->files); |
拉取/合并请求描述:(PR description)
[
为什么提交这份PR (why to submit this PR)
RT-Thread SAL currently expects socket protocol providers to be backed by a
network device. This model does not fit local IPC protocols.
Applications such as BusyBox
syslogdandloggerrely on pathname-baseAF_UNIX sockets, typically through
/dev/log. This PR adds native AF_UNIXsupport without introducing a synthetic network device.
你的解决方案是什么 (what is your solution)
S_IFSOCKnodes in tmpfs and devtmpfs.SCM_RIGHTSdescriptor passing.sockaddr_unmsghdrMSG_CTRUNCcomponents/net/af_unixprovider supporting:SOCK_DGRAMandSOCK_STREAMsocketpairSCM_RIGHTSdescriptor passing请提供验证的bsp和config (provide the config and bsp)
]
当前拉取/合并请求的状态 Intent for your PR
必须选择一项 Choose one (Mandatory):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up