Skip to content

Sim posix fixes#19456

Open
LingaoM wants to merge 3 commits into
apache:masterfrom
LingaoM:sim-posix-fixes
Open

Sim posix fixes#19456
LingaoM wants to merge 3 commits into
apache:masterfrom
LingaoM:sim-posix-fixes

Conversation

@LingaoM

@LingaoM LingaoM commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR improves NuttX sim compatibility for standard POSIX-facing APIs used by simulated applications.

Changes included:

  • Add AF_LOCAL / AF_UNIX support to sim usrsock, so applications can use the normal NuttX socket API to communicate with host Unix-domain sockets.
  • Translate hostfs POSIX byte-range lock operations. fcntl(F_GETLK/F_SETLK) reaches file systems as FIOC_* ioctl commands after VFS handling; hostfs now converts those lock
    requests back to host fcntl() for host-backed files.
  • Add built-in http and https service entries to libc netdb so getservbyname() / getaddrinfo() can resolve common service names without requiring numeric ports.

The changes keep applications using standard NuttX/POSIX interfaces rather than exposing host-only APIs directly to application code.

Impact

  • Affects sim builds using usrsock, hostfs, or libc netdb.
  • Applications can use socket(AF_LOCAL, ...) through the normal NuttX socket layer on sim.
  • Applications using hostfs can use non-blocking POSIX file locks through fcntl(F_GETLK/F_SETLK).
  • F_SETLKW is intentionally left unsupported for hostfs and returns -ENOSYS, because forwarding a blocking host fcntl() could block the whole sim OS.
  • http and https become available in the built-in service database when CONFIG_LIBC_NETDB=y.
  • No hardware targets are affected by the sim host-side changes.

Testing

Host machine:

  • Ubuntu 22.04 x86_64

Board/config:

  • sim:nsh

Build test:

make clean
make -j16

Runtime tests performed with temporary hello examples, then restored before commit.

AF_LOCAL / usrsock test:

- Built sim:nsh with usrsock enabled.
- Ran a temporary hello app that connected to host Unix-domain sockets using normal NuttX socket(AF_LOCAL, ...).
- Verified both SOCK_STREAM and SOCK_SEQPACKET.
- The app exchanged data with a host socket server and printed:

AF_LOCAL usrsock test passed

hostfs file lock test:

- Built sim:nsh with:

CONFIG_FS_HOSTFS=y
CONFIG_SIM_HOSTFS=y
CONFIG_EXAMPLES_HELLO=y

- Ran a temporary hello app that mounted /tmp through hostfs, opened a host-backed file, then verified:

fcntl(fd, F_SETLK, ...)
fcntl(fd, F_GETLK, ...)
fcntl(fd, F_SETLK, F_UNLCK)

- The app printed:

hostfs fcntl lock test passed

netdb service-name test:

- Built sim:nsh with:

CONFIG_LIBC_NETDB=y
CONFIG_EXAMPLES_HELLO=y

- Ran a temporary hello app that verified:

getservbyname("http", "tcp")  -> port 80
getservbyname("https", "tcp") -> port 443

- The app printed:

getservbyname http/https test passed

The sim host usrsock backend only accepted INET/NETLINK domains and translated socket addresses through plain struct sockaddr. That prevents simulated applications from using POSIX AF_LOCAL sockets through the standard socket API when CONFIG_NET_USRSOCK is used.

Add AF_LOCAL address conversion for struct sockaddr_un, allow PF_LOCAL sockets through usrsock, handle NuttX socket type flags, and poll host descriptors from the sim usrsock work item so nonblocking connect/read/write readiness is reported back to NuttX. Use sockaddr_storage for native address translation so larger address structures are not truncated.

Testing:

  - Host: Ubuntu 22.04 x86_64.

  - Board/config: sim:nsh with CONFIG_NET_USRSOCK=y and CONFIG_EXAMPLES_HELLO=y.

  - make clean && make -j16.

  - Ran a temporary hello example that connected to host AF_UNIX SOCK_STREAM and SOCK_SEQPACKET sockets through NuttX socket(), connect(), write(), and read(); both received pong and printed AF_LOCAL usrsock test passed.

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
@github-actions github-actions Bot added Arch: simulator Issues related to the SIMulator Area: File System File System issues Size: L The size of the change in this PR is large labels Jul 16, 2026
@github-actions

github-actions Bot commented Jul 16, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

qemu-armv8a

@LingaoM LingaoM requested a review from masayuki2009 as a code owner July 16, 2026 08:27
@github-actions github-actions Bot added Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: xtensa Issues related to the Xtensa architecture labels Jul 16, 2026
LingaoM added 2 commits July 16, 2026 16:47
fcntl(F_GETLK/F_SETLK/F_SETLKW) is handled by VFS and reaches file systems as private FIOC_* ioctl commands. hostfs previously forwarded those private ioctl command numbers to the host ioctl backend, which is not the POSIX file-locking interface and cannot be interpreted by the host OS.

Route F_GETLK and non-blocking F_SETLK through a host_fcntl() backend operation instead. The POSIX sim backend translates the NuttX fcntl commands, open flags and struct flock ABI to host values before calling host fcntl(). Backends that cannot forward host file locks return -ENOTTY from host_fcntl(), which lets VFS fall back to NuttX internal file-lock handling.

Do not forward F_SETLKW to the host. A blocking host fcntl() can stop the whole simulated OS, so hostfs returns -ENOTTY and lets VFS handle the blocking lock in NuttX.

Testing:

  - Host: Ubuntu 22.04 x86_64.

  - Board/config: sim:nsh with CONFIG_FS_HOSTFS=y, CONFIG_SIM_HOSTFS=y and CONFIG_EXAMPLES_HELLO=y.

  - make clean && make -j16.

  - Ran a temporary hello example that mounted /tmp through hostfs, opened a host-backed file, then successfully executed fcntl(F_SETLK), fcntl(F_GETLK), and fcntl(F_SETLK) with F_UNLCK. The app printed "hostfs fcntl lock test passed".

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
getaddrinfo() and getservbyname() use the built-in service table when resolving service names. The table only contained ntp, so common service names such as http and https could not be resolved without a numeric port.

Add http and https entries for both TCP and UDP to match the existing service table style.

Testing:

  - Host: Ubuntu 22.04 x86_64.

  - Board/config: sim:nsh with CONFIG_LIBC_NETDB=y and CONFIG_EXAMPLES_HELLO=y.

  - make clean && make -j16.

  - Ran a temporary hello example that called getservbyname("http", "tcp") and getservbyname("https", "tcp"). The app verified ports 80 and 443 and printed "getservbyname http/https test passed".

Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Arch: arm64 Issues related to ARM64 (64-bit) architecture Arch: risc-v Issues related to the RISC-V (32-bit or 64-bit) architecture Arch: simulator Issues related to the SIMulator Arch: xtensa Issues related to the Xtensa architecture Area: File System File System issues Size: L The size of the change in this PR is large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant