mirror of
https://github.com/openai/codex.git
synced 2026-12-06 01:10:58 +00:00
## Why Unix command hooks use a `pre_exec` callback to detach from the controlling terminal, forcing the launcher to fork. Native spawn attributes can create the detached session without that callback. ## What changed - Route Unix hooks through `codex_utils_pty::Command` with `ProcessMode::NewSession`. - Extend the native `posix_spawn` launcher to detached Linux sessions, retaining compatibility fallbacks for unsupported libc features and executable handling. - Preserve the session environment snapshot, hook overrides, and restricted-variable filtering. ## Testing Add regression coverage for launches without parent forks, terminal detachment, environment and argument handling, Linux `PATH` lookup, and executable compatibility. Cover process-group cleanup on cancellation and timeout, including descendants holding output pipes open, and preservation of background descendants after completed hooks. GitOrigin-RevId: dbe2cf12d84aa79868cfb55ace40c7d5b5282703
64 lines
2.5 KiB
Rust
64 lines
2.5 KiB
Rust
//! Linux executable lookup must match the compatibility launcher's PATH semantics.
|
|
|
|
use std::os::unix::fs::PermissionsExt;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
|
|
use crate::Command;
|
|
use crate::ProcessMode;
|
|
|
|
#[tokio::test]
|
|
async fn path_search_stops_at_invalid_candidates() -> anyhow::Result<()> {
|
|
let root = tempfile::tempdir()?;
|
|
std::fs::create_dir(root.path().join("bin"))?;
|
|
std::os::unix::fs::symlink("loop", root.path().join("loop"))?;
|
|
let script = root.path().join("bin/server");
|
|
std::fs::write(&script, "#!/bin/sh\nprintf later\n")?;
|
|
std::fs::set_permissions(script, std::fs::Permissions::from_mode(/*mode*/ 0o755))?;
|
|
for candidate in ["loop".to_owned(), "x".repeat(256)] {
|
|
let mut command = Command::new("server");
|
|
command
|
|
.current_dir(root.path())
|
|
.env("PATH", format!("{candidate}:bin"));
|
|
let expected = command
|
|
.inner
|
|
.spawn()
|
|
.expect_err("invalid first candidate must stop lookup");
|
|
command.process_mode(ProcessMode::NewSession);
|
|
let actual = command.spawn().err().expect("native lookup must stop too");
|
|
assert_eq!(actual.raw_os_error(), expected.raw_os_error());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn empty_path_entries_preserve_script_argv0() -> anyhow::Result<()> {
|
|
// A sibling test can fork while the script is open for writing, retaining
|
|
// the writable descriptor and making exec fail with ETXTBSY.
|
|
if std::env::var_os("CODEX_TEST_PATH_ARGV0").is_none() {
|
|
let output = std::process::Command::new(std::env::current_exe()?)
|
|
.args([
|
|
"--exact",
|
|
"child_command::linux_tests::empty_path_entries_preserve_script_argv0",
|
|
"--nocapture",
|
|
])
|
|
.env("CODEX_TEST_PATH_ARGV0", "1")
|
|
.output()?;
|
|
assert!(output.status.success(), "{output:?}");
|
|
return Ok(());
|
|
}
|
|
let root = tempfile::tempdir()?;
|
|
let script = root.path().join("server");
|
|
std::fs::write(&script, "#!/bin/sh\nprintf '%s' \"$0\"\n")?;
|
|
std::fs::set_permissions(script, std::fs::Permissions::from_mode(/*mode*/ 0o755))?;
|
|
for path in ["", ":missing", "missing:", ".", "./"] {
|
|
let mut command = Command::new("server");
|
|
command.current_dir(root.path()).env("PATH", path);
|
|
let expected = command.inner.output().await?;
|
|
command.process_mode(ProcessMode::NewSession);
|
|
let actual = command.spawn()?.wait_with_output().await?;
|
|
assert_eq!(actual, expected);
|
|
}
|
|
Ok(())
|
|
}
|