Created
November 28, 2025 20:07
-
-
Save jordanisaacs/1be6a1fd3ee7024e541279bbbe20133c to your computer and use it in GitHub Desktop.
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
| use std::os::unix::process::CommandExt; | |
| use std::process::Command; | |
| // Compile with rustc test_spawn.rs | |
| fn main() { | |
| println!("About to spawn /bin/sh with pre_exec"); | |
| let mut cmd = Command::new("/bin/sh"); | |
| cmd.arg("-c").arg("echo hello"); | |
| // Comment/uncomment this and run 'strace -kk -e socketpair ./test_spawn'. | |
| // It will run socketpair with pre_exec callback (aka custom spawn) but not without (aka posix_spawn). | |
| unsafe { | |
| cmd.pre_exec(|| { | |
| eprintln!("Inside pre_exec"); | |
| Ok(()) | |
| }); | |
| } | |
| match cmd.spawn() { | |
| Ok(mut child) => { | |
| println!("Spawned successfully"); | |
| match child.wait() { | |
| Ok(status) => println!("Exit status: {:?}", status), | |
| Err(e) => eprintln!("Wait failed: {:?}", e), | |
| } | |
| } | |
| Err(e) => { | |
| eprintln!("Spawn failed: {:?}", e); | |
| eprintln!("OS error: {:?}", e.raw_os_error()); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment