Skip to content

Add AF_UNIX sockets implementation - #11681

Open
BernardXiong wants to merge 5 commits into
RT-Thread:masterfrom
BernardXiong:feature/af-unix-sockets
Open

Add AF_UNIX sockets implementation#11681
BernardXiong wants to merge 5 commits into
RT-Thread:masterfrom
BernardXiong:feature/af-unix-sockets

Conversation

@BernardXiong

Copy link
Copy Markdown
Member

拉取/合并请求描述:(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 syslogd and logger rely on pathname-base
AF_UNIX sockets, typically through /dev/log. This PR adds native AF_UNIX
support without introducing a synthetic network device.

你的解决方案是什么 (what is your solution)

  • Add a local protocol-family registry to SAL while preserving existing netdev-backed socket behavior.
  • Add DFSv2 support for S_IFSOCK nodes in tmpfs and devtmpfs.
  • Add DFS file reference helpers for SCM_RIGHTS descriptor passing.
  • Extend LWP socket syscall conversion for:
    • sockaddr_un
    • musl msghdr
    • ancillary data
    • MSG_CTRUNC
  • Add an opt-in components/net/af_unix provider supporting:
    • SOCK_DGRAM and SOCK_STREAM
    • pathname-based bind and connect
    • socketpair
    • blocking and nonblocking I/O
    • send and receive timeouts
    • poll/select readiness
    • shutdown and peer-close handling
    • SCM_RIGHTS descriptor passing
  • Add Kconfig options, SCons integration, component documentation, and utest cases.

请提供验证的bsp和config (provide the config and bsp)

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

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.
@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/dfs/dfs_v2/filesystems/devfs/devtmpfs.c
  • components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.c
  • components/dfs/dfs_v2/filesystems/tmpfs/dfs_tmpfs.h
  • components/dfs/dfs_v2/include/dfs.h
  • components/dfs/dfs_v2/include/dfs_file.h
  • components/dfs/dfs_v2/src/dfs.c
  • components/dfs/dfs_v2/src/dfs_file.c
  • components/lwp/lwp_sys_socket.h
  • components/lwp/lwp_syscall.c
  • components/net/Kconfig
  • components/net/af_unix/Kconfig
  • components/net/af_unix/README.md
  • components/net/af_unix/SConscript
  • components/net/af_unix/include/af_unix.h
  • components/net/af_unix/src/af_unix_core.c
  • components/net/af_unix/src/af_unix_dgram.c
  • components/net/af_unix/src/af_unix_internal.h
  • components/net/af_unix/src/af_unix_namespace.c
  • components/net/af_unix/src/af_unix_rights.c
  • components/net/af_unix/src/af_unix_stream.c
  • components/net/af_unix/testcases/af_unix_test.c
  • components/net/sal/include/sal_low_lvl.h
  • components/net/sal/include/sal_socket.h
  • components/net/sal/socket/net_sockets.c
  • components/net/sal/src/sal_socket.c

🏷️ Tag: components_lwp

Reviewers: @xu18838022837

Changed Files (Click to expand)
  • components/lwp/lwp_sys_socket.h
  • components/lwp/lwp_syscall.c

📊 Current Review Status (Last Updated: 2026-08-03 09:50 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:feature/af-unix-sockets
  • 设置PR number为 \ Set the PR number to:11681
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 feature/af-unix-sockets 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the feature/af-unix-sockets branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

gd32105r-start

  • CODE: .text -104 B (-0.1%, 69,260 B / 262,144 B, total: 26% used)

hc32f334

  • FLASH: .text -104 B (-0.1%, 99,460 B / 131,072 B, total: 76% used)

infineon-psoc6

  • flash: .text -104 B (-0.1%, 116,104 B / 262,144 B, total: 44% used)

k230

  • SRAM: .eh_frame +144 B, .text -80 B (+0.0%, 1,122,795 B / 268,300,288 B, total: 0% used)

loongson-ls1cdev

  • Code: .text -168 B (-0.0%, 362,600 B)

nuvoton-m487

  • CODE: .text -352 B (-0.1%, 355,460 B / 524,288 B, total: 68% used)

qemu-virt64-aarch64

  • Code: .eh_frame +192 B, .text +2,432 B (+0.2%, 1,059,108 B)

raspberry-pico-rp2040

  • FLASH: .rodata -24 B, .text -112 B (-0.1%, 114,064 B / 2,097,152 B, total: 5% used)

simulator

  • Code: .eh_frame +96 B, .eh_frame_hdr +24 B, .rela.dyn +11,616 B, .rodata +1,632 B, .text +7,163 B (+0.8%, 2,654,634 B)
  • Data: .bss +64 B, .data +13,504 B (+0.9%, 1,596,804 B)

stm32f407-rt-spark

  • CODE: .text -104 B (-0.1%, 85,728 B / 1,048,576 B, total: 8% used)

stm32l475-atk-pandora-llvm

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.
@BernardXiong
BernardXiong requested a review from Rbb666 August 9, 2026 16:23
@Rbb666
Rbb666 requested a balanced review from Copilot August 16, 2026 11:57
@Rbb666

Rbb666 commented Aug 16, 2026

Copy link
Copy Markdown
Member

感觉可以在:https://github.com/RT-Thread/rt-thread/tree/master/components/dfs/utest 中添加一些测试用例进来

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +279 to +283
if (entry != RT_NULL && entry->sock == sock)
{
entry->sock = RT_NULL;
}
sock->namespace_entry = RT_NULL;
Comment on lines 5800 to +5804
*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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants