Skip to content

system/popen: add dpopen/dpclose fd-based interface and no-shell mode#3515

Open
cuiziweizw wants to merge 3 commits into
apache:masterfrom
cuiziweizw:dpopen
Open

system/popen: add dpopen/dpclose fd-based interface and no-shell mode#3515
cuiziweizw wants to merge 3 commits into
apache:masterfrom
cuiziweizw:dpopen

Conversation

@cuiziweizw
Copy link
Copy Markdown
Contributor

Summary

  • system/popen: add dpopen()/dpclose() as the descriptor-based
    counterpart of popen()/pclose(), analogous to how dprintf()
    relates to fprintf(). dpopen() returns a raw file descriptor
    instead of a FILE stream, avoiding the stdio.h dependency for
    callers that only need an fd. popen() is refactored as a thin
    wrapper: dpopen() + fdopen() + FILE container.
  • system/popen: support no-shell mode via posix_spawnp(). The hard
    dependency on NSH_LIBRARY is removed; when NSH is unavailable,
    commands are split by whitespace and executed directly. Adds
    CONFIG_SYSTEM_POPEN_MAXARGUMENTS (default 7).
  • testing/libc/popen: add popen/dpopen tests covering both shell
    (NSH) and no-shell modes.

Depends on the header declarations in the companion nuttx PR:
apache/nuttx#19008

Testing

Ran the new testing/libc/popen test in both shell and no-shell
configurations; all 8 cases pass.

Add dpopen()/dpclose() as the descriptor-based counterpart of
popen()/pclose(), analogous to how dprintf() relates to fprintf().
dpopen() returns a raw file descriptor instead of a FILE stream,
avoiding the stdio.h dependency for callers that only need an fd.

Refactor popen() as a thin wrapper: dpopen() + fdopen() + FILE
container.  All pipe creation and process spawning logic now lives
in dpopen.c.

Also remove the hard dependency on NSH_LIBRARY from SYSTEM_POPEN.
When NSH is available, commands are executed through sh -c with full
shell syntax support.  When NSH is not available, commands are split
by whitespace and executed directly via posix_spawnp().

Add CONFIG_SYSTEM_POPEN_MAXARGUMENTS (default 7) to control the
argv array size for the no-shell path.

Signed-off-by: cuiziwei <cuiziwei@xiaomi.com>
When NSH_LIBRARY is not available, dpopen()/popen() can still execute
commands by splitting the command string by whitespace and calling
posix_spawnp() directly.  Shell syntax (pipes, redirects, globbing)
is not supported in this mode.

Add CONFIG_SYSTEM_POPEN_MAXARGUMENTS (default 7) to control the
argv array size for the no-shell path.

Remove the hard dependency on NSH_LIBRARY from SYSTEM_POPEN so the
feature can be used in minimal configurations without a shell.

Signed-off-by: cuiziwei <cuiziwei@xiaomi.com>
Add tests for popen()/pclose() and dpopen()/dpclose() that work in
both shell (NSH) and no-shell (direct posix_spawnp) modes.  The test
binary doubles as its own target via the --echo sub-command.

Tests:
 1-4: popen  - basic read, multi-arg, pclose, invalid mode
 5-8: dpopen - basic read, multi-arg, dpclose, invalid mode

Signed-off-by: cuiziwei <cuiziwei@xiaomi.com>
@cederom
Copy link
Copy Markdown
Contributor

cederom commented Jun 1, 2026

  • Thank you @cuiziweizw :-)
  • CI failed, restarted, nuttx part is merged so should pass now :-)
  • Please remember do add test logs to the PR.
  • Do we want test added to ostest which is more commonly used for testing?
  • Do we need documentation update on this part?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants