Skip to content

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

Merged
xiaoxiang781216 merged 3 commits into
apache:masterfrom
cuiziweizw:dpopen
Jun 2, 2026
Merged

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

Conversation

@cuiziweizw
Copy link
Copy Markdown
Contributor

@cuiziweizw cuiziweizw commented Jun 1, 2026

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 7 cases pass.

qemu-armv8a-ap> popen_test
=== popen/dpopen test ===

[TEST 1] popen read...
  > hello_popen
[PASS]
[TEST 2] popen multi-arg...
  > aaa bbb ccc
[PASS]
[TEST 3] pclose completes...
[PASS]
[TEST 4] popen invalid mode...
[PASS]
[TEST 5] dpopen read...
  > hello_dpopen
[PASS]
[TEST 6] dpopen multi-arg...
  > xxx yyy zzz
[PASS]
[TEST 7] dpclose completes...
[PASS]

=== Results: 0 failed ===

acassis
acassis previously approved these changes Jun 1, 2026
@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?

@xiaoxiang781216
Copy link
Copy Markdown
Contributor

  • Do we want test added to ostest which is more commonly used for testing?

ostest doesn't test libc function.

@cederom
Copy link
Copy Markdown
Contributor

cederom commented Jun 1, 2026

@linguini1
Copy link
Copy Markdown
Contributor

Could you please add the logs from your test?

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>
xiaoxiang781216
xiaoxiang781216 previously approved these changes Jun 2, 2026
@cuiziweizw cuiziweizw dismissed stale reviews from xiaoxiang781216 and acassis via 795ae26 June 2, 2026 06:58
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-7: dpopen - basic read, multi-arg, dpclose

Signed-off-by: cuiziwei <cuiziwei@xiaomi.com>
@xiaoxiang781216 xiaoxiang781216 merged commit 97802bd into apache:master Jun 2, 2026
41 checks passed
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.

6 participants