casaroli opened a new pull request, #20778:
URL: https://github.com/apache/nuttx/pull/20778

   ## Summary
   
   `<stdio.h>` declares `popen()` and `pclose()`, and `<unistd.h>` declares 
`dpopen()` and `dpclose()`, but libc did not have them. Only 
`apps/system/popen` had them, so a program that links with libc only could not 
use them.
   
   This PR adds the four functions to libc:
   
   - `dpopen()` creates a pipe (or a socket pair for `O_RDWR`) and starts `sh 
-c command` with `posix_spawn()`. With `CONFIG_LIBC_ENVPATH`, `PATH` gives the 
location of `sh`; otherwise `_PATH_BSHELL` is used.
   - `popen()` wraps the descriptor in a stream with `fopencookie()`. The 
descriptor is close-on-exec, so a later command does not get the streams of 
earlier `popen()` calls.
   - `pclose()` closes the stream and returns the termination status from 
`waitpid()`.
   
   They need `CONFIG_PIPES`, `CONFIG_LIBC_EXECFUNCS` and 
`CONFIG_SCHED_WAITPID`. When `CONFIG_SYSTEM_POPEN` is set, libc does not build 
them, so the apps copy stays in use and the two copies do not clash.
   
   The second commit describes this on the `apps/system/popen` page.
   
   ## Impact
   
   New functions for configurations with pipes, `posix_spawn()` and 
`waitpid()`, and without `CONFIG_SYSTEM_POPEN`. No change for a configuration 
that selects `CONFIG_SYSTEM_POPEN`. As for every `waitpid()` call, the status 
of a command that already exited is available only with 
`CONFIG_SCHED_CHILD_STATUS`.
   
   ## Testing
   
   A test program on `qemu-armv8a:knsh` (QEMU 11 with HVF on an Apple M4) with 
`CONFIG_PIPES` and `CONFIG_SCHED_CHILD_STATUS`, 14 checks, all pass: `popen()` 
with `"r"` reads the output of `echo`, `popen()` with `"w"` writes to `cat > 
file` and the data arrives, `pclose()` returns 0 for `echo` and a failure 
status for `ls /no/such/dir`, `dpopen()`/`dpclose()` work, and a bad mode gives 
`NULL`.
   
   `ostest` on `qemu-armv8a:knsh` and `qemu-armv8a:nsh` ends with `ostest_main: 
Exiting with status 0`. `qemu-armv8a:fastboot` (`CONFIG_SYSTEM_POPEN=y`) links 
the apps copy. `tools/checkpatch.sh -c -u -m -g` passes.
   


-- 
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]

Reply via email to