This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new d0e047bf46c arch/sim: Update posix/sim_hostuart with POSIX
pseudo-terminal calls.
d0e047bf46c is described below
commit d0e047bf46c926ca3a7c64f0453e6664396f3db9
Author: Tomasz 'CeDeROM' CEDRO <[email protected]>
AuthorDate: Fri Nov 20 23:14:51 2026 +0100
arch/sim: Update posix/sim_hostuart with POSIX pseudo-terminal calls.
* Replace Linux specific (and Linux built only) pseudo-terminal handling
with portable POSIX code in the posix/sim_hostuart.c:host_uart_openpty().
* The Linux ioctls are replaced with the standard POSIX calls
posix_openpt(),
grantpt(), unlockpt() and ptsname(). These work on Linux, FreeBSD and
macOS,
so the CONFIG_HOST_LINUX / -ENOSYS guard is gone.
* macOS gains pty support as a side effect.
* O_NONBLOCK and FD_CLOEXEC are now set with fcntl(), because FreeBSD’s
posix_openpt() refuses O_NONBLOCK.
* _GNU_SOURCE is defined on Linux only. glibc hides these calls without
a feature macro, and the sim host build doesn’t define one.
* The startup message now prints the real slave path from ptsname()
instead of building /dev/pts/%u by hand.
Assisted-by: Claude:claude-opus-5.5
Signed-off-by: Tomasz 'CeDeROM' CEDRO <[email protected]>
---
arch/sim/src/sim/posix/sim_hostuart.c | 49 ++++++++++++++++++++++-------------
1 file changed, 31 insertions(+), 18 deletions(-)
diff --git a/arch/sim/src/sim/posix/sim_hostuart.c
b/arch/sim/src/sim/posix/sim_hostuart.c
index a98410393ad..7521b089e1d 100644
--- a/arch/sim/src/sim/posix/sim_hostuart.c
+++ b/arch/sim/src/sim/posix/sim_hostuart.c
@@ -24,6 +24,15 @@
* Included Files
****************************************************************************/
+/* posix_openpt(), grantpt(), unlockpt() and ptsname() are XSI interfaces
+ * that glibc only declares with an explicit feature test macro. BSD and
+ * macOS expose them by default.
+ */
+
+#if defined(__linux__) && !defined(_GNU_SOURCE)
+# define _GNU_SOURCE
+#endif
+
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdbool.h>
@@ -147,32 +156,40 @@ int host_uart_open(const char *pathname)
int host_uart_openpty(const char *name)
{
-#ifdef CONFIG_HOST_LINUX
- unsigned int ptyno;
- int lock = 0;
- int oflags;
+ const char *slave;
+ int flags;
int ret;
int fd;
- oflags = O_RDWR | O_NOCTTY | O_NONBLOCK;
-#ifdef O_CLOEXEC
- oflags |= O_CLOEXEC;
-#endif
- fd = open("/dev/ptmx", oflags);
+ /* Use the portable POSIX pseudo-terminal interface rather than the
+ * Linux-only TIOCGPTN/TIOCSPTLCK ioctls so that this builds and works on
+ * every POSIX host (Linux, FreeBSD and other BSDs, macOS).
+ */
+
+ fd = posix_openpt(O_RDWR | O_NOCTTY);
if (fd < 0)
{
return -errno;
}
- ret = ioctl(fd, TIOCGPTN, &ptyno);
- if (ret < 0)
+ if (grantpt(fd) < 0 || unlockpt(fd) < 0)
{
ret = -errno;
goto errout;
}
- ret = ioctl(fd, TIOCSPTLCK, &lock);
- if (ret < 0)
+ slave = ptsname(fd);
+ if (slave == NULL)
+ {
+ ret = -errno;
+ goto errout;
+ }
+
+ /* posix_openpt() does not accept O_NONBLOCK/O_CLOEXEC on all hosts */
+
+ flags = fcntl(fd, F_GETFL);
+ if (flags < 0 || fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0 ||
+ fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
{
ret = -errno;
goto errout;
@@ -180,17 +197,13 @@ int host_uart_openpty(const char *name)
setrawmode(fd);
- printf("%s connected to pseudotty: /dev/pts/%u\n", name, ptyno);
+ printf("%s connected to pseudotty: %s\n", name, slave);
return fd;
errout:
close(fd);
return ret;
-#else
- (void)name;
- return -ENOSYS;
-#endif
}
/****************************************************************************