ppisa commented on issue #18566: URL: https://github.com/apache/nuttx/issues/18566#issuecomment-5273311801
> This is because of the recent NuttX fork/vfork rework (commit [70c2ef5](https://github.com/apache/nuttx/commit/70c2ef5911cc281b4a6e8a6f7412cf68c9e3a99a)). It no longer provides fork() on systems without process address isolation enabled — the old fork() was not a real POSIX fork, but something similar to vfork. The semantics of vfork() also differ somewhat from the previous behavior. I might need to modify the implementation on the Microwindows side. Yes, I expected something like that and I would suggest to switch to switch to `posix_spawn` enabled by `LIBC_EXECFUNCS` in NuttX. `posix_spawn` is portable solution which enables to start processes/applications in environments without full MMU support and even in MMU case allows to minimize overhead caused by full fork before exec. Next code works for me, in longer term it could be alternative to introduce into mainline Microwindows nanoxterm code, because it would make it portable to POSIX subsets typical for RTOSes ```diff diff --git a/examples/nanoxterm/Kconfig b/examples/nanoxterm/Kconfig index 3d13d7a9f..3e548c672 100644 --- a/examples/nanoxterm/Kconfig +++ b/examples/nanoxterm/Kconfig @@ -1,7 +1,7 @@ config EXAMPLES_NANOXTERM tristate "Nano-X terminal emulator example" default n - depends on MICROWINDOWS_NANOX && PSEUDOTERM && ARCH_HAVE_FORK + depends on MICROWINDOWS_NANOX && PSEUDOTERM ---help--- Enable the Nano-X terminal emulator example application. It is a port of the nxterm demo from Microwindows. It starts the diff --git a/examples/nanoxterm/nxterm.c b/examples/nanoxterm/nxterm.c index 22a11114d..134c824a3 100755 --- a/examples/nanoxterm/nxterm.c +++ b/examples/nanoxterm/nxterm.c @@ -63,6 +63,7 @@ #include <sys/types.h> #include <sys/stat.h> #include <termios.h> +#include <spawn.h> /* NuttX has no filesystem shell binary, so the pty child runs an NSH * instance directly instead of exec'ing /bin/sh. @@ -1707,7 +1708,12 @@ int main(int argc, char **argv) int term_init(void) { int tfd; + int ptfd; pid_t child; + int status; + posix_spawnattr_t attr; + posix_spawn_file_actions_t file_actions; + char *const argv[] = {CONFIG_SYSTEM_NSH_PROGNAME, NULL}; tfd = posix_openpt(O_RDWR | O_NOCTTY | O_NONBLOCK); if (tfd < 0) { @@ -1719,27 +1725,33 @@ int term_init(void) signal(SIGCHLD, SIG_DFL); /* required before grantpt()*/ grantpt(tfd); unlockpt(tfd); - - if ((child = fork()) == -1) { - GrError("No processes\n"); + + if ((ptfd = open(ptyname, O_RDWR)) < 0) { + GrError("Can't open %s\n", ptyname); return -1; } - if (!child) { - close(STDIN_FILENO); - close(STDOUT_FILENO); - close(tfd); - - if ((tfd = open(ptyname, O_RDWR)) < 0) { - GrError("Can't open %s\n", ptyname); - exit(1); - } - close(STDERR_FILENO); - dup2(tfd, STDIN_FILENO); - dup2(tfd, STDOUT_FILENO); - dup2(tfd, STDERR_FILENO); - nsh_consolemain(0, NULL); - exit(1); + posix_spawnattr_init(&attr); + posix_spawnattr_setstacksize(&attr, CONFIG_SYSTEM_NSH_STACKSIZE); + + posix_spawn_file_actions_init(&file_actions); + posix_spawn_file_actions_addclose(&file_actions, STDIN_FILENO); + posix_spawn_file_actions_addclose(&file_actions, STDOUT_FILENO); + posix_spawn_file_actions_addclose(&file_actions, STDERR_FILENO); + posix_spawn_file_actions_adddup2(&file_actions, ptfd, STDIN_FILENO); + posix_spawn_file_actions_adddup2(&file_actions, ptfd, STDOUT_FILENO); + posix_spawn_file_actions_adddup2(&file_actions, ptfd, STDERR_FILENO); + posix_spawn_file_actions_addclose(&file_actions, ptfd); + + status = posix_spawn(&child, CONFIG_SYSTEM_NSH_PROGNAME, &file_actions, + &attr, argv, environ); + + posix_spawn_file_actions_destroy(&file_actions); + close(ptfd); + + if (status != 0) { + GrError("Error: posix_spawn returned %d\n", status); + return -1; } return tfd; } ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
