You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "casaroli (via GitHub)" <gi...@apache.org> on 2026/10/18 14:23:49 UTC

[PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #20778:
URL: https://github.com/apache/nuttx/pull/20778#discussion_r4307538392


##########
libs/libc/unistd/CMakeLists.txt:
##########
@@ -129,4 +129,14 @@ if(CONFIG_PIPES)
   list(APPEND SRCS lib_pipe.c)
 endif()
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   let's remove apps/system/popen?



##########
libs/libc/unistd/Make.defs:
##########
@@ -78,6 +78,15 @@ ifeq ($(CONFIG_PIPES),y)
 CSRCS += lib_pipe.c
 endif
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   if so, why not remove apps/system/popen?



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #20778:
URL: https://github.com/apache/nuttx/pull/20778#issuecomment-6251132603

   <!-- membrowse-pr-comment -->
   ## <img src="https://membrowse.com/membrowse-logo.svg" height="24" align="top"> MemBrowse Memory Report
   
   *No memory changes detected for:*
   - [arduino-mega2560](https://membrowse.com/public/apache/nuttx/target/arduino-mega2560/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [esp32-devkitc](https://membrowse.com/public/apache/nuttx/target/esp32-devkitc/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [hifive1-revb](https://membrowse.com/public/apache/nuttx/target/hifive1-revb/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [mirtoo](https://membrowse.com/public/apache/nuttx/target/mirtoo/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [qemu-armv8a](https://membrowse.com/public/apache/nuttx/target/qemu-armv8a/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [qemu-intel64](https://membrowse.com/public/apache/nuttx/target/qemu-intel64/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [rx65n-rsk2mb](https://membrowse.com/public/apache/nuttx/target/rx65n-rsk2mb/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [s698pm-dkit](https://membrowse.com/public/apache/nuttx/target/s698pm-dkit/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   - [stm32-nucleo-f103rb](https://membrowse.com/public/apache/nuttx/target/stm32-nucleo-f103rb/compare?head=788a8eb0e504d65ce6637572c3ce5d241bbc4699&base=6bdd71565ad4d0095b73f09ab81c4f570b2aeec5)
   


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on code in PR #20778:
URL: https://github.com/apache/nuttx/pull/20778#discussion_r4307837288


##########
libs/libc/unistd/CMakeLists.txt:
##########
@@ -129,4 +129,14 @@ if(CONFIG_PIPES)
   list(APPEND SRCS lib_pipe.c)
 endif()
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   nice plan, let's go!



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #20778:
URL: https://github.com/apache/nuttx/pull/20778#discussion_r4308681126


##########
libs/libc/unistd/CMakeLists.txt:
##########
@@ -129,4 +129,14 @@ if(CONFIG_PIPES)
   list(APPEND SRCS lib_pipe.c)
 endif()
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   Correction to the order: fastboot, the popen test and the popen example depend on `SYSTEM_POPEN`, and dhcpd and gdbstub test it in code. So first an apps PR lets them use either copy, then the defconfig PR, then the removal of apps/system/popen, then the guard.



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "casaroli (via GitHub)" <gi...@apache.org>.
casaroli commented on code in PR #20778:
URL: https://github.com/apache/nuttx/pull/20778#discussion_r4307723428


##########
libs/libc/unistd/CMakeLists.txt:
##########
@@ -129,4 +129,14 @@ if(CONFIG_PIPES)
   list(APPEND SRCS lib_pipe.c)
 endif()
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   Agreed. libc now also covers the case that apps/system/popen handles without NSH: when no shell can be started, dpopen() runs the command directly with posix_spawnp(). A flat build finds the NSH `sh` builtin through `/bin/sh`, so NSH's in-process mode is not needed either. Since both copies must never be built together, I will do it in this order: (1) this PR keeps the `CONFIG_SYSTEM_POPEN` guard; (2) a PR that removes `CONFIG_SYSTEM_POPEN` from the 15 defconfigs and enables `CONFIG_SCHED_WAITPID` (and `CONFIG_LIBC_EXECFUNCS` where missing); (3) an apps PR that removes apps/system/popen and moves fastboot and the popen tests to the libc conditions; (4) a last PR that removes the guard.



##########
libs/libc/unistd/Make.defs:
##########
@@ -78,6 +78,15 @@ ifeq ($(CONFIG_PIPES),y)
 CSRCS += lib_pipe.c
 endif
 
+# dpopen() needs pipes, posix_spawn() and waitpid().  CONFIG_SYSTEM_POPEN
+# selects the copy in apps/system/popen instead.

Review Comment:
   Yes, see the reply on CMakeLists.txt: I will remove it after this PR, in the order described there.



-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] libs/libc: Add popen(), pclose(), dpopen() and dpclose(). [nuttx]

Posted by "acassis (via GitHub)" <gi...@apache.org>.
acassis merged PR #20778:
URL: https://github.com/apache/nuttx/pull/20778


-- 
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: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org