Sim posix fixes#19456
Open
LingaoM wants to merge 5 commits into
Open
Conversation
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>
LingaoM
requested review from
Ouss4,
jerpelea,
raiden00pl,
xiaoxiang781216 and
yamt
as code owners
July 16, 2026 07:44
|
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>
hostfs_mkpath() appends a relative path to the configured host root with strlcat(). The third argument to strlcat() is the total destination buffer size, not the remaining free space. Passing pathlen - strlen(path) makes the effective limit shrink after a long host root has already been copied. With a sufficiently long root, a valid relative path can be dropped or truncated, so operations under the mount point may resolve to the host root instead of the requested child path. Pass the full destination buffer size and let strlcat() account for the current string length internally. The companion examples/hostfs_longpath app validates this regression by mounting hostfs with a long host root, writing a probe file below the mount point, and reading it back. The old size argument drops the relative component in that scenario; this fix preserves it. Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
hostfs keeps its own HOSTFS_MAX_PATH wrapper for internal buffers, but it should not hard-code a path length separate from the system path configuration. Define HOSTFS_MAX_PATH from PATH_MAX instead. PATH_MAX is backed by CONFIG_PATH_MAX, whose default remains 256, so the default hostfs behavior does not change while configurations that choose a larger path limit are honored consistently. Signed-off-by: Lingao Meng <menglingao@xiaomi.com>
| @@ -103,9 +103,9 @@ static int usrsock_sockif_setup(FAR struct socket *psock) | |||
| int ret; | |||
|
|
|||
| if (psock->s_domain != PF_INET && psock->s_domain != PF_INET6 && | |||
| psock->s_domain != PF_NETLINK) | |||
| psock->s_domain != PF_NETLINK && psock->s_domain != PF_LOCAL) | |||
Contributor
There was a problem hiding this comment.
should we prefer nuttx local socket if the current defconfig enable it
| if (ret < 0) | ||
| { | ||
| if (errno == EISCONN) | ||
| { | ||
| host_usrsock_clear_fd(sockfd, &g_active_write_fds); |
| const socklen_t naddrlen, | ||
| struct nuttx_sockaddr *addr, | ||
| nuttx_socklen_t *addrlen) | ||
| static int sockaddr_to_nuttx(const struct sockaddr *naddr, |
Contributor
There was a problem hiding this comment.
change sockaddr to sockaddr_storage and remove the cast in caller
Contributor
|
Can you include the test application code you used? Also, just a note, the PR title should follow the same format as your commit subject since we use the titles to generate and sort release notes. You should also included the 'Assisted-by' field in the commit message when using AI tooling. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR improves NuttX sim compatibility for standard POSIX-facing APIs used by simulated applications.
Changes included:
AF_LOCAL/AF_UNIXsupport to simusrsock, so applications can use the normal NuttX socket API to communicate with host Unix-domain sockets.fcntl(F_GETLK/F_SETLK)reaches file systems asFIOC_*ioctl commands after VFS handling; hostfs now converts those lockrequests back to host
fcntl()for host-backed files.httpandhttpsservice entries to libc netdb sogetservbyname()/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
usrsock,hostfs, or libcnetdb.socket(AF_LOCAL, ...)through the normal NuttX socket layer on sim.fcntl(F_GETLK/F_SETLK).F_SETLKWis intentionally left unsupported for hostfs and returns-ENOSYS, because forwarding a blocking hostfcntl()could block the whole sim OS.httpandhttpsbecome available in the built-in service database whenCONFIG_LIBC_NETDB=y.Testing
Host machine:
Board/config:
sim:nshBuild test: