Repository navigation
pipe: Preserve "sh" as argv0 in the Rust os_process_pipe_create - #740
Merged
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
With
ENABLE_RUST_LIBOBS=ONon Unix,os_process_pipe_create(cmd, type)runs/bin/sh -c cmdwithargv[0]set to/bin/sh. The C implementation setsargv[0]tosh. A command that reads$0therefore sees different text depending on the build:printf '%s' "$0"printsshwith the C code and/bin/shwith 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).char *argv[4] = {"sh", "-c", cmd_line, NULL}and passes the program"/bin/sh"separately (libobs/util/pipe-posix.c:134-135). The program andargv[0]are two different values, andposix_spawn(&pid, bin, ..., argv, ...)receives both (:97).spawn(argv0, rest, read_mode)buildsCommand::new(argv0)(rust/obs-util/src/platform/unix/pipe.rs:27-30), andPipe::createpasses"/bin/sh"as that value (:69-71).std::process::Commanduses the program path asargv[0]unless told otherwise, so the shell is started withargv[0] == "/bin/sh".os_process_pipe_createreaches this throughPipe::create(rust/obs-util/src/ffi/pipe.rs:122, call at:130).Fix
Only
rust/obs-util/src/platform/unix/pipe.rschanges (GREEN, +8/-5).spawnnow takes the program andargv[0]as separate parameters (spawn(program, argv0, rest, read_mode),:27), buildsCommand::new(program)and setsargv[0]withCommandExt::arg0(:30-31, import at:7).Pipe::createpasses program/bin/shandargv[0]sh(:70-73), the same pair as the C code.Pipe::create2passesargs.args[0]as both (:84-89), which is what it effectively did before, soos_process_pipe_create2behaves as before.Alternatives:
Command::new("sh")with aPATHlookup. Not taken: C runs the exact/bin/sh(pipe-posix.c:135), and aPATHlookup could pick a different shell.Evidence
Branch
fix/pipe-shell-argv0on the fork, two commits on483c0f341(an olderupstream/master):786a4428408c5d8ca4cd52e6ef6271bc89a2db48"pipe: Test shell invocation argv0 (RED)".0d42b113a0ddb7098a3aef2704353be0616a941c"pipe: Preserve shell invocation argv0 (GREEN)".Test:
test_create_uses_sh_as_argv0intest/cmocka/test_pipe_shell_argv0.c, registered intest/cmocka/tests.d/test_pipe_shell_argv0.cmake(if(UNIX AND TARGET OBS::libobs), CTest timeout 20 s). It runsos_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 exactlysh(:12-27). It has no sleeps. Because it goes through the publicutil/pipe.hAPI, the same source runs against C libobs (OFF) and the Rust port (ON).Local results (macOS, Xcode Debug build of
libobs,test_pipeand the new test, run through CTest; this is not the CI configuration):RED test with
ENABLE_RUST_LIBOBS=ONfails with the stated difference (excerpt; the source-location line and blank lines are omitted):RED test with
ENABLE_RUST_LIBOBS=OFF(C code) passes, which confirms the test encodes the C behaviour:GREEN,
ENABLE_RUST_LIBOBS=ONandOFF: both pass, and the existingtest_pipestill passes next to it:(the
ONrun;OFFshows 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-utilreportedpassed 472 failed 0on the branch in the review. The review also reportedcargo clippy --all-targets -D warningsandcargo fmt --checkclean, 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/shthrough 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_createpasses a single command line toCreateProcessWand has noargv[0]split.Behaviour: only the shell's
$0changes, from/bin/shtosh, matching C.os_process_pipe_create2is unchanged. A review probe comparedcreate2results before and after and found them identical.Limits:
/bin/shfrom aPATHlookup ofsh. A change that ranCommand::new("sh")withargv[0]shwould still pass under a normalPATH. The fix itself uses the exact path as C does, and we did not add a case that emptiesPATH.483c0f341. Upstream has since changed neighbouring lines of the samespawnfunction (97572de67, which drops the explicit/dev/nullredirection). 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 withupstream/masterat 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.