Skip to content

pipe: Preserve "sh" as argv0 in the Rust os_process_pipe_create - #740

Merged
zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/pipe-shell-argv0
Oct 14, 2026
Merged

zackees merged 2 commits into
obs-rust:masterfrom
chriscoveries:fix/pipe-shell-argv0

Conversation

@chriscoveries

Copy link
Copy Markdown
Contributor

Draft. Opened so upstream PR CI can run it. Our own CI is down, so this hasn't been through our usual CI runs: see our note on #699.

Problem

With ENABLE_RUST_LIBOBS=ON on Unix, os_process_pipe_create(cmd, type) runs /bin/sh -c cmd with argv[0] set to /bin/sh. The C implementation sets argv[0] to sh. A command that reads $0 therefore sees different text depending on the build: printf '%s' "$0" prints sh with the C code and /bin/sh with the Rust port. The impact is small (it only shows to commands that inspect their own name), but it is a behaviour difference from the C original.

Root cause

Line numbers are upstream/master (a48a9a13d).

  • C builds the shell invocation as char *argv[4] = {"sh", "-c", cmd_line, NULL} and passes the program "/bin/sh" separately (libobs/util/pipe-posix.c:134-135). The program and argv[0] are two different values, and posix_spawn(&pid, bin, ..., argv, ...) receives both (:97).
  • The Rust port has one parameter for both. spawn(argv0, rest, read_mode) builds Command::new(argv0) (rust/obs-util/src/platform/unix/pipe.rs:27-30), and Pipe::create passes "/bin/sh" as that value (:69-71). std::process::Command uses the program path as argv[0] unless told otherwise, so the shell is started with argv[0] == "/bin/sh".
  • os_process_pipe_create reaches this through Pipe::create (rust/obs-util/src/ffi/pipe.rs:122, call at :130).

Fix

Only rust/obs-util/src/platform/unix/pipe.rs changes (GREEN, +8/-5). spawn now takes the program and argv[0] as separate parameters (spawn(program, argv0, rest, read_mode), :27), builds Command::new(program) and sets argv[0] with CommandExt::arg0 (:30-31, import at :7).

  • Pipe::create passes program /bin/sh and argv[0] sh (:70-73), the same pair as the C code.
  • Pipe::create2 passes args.args[0] as both (:84-89), which is what it effectively did before, so os_process_pipe_create2 behaves as before.

Alternatives:

  • Command::new("sh") with a PATH lookup. Not taken: C runs the exact /bin/sh (pipe-posix.c:135), and a PATH lookup could pick a different shell.
  • Changing nothing and documenting the difference. Not taken: the change is small and removes a C-versus-Rust difference.

Evidence

Branch fix/pipe-shell-argv0 on the fork, two commits on 483c0f341 (an older upstream/master):

  • RED, test only: 786a4428408c5d8ca4cd52e6ef6271bc89a2db48 "pipe: Test shell invocation argv0 (RED)".
  • GREEN, fix: 0d42b113a0ddb7098a3aef2704353be0616a941c "pipe: Preserve shell invocation argv0 (GREEN)".

Test: test_create_uses_sh_as_argv0 in test/cmocka/test_pipe_shell_argv0.c, registered in test/cmocka/tests.d/test_pipe_shell_argv0.cmake (if(UNIX AND TARGET OBS::libobs), CTest timeout 20 s). It runs os_process_pipe_create("printf '%s' \"$0\"", "r"), reads the pipe to EOF, destroys it, and asserts that the exit status is 0 and the output is exactly sh (:12-27). It has no sleeps. Because it goes through the public util/pipe.h API, the same source runs against C libobs (OFF) and the Rust port (ON).

Local results (macOS, Xcode Debug build of libobs, test_pipe and the new test, run through CTest; this is not the CI configuration):

  • RED test with ENABLE_RUST_LIBOBS=ON fails with the stated difference (excerpt; the source-location line and blank lines are omitted):

    2/2 Test #76: test_pipe_shell_argv0 ............***Failed    0.50 sec
    [ RUN      ] test_create_uses_sh_as_argv0
    [  ERROR   ] --- "/bin/sh" != "sh"
    [  FAILED  ] test_create_uses_sh_as_argv0
    50% tests passed, 1 tests failed out of 2
    
  • RED test with ENABLE_RUST_LIBOBS=OFF (C code) passes, which confirms the test encodes the C behaviour:

    2/2 Test #76: test_pipe_shell_argv0 ............   Passed    0.42 sec
    100% tests passed out of 2
    
  • GREEN, ENABLE_RUST_LIBOBS=ON and OFF: both pass, and the existing test_pipe still passes next to it:

    1/2 Test #28: test_pipe ........................   Passed    0.97 sec
    2/2 Test #76: test_pipe_shell_argv0 ............   Passed    0.43 sec
    100% tests passed out of 2
    

    (the ON run; OFF shows the same two tests passing, 0.84 s and 0.45 s).

  • Repeat runs, as logged by the review: pipe-shell-argv0: RED-ON unexpected passes=0/30, GREEN-ON failures=0/30, OFF failures=0/30. These ran on earlier commits with the same trees as RED and GREEN here (only the commit subjects were reworded).

  • Rust: cargo test -p obs-util reported passed 472 failed 0 on the branch in the review. The review also reported cargo clippy --all-targets -D warnings and cargo fmt --check clean, and the C test compiling under clang and gcc with -Wall -Wextra -Werror. Those lines are from the review summary, not reproduced here.

  • Fork CI: not run. Our self-hosted CI is down (see our note on #699), so this relies on upstream PR CI.

  • Real-run evidence: the full app was not run. The test spawns a real /bin/sh through the real library on both code paths.

Compatibility & risk

No C API, ABI, export or header change. The C implementation is untouched. Unix only: Windows is not affected, because its os_process_pipe_create passes a single command line to CreateProcessW and has no argv[0] split.

Behaviour: only the shell's $0 changes, from /bin/sh to sh, matching C. os_process_pipe_create2 is unchanged. A review probe compared create2 results before and after and found them identical.

Limits:

  • The test does not distinguish /bin/sh from a PATH lookup of sh. A change that ran Command::new("sh") with argv[0] sh would still pass under a normal PATH. The fix itself uses the exact path as C does, and we did not add a case that empties PATH.
  • Run on macOS only. Linux was not run locally, and the test is Unix-only by its CMake guard.
  • The branch is based on 483c0f341. Upstream has since changed neighbouring lines of the same spawn function (97572de67, which drops the explicit /dev/null redirection). The hunks do not overlap with this change, but a merge check against the current tip has not been re-run since the review, which found a clean merge with upstream/master at that time.

Status

Draft. Opened so upstream PR CI can run it.


Thank you to the obs-rust maintainers and contributors for building this project in the open. In the American spirit of free expression and individual independence, and with the principle of judging contributions on their merit alone, this PR is a small piece of what independent people can build when they come together.

@zackees
zackees marked this pull request as ready for review October 14, 2026 23:10
@zackees
zackees self-requested a review as a code owner October 14, 2026 23:10
@zackees
zackees merged commit 9329c18 into obs-rust:master Oct 14, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

2 participants